Version: 0.28.0

Tezos

let balance: tez

Get the balance for the contract.

let main = ([p, s] : [unit, tez]):[list<operation>, tez] =>
[(list([]) as list<operation>), Tezos.balance];
let now: timestamp

Returns the current time as a UNIX timestamp.

In LIGO, timestamps are type compatible in operations with integers. This lets you set for instance time constraints for your smart contracts like this:

Examples#

24 hours from now#

let today: timestamp = Tezos.now;
let one_day: int = 86_400;
let in_24_hrs: timestamp = today + one_day;
let some_date: timestamp = ("2000-01-01t10:10:10Z" as timestamp);
let one_day_later: timestamp = some_date + one_day;

24 hours ago#

let today: timestamp = Tezos.now;
let one_day: int = 86_400;
let in_24_hrs: timestamp = today - one_day;

Comparing Timestamps#

You can also compare timestamps using the same comparison operators as for numbers

let not_tomorrow: bool = (Tezos.now == in_24_hrs);
let amount: tez

Get the amount of tez provided by the sender to complete this transaction.

let threshold = (p : unit) : int => {
if (Tezos.amount == (100 as tez)) { return 42; } else { return 0; };
};
let sender: address

Get the address that initiated the current transaction.

let main = (p : unit) : address => Tezos.sender;
let address: (contract: contract<'a>) => address

Get the address associated with a value of type contract.

let main = (p : key_hash): address => {
let c: contract<unit> = Tezos.implicit_account(p);
return Tezos.address(c);
};
let self_address: address

Get the address of the currently running contract.

let main = (p : unit): address => Tezos.self_address;
let self: (entrypoint: string) => contract<'a>

Typecast the currently running contract with an entrypoint annotation. If your are using entrypoints: use "%bar" for constructor Bar If you are not using entrypoints: use "%default"

let main = (p: unit) : contract<unit> =>
(Tezos.self("%default") as contract<unit>);

Get the default contract associated with an on-chain key-pair. This contract does not execute code, instead it exists to receive tokens on behalf of a key's owner.

See also: http://tezos.gitlab.io/user/glossary.html#implicit-account

let main = (kh: key_hash): contract<unit> =>
Tezos.implicit_account(kh);
let source: address

Get the originator (address) of the current transaction. That is, if a chain of transactions led to the current execution get the address that began the chain. Not to be confused with Tezos.sender, which gives the address of the contract or user which directly caused the current transaction.

⚠️ There are a few caveats you should keep in mind before using Tezos.source over Tezos.sender:

  1. Tezos.source will never be a contract, so if you want to allow contracts (multisigs etc) to operate your contract, you need to use Tezos.sender
  2. https://vessenes.com/tx-origin-and-ethereum-oh-my/ -- in general it is somewhat unsafe to assume that Tezos.source understands everything that is going to happen in a transaction. If Tezos.source transfers to a malicious (or sufficiently attackable) contract, that contract might potentially transfer to yours, without Tezos.source's consent. So if you are using Tezos.source for authentication, you risk being confused. A good historical example of this is bakers paying out delegation rewards. Naive bakers did (and probably still do) just use tezos-client to transfer to whatever KT1 delegates they had, even if those KT1 were malicious scripts.
let main = (p : unit) : address => Tezos.source;
let failwith: (message: 'a) => unit

See failwith

let chain_id: chain_id

Get the identifier of the chain to distinguish between main and test chains.

This is mainly intended to avoid replay attacks between the chains, and can currently only be used together with Bytes.pack and Bytes.unpack.

type storage = bytes;
let main = ([ignore, storage]: [unit, storage]):[list<operation>, storage] => {
let packed = Bytes.pack(Tezos.chain_id);
if (storage != packed) {
failwith("wrong chain") as [list<operation>, storage];
} else {
return [(list([]) as list<operation>), packed];
};
};
let transaction: (action: 'parameter, amount: mutez, contract: contract<'parameter>) => operation

Transfer tez to an account, or run code of another smart contract.

To indicate an account, use unit as parameter.

let set_delegate: (delegate: option<key_hash>) => operation

Modify the delegate of the current contract.

The operation fails when:

  • the delegate is the same as current delegate
  • the keyhash is not of a registered delegate

Use None to withdraw the current delegate.

let get_contract_opt : (a: address) => option<contract<'parameter>>

Get a contract from an address.

When no contract is found or the contract doesn't match the type, None is returned.

let get_contract_with_error : (a: address,s: string) => contract<'parameter>>

Get a contract from an address.

When no contract is found, fail with the provided string

let get_entrypoint_opt: (entrypoint: string, a: address) => option<contract<'parameter>>

Get a contract from an address and entrypoint.

Entrypoints are written in the form of: %entrypoint.

When no contract is found or the contract doesn't match the type, None is returned.

let level : nat

Get the current block level.

let pairing_check: list<[bls12_381_g1, bls12_381_g2]>) => bool

Verify that the product of pairings of the given list of points is equal to 1 in Fq12. Returns true if the list is empty. Can be used to verify if two pairings P1 and P2 are equal by verifying P1 * P2^(-1) = 1. (extracted from Tezos documentation)

let never: (never: never) => 'a

Eliminate a value of the type never using the instruction NEVER from Michelson.

Sapling

Delphi protocol introduced the following sapling types (state and transaction) with N being an int singleton

type st = sapling_state<8>;
type tr = sapling_transaction<8>;
let sapling_empty_state: sapling_state<N>
let x : st = Tezos.sapling_empty_state ;

Sapling empty state

let sapling_verify_update: sapling_transaction<N> => sapling_state<N> => option<int, sapling_state<N>>

Verify sapling update

let f = (tr : tr) : [int , st] =>
match (Tezos.sapling_verify_update(tr, x), {
Some: (x: [int, st]) => x,
None: () => (failwith ("failed") as [int , st])
});

Tickets

let create_ticket: 'value => nat => ticket<'value>

To create a ticket, the value and the amount of tickets to be created needs to be provided. The ticket will also contain the contract address it originated from (which corresponds to Tezos.self).

let my_ticket1 : ticket<int> = Tezos.create_ticket(1, 10 as nat);
let my_ticket2 : ticket<string> = Tezos.create_ticket("one", 10 as nat);
let read_ticket: ticket<'value> => <<address, <'value , nat>> , ticket<'value>>

Reading a ticket will return a tuple with the ticket address, the value and the same ticket for later use. A ticket is only consumed when it is dropped (e.g. DROP-ed from the Michelson stack) so if the returned ticket isn't stored in some form by your contract, it will be fully consumed.

To read the content of a ticket, you need to use tuple destructuring

let v2 = (_: unit): string => {
let [[addr, [v, amt]], ticket] = Tezos.read_ticket(my_ticket2);
return v;
}
let split_ticket: ticket<'value> => <nat , nat> => option <<ticket<'value>, ticket<'value>>>

To partially use/consume a ticket, you have to split it. Provided a ticket and two amounts, two new tickets will be returned to you if, and only if, the sum equals to the amount of the original ticket.

let [ta, tb] =
match(Tezos.split_ticket(my_ticket1, [6 as nat, 4 as nat]), {
None: () => (failwith("amt_a + amt_v != amt") as [ticket<int>, ticket<int>]),
Some: (split_tickets: [ticket<int>, ticket<int>]) => split_tickets
});

To add two tickets, you have to join them. This works as the inverse of Tezos.split_ticket. Provided two tickets with the same ticketer and content, they are deleted and a new ticket will be returned with an amount equal to the sum of the amounts of the input tickets.

let ta = Tezos.create_ticket(1, 10 as nat);
let tb = Tezos.create_ticket(1, 5 as nat);
let tc = Tezos.join_tickets([ta, tb]);

Linearity#

If a contract storage type contains a ticket, you must destructure the parameter-storage pair within the body to preserve storage linearity (e.g. avoid DUP-ing storage). For the same reasons, if tickets are stored in a map/big_map you must use the new operator get_and_update to update your bindings.

type storage = big_map<string, ticket<int>> ;
type parameter = int ;
type return_ = [list<operation>, storage];
let main = (x: [parameter, storage]): return_ => {
let [i, store] = x ;
let my_ticket1: ticket<int> = Tezos.create_ticket (i, 10 as nat);
let [_, x] = Big_map.get_and_update ("hello", Some(my_ticket1), store);
return [list([]) as list<operation>, x]
};