From 2d442a0060d5ff787c5fc7742bffe00168789759 Mon Sep 17 00:00:00 2001 From: vird Date: Thu, 6 Aug 2020 20:12:14 +0000 Subject: [PATCH 1/2] fix readme example --- README.md | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c48c43..a3505a5 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Input solidity code ```solidity contract FooBarContract { - function foo(uint number) internal returns (int) { + function foo(uint number) returns (int) { string[2] memory arr = ["hello", "world"]; bool isEven = number % 2 == 0; int result = 42 * 42; @@ -23,7 +23,17 @@ contract FooBarContract { Translated LIGO code ```js -function foo (const self : state; const number : nat) : (state * int) is +type foo_args is record + number : nat; + callbackAddress : address; +end; + +type state is unit; + +type router_enum is + | Foo of foo_args; + +function foo (const number : nat) : (int) is block { const arr : map(nat, string) = map 0n -> "hello"; @@ -31,7 +41,15 @@ function foo (const self : state; const number : nat) : (state * int) is end; const isEven : bool = ((number mod 2n) = 0n); const result : int = (42 * 42); - } with (self, (case isEven of | True -> -(1) | False -> result end)); + } with ((case isEven of | True -> -(1) | False -> result end)); + +function main (const action : router_enum; const self : state) : (list(operation) * state) is + (case action of + | Foo(match_action) -> block { + const tmp : (int) = foo(match_action.number); + var opList : list(operation) := list transaction((tmp), 0mutez, (get_contract(match_action.callbackAddress) : contract(int))) end; + } with ((opList, self)) + end); ``` ### 📚️ More examples From 2af2a47743d65736772d581065865451a8910507 Mon Sep 17 00:00:00 2001 From: vird Date: Tue, 8 Sep 2020 13:20:19 +0000 Subject: [PATCH 2/2] use simplecoin as example --- README.md | 99 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c14f644..5fbde79 100755 --- a/README.md +++ b/README.md @@ -11,43 +11,98 @@ The project is in _EXPERIMENTAL_: it may crash or silently skip some statements, Input solidity code ```solidity -contract FooBarContract { - function foo(uint number) 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 -type foo_args is record - number : nat; +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 unit; +type state is record + minter : address; + balances : map(address, nat); +end; type router_enum is - | Foo of foo_args; + | Constructor of constructor_args + | Mint of mint_args + | Send of send_args + | QueryBalance of queryBalance_args; -function foo (const number : nat) : (int) is +function constructor (const self : state) : (state) is + block { + 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 arr : map(nat, string) = map - 0n -> "hello"; - 1n -> "world"; - end; - const isEven : bool = ((number mod 2n) = 0n); - const result : int = (42 * 42); - } with ((case isEven of | True -> -(1) | False -> result end)); + 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 - | Foo(match_action) -> block { - const tmp : (int) = foo(match_action.number); - var opList : list(operation) := list transaction((tmp), 0mutez, (get_contract(match_action.callbackAddress) : contract(int))) end; + | 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); ```