In order for a contract to be considered an FT contract, it must follow the NEP-141 and NEP-148 standards, which define the minimum interface required and expected functionality.
Creating a New Token
The simplest way to create a new Fungible Token is by interacting with a factory contract, to which you only need to provide the token metadata, and they will automatically deploy and initialize a canonical FT contract.<your_token_symbol>.token.primitives.near (e.g. test.token.primitives.near).
Querying Metadata
You can query the FT’s metadata by calling theft_metadata method.
Checking Balance
To know how many tokens a user has, you need to query theft_balance_of method.
Remember to account for the token’s precision (decimals). Check the metadata to get the decimals value and display balances correctly.
Registering a User
In order for a user to own and transfer tokens, they need to first register in the contract by callingstorage_deposit and attaching 0.00125 NEAR.
Transferring Tokens
To send FT to another account, you use theft_transfer method, indicating the receiver and the amount.
Attaching FTs to a Call
Natively, only NEAR tokens can be attached to function calls. However, the FT standard enables attaching fungible tokens by using the FT contract as an intermediary. Instead of attaching tokens directly, you ask the FT contract to do both a transfer and a function call on your behalf usingft_transfer_call.
How it Works
- You call
ft_transfer_callin the FT contract passing: the receiver, a message, and the amount. - The FT contract transfers the amount to the receiver.
- The FT contract calls
receiver.ft_on_transfer(sender, msg, amount). - The FT contract handles errors in the
ft_resolve_transfercallback. - The FT contract returns how much of the attached amount was actually used.
Handling Deposits
If you want your contract to handle deposits in FTs, you must implement theft_on_transfer method:
- Which FT was transferred (predecessor account)
- Who is sending the FT (sender_id parameter)
- How many FT were transferred (amount parameter)
- Any parameters encoded as a message (msg parameter)