- 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
- NEP-141 (fungible token), extension NEP-148
- NEP-145 (storage management), and integrations for the fungible token and non-fungible token standards
- NEP-171 (non-fungible token), extensions NEP-177, NEP-178, NEP-181
- NEP-297 (events)
Introduction
While one can create a fungible token (FT) contract from scratch using only thenear-sdk and near_contract_standards (e.g. FT contract), a simpler approach is to use near-sdk-contract-tools.
near-sdk-contract-tools allows us implement the logic for minting/burning logic, access control, and other FT standards by simply deriving macros on our contract struct, as OpenZeppelin does for Ethereum contracts.
Basic FT Methods
To derive basic FT methods to our contract, we need to deriveFungibleToken macro to our contract struct:
This will bring all the basic FT methods defined in NEP-141 standard to our contract:
newcontract_source_metadataft_balance_offt_metadataft_total_supplystorage_balance_boundsstorage_balance_offt_resolve_transferft_transferft_transfer_callstorage_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 FT contract with custom owner, metadata and storage bounds implementnew method:
Transfer Hook
If we want to customize how the transfer of tokens work (i.e. modify theft_transfer method), we 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 FT standards do not include a minting method. However, we can easily mint tokens for the owner by implementing amint method, which only the owner can call:
Burning
In the same way that minting is not included in the FT standards, burning is also not included. However, we can also easily implement it. To burn tokens from the owner’s account, we can add aburn method which is also only callable by the owner:
Conclusion
Usingnear-sdk-contract-tools is a very simple and flexible way to create FT contract with minimal boilerplate which allows us 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.