1

I have estblished a wallet connection in the browser, but I'm only able to see my NEAR balance. How can I for example retrieve my other NEAR-related assets such as wrapped USDC, DAI, Aurora, etc? (NEP-141)

Here is the account object I get back from my wallet connection:
{
    "accessKeyByPublicKeyCache": {},
    "connection": {
        "networkId": "testnet",
        "provider": {
            "connection": {
                "url": "https://rpc.testnet.near.org"
            }
        },
        "signer": {
            "keyStore": {
                "localStorage": {
                    "undefined_wallet_auth_key": "{\"accountId\":\"xxx.testnet\",\"allKeys\":[\"xxx\"]}",
                    "near-api-js:keystore:xxx.testnet:testnet": "ed25519:xxx",
                    "showBalance": "true",
                    "theme": "dark"
                },
                "prefix": "near-api-js:keystore:"
            }
        }
    },
    "accountId": "xxx.testnet"
}

Examples would be greatly appreciated!

I tried the above example. But I did not receive all balances, but just NEAR.

onst account = await nearConnection.account("example-account.testnet"); await account.getAccountBalance();

Matt
  • 21
  • 2

1 Answers1

2

Unfortunately, there is no trivial or native way of doing this. Due to the fact that your user balance for any given asset (non native) lives on the asset's own smart contract and not your account's state, there isn't really a clean way to query for how much of each you own.

If you knew which contracts you had assets on, you could loop through each of them and call the method ft_balance_of which is actually what many wallets do behind the scenes.

You may be wondering how these wallets keep track / know of which contracts any given account has assets on and this comes down to the idea of indexers and events. A very high level explanation is that they run indexers to sift through all transactions on the blockchain and check for any events being emitted by smart contracts that would indicate that your account has assets. The indexers would then add this information in a database and then when you open up the wallet, an API call is made to retrieve the set of contracts that your account has assets on.

The long winded answer to your question is that you have two options that I can think of. You can:

  • Emulate what these wallets are doing and run your own indexer to populate a database and expose an API that you can use (this is not preferred and is quite complicated)
  • Search for an existing API that contains this sort of information
Benjamin Kurrek
  • 1,044
  • 3
  • 14