- Storage fee management
- Escrow pattern and derive macro
- Owner pattern and derive macro
- Pause pattern and derive macro
- Role-based access control
- Derive macros for NEP standards
Introduction
While one can create a non-fungible token (NFT) contract from scratch using only thenear-sdk and near_contract_standards (e.g. NFT contract), a simpler approach is to use near-sdk-contract-tools.
near-sdk-contract-tools allows you to implement the minting/burning logic, access control, and other NFT standards by simply deriving macros on the contract’s struct, as OpenZeppelin does for Ethereum contracts.
Basic NFT Methods
To derive basic NFT methods to your smart contract, you need to derive theNonFungibleToken macro to the contract’s struct:
This will bring all the basic NFT methods to the contract:
newcontract_source_metadatanft_is_approvednft_metadatanft_supply_for_ownernft_tokennft_tokensnft_tokens_for_ownernft_total_supplynft_approvenft_resolve_transfernft_revokenft_revoke_allnft_transfernft_transfer_callstorage_balance_boundsstorage_balance_ofstorage_depositstorage_unregisterstorage_withdraw
Owner macro, which adds the following methods:
own_get_ownerown_get_proposed_ownerown_accept_ownerown_propose_ownerown_renounce_owner
Initialization
To initialize the basic NFT contract with a custom owner, metadata, and storage bounds, implement thenew method:
Transfer Hook
If you want to customize how the token transfer works (i.e., modify thenft_transfer method), you need to implement a hook. Hooks are a way to wrap (inject code before and after) component functions:
Then derive it to our contract struct:
Minting
By default, the NFT standards do not include a minting method. However, you can easily mint tokens for the owner by implementing annft_mint method:
Burning
In the same way that minting is not included in the NFT standards, burning is also not included. However, you can also easily implement it. To allow users to burn their tokens, you can add aburn method:
Conclusion
Usingnear-sdk-contract-tools is a simple and flexible way to create an NFT contract with minimal boilerplate, which allows you to focus on the business logic.
You can further extend this contract with more features like pausing, role-based access control, escrow pattern, and more by deriving corresponding macros from the package.