useNearWallet hook from the Hello NEAR provider setup.
Creating a New Token
Use the existing global FT contract to create a token. Reuse its deployed code and configure the owner, initial supply, and metadata through initialization arguments.Global Contracts
You can deploy a new Fungible Token using our global FT contract - a pre-deployed standard FT contract that you can reuse. Global contracts are deployed once and can be reused by any account without incurring high storage costs.- By Account
- By Hash
Deploying by hash creates an immutable contract that never changes. Deploying by account ID creates an updatable contract that changes when the referenced account’s contract is updated. Choose based on whether you want your FT contract to be updatable or permanent.
Creating a Token with a Factory
You can also create a Fungible Token by interacting with a factory contract, to which you provide the owner, initial supply, and token metadata. The factory will automatically deploy and initialize a canonical FT contract.
Both factories expose
get_required and create_token, and both methods take { args } on either network. The token account is the symbol in lowercase, e.g. TTTEST becomes tttest.token.primitives.near, so pick a symbol that has not been taken yet.
Manual Interaction
Manual Interaction
Creating a token takes two steps: call Learn more about setting up the NEAR provider in your application
get_required to quote the storage deposit (which depends on the metadata size, including the required icon), then call create_token attaching that amount. total_supply is a string in the smallest token units: multiply the number of whole tokens by 10^decimals.- 🌐 WebApp
- 🖥️ CLI
- Lantstool
Querying Metadata
You can query the FT’s metadata by calling theft_metadata.
- 🌐 WebApp
- 🖥️ CLI
- Lantstool
Checking Balance
To know how many coins a user has you will need to query the methodft_balance_of.
- 🌐 WebApp
- 🖥️ CLI
- Lantstool
Remember about fungible token precision. You may need this value to show a response of balance requests in an understandable-to-user way in your app. How to get precision value (decimals) you may find here.
Example response
Example response
Registering a User
In order for a user to own and transfer tokens they need to first register in the contract. This is done by callingstorage_deposit and attaching 0.00125Ⓝ.
By calling this storage_deposit the user can register themselves or register other users.
- 🌐 WebApp
- 🖥️ CLI
- Lantstool
You can make sure a user is registered by calling
storage_balance_of.Transferring Tokens
To send FT to another account you will use theft_transfer method, indicating the receiver and the amount of FT you want to send.
- 🌐 WebApp
- 🖥️ CLI
- Lantstool
- 📄 Contract
Attaching FTs to a Call
Natively, only NEAR tokens (Ⓝ) can be attached to a function calls. However, the FT standard enables to attach fungible tokens in a call by using the FT-contract as intermediary. This means that, instead of you attaching tokens directly to the call, you ask the FT-contract to do both a transfer and a function call in your name. Let’s assume that you need to deposit FTs on Rhea Finance.- 🌐 WebApp
- 🖥️ CLI
- Lantstool
- 📄 Contract
- 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 you how much of the attached amount was actually used.
Handling Deposits
If you want your contract to handle deposit in FTs you have to implement theft_on_transfer method. When executed, such method will know:
- Which FT was transferred, since it is the predecessor account.
- Who is sending the FT, since it is a parameter
- How many FT were transferred, since it is a parameter
- If there are any parameters encoded as a message
ft_on_transfer must return how many FT tokens have to be refunded, so the FT contract gives them back to the sender.
Here is an example from our auctions tutorial where we implement ft_on_transfer to handle bids in FTs:
lib.rs
near_contract_standards::fungible_token::receiver module exposes a FungibleTokenReceiver trait that you could implement on your contract
Burn Tokens
While the FT standard does not define aburn method, you can simply transfer tokens to an account that no one controls, such as 0000000000000000000000000000000000000000000000000000000000000000 (64 zeros).
- 🌐 WebApp
- 🖥️ CLI
Additional Application Behavior
Use the global FT contract by default. Only if your application truly requires behavior it does not provide, usenear-contract-standards or near-sdk-contract-tools for the token implementation. Keep balances, transfers, storage management, and transfer resolution in these libraries; do not implement the FT standard by hand.
Additional Resources
- NEP-141 standard
- NEP-148 standard
- FT Event Standards
- FT reference implementation
- FT with Contract Tools - library-provided token functionality when a global contract cannot meet the application’s needs.