Skip to content
On this page

Mixins โ€‹

The FA2 library provides small classes from which you can inherit separately in order to add additional features. They are called mixins.

INFO

In Python the order in which superclasses are listed is important. All mixins should come before the base class, which is typically the last one.

Examples โ€‹

Examples with nearly all mixins activated.

python
class ExampleFa2Nft(
    FA2.Admin,
    FA2.ChangeMetadata,
    FA2.WithdrawMutez,
    # FA2.MintNft,
    # FA2.BurnNft,
    # FA2.OffchainviewTokenMetadata,
    FA2.OnchainviewBalanceOf,
    FA2.Fa2Nft
):
    # ...

class ExampleFa2Fungible(
    FA2.Admin,
    FA2.ChangeMetadata,
    FA2.WithdrawMutez,
    # FA2.MintFungible,
    # FA2.BurnFungible,
    # FA2.OffchainviewTokenMetadata,
    FA2.OnchainviewBalanceOf,
    FA2.Fa2Fungible,
):
    # ...

class ExampleFa2SingleAsset(
    FA2.Admin,
    FA2.ChangeMetadata,
    FA2.WithdrawMutez,
    # FA2.MintSingleAsset,
    # FA2.BurnSingleAsset,
    # FA2.OffchainviewTokenMetadata,
    FA2.OnchainviewBalanceOf,
    FA2.Fa2SingleAsset,
):
    # ...

All mixins โ€‹

Here are all the available mixins:

mixindescription
Adminis_administrator method and set_administrator entrypoint.
BurnNftAn example of a burn entrypoint that relies on the transfer policy permissions for the Fa2Nft base class. The entrypoint does remove the token_metadata when burning a token.
BurnFungibleSame for the Fa2Fungible base class. The entrypoint does not remove the token_metadata when burning a token.
BurnSingleAssetSame for the Fa2SingleAsset base class. The entrypoint does not remove the token_metadata when burning a token.
ChangeMetadataset_metadata entrypoint to change contract's metadata. Requires Admin mixin.
MintNftAn example of a mint entrypoint for the Fa2Nft base class.
MintFungibleSame for the Fa2Fungible base class.
MintSingleAssetSame for the Fa2Fungible base class.
OnchainviewBalanceOfNon-standard get_balance_of on-chain view that mimics balance_of entrypoint logic with a view.
OffChainViewTokenMetadatatoken_metadata off-chain view. If present indexers use it to retrieve the token's metadata. Warning โš ๏ธ If someone can change the contract's metadata, he can change how indexers see every token metadata.
WithdrawMutezwithdraw_mutez entrypoint for withdrawal of Tez from the contract's balance. Requires Admin mixin.

Mixins' doc can be found in detail in the pdoc of fa2_lib.

Init โ€‹

When imported certain mixins need to be initialized.

Example:

python
class ExampleFa2Nft(
    FA2.Admin,
    FA2.ChangeMetadata,
    FA2.WithdrawMutez,
    FA2.MintNft,
    FA2.OnchainviewBalanceOf,
    FA2.Fa2Nft,
):
    def __init__(self, admin, metadata, token_metadata = {}, ledger = {}, policy = None, metadata_base = None):
        FA2.Fa2Nft.__init__(self, metadata, token_metadata = token_metadata, ledger = ledger, policy = policy, metadata_base = metadata_base)
        FA2.Admin.__init__(self, admin)

Mint mixins โ€‹

Mint and burn entrypoints are not standard. One can imagine a lot of type of mint operation.

The mint logic can even be placed in another contract named a minter. In this case, the mint entrypoint is simply verifying the minter permission and register the mint without any other logic.

We provide mint and burn mixins that can be used as an example.

WARNING

The Mint* mixins we provide doesn't give way to modify the metadata of tokens after they are minted.

The Mint* mixins need the Admin mixin to work.

Example:

python
class NftWithMint(FA2.Admin, FA2.MintNft, FA2.Fa2Nft):
    def __init__(self, admin, **kwargs):
        Fa2Nft.__init__(self, **kwargs)
        Admin.__init__(self, admin)

Mint (NFT) โ€‹

The MintNft mixin provides a mint entrypoint that takes a list of mint actions. Each mint action is a record of:

  • to_: address of who receives the minted tokens
  • metadata: the token metadata

Example:

python
fa2_admin = sp.test_account("fa2_admin")
    NFT0 = FA2.make_metadata(
        name     = "Example FA2",
        decimals = 0,
        symbol   = "EFA2" )
    NFT1 = FA2.make_metadata(
        name     = "Example FA2",
        decimals = 0,
        symbol   = "EFA2-2" )
    example_fa2_nft.mint(
        [
            sp.record(
                to_  = fa2_admin.address, # Who will receive the original mint
                metadata = NFT0
            ),
            sp.record(
                to_  = fa2_admin.address,
                metadata = NFT1
            ),
        ]
    ).run(sender = fa2_admin)

The token id is an incremental nat.

Mint (fungible) โ€‹

The MintFungible mixin provides a mint entrypoint that takes a list of mint actions. Each mint action is a record of:

  • amount: the amount of token to mint
  • to_: address of who receives the mint
  • token: variant with 2 possible values:
    • "new": the token metadata
    • "existing": the token id of an existing token.

Example:

python
TOKEN0 = FA2.make_metadata(
        name     = "Example FA2",
        decimals = 0,
        symbol   = "EFA2" )
    example_fa2_fungible.mint(
        [
            sp.record(
                to_  = FA2_admin.address, # Who will receive the original mint
                amount   = 100_000_000_000,
                token = variant("new", TOKEN0)
            ),
            sp.record(
                to_  = FA2_admin.address
                amount   = 100_000_000_000,
                token = variant("existing", 0)
            ),
        ]
    ).run(sender = FA2_admin)

The token id is an incremental nat.

Mint (single asset) โ€‹

The Fa2SingleAsset class doesn't come with a mint mixin. The reason is that the majority of time the token is minted at origination time using the pre-mint mechanism.

Feel free to write your own entrypoint if you have a specific use case.