Reputation: 21
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();
Upvotes: 1
Views: 381
Reputation: 66
NearBlocks released its Indexer API in 2024, which implements an endpoint to retrieve a list of fungible and non-fungible tokens of a particular Near account. Your NEAR balance must still be fetched over account.getAccountBalance()
.
The following endpoint returns a list of FTs and NFTs owned by {account}
(please replace with a real account id, as root.near
):
GET https://api.nearblocks.io/v1/account/{account}/inventory
curl -X GET "https://api.nearblocks.io/v1/account/root.near/inventory
{
"inventory": {
"fts": [
{
"contract": "token.v2.ref-finance.near",
"amount": "9569963195962599927",
"ft_meta": {
"name": "Ref Finance Token",
"symbol": "REF",
"decimals": 18,
"icon": "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='16 24 248 248' style='background: %23000'%3E%3Cpath d='M164,164v52h52Zm-45-45,20.4,20.4,20.6-20.6V81H119Zm0,18.39V216h41V137.19l-20.6,20.6ZM166.5,81H164v33.81l26.16-26.17A40.29,40.29,0,0,0,166.5,81ZM72,153.19V216h43V133.4l-11.6-11.61Zm0-18.38,31.4-31.4L115,115V81H72ZM207,121.5h0a40.29,40.29,0,0,0-7.64-23.66L164,133.19V162h2.5A40.5,40.5,0,0,0,207,121.5Z' fill='%23fff'/%3E%3Cpath d='M189 72l27 27V72h-27z' fill='%2300c08b'/%3E%3C/svg%3E%0A",
"reference": null,
"price": 0.355141
}
}
],
"nfts": []
}
}
Remember that the free tier is limited to a certain number of requests per day, and for more significant use cases, you may consider upgrading to a paid plan.
Upvotes: 0
Reputation: 1094
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:
Upvotes: 2