Add CameLIGO and ReasonLIGO examples to docs where they didn't already exist

This commit is contained in:
John David Pressman 2020-01-17 22:56:54 -08:00
parent 481801ea91
commit e8935ffa81
5 changed files with 213 additions and 13 deletions

View File

@ -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)
```
<!--CameLIGO-->
```
(parameter, store: my_type * my_store_type) : operation list * my_store_type
```
<!--ReasonLIGO-->
```
(parameter_store: (my_type, my_store_type)) : (list(operation), my_store_type)
```
<!--END_DOCUSAURUS_CODE_TABS-->
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-->
```cameligo group=a
type parameter = unit
type store = unit
let main (parameter, store: parameter * store) : operation list * store =
(([]: operation list), store)
```
<!--ReasonLIGO-->
```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);
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
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-->
```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-->
```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)), ());
};
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
### 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-->
```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-->
```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)), ());
};
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Cross contract calls

View File

@ -49,6 +49,39 @@ function main (const p : action ; const s : int) : (list(operation) * int) is
| Decrement (n) -> s - n
end)
```
<!--CameLIGO-->
```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-->
```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);
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
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)'
<!--END_DOCUSAURUS_CODE_TABS-->
Now we can use `(Right 5)` which is a Michelson value, to invoke our contract - e.g. via `tezos-client`
Now we can use `(Right 5)` which is a Michelson value, to invoke our contract - e.g. via `tezos-client`

View File

@ -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-->
```cameligo group=a
let today: timestamp = Current.time
```
<!--ReasonLIGO-->
```reasonligo group=a
let today: timestamp = Current.time;
```
<!--END_DOCUSAURUS_CODE_TABS-->
> 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-->
```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-->
```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;
```
<!--END_DOCUSAURUS_CODE_TABS-->
#### 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-->
```cameligo group=c
let today: timestamp = Current.time
let one_day: int = 86400
let in_24_hrs: timestamp = today - one_day
```
<!--ReasonLIGO-->
```reasonligo group=c
let today: timestamp = Current.time;
let one_day: int = 86400;
let in_24_hrs: timestamp = today - one_day;
```
<!--END_DOCUSAURUS_CODE_TABS-->
### 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-->
```cameligo group=c
let not_tomorrow: bool = (Current.time = in_24_hrs)
```
<!--ReasonLIGO-->
```reasonligo group=c
let not_tomorrow: bool = (Current.time == in_24_hrs);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Addresses
@ -69,6 +125,17 @@ Here's how you can define an address:
```pascaligo group=d
const my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
```
<!--CameLIGO-->
```cameligo group=d
let my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address)
```
<!--ReasonLIGO-->
```reasonligo group=d
let my_account: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Signatures
@ -111,4 +178,4 @@ let my_key: key = ("edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav": key
```reasonligo group=f
let my_key: key = ("edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav": key);
```
<!--END_DOCUSAURUS_CODE_TABS-->
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -82,6 +82,42 @@ function main (const p : action ; const s : int) : (list(operation) * int) is
| Reset(n) -> 0
end)
```
<!--CameLIGO-->
```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-->
```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);
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
@ -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
```
```

View File

@ -107,15 +107,6 @@ value.
<!--END_DOCUSAURUS_CODE_TABS-->
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=b
const increment : (int -> int) = (function (const i : int) : int is i + 1);
// a = 2
const a: int = increment(1);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## 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.