388 lines
8.7 KiB
Markdown
388 lines
8.7 KiB
Markdown
---
|
|
id: current-reference
|
|
title: Tezos - Things relating to the current execution context
|
|
---
|
|
|
|
# Tezos.balance
|
|
|
|
Get the balance for the contract.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function main (const p : unit; const s: tez) : list (operation) * tez is
|
|
((nil : list (operation)), Tezos.balance)
|
|
```
|
|
|
|
> Note that `balance` and `Current.balance` are *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p,s : unit * tez) = ([] : operation list), Tezos.balance
|
|
```
|
|
|
|
> Note that `balance` and `Current.balance` are *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = ((p,s) : (unit, tez)) =>
|
|
([]: list (operation), Tezos.balance);
|
|
```
|
|
|
|
> Note that `balance` and `Current.balance` are *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Tezos.now
|
|
|
|
Returns the current time as a [unix timestamp](https://en.wikipedia.org/wiki/Unix_time).
|
|
|
|
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
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
<!--Pascaligo-->
|
|
```pascaligo group=b
|
|
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*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo group=b
|
|
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*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo group=b
|
|
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*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
#### 24 hours ago
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
<!--Pascaligo-->
|
|
```pascaligo group=c
|
|
const today: timestamp = Tezos.now;
|
|
const one_day: int = 86_400;
|
|
const in_24_hrs: timestamp = today - one_day;
|
|
```
|
|
|
|
> Note that `now` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo group=c
|
|
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*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo group=c
|
|
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*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
#### Comparing Timestamps
|
|
|
|
You can also compare timestamps using the same comparison operators as
|
|
for numbers
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
<!--Pascaligo-->
|
|
```pascaligo group=c
|
|
const not_tommorow: bool = (Tezos.now = in_24_hrs)
|
|
```
|
|
|
|
> Note that `now` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo group=c
|
|
let not_tomorrow: bool = (Tezos.now = in_24_hrs)
|
|
```
|
|
|
|
> Note that `Current.time` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo group=c
|
|
let not_tomorrow: bool = (Tezos.now == in_24_hrs);
|
|
```
|
|
|
|
> Note that `Current.time` is *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
## Amount
|
|
|
|
Get the amount of tez provided by the sender to complete this
|
|
transaction.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function threshold (const p : unit) : int is
|
|
if Tezos.amount = 100tz then 42 else 0
|
|
```
|
|
|
|
> Note that `amount` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let threshold (p : unit) : int = if Tezos.amount = 100tz then 42 else 0
|
|
```
|
|
|
|
> Note that `Current.amount` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let threshold = (p : unit) : int =>
|
|
if (Tezos.amount == 100tz) { 42; } else { 0; };
|
|
```
|
|
|
|
> Note that `Current.amount` is *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Sender
|
|
|
|
Get the address that initiated the current transaction.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function main (const p : unit) : address is Tezos.sender
|
|
```
|
|
|
|
> Note that `sender` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p: unit) : address = Tezos.sender
|
|
```
|
|
|
|
> Note that `Current.sender` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = (p : unit) : address => Tezos.sender;
|
|
```
|
|
|
|
> Note that `Current.sender` is *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
## Address
|
|
|
|
Get the address associated with a value of type `contract`.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
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` and `address` are *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p : key_hash) =
|
|
let c : unit contract = Tezos.implicit_account p
|
|
in Tezos.address c
|
|
```
|
|
|
|
> Note that `Current.implicit_account` and `Current.address` are
|
|
> *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = (p : key_hash) : address => {
|
|
let c : contract (unit) = Tezos.implicit_account (p);
|
|
Tezos.address (c);
|
|
};
|
|
```
|
|
|
|
> Note that `Current.implicit_account` and `Current.address` are
|
|
> *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Self Address
|
|
|
|
Get the address of the currently running contract.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function main (const p : unit) : address is Tezos.self_address
|
|
```
|
|
|
|
> Note that `self_address` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p : unit) : address = Tezos.self_address
|
|
```
|
|
|
|
> Note that `Current.self_address` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = (p : unit) : address => Tezos.self_address;
|
|
```
|
|
|
|
> Note that `Current.self_address` is *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Implicit Account
|
|
|
|
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.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function main (const kh: key_hash) : contract (unit) is
|
|
Tezos.implicit_account (kh)
|
|
```
|
|
|
|
> Note that `implicit_account` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (kh : key_hash) : unit contract = Tezos.implicit_account kh
|
|
```
|
|
|
|
> Note that `Current.implicit_account` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = (kh : key_hash): contract (unit) =>
|
|
Tezos.implicit_account (kh);
|
|
```
|
|
|
|
> Note that `Current.implicit_account` is *deprecated*.
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Source
|
|
|
|
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.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
function main (const p: unit) : address is Tezos.source
|
|
```
|
|
|
|
> Note that `source` is *deprecated*.
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p : unit) : address = Tezos.source
|
|
```
|
|
|
|
> Note that `Current.source` is *deprecated*.
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = (p : unit) : address => Tezos.source;
|
|
```
|
|
|
|
> Note that `Current.source` is *deprecated*.
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
## Failwith
|
|
|
|
Cause the contract to fail with an error message.
|
|
|
|
> ⚠ Using this currently requires in general a type annotation on the
|
|
> `failwith` call.
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--PascaLIGO-->
|
|
```pascaligo
|
|
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)
|
|
```
|
|
|
|
<!--CameLIGO-->
|
|
```cameligo
|
|
let main (p,s : int * unit) = if p > 10 then failwith "Failure."
|
|
```
|
|
|
|
<!--ReasonLIGO-->
|
|
```reasonligo
|
|
let main = ((p,s) : (int, unit)) =>
|
|
if (p > 10) { failwith ("Failure."); };
|
|
```
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|