I figured out how to decode the data buffer manually for a solana account data. I used the USDC account address to get the account data and then I used the @solana/buffer-layout for u64 buffer decoding and javascript Buffer functions to read the data bytes and decode them manually. The data for this case represents the fields of Mint struct defined in the state.rs located at https://github.com/solana-labs/solana-program-library/blob/8f75f5905cbd402e1853153117506193277f70f0/token/program/src/state.rs#L16.
Below is my code and the results of the code. The results match what is shown on the solana explorer. I am hoping that this will help other solana developers.
import {Connection, PublicKey} from '@solana/web3.js'; import {Buffer} from "buffer"; import * as BufferLayout from '@solana/buffer-layout';
let connection: Connection = new Connection("http://api.mainnet-beta.solana.com", "confirmed");;
async function main() {
let usdc = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
let usdcAcctInfo = await connection.getAccountInfo(usdc);
console.log('\n USDC Token Account Info:')
console.log(usdcAcctInfo);
console.log("\n usdc data is ");
console.log(usdcAcctInfo.data);
// Decode first 4 bytes of the "data" buffer for the usdcAcctInfo
// little endian reading of buffer for the first four bytes using
// Buffer library functions of Javascript
let mintAuthorityCoption = usdcAcctInfo.data.readUIntLE(0,4);
console.log("\n Mint_Authority_Coption value is");
console.log(mintAuthorityCoption);
// Decode next 32 bytes to a PublicKey based on the Mint Struct
let mintAuthority = new PublicKey(usdcAcctInfo.data.subarray(4, 36)).toBase58();
console.log("\n Mint Authoriy Public Key is ");
console.log(mintAuthority);
let ns64 = new BufferLayout.NearUInt64();
let supplySlice = usdcAcctInfo.data.subarray(36, 44);
let arr = new Uint8Array(supplySlice);
console.log("Uint8Array is");
console.log(arr);
let supply = ns64.decode(arr);
console.log("\n Supply decoded ");
console.log(supply);
// decode next 1 byte representing the field decimal u8
let decimal = usdcAcctInfo.data.readUIntLE(44, 1);
console.log("\n decimal value is");
console.log(decimal);
}
main().then(
() => process.exit(),
err => {
console.error(err);
process.exit(-1)
} )
The output of the above code is as follows: