API Reference

NonFungibleToken API

This module provides the full NonFungibleToken module API.

For an overview of the module, read the NonFungibleToken guide.

Core

NonFungibleToken

View on GitHub

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.
k=10, rows=65

balanceOf(owner: Either<ZswapCoinPublicKey, ContractAddress>) → Uint<128>

circuit

#

Returns the number of tokens in owner's account.

Requirements:

  • Contract is initialized.
k=10, rows=309

ownerOf(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>

circuit

#

Returns the owner of the tokenId token.

Requirements:

  • The contract is initialized.
  • The tokenId must exist.
k=10, rows=290

name() → Opaque<"string">

circuit

#

Returns the token name.

Requirements:

  • Contract is initialized.
k=10, rows=36

symbol() → Opaque<"string">

circuit

#

Returns the symbol of the token.

Requirements:

  • Contract is initialized.
k=10, rows=36

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.

k=10, rows=296

_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.
k=10, rows=253

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.
k=10, rows=966

getApproved(tokenId: Uint<128>) → Either<ZswapCoinPublicKey, ContractAddress>

circuit

#

Returns the account approved for tokenId token.

Requirements:

  • The contract is initialized.
  • tokenId must exist.
k=10, rows=409

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.
k=10, rows=409

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.
k=10, rows=621

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 by from.
  • If the caller is not from, it must be approved to move this token by either approve or setApprovalForAll.
k=11, rows=1966

_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 by from.
  • If the caller is not from, it must be approved to move this token by either approve or setApprovalForAll.
k=11, rows=1963

_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.
k=10, rows=253

_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.
k=10, rows=253

_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.

k=11, rows=1098

_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 from owner for tokenId OR spender has approval to manage all of owner's assets.

This function assumes that owner is the actual owner of tokenId and does not verify this assumption.

k=11, rows=1121

_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 that auth is either the owner of the token, or approved to operate on the token (by the owner).
k=12, rows=2049

_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.
k=10, rows=1013

_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.

k=10, rows=1010

_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.
k=10, rows=479

_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 by from.
k=11, rows=1224

_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 by from.
k=11, rows=1221

_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 that auth is either the owner of the token, or approved to operate on the token (by the owner).
k=11, rows=1109

_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.
k=10, rows=524

_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.
k=10, rows=288