Implement a NFT contract
Import FA2 library
import smartpy as sp
FA2 = sp.io.import_script_from_url("https://smartpy.io/templates/fa2_lib.py")
Compilation target
sp.add_compilation_target(
"fa2_nft_tokens",
ExampleFa2Nft(
metadata = sp.utils.metadata_of_url("http://example.com")
)
)
Test
@sp.add_test(name="FA2 NFT tokens")
def test():
sc = sp.test_scenario()
sc.table_of_contents()
sc.h2("FA2")
example_fa2_nft = ExampleFa2Nft(
metadata = sp.utils.metadata_of_url("https://example.com")
)
sc += example_fa2_nft
Now you can run and compile the contract.
Customisation
Mixins The FA2 library provides small classes from which you can inherit separately in order to add additional features. You can see more mixins in the documentation.
class ExampleFa2Nft(
FA2.Admin,
FA2.ChangeMetadata,
FA2.WithdrawMutez,
FA2.OnchainviewBalanceOf,
FA2.Fa2Nft
):
Init Some mixins need be initialized.
class ExampleFa2Nft(FA2.Admin, FA2.Fa2Nft):
def __init__(self, admin, metadata):
FA2.Fa2Nft.__init__(self, metadata)
FA2.Admin.__init__(self, admin)
Storage
class ExampleFa2Nft(FA2.Admin, FA2.Fa2Nft):
def __init__(self, admin, metadata):
FA2.Fa2Nft.__init__(self, metadata)
FA2.Admin.__init__(self, admin)
self.update_initial_storage(
closed = False,
)
Entrypoint Inside the class we can add new entrypoints or reimplement the ones provided by the library.
@sp.entry_point
def mint(self, to_, metadata):
sp.verify(sp.sender == self.data.administrator, "FA2_NOT_ADMIN")
token_id = sp.compute(self.data.next_token_id)
self.data.token_metadata[token_id] = sp.record(
token_id=token_id, token_info=metadata
)
self.data.ledger[token_id] = to_
self.data.next_token_id += 1
You can see more entrypoint examples in Template Minimal.
Contract metadata Contains general descriptions of the contract and the offchain-views. This can be in a JSON file stored on IPFS:
sp.add_compilation_target(
"Fa2Nft_tokens",
ExampleFa2Nft(
metadata = sp.utils.metadata_of_url("http://example.com")
)
)
Or in self contract:
sp.add_compilation_target(
"Fa2Nft_tokens",
ExampleFa2Nft(
metadata_base = {
"version": "1.0.0",
"description" : "This implements FA2 (TZIP-012) using SmartPy.",
"interfaces": ["TZIP-012", "TZIP-016"],
"authors": ["SmartPy <https://smartpy.io/#contact>"],
"homepage": "https://smartpy.io/ide?template=FA2.py",
"source": {
"tools": ["SmartPy"],
"location": "https://gitlab.com/SmartPy/smartpy/-/raw/master/python/templates/FA2.py"
},
"permissions": {
"receiver": "owner-no-hook",
"sender": "owner-no-hook"
}
}
)
)
Reference Links
Last updated