How do I get a token's name, symbol, decimals, etc.?

Viewed 181

e.g. does getting the name look like this?

args := fmt.Sprintf("{\"tokenOwner\":\"%s\"}", "bob.near")
argsBase64 := base64.StdEncoding.EncodeToString([]byte(args))
param := map[string]string{
        "request_type": "call_function",
        "finality":     "final",
        "account_id":   "ref-finance.near",
        "method_name":  "name",
        "args_base64":  argsBase64,
}
1 Answers

This is part of the metadata of each token. You can read the metadata standard at nomicon.io.

In particular you can query the metadata of an NEP-141 Fungible Token using the function ft_metadata as following:

❯ export NEAR_ENV=mainnet

❯ near view 76a6baa20598b6d203d3eae6cc87e326bcb60e43.factory.bridge.near ft_metadata "{}"

View call: 76a6baa20598b6d203d3eae6cc87e326bcb60e43.factory.bridge.near.ft_metadata({})
{
  spec: 'ft-1.0.0',
  name: 'Law Diamond Token',
  symbol: 'nLDT',
  icon: 'https://near.org/wp-content/themes/near-19/assets/img/brand-icon.png',
  reference: '',
  reference_hash: '',
  decimals: 18
}

Update: Make this call directly from the RPC.

You can query the RPC directly as follows:

curl --location --request POST 'https://archival-rpc.mainnet.near.org/' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": "dontcare",
  "method": "query",
  "params": {
    "request_type": "call_function",
    "finality": "final",
    "account_id": "76a6baa20598b6d203d3eae6cc87e326bcb60e43.factory.bridge.near",
    "method_name": "ft_metadata",
    "args_base64": "e30="
  }
}'

args_base64 field are the arguments serialised as base64. In this case it is an empty json:

base64("{}") = "e30="

The result is given as a sequence of bytes. In the case of ft_metadata it should be first decoded as a string and then decoded as json.

Related