Transfers, contract calls

Transfer

The transfer instruction is used to transfer Tezis to an account.
1
effect {
2
transfer 1tz to coder;
3
}
Copied!
where coder is a role variable.

Calling a contract

It is possible to call a smart contract with the transfer instruction. Say for example you want to call the set_value entry point of the following contract:
1
archetype contract_called
2
3
variable v : nat = 0
4
5
entry set_value(x : nat) { v := x }
Copied!
The contract may then be called with the transfer ... to ... call ... instruction:
1
effect {
2
var c = @KT1RNB9PXsnp7KMkiMrWNMRzPjuefSWojBAm;
3
transfer 0tz to c call add_value<nat>(3);
4
}
Copied!
Note that it fails if the contract at address c does not provide an entry point annotated %add_value

Self

It is possible to call the current contract itself. Say the current contract has the my_add_value entry point; it is possible to call it from another entry point with the following instruction:
1
entry my_add_value(a : nat, b : nat) {
2
sum := a + b;
3
}
4
5
entry add_1_3 () {
6
transfer 0tz to entry self.my_add_value(1,3)
7
}
Copied!

Getter & contract

A common pattern for contracts to exchange values is to call an entry point with a callback.
This pattern is presented in the two-way-inter-contract invocation article below:
Enabling Smart Contract Interaction in Tezos with LIGO Functions and CPS
Medium
The archetype version of the sender contract (see article above):
1
archetype sender
2
3
variable bar : nat = 0
4
5
getter getBar () { return bar }
6
7
entry setBar (b : nat) { bar := b }
Copied!
It uses the getter keyword used to declare an entry point to the contract called "getBar"; the Michelson version of this entry actually takes a callback function (a setter) used to set/use the bar value in another contract. It is syntactic sugar equivalent to the following entry declaration:
1
entry getBar (cb : contract<nat>) { transfer 0tz to entry cb(bar) }
Copied!
The contract type is used to declare the callback type; it is parametrized by the signature of the callback, represented as the tuple of argument types.
The difference between the getter and entry versions of the getBar entry above is that the callback argument is anonymous in the getter version.
The archetype version of the inspector contract (see article above):
1
archetype inspector
2
3
variable foo : int = 0
4
5
entry setFoo(v : int) { foo := v }
6
7
entry getFoo(asender : address) {
8
transfer 0tz to asender call getBar<contract<int>>(self.setFoo)
9
}
Copied!

Entrypoint

The entrypoint function may be used to build a contract value from the name and the address. It returns an option value of contract type, so that it is possible to handle the case when the entry point does not exist.
The require_entrypoint function builds a contract and fails with error message if contract is invalid. The above inspector contract may be rewritten as:
1
archetype inspector
2
3
variable foo : int = 0
4
5
entry setFoo(v : int) { foo := v }
6
7
entry getFoo(asender : address) {
8
var e = require_entrypoint<int>("%getBar", asender, "invalid address");
9
transfer 0tz to entry e(self.setFoo))
10
}
Copied!
Last modified 2mo ago
Export as PDF
Copy link
Edit on GitHub