NonFungibleToken API
This module provides the full NonFungibleToken module API.
For an overview of the module, read the NonFungibleToken guide.
Core
NonFungibleToken
import "./node-modules/@openzeppelin-compact/contracts/src/token/NonFungibleToken";
Ledger
_name: Opaque<"string">
sealed ledger
#The immutable token name.
_symbol: Opaque<"string">
sealed ledger
#The immutable token symbol.
_owners: Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>
ledger
#Mapping from token IDs to their owner addresses.
_balances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>
ledger
#Mapping from account addresses to their token balances.
_tokenApprovals: Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>
ledger
#Mapping from token IDs to approved addresses.
_operatorApprovals: Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>>
ledger
#Mapping from owner addresses to operator approvals.
_tokenURIs: Map<Uint<128>, Opaque<"string">>
ledger
#Mapping from token IDs to their metadata URIs.
Witnesses
None.
Circuits
initialize(name_: Opaque<"string">, symbol_: Opaque<"string">) → []
circuit
#Initializes the contract by setting the name and symbol.
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.
balanceOf(owner: Either<ZswapCoinPublicKey, ContractAddress>) → Uint<128>
circuit
#Returns the number of tokens in owner
's account.
Requirements:
- Contract is initialized.
ownerOf(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>
circuit
#Returns the owner of the tokenId
token.
Requirements:
- The contract is initialized.
- The
tokenId
must exist.
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.
tokenURI(tokenId: Uint<128>) → Opaque<"string">
circuit
#Returns the token URI for the given tokenId
.
Returns an empty string if a tokenURI does not exist.
Requirements:
- The contract is initialized.
- The
tokenId
must exist.
Native strings and string operations aren’t supported within the Compact language, e.g. concatenating a base URI + token ID is not possible like in other NFT implementations. Therefore, we propose the URI storage approach; whereby, NFTs may or may not have unique "base" URIs. It’s up to the implementation to decide on how to handle this.
_setTokenURI(tokenId: Uint<128>, tokenURI: Opaque<"string">) → []
circuit
#Sets the the URI as tokenURI
for the given tokenId
.
Requirements:
- The contract is initialized.
- The
tokenId
must exist.
approve(to: Either<ZswapCoinPublicKey, ContractAddress>, tokenId: Uint<128>) → []
circuit
#Gives permission to to
to transfer tokenId
token to another account.
The approval is cleared when the token is transferred.
Only a single account can be approved at a time, so approving the zero address clears previous approvals.
Requirements:
- The contract is initialized.
- The caller must either own the token or be an approved operator.
tokenId
must exist.
getApproved(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>
circuit
#Returns the account approved for tokenId
token.
Requirements:
- The contract is initialized.
tokenId
must exist.
setApprovalForAll(operator: Either<ZswapCoinPublicKey, ContractAddress>, approved: Boolean) → []
circuit
#Approve or remove operator
as an operator for the caller.
Operators can call transferFrom for any token owned by the caller.
Requirements:
- The contract is initialized.
- The
operator
cannot be the zero address.
isApprovedForAll(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
operator: Either<ZswapCoinPublicKey, ContractAddress>
) → Boolean
circuit
#Returns if the operator
is allowed to manage all of the assets of owner
.
Requirements:
- The contract must have been initialized.
transferFrom(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
circuit
#Transfers tokenId
token from from
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:
- The contract is initialized.
from
is not the zero address.to
is not the zero address.to
is not a ContractAddress.tokenId
token must be owned byfrom
.- If the caller is not
from
, it must be approved to move this token by either approve or setApprovalForAll.
_unsafeTransferFrom(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
circuit
#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:
- The contract is initialized.
from
is not the zero address.to
is not the zero address.tokenId
token must be owned byfrom
.- If the caller is not
from
, it must be approved to move this token by either approve or setApprovalForAll.
_ownerOf(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>
circuit
#Returns the owner of the tokenId
. Does NOT revert if token doesn’t exist
Requirements:
- The contract is initialized.
_getApproved(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>
circuit
#Returns the approved address for tokenId
.
Returns the zero address if tokenId
is not minted.
Requirements:
- The contract is initialized.
_isAuthorized(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → Boolean
circuit
#Returns whether spender
is allowed to manage owner
's tokens,
or tokenId
in particular (ignoring whether it is owned by owner
).
Requirements:
- The contract is initialized.
This function assumes that owner
is the actual owner of tokenId
and does not verify this assumption.
_checkAuthorized(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
circuit
#Checks if spender
can operate on tokenId
, assuming the provided owner
is the actual owner.
Requirements:
- The contract is initialized.
spender
has approval fromowner
fortokenId
ORspender
has approval to manage all ofowner
's assets.
This function assumes that owner
is the actual owner of tokenId
and does not verify this assumption.
_checkAuthorized(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
spender: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
internal
#Transfers tokenId
from its current owner to to
,
or alternatively mints (or burns) if the current owner (or to
) is the zero address.
Returns the owner of the tokenId
before the update.
Requirements:
- The contract is initialized.
- If
auth
is non 0, then this function will check thatauth
is either the owner of the token, or approved to operate on the token (by the owner).
_mint(to: Either<ZswapCoinPublicKey, ContractAddress>, tokenId: Uint<128>) → []
circuit
#Mints tokenId
and transfers it to to
.
Requirements:
- The contract is initialized.
tokenId
must not exist.to
is not the zero address.to
is not a ContractAddress.
_unsafeMint(account: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>) → []
circuit
#Unsafe variant of _mint which allows transfers to contract addresses.
Requirements:
- Contract is initialized.
tokenId
must not exist.to
is not the zero address.
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.
_burn(tokenId: Uint<128>) → []
circuit
#Destroys tokenId
.
The approval is cleared when the token is burned.
This circuit does not check if the sender is authorized to operate on the token.
Requirements:
- The contract is initialized.
tokenId
must exist.
_transfer(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
circuit
#Transfers tokenId
from from
to to
. As opposed to transferFrom,
this imposes no restrictions on ownPublicKey()
.
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:
- The contract is initialized.
to
is not the zero address.to
is not a ContractAddress.tokenId
token must be owned byfrom
.
_unsafeTransfer(
from: Either<ZswapCoinPublicKey, ContractAddress>,
to: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>
) → []
circuit
#Unsafe variant of _transfer which allows transfers to contract addresses.
Transfers tokenId
from from
to to
. As opposed to _unsafeTransferFrom, this imposes no restrictions on ownPublicKey()
. It does NOT check if the recipient is a ContractAddress
.
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.
to
is not the zero address.tokenId
token must be owned byfrom
.
_approve(
to: Either<ZswapCoinPublicKey, ContractAddress>,
tokenId: Uint<128>,
auth: Either<ZswapCoinPublicKey, ContractAddress>
) → []
circuit
#Approve to
to operate on tokenId
.
Requirements:
- The contract is initialized.
- If
auth
is non 0, then this function will check thatauth
is either the owner of the token, or approved to operate on the token (by the owner).
_setApprovalForAll(
owner: Either<ZswapCoinPublicKey, ContractAddress>,
operator: Either<ZswapCoinPublicKey, ContractAddress>,
approved: Boolean
) → []
circuit
#Approve operator
to operate on all of owner
tokens
Requirements:
- The contract is initialized.
operator
is not the zero address.
_requireOwned(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>
circuit
#Reverts if the tokenId
doesn’t have a current owner (it hasn’t been minted, or it has been burned).
Returns the owner.
Requirements:
- The contract is initialized.
tokenId
must exist.