FungibleToken API
This module provides the full FungibleToken module API.
For an overview of the module, read the FungibleToken guide.
import "./compact-contracts/node_modules/@openzeppelin-compact/contracts/src/token/FungibleToken";Ledger
_balances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>
ledger
#Mapping from account addresses to their token balances.
_allowances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>>
ledger
#Mapping from owner accounts to spender accounts to their allowances.
_totalSupply: Uint<128>
ledger
#The total token supply.
_name: Opaque<"string">
sealed ledger
#The immutable token name.
_symbol: Opaque<"string">
sealed ledger
#The immutable token symbol.
_decimals: Uint<8>
sealed ledger
#The immutable token decimals.
Witnesses
None.
Circuits
initialize(
name_: Opaque<"string">,
symbol_: Opaque<"string">,
decimals_: Uint<8>
) → []circuit
#Initializes the contract by setting the name, symbol, and decimals.
This MUST be called in the implementing contract’s constructor. Failure to do so can lead to an irreparable contract.
Requirements:
- Contract is not initialized.
name() → Opaque<"string">
circuit
#Returns the token name.
Requirements:
- Contract is initialized.
symbol() → Opaque<"string">
circuit
#Returns the symbol of the token.
Requirements:
- Contract is initialized.
decimals() → Uint<8>
circuit
#Returns the number of decimals used to get its user representation.
Requirements:
- Contract is initialized.
totalSupply() → Uint<128>
circuit
#Returns the value of tokens in existence.
Requirements:
- Contract is initialized.
balanceOf(account: Either<ZswapCoinPublicKey, ContractAddress>) → Uint<128>
circuit
#Returns the value of tokens owned by account.
Requirements:
- Contract is initialized.
transfer(to: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → Boolean
circuit
#Moves a value amount of tokens from the caller’s account to to.
Transfers to contract addresses are currently disallowed until contract-to-contract interactions are supported in Compact. This restriction prevents assets from being inadvertently locked in contracts that cannot currently handle token receipt.
Requirements:
- Contract is initialized.
tois not a ContractAddress.tois not the zero address.- The caller has a balance of at least
value.
_unsafeTransfer(to: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → Boolean
circuit
#Unsafe variant of transfer which allows transfers to contract addresses.
Transfers to contract addresses are considered unsafe because contract-to-contract calls are not currently supported. Tokens sent to a contract address may become irretrievable. Once contract-to-contract calls are supported, this circuit may be deprecated.
Requirements:
- Contract is initialized.
tois not the zero address.- The caller has a balance of at least
value.
allowance(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>
) → Uint<128>circuit
#Returns the remaining number of tokens that spender will be allowed to spend on behalf of owner through transferFrom.
This value changes when approve or transferFrom are called.
Requirements:
- Contract is initialized.
approve(spender: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → Boolean
circuit
#Sets a value amount of tokens as allowance of spender over the caller’s tokens.
Requirements:
- Contract is initialized.
spenderis not the zero address.
transferFrom(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → Booleancircuit
#Moves value tokens from from to to using the allowance mechanism.
value is then deducted from the caller’s allowance.
Transfers to contract addresses are currently disallowed until contract-to-contract interactions are supported in Compact. This restriction prevents assets from being inadvertently locked in contracts that cannot currently handle token receipt.
Requirements:
- Contract is initialized.
fromis not the zero address.frommust have a balance of at leastvalue.tois not the zero address.tois not a ContractAddress.- The caller has an allowance of
from’s tokens of at leastvalue.
_unsafeTransferFrom(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → Booleancircuit
#Unsafe variant of transferFrom which allows transfers to contract addresses.
Transfers to contract addresses are considered unsafe because contract-to-contract calls are not currently supported. Tokens sent to a contract address may become irretrievable. Once contract-to-contract calls are supported, this circuit may be deprecated.
Requirements:
- Contract is initialized.
fromis not the zero address.frommust have a balance of at leastvalue.tois not the zero address.- The caller has an allowance of
from’s tokens of at leastvalue.
_transfer(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → []circuit
#Moves a value amount of tokens from from to to.
This circuit is equivalent to transfer, and can be used to e.g.
implement automatic token fees, slashing mechanisms, etc.
Transfers to contract addresses are currently disallowed until contract-to-contract interactions are supported in Compact. This restriction prevents assets from being inadvertently locked in contracts that cannot currently handle token receipt.
Requirements:
- Contract is initialized.
fromis not the zero address.frommust have at least a balance ofvalue.tomust not be the zero address.tomust not be a ContractAddress.
_unsafeUncheckedTransfer(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → []circuit
#Unsafe variant of _transfer which allows transfers to contract addresses.
Transfers to contract addresses are considered unsafe because contract-to-contract calls are not currently supported. Tokens sent to a contract address may become irretrievable. Once contract-to-contract calls are supported, this circuit may be deprecated.
Requirements:
- Contract is initialized.
fromis not the zero address.tois not the zero address.
_update(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → []internal
#Transfers a value amount of tokens from from to to,
or alternatively mints (or burns) if from (or to) is the zero address.
Requirements:
- Contract is initialized.
_mint(account: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → []
circuit
#Creates a value amount of tokens and assigns them to account, by transferring it from the zero address.
Relies on the update mechanism.
Requirements:
- Contract is initialized.
tois not a ContractAddress.accountis not the zero address.
_unsafeMint(account: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → []
circuit
#Unsafe variant of _mint which allows transfers to contract addresses.
Transfers to contract addresses are considered unsafe because contract-to-contract calls are not currently supported. Tokens sent to a contract address may become irretrievable. Once contract-to-contract calls are supported, this circuit may be deprecated.
Requirements:
- Contract is initialized.
accountis not the zero address.
_burn(account: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → []
circuit
#Destroys a value amount of tokens from account, lowering the total supply.
Relies on the _update mechanism.
Requirements:
- Contract is initialized.
accountis not the zero address.accountmust have at least a balance ofvalue.
_approve(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → []circuit
#Sets value as the allowance of spender over the owner’s tokens.
This circuit is equivalent to approve, and can be used to e.g. set automatic allowances for certain subsystems, etc.
Requirements:
- Contract is initialized.
owneris not the zero address.spenderis not the zero address.
_spendAllowance(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>,
value: Uint<128>
) → []circuit
#Updates owner’s allowance for spender based on spent value.
Does not update the allowance value in case of infinite allowance.
Requirements:
- Contract is initialized.
spendermust have at least an allowance ofvaluefromowner.