Learn how to create, transfer, and integrate FT in your dApp
Wanting to use Fungible Tokens (FT) in your dApp? Here you will find all the information you need to get started creating your own tokens, registering users
, querying balances, transferring tokens, and integrating them into your smart contracts.
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
Here is how to directly interact with the factory contract through your application:
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 an owner, 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.
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.
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.
To know how many coins a user has you will need to query the method ft_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.
In order for a user to own and transfer tokens they need to first register in the contract. This is done by calling storage_deposit and attaching 0.00125β.By calling this storage_deposit the user can register themselves or register other users.
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.
If you want your contract to handle deposit in FTs you have to implement the ft_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
The 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