Reputation: 2930
I have created a token using SPL. And I have minted some supply into a wallet address on testnet
Token address: 668JcT5AiLYNi8XVaDNntTaLWzuQ8EnbBzA9zSmKbipW (https://explorer.solana.com/address/668JcT5AiLYNi8XVaDNntTaLWzuQ8EnbBzA9zSmKbipW?cluster=testnet)
Recipient wallet address: A9CCmocX2jNXMFuqqHg6uxT4sptFTTws3qXn5tTo5sy9 (https://explorer.solana.com/address/A9CCmocX2jNXMFuqqHg6uxT4sptFTTws3qXn5tTo5sy9/tokens?cluster=testnet)
In the token holdings section of the recipient I can see a balance of .026203001
Meanwhile back in my React frontend I am trying to display the balances for various SPL tokens held by the recipient address
import * as solanaWeb3 from '@solana/web3.js';
..
..
const solanaWeb3Connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl('testnet'))
const balance = await solanaWeb3Connection.getBalance( new solanaWeb3.PublicKey(metadata.publicAddress)) //SOL balance I guess
const tokenAccounts = await solanaWeb3Connection.getTokenAccountsByOwner(new solanaWeb3.PublicKey(<recipient address>),
{ mint : new solanaWeb3.PublicKey(<spl token address>) }, "finalized")
but I get an odd response showing a lamports value of 2039280 (see below). What is the correct way retrieve the .026203001 value
{
"context": {
"slot": 113853707
},
"value": [
{
"account": {
"data": {
"type": "Buffer",
"data": [
'.....'
]
},
"executable": false,
"lamports": 2039280,
"owner": {
"_bn": "06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9"
},
"rentEpoch": 276
},
"pubkey": {
"_bn": "f3b25516176b4d976c2808080643441ded068758cf33244fad11d6e388d54f55"
}
}
]
}
Upvotes: 2
Views: 1754
Reputation: 1317
You would find the balance in the account data which would need to be parsed from the returned buffer.
Alternatively, the RPC will do this for you if you use getParsedTokenAccountsByOwner
instead of getTokenAccountsByOwner
. Then the response would look something like this:
{
"data": {
"parsed": {
"info": {
"isNative": false,
"mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"owner": "34M7Z6d1SWhnNkFk5YjPCJqVV67hLqaJsG5RXmjyLBki",
"state": "initialized",
"tokenAmount": {
"amount": "950709",
"decimals": 6,
"uiAmount": 0.950709,
"uiAmountString": "0.950709"
}
},
"type": "account"
},
"program": "spl-token",
"space": 165
},
"executable": false,
"lamports": 2039280,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"rentEpoch": 18446744073709552000,
"space": 165
}
Upvotes: 1