1
let response = await connection.getTokenAccountsByOwner(wallet.publicKey, {"mint": new PublicKey("Mint token")})
response.value.forEach((e) => {
     console.log(`pubkey: ${e.pubkey.toBase58()}`);
     const accountInfo = SPLToken.AccountLayout.decode(e.account.data);
     console.log(`mint: ${new PublicKey(accountInfo.mint)}`);
     console.log(`amount: ${SPLToken.u64.fromBuffer(accountInfo.amount)}`);
              });

having an error with this one ${SPLToken.u64.fromBuffer(accountInfo.amount)}`);

TypeError: Cannot read properties of undefined (reading 'fromBuffer')

1
  • 1
    Please provide some more context.
    – beeman
    Feb 15 at 15:02

1 Answer 1

1

You can see here https://solana-labs.github.io/solana-program-library/token/js/ that the function

Token.getAccountInfo 

does not exists anymore as of version 0.2.0

You will want to use this instead:

import { getAccount } from '@solana/spl-token';

const parsedTokenAccountInfo = await getAccount(connection, tokenAccount);

which will result in this output:

tokenAccountInfo {
  address: PublicKey [PublicKey(7iFn7U1uPWwe4z47v3fP28Mc5F9pqmgYrz9UsjdtzvAj)] {
    _bn: <BN: 63b7a4dec1368066ff069b2a1e84d584dfde9d7a6ea7a520e4945352abef4cc0>
  },
  mint: PublicKey [PublicKey(GiyzqXQUeQr3HZcF9W7a4pDGGbj6qsqxcAKmkFH75tXY)] {
    _bn: <BN: e9a0906181e586a3ad825d611c6e92e4fabb5d5152cdbd8a4f3d52dbd9cc1d17>
  },
  owner: PublicKey [PublicKey(5vJwnLeyjV8uNJSp1zn7VLW8GwiQbcsQbGaVSwRmkE4r)] {
    _bn: <BN: 4916d20d019fe32a08c4facde04ed651a95b3798ed738891df27f4e610cbe02b>
  },
  amount: 5000000000n,
  delegate: null,
  delegatedAmount: 0n,
  isInitialized: true,
  isFrozen: false,
  isNative: false,
  rentExemptReserve: null,
  closeAuthority: null,
  tlvData: <Buffer 07 00 00 00>
}

For your specific case you can now use:

import { AccountLayout } from "@solana/spl-token";

let response = await connection.getTokenAccountsByOwner(wallet.publicKey, {"mint": mintkeyPair.publicKey})
    response.value.forEach((e) => {
        console.log(`pubkey: ${e.pubkey.toBase58()}`);
        const accountInfo = AccountLayout.decode(e.account.data);
        console.log(`amount: ${accountInfo.amount}`);
});
2
  • thanks I need to convert accountInfo.amount to number Feb 17 at 6:54
  • I have just called getTokenAccountsByOwner via http request. Feb 18 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.