Skip to main content
Version: Next

Tezos

let get_balance: (_u: unit) => tez

Get the balance for the contract.

let check = (p: unit, s: tez):[list<operation>, tez] =>
[list([]), 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 = Tezos.get_now();
let one_day = 86_400;
let in_24_hrs = today + one_day;
let some_date = ("2000-01-01t10:10:10Z" as timestamp);
let one_day_later = some_date + one_day;

24 hours ago

let today = Tezos.get_now();
let one_day = 86_400;
let in_24_hrs = today - one_day;

Comparing Timestamps

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

let not_tomorrow = (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.

function threshold (p : unit) {
if (Tezos.get_amount() == 100tez) return 42 else return 0;
};

let get_sender: (_u : unit) => address

Get the address that initiated the current transaction.

let check = (p : unit) => Tezos.get_sender ();

let address: (contract: contract<'a>) => address

Get the address associated with a value of type contract.

let check = (p : key_hash) => {
let c = Tezos.implicit_account(p);
return Tezos.address(c);
};

let get_self_address: (_u : unit) => address

Get the address of the currently running contract.

let check = (p : unit) => Tezos.get_self_address();

let self: (entrypoint: string) => contract<'a>

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

let check = (p: unit) => Tezos.self("%default");

let implicit_account : (_: key_hash) => 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 check = (kh: key_hash) => 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 check = (p : unit) => 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;
@entry
let main = (_ignore: unit, storage: 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([]), 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 = Tezos.sapling_empty_state ;

Sapling empty state

let sapling_verify_update: sapling_transaction<'a> => sapling_state<'a> => option<[bytes, [int, sapling_state<'a>]]>

Verify sapling update

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

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 result = [list<operation>, storage];
@entry
function main (i: parameter, store : storage): result {
let my_ticket1 = Option.unopt (Tezos.create_ticket (i, 10n));
let [_x, ret] = Big_map.get_and_update ("hello", Some(my_ticket1), store);
return [list([]), ret]
};

Timelock

let open_chest : (key: chest_key, chest: chest, time: nat) => option<bytes>

Open a timelocked chest given its key and the time.