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.
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:
mixin | description |
---|---|
Admin | is_administrator method and set_administrator entrypoint. |
BurnNft | An 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. |
BurnFungible | Same for the Fa2Fungible base class. The entrypoint does not remove the token_metadata when burning a token. |
BurnSingleAsset | Same for the Fa2SingleAsset base class. The entrypoint does not remove the token_metadata when burning a token. |
ChangeMetadata | set_metadata entrypoint to change contract's metadata. Requires Admin mixin. |
MintNft | An example of a mint entrypoint for the Fa2Nft base class. |
MintFungible | Same for the Fa2Fungible base class. |
MintSingleAsset | Same for the Fa2Fungible base class. |
OnchainviewBalanceOf | Non-standard get_balance_of on-chain view that mimics balance_of entrypoint logic with a view. |
OffChainViewTokenMetadata | token_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. |
WithdrawMutez | withdraw_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:
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:
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 tokensmetadata
: the token metadata
Example:
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 mintto_
: address of who receives the minttoken
: variant with 2 possible values:"new"
: the token metadata"existing"
: the token id of an existing token.
Example:
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.