Skip to content
On this page

Base classes โ€‹

FA2 contracts can be created by extending one of the 3 base classes:

  • FA2.Fa2Nft
  • FA2.Fa2Fungible
  • FA2.Fa2SingleAsset

These classes have different specialized ledger type. Apart from that they share the same functionalities and interface. We don't recommend having both fungible and non-fungible tokens in the same contract.

When inheriting a base class you obtain a standard FA2 contract with offchain views and metadata.

Fa2Nft โ€‹

This class represents NFTs.

Ledger type: sp.TBigmap(sp.TNat, sp.TAddress)
Key: <token_id>
Value: <owner>

python
class ExampleFa2Nft(FA2.Fa2Nft):
    pass

Code documentation:

Fa2Nft

Fa2Fungible โ€‹

This class represents fungible tokens.

Ledger type: sp.TBigmap(sp.TPair(sp.TAddress, sp.TNat), sp.TNat)
Key: (<owner>,ย <token_id>)
Value: <amount>

python
class ExampleFa2Fungible(FA2.Fa2Fungible):
    pass

Code documentation:

Fa2Fungible

Fa2SingleAsset โ€‹

This class represents a single (fungible) token.

Ledger type: sp.TBigmap(sp.Address, sp.TNat)
Key: <owner>
Value: <amount>

python
class ExampleFa2SingleAsset(FA2.Fa2SingleAsset):
    pass

Code documentation:

Fa2SingleAsset

Updating the storage โ€‹

The base classes defines the storage.

If you want to add custom entry you can use self.update_initial_storage(attributeย = value, ...). See sp.update_initial_storage

Example:

python
class NftWithCustomStorage(FA2.Fa2Nft):
    def __init__(self, metadata, **kwargs):
        FA2.Fa2Nft.__init__(self, metadata, **kwargs)
        self.update_initial_storage(
            closed = False,
            special_rights = sp.big_map({}, tkey = sp.Address, tvalue = sp.TSet(sp.TBytes))
        )