Info {
data: <Buffer 00 00 00 00 94 f7 95 8c c0 31 ae b3 72 e0 42 65 62 06 ea c6 76 c4 ef ae bb a2 16 bd 87 07 5a 05 10 44 4b c4 2f 29 b5 f7 4c 59 f0 06 09
01 00 00 00 00 ... 32 more bytes>,
executable: false,
lamports: 2378332431981,
owner: PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)] {
_bn: <BN: 6ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9>
},
rentEpoch: 18446744073709552000,
space: 82
}
here is info about account 7BgBvyjrZX1YKz4oh9mjb8ZScatkkwb8DzFx7LoiVkM3.I made it with this command: const accountInfo = await connection.getAccountInfo(
new web3.PublicKey(address)
);
How I can deserialize something from data?
3 Answers
Copying a bit from How to deserialize data from getProgramAccounts?:
Try using getParsedAccountInfo
, https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getParsedAccountInfo, which will correctly parse this account.
Given the owner (TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
) and the length (82), this is an SPL Token Mint account.
In js,you can use getParsedAccountInfo
from @solana/web3.js
which can simply just parse a pubkey to get the Account Info
But I want to know how to do that in a Rust client
Below is a code example to get Decimals and Supply in Solana.
But if you want to get Name and Symbol, the method is different. To do this, you should determine the address of the Metadata Account in Metaplex.
import { PublicKey } from '@solana/web3.js';
import { MintLayout } from '@solana/spl-token';
/**
* Function to get token details
* @param {Connection} connection - Connection to the Solana network
* @param {string} tokenMintAddress - Address of the token's mint
* @returns {Object} - Token details { name, symbol, decimals }
*/
export async function getDecimalsAndSupplyToken(solanaConnection, tokenMintAddress) {
let mintPublicKey;
let mintAccountInfo;
try {
mintPublicKey = new PublicKey(tokenMintAddress);
} catch (error) {
console.error(`mintPublicKey: `, error);
return null;
}
try {
// Get information about the mint account
// mintAccountInfo = await solanaConnection.getParsedAccountInfo(mintPublicKey);
mintAccountInfo = await solanaConnection.getAccountInfo(mintPublicKey);
// console.log(`mintAccountInfo`);
// console.log(mintAccountInfo.value.data);
// console.log(mintAccountInfo.value.data.parsed);
} catch (error) {
console.error(`mintAccountInfo: `, error);
return null;
}
// console.log(mintAccountInfo.data.length);
// console.log(AccountLayout.span);
let decodedData;
try {
// Decode the mint data using the SPL Token layout
decodedData = MintLayout.decode(mintAccountInfo.data);
// console.log(decodedData);
} catch (error) {
console.error(`decodedData:`, error);
return null;
}
const supply = decodedData.supply;
const decimals = decodedData.decimals;
console.log(supply);
console.log(decimals);
return { supply, decimals };
}
We have two methods: getParsedAccountInfo and getAccountInfo
I think getAccountInfo is better than the other.