From e8935ffa8102d036a8237a75606c62a7e52beead Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 17 Jan 2020 22:56:54 -0800 Subject: [PATCH] Add CameLIGO and ReasonLIGO examples to docs where they didn't already exist --- .../docs/advanced/entrypoints-contracts.md | 75 ++++++++++++++++++- gitlab-pages/docs/advanced/first-contract.md | 35 ++++++++- .../docs/advanced/timestamps-addresses.md | 69 ++++++++++++++++- gitlab-pages/docs/intro/what-and-why.md | 38 +++++++++- .../docs/language-basics/functions.md | 9 --- 5 files changed, 213 insertions(+), 13 deletions(-) diff --git a/gitlab-pages/docs/advanced/entrypoints-contracts.md b/gitlab-pages/docs/advanced/entrypoints-contracts.md index 273537c77..eeb4fc0ae 100644 --- a/gitlab-pages/docs/advanced/entrypoints-contracts.md +++ b/gitlab-pages/docs/advanced/entrypoints-contracts.md @@ -12,6 +12,17 @@ Each LIGO smart contract is essentially a single function, that has the followin ``` (const parameter: my_type, const store: my_store_type): (list(operation), my_store_type) ``` + + +``` +(parameter, store: my_type * my_store_type) : operation list * my_store_type +``` + + +``` +(parameter_store: (my_type, my_store_type)) : (list(operation), my_store_type) +``` + This means that every smart contract needs at least one entrypoint function, here's an example: @@ -26,6 +37,25 @@ type store is unit; function main(const parameter: parameter; const store: store): (list(operation) * store) is block { skip } with ((nil : list(operation)), store) ``` + + +```cameligo group=a +type parameter = unit +type store = unit +let main (parameter, store: parameter * store) : operation list * store = + (([]: operation list), store) +``` + + +```reasonligo group=a +type parameter = unit; +type store = unit; +let main = (parameter_store: (parameter, store)) : (list(operation), store) => { + let parameter, store = parameter_store; + (([]: list(operation)), store); +}; +``` + Each entrypoint function receives two arguments: @@ -52,9 +82,30 @@ function main (const p : unit ; const s : unit) : (list(operation) * unit) is if amount > 0mutez then failwith("This contract does not accept tez") else skip } with ((nil : list(operation)), unit); ``` + + +```cameligo group=b +let main (p, s: unit * unit) : operation list * unit = + if amount > 0mutez + then (failwith "This contract does not accept tez": operation list * unit) + else (([]: operation list), unit) +``` + + +```reasonligo group=b +let main = (p_s: (unit, unit)) : (list(operation), unit) => { + if (amount > 0mutez) { + (failwith("This contract does not accept tez"): (list(operation), unit)); + } + else { + (([]: list(operation)), ()); + }; +}; +``` + -### Access control locking +### Access Control This example shows how `sender` or `source` can be used to deny access to an entrypoint. @@ -67,6 +118,28 @@ function main (const p : unit ; const s : unit) : (list(operation) * unit) is if source =/= owner then failwith("This address can't call the contract") else skip } with ((nil : list(operation)), unit); ``` + + +```cameligo group=c +let owner: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) +let main (p,s: unit * unit) : operation list * unit = + if source <> owner + then (failwith "This address can't call the contract": operation list * unit) + else (([]: operation list), ()) +``` + + +```reasonligo group=c +let owner: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address); +let main = (p_s: (unit, unit)) : (list(operation), unit) => { + if (source != owner) { + (failwith("This address can't call the contract"): (list(operation), unit)); + } + else { + (([]: list(operation)), ()); + }; +}; +``` ### Cross contract calls diff --git a/gitlab-pages/docs/advanced/first-contract.md b/gitlab-pages/docs/advanced/first-contract.md index 4932cd5f5..19861291f 100644 --- a/gitlab-pages/docs/advanced/first-contract.md +++ b/gitlab-pages/docs/advanced/first-contract.md @@ -49,6 +49,39 @@ function main (const p : action ; const s : int) : (list(operation) * int) is | Decrement (n) -> s - n end) ``` + + +```cameligo +type action = +| Increment of int +| Decrement of int + +let main (p, s: action * int) : operation list * int = + let result = + match p with + | Increment n -> s + n + | Decrement n -> s - n + in + (([]: operation list), result) +``` + + +```reasonligo +type action = +| Increment(int) +| Decrement(int); + +let main = (p_s: (action, int)) : (list(operation), int) => { + let p, s = p_s; + let result = + switch (p) { + | Increment(n) => s + n + | Decrement(n) => s - n + }; + (([]: list(operation)), result); +}; +``` + To dry-run the counter contract, we will use the `main` entrypoint, provide a variant parameter of `Increment(5)` and an initial storage value of `5`. @@ -149,4 +182,4 @@ ligo compile-parameter src/counter.ligo main 'Increment(5)' -Now we can use `(Right 5)` which is a Michelson value, to invoke our contract - e.g. via `tezos-client` \ No newline at end of file +Now we can use `(Right 5)` which is a Michelson value, to invoke our contract - e.g. via `tezos-client` diff --git a/gitlab-pages/docs/advanced/timestamps-addresses.md b/gitlab-pages/docs/advanced/timestamps-addresses.md index bbd17e2c2..e77ba76ee 100644 --- a/gitlab-pages/docs/advanced/timestamps-addresses.md +++ b/gitlab-pages/docs/advanced/timestamps-addresses.md @@ -17,6 +17,17 @@ You can obtain the current time using the built-in syntax specific expression, p ```pascaligo group=a const today: timestamp = now; ``` + + +```cameligo group=a +let today: timestamp = Current.time +``` + + +```reasonligo group=a +let today: timestamp = Current.time; +``` + > When running code with ligo CLI, the option `--predecessor-timestamp` allows you to control what `now` returns. @@ -35,6 +46,25 @@ const in_24_hrs: timestamp = today + one_day; const some_date: timestamp = ("2000-01-01T10:10:10Z" : timestamp); const one_day_later: timestamp = some_date + one_day; ``` + + +```cameligo group=b +let today: timestamp = Current.time +let one_day: int = 86400 +let in_24_hrs: timestamp = today + one_day +let some_date: timestamp = ("2000-01-01t10:10:10Z" : timestamp) +let one_day_later: timestamp = some_date + one_day +``` + + +```reasonligo group=b +let today: timestamp = Current.time; +let one_day: int = 86400; +let in_24_hrs: timestamp = today + one_day; +let some_date: timestamp = ("2000-01-01t10:10:10Z" : timestamp); +let one_day_later: timestamp = some_date + one_day; +``` + #### 24 hours ago @@ -45,6 +75,21 @@ const today: timestamp = now; const one_day: int = 86400; const in_24_hrs: timestamp = today - one_day; ``` + + +```cameligo group=c +let today: timestamp = Current.time +let one_day: int = 86400 +let in_24_hrs: timestamp = today - one_day +``` + + +```reasonligo group=c +let today: timestamp = Current.time; +let one_day: int = 86400; +let in_24_hrs: timestamp = today - one_day; +``` + ### Comparing timestamps @@ -56,6 +101,17 @@ You can also compare timestamps using the same comparison operators as for numbe ```pascaligo group=c const not_tommorow: bool = (now = in_24_hrs) ``` + + +```cameligo group=c +let not_tomorrow: bool = (Current.time = in_24_hrs) +``` + + +```reasonligo group=c +let not_tomorrow: bool = (Current.time == in_24_hrs); +``` + ## Addresses @@ -69,6 +125,17 @@ Here's how you can define an address: ```pascaligo group=d const my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address); ``` + + +```cameligo group=d +let my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) +``` + + +```reasonligo group=d +let my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address); +``` + ## Signatures @@ -111,4 +178,4 @@ let my_key: key = ("edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav": key ```reasonligo group=f let my_key: key = ("edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav": key); ``` - \ No newline at end of file + diff --git a/gitlab-pages/docs/intro/what-and-why.md b/gitlab-pages/docs/intro/what-and-why.md index 444822720..4cddae9a7 100644 --- a/gitlab-pages/docs/intro/what-and-why.md +++ b/gitlab-pages/docs/intro/what-and-why.md @@ -82,6 +82,42 @@ function main (const p : action ; const s : int) : (list(operation) * int) is | Reset(n) -> 0 end) ``` + + +```cameligo +type action = +| Increment of int +| Decrement of int +| Reset of unit + +let main (p, s: action * int) : operation list * int = + let result = + match p with + | Increment n -> s + n + | Decrement n -> s - n + | Reset n -> 0 + in + (([]: operation list), result) +``` + + +```reasonligo +type action = +| Increment(int) +| Decrement(int) +| Reset(unit); + +let main = (p_s: (action, int)) : (list(operation), int) => { + let p, s = p_s; + let result = + switch (p) { + | Increment(n) => s + n + | Decrement(n) => s - n + | Reset n => 0 + }; + (([]: list(operation)), result); +}; +``` @@ -112,4 +148,4 @@ In certain cases it makes sense to be able to run/evaluate the given snippet or ```shell ligo evaluate-value -s pascaligo gitlab-pages/docs/language-basics/src/variables-and-constants/const.ligo age # Outputs: 25 -``` \ No newline at end of file +``` diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index 8bd30ee3b..6b5739205 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -107,15 +107,6 @@ value. - - -```pascaligo group=b -const increment : (int -> int) = (function (const i : int) : int is i + 1); -// a = 2 -const a: int = increment(1); -``` - - ## Anonymous functions Functions without a name, also known as anonymous functions are useful in cases when you want to pass the function as an argument or assign it to a key in a record/map.