admin管理员组

文章数量:1359268

I am able to get the tokens in my wallet but then am not able to derive the mint address from the received data. I want to get the mint address of all tokens in my wallet to then use that with some APIs. Usning solana/web3.js version 2.0 and there arent much examples out yet. Appreciate the help.

const TOKEN_PROGRAM_ID = address('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
const { value: tokensInWallet} = await rpc
  .getTokenAccountsByOwner(
    address('XXX'),
    { programId: TOKEN_PROGRAM_ID },
    { encoding: 'base64'}
  )
  .send();
console.log(tokensInWallet);

//expect mint jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL for JTO token
tokensInWallet.map(tokenAccount => {
  const encoded = getBase64Encoder().encode(tokenAccount.account.data[0]);
  console.log(encoded);
  //now turn into this format of the mint
  //base58 encode
  const mint = getBase58Encoder().encode(tokenAccount.pubkey);
  console.log(Buffer.from(mint).toString('base64'));
})

I am trying to somehow encode / decode the string in data but am not getting anywhere

tokenInWallet looks like that

[
  {
    account: {
      data: [
        "Cvz4louNq4hIHi0q5onJUsdXrrpkPjkZ6J8uVXlcdsEpwCylBRLH6qBLpURZ/jc3h9wTxgzMrD4rEPcp06WvvwAoa+4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
        "base64",
      ],
      executable: false,
      lamports: 2039280n,
      owner: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
      rentEpoch: 18446744073709551615n,
      space: 165n,
    },
    pubkey: "3UoZPNLp1eynjjCEEcKyv3VcXtoZCe5GdEt2agDezD31",
  },
]

I am able to get the tokens in my wallet but then am not able to derive the mint address from the received data. I want to get the mint address of all tokens in my wallet to then use that with some APIs. Usning solana/web3.js version 2.0 and there arent much examples out yet. Appreciate the help.

const TOKEN_PROGRAM_ID = address('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
const { value: tokensInWallet} = await rpc
  .getTokenAccountsByOwner(
    address('XXX'),
    { programId: TOKEN_PROGRAM_ID },
    { encoding: 'base64'}
  )
  .send();
console.log(tokensInWallet);

//expect mint jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL for JTO token
tokensInWallet.map(tokenAccount => {
  const encoded = getBase64Encoder().encode(tokenAccount.account.data[0]);
  console.log(encoded);
  //now turn into this format of the mint
  //base58 encode
  const mint = getBase58Encoder().encode(tokenAccount.pubkey);
  console.log(Buffer.from(mint).toString('base64'));
})

I am trying to somehow encode / decode the string in data but am not getting anywhere

tokenInWallet looks like that

[
  {
    account: {
      data: [
        "Cvz4louNq4hIHi0q5onJUsdXrrpkPjkZ6J8uVXlcdsEpwCylBRLH6qBLpURZ/jc3h9wTxgzMrD4rEPcp06WvvwAoa+4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
        "base64",
      ],
      executable: false,
      lamports: 2039280n,
      owner: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
      rentEpoch: 18446744073709551615n,
      space: 165n,
    },
    pubkey: "3UoZPNLp1eynjjCEEcKyv3VcXtoZCe5GdEt2agDezD31",
  },
]
Share Improve this question asked Mar 27 at 16:12 Sebastian_BSebastian_B 615 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Solved this by using

{ encoding: 'jsonParsed'}

which gives the parsed data including balance and token mint

本文标签: How to get mint address of tokens in wallet with solanaweb3js v2Stack Overflow