Version: 0.49.0

Tezos

let get_balance: (_u: unit) => tez

Get the balance for the contract.

let main = ([p, s] : [unit, tez]):[list<operation>, tez] =>
[(list([]) as list<operation>), Tezos.get_balance()];
let get_now: (_u : unit) => 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.get_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.get_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.get_now() == in_24_hrs);
let get_amount: (_u : unit) => tez

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

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

Get the address that initiated the current transaction.

let main = (p : unit) : address => Tezos.get_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 get_self_address: (_u : unit) => address

Get the address of the currently running contract.

let main = (p : unit): address => Tezos.get_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 get_source: (_u : unit) => 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.get_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.get_source over Tezos.get_sender:

  1. Tezos.get_source will never be a contract, so if you want to allow contracts (multisigs etc) to operate your contract, you need to use Tezos.get_sender
  2. https://vessenes.com/tx-origin-and-ethereum-oh-my/ -- in general it is somewhat unsafe to assume that Tezos.get_source understands everything that is going to happen in a transaction. If Tezos.get_source transfers to a malicious (or sufficiently attackable) contract, that contract might potentially transfer to yours, without Tezos.get_source's consent. So if you are using Tezos.get_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.get_source();
let failwith: (message: 'a) => unit

See failwith

let get_chain_id: (_u : unit) => 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.get_chain_id());
if (storage != packed) {
return failwith("wrong chain") as [list<operation>, storage];
} else {
return [(list([]) as list<operation>), packed];
};
};
let transaction: (action: 'param, amount: mutez, contract: contract<'param>) => operation

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

To indicate an account, use unit as param.

let create_contract = (contract: ('param, 'storage) => (list <operation>, 'storage), delegate: option<key_hash>, balance: tez, init: 'storage) => [operation, address]

Construct an operation that originates a contract from a function. The optional argument of type key_hash represents a delegate.

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<'param>>

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<'param>>

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<'param>>

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 get_level : (_u : unit) => nat

Get the current block level.

let min_block_time: unit => nat;

Returns the current minimal time between blocks, the value is obtained from the protocol’s minimal_block_delay constant.

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.

let get_total_voting_power: (_u : unit) => nat

Return the total voting power of all contracts. The total voting power coincides with the sum of the rolls count of every contract in the voting listings. The voting listings is calculated at the beginning of every voting period.

let voting_power: (key_hash:key_hash) => nat

Return the voting power of a given contract. The voting power value is the full staking power of the delegate, currently expressed in mutez. Though, developers should not rely on Tezos.voting_power to query the staking power of a contract in mutez: the value returned by Tezos.voting_power is still of typenat and it should only be considered relative toTezos.total_voting_power`.

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<'a> => sapling_state<'a> => option<int, sapling_state<'a>>

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
});
let join_tickets = <ticket<'value>, ticket<'value>> => option <ticket<'value>>

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]
};

Chest#

type chest
A type for chests
type chest_key
A type for chest keys
type chest_opening_result = ["Ok_opening", bytes] | ["Fail_decrypt"] | ["Fail_timelock"];

A type for the result of chest opening, see Tezos.open_chest

let open_chest : chest_key => chest => nat => chest_opening_result

On Chain Views#

let call_view : string => 'arg => address => option <'ret>

The primitive Tezos.call_view will allow you to call another contract view and get its result by providing the view name; the contract address and the parameter of the view. If the address is nonexistent; the name does not match of of the contract view or the parameter type do not match, Tezos.call_view will return None.

Global Constants#

let constant : string => 'a

The new primitive Tezos.constant allows you to use a predefined constant already registered on chain. It accepts a hash in the form of a string and will require a type annotation.