ligo/gitlab-pages/docs/advanced/entrypoints-contracts.md

481 lines
12 KiB
Markdown
Raw Normal View History

---
id: entrypoints-contracts
2020-02-21 21:13:09 +04:00
title: Main function and Entrypoints
---
import Syntax from '@theme/Syntax';
2020-02-10 22:07:20 +04:00
## Access Functions
2020-02-05 19:28:40 +04:00
A LIGO contract is made of a series of constant and function
declarations. Only functions having a special type can be called when
2020-02-24 14:39:45 +04:00
the contract is activated: we call them *main functions*. A main
2020-02-21 21:13:09 +04:00
function takes two parameters, the *contract parameter* and the
2020-02-10 22:07:20 +04:00
*on-chain storage*, and returns a pair made of a *list of operations*
and a (new) storage.
2020-02-05 19:28:40 +04:00
When the contract is originated, the initial value of the storage is
2020-02-21 21:13:09 +04:00
provided. When a main function is later called, only the parameter is
provided, but the type of a main function contains both.
2020-02-05 19:28:40 +04:00
The type of the contract parameter and the storage are up to the
contract designer, but the type for list operations is not. The return
2020-02-21 21:13:09 +04:00
type of a main function is as follows, assuming that the type
2020-02-12 01:29:12 +04:00
`storage` has been defined elsewhere. (Note that you can use any type
with any name for the storage.)
<Syntax syntax="pascaligo">
2020-02-05 19:28:40 +04:00
```pascaligo skip
type storage is ... // Any name, any type
type return is list (operation) * storage
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
```cameligo skip
type storage = ... // Any name, any type
type return = operation list * storage
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-05 19:28:40 +04:00
```reasonligo skip
type storage = ...; // Any name, any type
type return = (list (operation), storage);
```
</Syntax>
2020-02-21 21:13:09 +04:00
The contract storage can only be modified by activating a main
2020-02-24 14:39:45 +04:00
function: given the state of the storage *on-chain*, a main function
specifies how to create another state for it, depending on the
contract's parameter.
2020-02-05 19:28:40 +04:00
Here is an example where the storage is a single natural number that
is updated by the parameter.
2020-02-05 19:28:40 +04:00
<Syntax syntax="pascaligo">
2020-02-05 19:28:40 +04:00
2019-12-26 17:21:05 +04:00
```pascaligo group=a
2020-02-12 01:29:12 +04:00
type parameter is nat
2020-02-05 19:28:40 +04:00
type storage is nat
type return is list (operation) * storage
2020-02-12 01:29:12 +04:00
function save (const action : parameter; const store : storage) : return is
((nil : list (operation)), store)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=a
2020-02-12 01:29:12 +04:00
type parameter = nat
2020-02-05 19:28:40 +04:00
type storage = nat
2020-02-12 01:29:12 +04:00
type return = operation list * storage
2020-02-05 19:28:40 +04:00
2020-02-12 01:29:12 +04:00
let save (action, store: parameter * storage) : return =
(([] : operation list), store)
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=a
2020-02-12 01:29:12 +04:00
type parameter = nat;
2020-02-05 19:28:40 +04:00
type storage = nat;
2020-02-12 01:29:12 +04:00
type return = (list (operation), storage);
2020-02-05 19:28:40 +04:00
2020-02-12 01:29:12 +04:00
let main = ((action, store): (parameter, storage)) : return =>
(([] : list (operation)), store);
```
</Syntax>
2020-02-10 22:07:20 +04:00
## Entrypoints
2020-02-21 21:13:09 +04:00
In LIGO, the design pattern is to have *one* main function called
`main`, that dispatches the control flow according to its
parameter. Those functions called for those actions are called
*entrypoints*.
2020-02-10 22:07:20 +04:00
As an analogy, in the C programming language, the `main` function is
2020-02-21 21:13:09 +04:00
the unique main function and any function called from it would be an
2020-02-10 22:07:20 +04:00
entrypoint.
The parameter of the contract is then a variant type, and, depending
on the constructors of that type, different functions in the contract
2020-02-21 21:13:09 +04:00
are called. In other terms, the unique main function dispatches the
2020-02-10 22:07:20 +04:00
control flow depending on a *pattern matching* on the contract
parameter.
2020-02-10 22:07:20 +04:00
In the following example, the storage contains a counter of type `nat`
and a name of type `string`. Depending on the parameter of the
contract, either the counter or the name is updated.
2020-02-05 19:28:40 +04:00
<Syntax syntax="pascaligo">
2020-02-05 19:28:40 +04:00
```pascaligo group=b
type parameter is
2020-02-12 01:29:12 +04:00
Action_A of nat
| Action_B of string
2020-02-05 19:28:40 +04:00
type storage is record [
counter : nat;
name : string
]
type return is list (operation) * storage
2020-02-12 01:29:12 +04:00
function entry_A (const n : nat; const store : storage) : return is
2020-02-05 19:28:40 +04:00
((nil : list (operation)), store with record [counter = n])
2020-02-12 01:29:12 +04:00
function entry_B (const s : string; const store : storage) : return is
2020-02-05 19:28:40 +04:00
((nil : list (operation)), store with record [name = s])
2020-02-21 21:13:09 +04:00
function main (const action : parameter; const store : storage): return is
2020-02-12 01:29:12 +04:00
case action of
Action_A (n) -> entry_A (n, store)
| Action_B (s) -> entry_B (s, store)
2020-02-05 19:28:40 +04:00
end
2020-01-28 20:00:36 +04:00
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
```cameligo group=b
type parameter =
2020-02-12 01:29:12 +04:00
Action_A of nat
| Action_B of string
2020-02-05 19:28:40 +04:00
type storage = {
counter : nat;
name : string
}
type return = operation list * storage
2020-02-12 01:29:12 +04:00
let entry_A (n, store : nat * storage) : return =
2020-02-05 19:28:40 +04:00
([] : operation list), {store with counter = n}
2020-02-12 01:29:12 +04:00
let entry_B (s, store : string * storage) : return =
2020-02-05 19:28:40 +04:00
([] : operation list), {store with name = s}
2020-02-21 21:13:09 +04:00
let main (action, store: parameter * storage) : return =
2020-02-12 01:29:12 +04:00
match action with
Action_A n -> entry_A (n, store)
| Action_B s -> entry_B (s, store)
2020-01-28 20:00:36 +04:00
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-05 19:28:40 +04:00
```reasonligo group=b
type parameter =
2020-02-12 01:29:12 +04:00
| Action_A (nat)
| Action_B (string);
2020-02-05 19:28:40 +04:00
type storage = {
counter : nat,
name : string
};
type return = (list (operation), storage);
2020-02-12 01:29:12 +04:00
let entry_A = ((n, store): (nat, storage)) : return =>
(([] : list (operation)), {...store, counter : n});
2020-02-05 19:28:40 +04:00
2020-02-12 01:29:12 +04:00
let entry_B = ((s, store): (string, storage)) : return =>
(([] : list (operation)), {...store, name : s});
2020-02-05 19:28:40 +04:00
2020-02-21 21:13:09 +04:00
let main = ((action, store): (parameter, storage)) : return =>
2020-02-12 01:29:12 +04:00
switch (action) {
| Action_A (n) => entry_A ((n, store))
| Action_B (s) => entry_B ((s, store))
};
2020-01-28 20:00:36 +04:00
```
</Syntax>
2020-02-05 19:28:40 +04:00
## Tezos-specific Built-ins
2020-02-05 19:28:40 +04:00
A LIGO smart contract can query part of the state of the Tezos
blockchain by means of built-in values. In this section you will find
how those built-ins can be utilized.
2020-02-05 19:28:40 +04:00
### Accepting or Declining Tokens in a Smart Contract
This example shows how `Tezos.amount` and `failwith` can be used to
decline any transaction that sends more tez than `0tez`, that is, no
2020-02-05 19:28:40 +04:00
incoming tokens are accepted.
<Syntax syntax="pascaligo">
2020-02-05 19:28:40 +04:00
```pascaligo group=c
type parameter is unit
type storage is unit
type return is list (operation) * storage
2020-02-12 01:29:12 +04:00
function deny (const action : parameter; const store : storage) : return is
if Tezos.amount > 0tez then
2020-02-05 19:28:40 +04:00
(failwith ("This contract does not accept tokens.") : return)
else ((nil : list (operation)), store)
```
2020-03-17 19:05:03 +04:00
> Note that `amount` is *deprecated*. Please use `Tezos.amount`.
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
```cameligo group=c
type parameter = unit
type storage = unit
type return = operation list * storage
2020-02-12 01:29:12 +04:00
let deny (action, store : parameter * storage) : return =
if Tezos.amount > 0tez then
(failwith "This contract does not accept tokens." : return)
2020-02-05 19:28:40 +04:00
else (([] : operation list), store)
```
2020-03-17 19:05:03 +04:00
> Note that `amount` is *deprecated*. Please use `Tezos.amount`.
</Syntax>
<Syntax syntax="reasonligo">
2020-02-05 19:28:40 +04:00
```reasonligo group=c
type parameter = unit;
type storage = unit;
type return = (list (operation), storage);
2020-02-12 01:29:12 +04:00
let deny = ((action, store): (parameter, storage)) : return => {
if (Tezos.amount > 0tez) {
2020-02-05 19:28:40 +04:00
(failwith("This contract does not accept tokens."): return); }
else { (([] : list (operation)), store); };
};
```
2020-03-17 19:05:03 +04:00
> Note that `amount` is *deprecated*. Please use `Tezos.amount`.
</Syntax>
### Access Control
This example shows how `Tezos.source` can be used to deny access to an
entrypoint.
<Syntax syntax="pascaligo">
2019-12-26 17:21:05 +04:00
```pascaligo group=c
2020-02-05 19:28:40 +04:00
const owner : address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
2020-02-21 21:13:09 +04:00
function main (const action : parameter; const store : storage) : return is
if Tezos.source =/= owner then (failwith ("Access denied.") : return)
else ((nil : list (operation)), store)
```
2020-03-17 19:05:03 +04:00
> Note that `source` is *deprecated*. Please use `Tezos.source`.
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=c
2020-02-05 19:28:40 +04:00
let owner : address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address)
2020-02-21 21:13:09 +04:00
let main (action, store: parameter * storage) : return =
if Tezos.source <> owner then (failwith "Access denied." : return)
2020-02-05 19:28:40 +04:00
else (([] : operation list), store)
```
2020-03-17 19:05:03 +04:00
> Note that `source` is *deprecated*. Please use `Tezos.source`.
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=c
2020-02-05 19:28:40 +04:00
let owner : address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address);
2020-03-07 05:39:39 +04:00
let main = ((action, store) : (parameter, storage)) : return => {
if (Tezos.source != owner) { (failwith ("Access denied.") : return); }
2020-02-05 19:28:40 +04:00
else { (([] : list (operation)), store); };
};
```
2020-03-17 19:05:03 +04:00
> Note that `source` is *deprecated*. Please use `Tezos.source`.
</Syntax>
2020-02-05 19:28:40 +04:00
### Inter-Contract Invocations
2020-02-05 19:28:40 +04:00
It would be somewhat misleading to speak of "contract calls", as this
wording may wrongly suggest an analogy between contract "calls" and
function "calls". Indeed, the control flow returns to the site of a
function call, and composed function calls therefore are *stacked*,
that is, they follow a last in, first out ordering. This is not what
happens when a contract invokes another: the invocation is *queued*,
that is, follows a first in, first our ordering, and the dequeuing
only starts at the normal end of a contract (no failure). That is why
we speak of "contract invocations" instead of "calls".
2020-02-05 19:28:40 +04:00
The following example shows how a contract can invoke another by
emiting a transaction operation at the end of an entrypoint.
2020-02-05 19:28:40 +04:00
> The same technique can be used to transfer tokens to an implicit
> account (tz1, ...): all you have to do is use a unit value as the
> parameter of the smart contract.
2020-02-12 01:29:12 +04:00
In our case, we have a `counter.ligo` contract that accepts an action
of type `parameter`, and we have a `proxy.ligo` contract that accepts
the same parameter type, and forwards the call to the deployed counter
contract.
2020-02-05 19:28:40 +04:00
<Syntax syntax="pascaligo">
2020-01-29 20:15:03 +04:00
```pascaligo skip
// counter.ligo
2020-02-05 19:28:40 +04:00
type parameter is
Increment of nat
| Decrement of nat
| Reset
2020-02-05 19:28:40 +04:00
type storage is unit
type return is list (operation) * storage
```
2020-02-05 19:28:40 +04:00
```pascaligo group=d
// proxy.ligo
2020-02-05 19:28:40 +04:00
type parameter is
Increment of nat
| Decrement of nat
| Reset
type storage is unit
type return is list (operation) * storage
const dest : address = ("KT19wgxcuXG9VH4Af5Tpm1vqEKdaMFpznXT3" : address)
2020-02-12 01:29:12 +04:00
function proxy (const action : parameter; const store : storage): return is
2020-02-05 19:28:40 +04:00
block {
const counter : contract (parameter) =
case (Tezos.get_contract_opt (dest) : option (contract (parameter))) of
Some (contract) -> contract
| None -> (failwith ("Contract not found.") : contract (parameter))
end;
2020-02-05 19:28:40 +04:00
(* Reuse the parameter in the subsequent
transaction or use another one, `mock_param`. *)
const mock_param : parameter = Increment (5n);
const op : operation = Tezos.transaction (action, 0tez, counter);
2020-02-05 19:28:40 +04:00
const ops : list (operation) = list [op]
} with (ops, store)
```
2020-02-05 19:28:40 +04:00
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
```cameligo skip
// counter.mligo
2020-02-05 19:28:40 +04:00
2020-02-12 01:29:12 +04:00
type parameter =
2020-02-05 19:28:40 +04:00
Increment of nat
| Decrement of nat
| Reset
// ...
```
2020-02-05 19:28:40 +04:00
```cameligo group=d
// proxy.mligo
2020-02-05 19:28:40 +04:00
type parameter =
Increment of nat
| Decrement of nat
| Reset
type storage = unit
2020-02-05 19:28:40 +04:00
type return = operation list * storage
2020-02-05 19:28:40 +04:00
let dest : address = ("KT19wgxcuXG9VH4Af5Tpm1vqEKdaMFpznXT3" : address)
2020-02-12 01:29:12 +04:00
let proxy (action, store : parameter * storage) : return =
let counter : parameter contract =
match (Tezos.get_contract_opt (dest) : parameter contract option) with
Some contract -> contract
| None -> (failwith "Contract not found." : parameter contract) in
2020-02-05 19:28:40 +04:00
(* Reuse the parameter in the subsequent
transaction or use another one, `mock_param`. *)
let mock_param : parameter = Increment (5n) in
let op : operation = Tezos.transaction action 0tez counter
2020-02-05 19:28:40 +04:00
in [op], store
```
> Note that `Operation.get_contract` and `Operation.transaction` are
> *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
2020-02-05 19:28:40 +04:00
```reasonligo skip
// counter.religo
2020-02-05 19:28:40 +04:00
type parameter =
| Increment (nat)
| Decrement (nat)
| Reset
// ...
```
2020-02-05 19:28:40 +04:00
```reasonligo group=d
// proxy.religo
2020-02-05 19:28:40 +04:00
type parameter =
| Increment (nat)
| Decrement (nat)
| Reset;
type storage = unit;
type return = (list (operation), storage);
2020-02-05 19:28:40 +04:00
let dest : address = ("KT19wgxcuXG9VH4Af5Tpm1vqEKdaMFpznXT3" : address);
2020-02-12 01:29:12 +04:00
let proxy = ((action, store): (parameter, storage)) : return => {
let counter : contract (parameter) =
switch (Tezos.get_contract_opt (dest) : option (contract (parameter))) {
| Some (contract) => contract;
| None => (failwith ("Contract not found.") : contract (parameter));
};
2020-02-05 19:28:40 +04:00
(* Reuse the parameter in the subsequent
transaction or use another one, `mock_param`. *)
let mock_param : parameter = Increment (5n);
let op : operation = Tezos.transaction (action, 0tez, counter);
2020-02-06 14:47:41 +04:00
([op], store)
};
```
> Note that `Operation.get_contract` and `Operation.transaction` are
> *deprecated*.
</Syntax>