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.Manual Interaction
Manual Interaction
Here is how to directly interact with the factory contract through your application:Learn more about adding Near Connect to your applicationThe FT you create will live in the account
- π WebApp
- π₯οΈ CLI
- Lantstool
<your_token_symbol>.token.primitives.near (e.g. test.token.primitives.near).Deploying Your Own Contract
You can also create a fungible token by deploying and initializing a canonical FT contract. On initialization, you will define the tokenβs metadata such as its name (e.g. Ethereum), symbol (e.g. ETH) and total supply (e.g. 10M). You will also define anowner, which will own the tokens total supply.
To initialize a FT contract you will need to deploy it and then call the new method defining the tokenβs metadata.
- π₯οΈ CLI
- Lantstool
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.
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 Ref 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:
Note: The 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 Resources
- NEP-141 standard
- NEP-148 standard
- FT Event Standards
- FT reference implementation
- Fungible Tokens 101 - a set of tutorials that cover how to create a FT contract using Rust.