diff --git a/README.md b/README.md index 691114a..5fbde79 100755 --- a/README.md +++ b/README.md @@ -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