Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 88 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,100 @@ The project is in _EXPERIMENTAL_: it may crash or silently skip some statements,
Input solidity code

```solidity
contract FooBarContract {
function foo(uint number) internal returns (int) {
string[2] memory arr = ["hello", "world"];
bool isEven = number % 2 == 0;
int result = 42 * 42;
return isEven ? -1 : result;
}
pragma solidity ^0.5.11;

contract Coin {
address minter;
mapping (address => uint) balances;

constructor() public {
minter = msg.sender;
}
function mint(address owner, uint amount) public {
if (msg.sender == minter) {
balances[owner] += amount;
}
}
function send(address receiver, uint amount) public {
if (balances[msg.sender] >= amount) {
balances[msg.sender] -= amount;
balances[receiver] += amount;
}
}
function queryBalance(address addr) public view returns (uint balance) {
return balances[addr];
}
}
```

Translated LIGO code
```js
function foo (const self : state; const number : nat) : (state * int) is
type constructor_args is unit;
type mint_args is record
owner : address;
res__amount : nat;
end;

type send_args is record
receiver : address;
res__amount : nat;
end;

type queryBalance_args is record
addr : address;
callbackAddress : address;
end;

type state is record
minter : address;
balances : map(address, nat);
end;

type router_enum is
| Constructor of constructor_args
| Mint of mint_args
| Send of send_args
| QueryBalance of queryBalance_args;

function constructor (const self : state) : (state) is
block {
const arr : map(nat, string) = map
0n -> "hello";
1n -> "world";
end;
const isEven : bool = ((number mod 2n) = 0n);
const result : int = (42 * 42);
} with (self, (case isEven of | True -> -(1) | False -> result end));
self.minter := Tezos.sender;
} with (self);

function mint (const self : state; const owner : address; const res__amount : nat) : (state) is
block {
if (Tezos.sender = self.minter) then block {
self.balances[owner] := ((case self.balances[owner] of | None -> 0n | Some(x) -> x end) + res__amount);
} else block {
skip
};
} with (self);

function send (const self : state; const receiver : address; const res__amount : nat) : (state) is
block {
if ((case self.balances[Tezos.sender] of | None -> 0n | Some(x) -> x end) >= res__amount) then block {
self.balances[Tezos.sender] := abs((case self.balances[Tezos.sender] of | None -> 0n | Some(x) -> x end) - res__amount);
self.balances[receiver] := ((case self.balances[receiver] of | None -> 0n | Some(x) -> x end) + res__amount);
} else block {
skip
};
} with (self);

function queryBalance (const self : state; const addr : address) : (nat) is
block {
const res__balance : nat = 0n;
} with ((case self.balances[addr] of | None -> 0n | Some(x) -> x end));

function main (const action : router_enum; const self : state) : (list(operation) * state) is
(case action of
| Constructor(match_action) -> ((nil: list(operation)), constructor(self))
| Mint(match_action) -> ((nil: list(operation)), mint(self, match_action.owner, match_action.res__amount))
| Send(match_action) -> ((nil: list(operation)), send(self, match_action.receiver, match_action.res__amount))
| QueryBalance(match_action) -> block {
const tmp : (nat) = queryBalance(self, match_action.addr);
var opList : list(operation) := list transaction((tmp), 0mutez, (get_contract(match_action.callbackAddress) : contract(nat))) end;
} with ((opList, self))
end);
```

### 📚️ More examples
Expand Down