17 KiB
id | title | description | hide_table_of_contents |
---|---|---|---|
current-reference | Tezos | General operations for Tezos | true |
import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle';
type timestamp type timestamp type timestampA date in the real world.
type tez type tez type tezA specific type for tokens.
type address type address type addressAn untyped address which can refer to a smart contract or account.
type contract('parameter) type 'parameter contract type contract('parameter)A typed contract.
Use unit
as parameter
to indicate an implicit account.
An operation emitted by the contract
type chain_id type chain_id type chain_idThe identifier of a chain, used to indicate test or main chains.
function balance : tez val balance : tez let balance: tezGet the balance for the contract.
function main (const p : unit; const s: tez) : list (operation) * tez is
((nil : list (operation)), Tezos.balance)
Note that
balance
andCurrent.balance
are deprecated.
let main (p,s : unit * tez) = ([] : operation list), Tezos.balance
Note that
balance
andCurrent.balance
are deprecated.
let main = ((p,s) : (unit, tez)) =>
([]: list (operation), Tezos.balance);
function now : timestamp val now : timestamp let now: timestampNote that
balance
andCurrent.balance
are deprecated.
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
const today: timestamp = Tezos.now;
const one_day: int = 86_400;
const in_24_hrs: timestamp = today + one_day;
const some_date: timestamp = ("2000-01-01T10:10:10Z" : timestamp);
const one_day_later: timestamp = some_date + one_day;
Note that
now
is deprecated. Please useTezos.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" : timestamp)
let one_day_later: timestamp = some_date + one_day
Note that
Current.time
is deprecated.
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" : timestamp);
let one_day_later: timestamp = some_date + one_day;
Note that
Current.time
is deprecated.
24 hours ago
const today: timestamp = Tezos.now;
const one_day: int = 86_400;
const in_24_hrs: timestamp = today - one_day;
Note that
now
is deprecated. Please useTezos.now
.
let today: timestamp = Tezos.now
let one_day: int = 86_400
let in_24_hrs: timestamp = today - one_day
Note that
Current.time
is deprecated.
let today: timestamp = Tezos.now;
let one_day: int = 86_400;
let in_24_hrs: timestamp = today - one_day;
Note that
Current.time
is deprecated.
Comparing Timestamps
You can also compare timestamps using the same comparison operators as for numbers
const not_tommorow: bool = (Tezos.now = in_24_hrs)
Note that
now
is deprecated. Please useTezos.now
.
let not_tomorrow: bool = (Tezos.now = in_24_hrs)
Note that
Current.time
is deprecated.
let not_tomorrow: bool = (Tezos.now == in_24_hrs);
function amount : tez val amount : tez let amount: tezNote that
Current.time
is deprecated.
Get the amount of tez provided by the sender to complete this transaction.
function threshold (const p : unit) : int is
if Tezos.amount = 100tz then 42 else 0
Note that
amount
is deprecated.
let threshold (p : unit) : int = if Tezos.amount = 100tz then 42 else 0
Note that
Current.amount
is deprecated.
let threshold = (p : unit) : int =>
if (Tezos.amount == 100tz) { 42; } else { 0; };
function sender : address val sender : address let sender: addressNote that
Current.amount
is deprecated.
Get the address that initiated the current transaction.
function main (const p : unit) : address is Tezos.sender
Note that
sender
is deprecated. Please useTezos.sender
.
let main (p: unit) : address = Tezos.sender
Note that
Current.sender
is deprecated.
let main = (p : unit) : address => Tezos.sender;
function address : contract 'a -> address val address : 'a contract -> address let address: contract('a) => addressNote that
Current.sender
is deprecated.
Get the address associated with a value of type contract
.
function main (const p : key_hash) : address is block {
const c : contract (unit) = Tezos.implicit_account (p)
} with Tezos.address(c)
Note that
implicit_account
andaddress
are deprecated. Please useTezos.implicit_account
andTezos.address
instead.
let main (p : key_hash) =
let c : unit contract = Tezos.implicit_account p
in Tezos.address c
Note that
Current.implicit_account
andCurrent.address
are deprecated.
let main = (p : key_hash) : address => {
let c : contract (unit) = Tezos.implicit_account (p);
Tezos.address (c);
};
function self_address : address val self_address : address let self_address: addressNote that
Current.implicit_account
andCurrent.address
are deprecated.
Get the address of the currently running contract.
function main (const p : unit) : address is Tezos.self_address
Note that
self_address
is deprecated. Please useTezos.self_address
.
let main (p : unit) : address = Tezos.self_address
Note that
Current.self_address
is deprecated.
let main = (p : unit) : address => Tezos.self_address;
function self : string -> contract 'a val self : string -> 'a contract let self: string => contract('a)Note that
Current.self_address
is deprecated.
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"
function main (const p : unit) : contract(unit) is block {
const c : contract(unit) = Tezos.self("%Default") ;
} with c
let main (p : unit) : unit contract =
(Tezos.self("%Default") : unit contract)
let main = (p: unit) : contract(unit) =>
(Tezos.self("%Default") : contract(unit));
function implicit_account : key_hash -> contract 'a
val implicit_account : key_hash -> 'a contract
let implicit_account: key_hash => contract('a)
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
function main (const kh: key_hash) : contract (unit) is
Tezos.implicit_account (kh)
Note that
implicit_account
is deprecated. Please useTezos.implicit_account
.
let main (kh : key_hash) : unit contract = Tezos.implicit_account kh
Note that
Current.implicit_account
is deprecated.
let main = (kh : key_hash): contract (unit) =>
Tezos.implicit_account (kh);
function source : address val source : address let source: addressNote that
Current.implicit_account
is deprecated.
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
overTezos.sender
:
Tezos.source
will never be a contract, so if you want to allow contracts (multisigs etc) to operate your contract, you need to useTezos.sender
- 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. IfTezos.source
transfers to a malicious (or sufficiently attackable) contract, that contract might potentially transfer to yours, withoutTezos.source
's consent. So if you are usingTezos.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.
function main (const p: unit) : address is Tezos.source
Note that
source
is deprecated. Please useTezos.source
.
let main (p : unit) : address = Tezos.source
Note that
Current.source
is deprecated.
let main = (p : unit) : address => Tezos.source;
function failwith : string -> unit function failwith : string -> unit function failwith : string -> unitNote that
Current.source
is deprecated.
Cause the contract to fail with an error message.
⚠ Using this currently requires in general a type annotation on the
failwith
call.
function main (const p : int; const s : unit) : list (operation) * unit is
block {
if p > 10 then failwith ("Failure.") else skip
}
with ((nil : list (operation)), s)
let main (p,s : int * unit) = if p > 10 then failwith "Failure."
let main = ((p,s) : (int, unit)) =>
if (p > 10) { failwith ("Failure."); };
function chain_id : chain_id
val chain_id : chain_id
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 is bytes
function main (const ignore : unit; const storage: storage) :
(list(operation) * storage) is block {
const packed : bytes = Bytes.pack (Tezos.chain_id);
if (storage =/= packed) then {
failwith("wrong chain")
} else
skip;
} with ((nil: list(operation)), packed)
type storage = bytes
let main ((ignore, storage): (unit * storage)) =
let packed = Bytes.pack Tezos.chain_id in
if (storage <> packed) then
(failwith "wrong chain" : (operation list * storage))
else
(([]: operation list), (packed: storage))
type storage = bytes;
let main = ((ignore, storage): (unit, storage)) => {
let packed = Bytes.pack(Tezos.chain_id);
if (storage != packed) {
(failwith("wrong chain"): (list(operation), storage));
} else {
([]: list(operation), packed);
}
};
function transaction : 'parameter -> mutez -> contract('parameter) -> operation
val transaction : 'parameter -> mutez -> 'parameter contract -> operation
let transaction: 'parameter -> mutez -> contract('parameter) -> operation
Create a transaction to a contract or account.
To indicate an account, use unit
as parameter
.
Create a delegation.
See also: http://tezos.gitlab.io/user/glossary.html?highlight=delegate#delegate
function get_contract_opt : address -> option(contract('parameter)) val get_contract_opt : address -> 'parameter contract option let get_contract_opt : 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.
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.