Fungible tokens
Introduction
Please see the spec for the fungible token standard and an example implementation for reference details.
One notable aspect of the standard is that method names are prefixed with ft_
. This will be a helpful convention when querying for transactions related to fungible tokens.
Get balance
Using the abstraction of the NEAR CLI tool, we can check the balance of a user's account with near view
:
near view ft.demo.testnet ft_balance_of '{"account_id": "mike.testnet"}'
Returns:
View call: ft.demo.testnet.ft_balance_of({"account_id": "mike.testnet"})
'1000000'
Alternatively, you can call a contract function using the query
RPC endpoint. Below is an example using HTTPie:
http post https://rpc.testnet.near.org jsonrpc=2.0 id=ftbalance method=query \
params:='{
"request_type": "call_function",
"finality": "final",
"account_id": "ft.demo.testnet",
"method_name": "ft_balance_of",
"args_base64": "eyJhY2NvdW50X2lkIjogIm1pa2UudGVzdG5ldCJ9"
}'
Returns:
HTTP/1.1 200 OK
Alt-Svc: clear
Via: 1.1 google
access-control-allow-origin:
content-length: 176
content-type: application/json
date: Thu, 27 May 2021 12:53:38 GMT
{
"id": "dontcare",
"jsonrpc": "2.0",
"result": {
"block_hash": "3mvNHpZAsXiJ6SuHU1mbLVB4iXCfh5i5d41pnkaSoaJ5",
"block_height": 49282350,
"logs": [],
"result": [ 34, 49, 48, 48, 48, 48, 48, 48, 34 ]
}
}
As mentioned earlier, the result
is an array of bytes. There are various ways to convert bytes into a more human-readable form such as the dtool CLI.
dtool a2h '[34,49,48,48,48,48,48,48,34]' | dtool h2s
Returns:
"1000000"
Note: The fungible token balance of the account mike.testnet
is 1000000
wrapped in double-quotes. This is because of an issue with JSON serialization. Amounts given in arguments and results must be serialized as Base-10 strings, e.g. "100". This is done to avoid JSON limitation of max integer value of 2**53, which can certainly happen with fungible tokens.
Get info about the FT
You can get name
, decimals
, icon
and other parameters by calling the next function:
- using NEAR CLI:
near view <contract_account_id> ft_metadata
Result:
View call: ft.demo.testnet.ft_metadata()
{
spec: 'ft-1.0.0',
name: 'Example Token Name',
symbol: 'MOCHI',
icon: null,
reference: null,
reference_hash: null,
decimals: 24
}
- with JSON RPC call:
http post https://rpc.testnet.near.org jsonrpc=2.0 id=ftmetadata method=query \
params:='{
"request_type": "call_function",
"finality": "final",
"account_id": "<contract_account_id>",
"method_name": "ft_metadata",
"args_base64": ""
}'