In Solana docs they use a lot of TS code snippets, for example in the below link (the first code snippet) should run and return a value: https://solanacookbook.com/guides/get-program-accounts.html#filters
I tried to run the first snippet using: ts-node file.ts
but I got this error:
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
index.ts:33:26 - error TS7053: Element implicitly has an 'any' type because expression of type '"parsed"' can't be used to index type 'Buffer | ParsedAccountData'.
Property 'parsed' does not exist on type 'Buffer | ParsedAccountData'.
33 console.log(`Mint: ${account.account.data["parsed"]["info"]["mint"]}`);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index.ts:35:18 - error TS7053: Element implicitly has an 'any' type because expression of type '"parsed"' can't be used to index type 'Buffer | ParsedAccountData'.
Property 'parsed' does not exist on type 'Buffer | ParsedAccountData'.
35 `Amount: ${account.account.data["parsed"]["info"]["tokenAmount"]["uiAmount"]}`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at createTSError (/usr/local/lib/node_modules/ts-node/src/index.ts:750:12)
at reportTSError (/usr/local/lib/node_modules/ts-node/src/index.ts:754:19)
at getOutput (/usr/local/lib/node_modules/ts-node/src/index.ts:941:36)
at Object.compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1243:30)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1370:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1374:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
diagnosticText: `\x1B[96mindex.ts\x1B[0m:\x1B[93m33\x1B[0m:\x1B[93m26\x1B[0m - \x1B[91merror\x1B[0m\x1B[90m TS7053: \x1B[0mElement implicitly has an 'any' type because expression of type '"parsed"' can't be used to index type 'Buffer | ParsedAccountData'.\n`
" Property 'parsed' does not exist on type 'Buffer | ParsedAccountData'.\n"
'\n'
'\x1B[7m33\x1B[0m console.log(`Mint: ${account.account.data["parsed"]["info"]["mint"]}`);\n'
'\x1B[7m \x1B[0m \x1B[91m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\x1B[0m\n'
`\x1B[96mindex.ts\x1B[0m:\x1B[93m35\x1B[0m:\x1B[93m18\x1B[0m - \x1B[91merror\x1B[0m\x1B[90m TS7053: \x1B[0mElement implicitly has an 'any' type because expression of type '"parsed"' can't be used to index type 'Buffer | ParsedAccountData'.\n`
" Property 'parsed' does not exist on type 'Buffer | ParsedAccountData'.\n"
'\n'
'\x1B[7m35\x1B[0m `Amount: ${account.account.data["parsed"]["info"]["tokenAmount"]["uiAmount"]}`\n'
'\x1B[7m \x1B[0m \x1B[91m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\x1B[0m\n',
diagnosticCodes: [ 7053, 7053 ]
}
Am I doing this correctly?
CodePudding user response:
You're running it correctly from what I can tell. Looks like there was one issue with the code on compatibility with Typescript. account.account.data
is of type Buffer | ParsedAccountData
.
If you set it like the code below, it will run successfully:
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { ParsedAccountData } from "@solana/web3.js";
import { clusterApiUrl, Connection } from "@solana/web3.js";
(async () => {
const MY_WALLET_ADDRESS = "FriELggez2Dy3phZeHHAdpcoEXkKQVkv6tx3zDtCVP8T";
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const accounts = await connection.getParsedProgramAccounts(
TOKEN_PROGRAM_ID, // new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
{
filters: [
{
dataSize: 165, // number of bytes
},
{
memcmp: {
offset: 32, // number of bytes
bytes: MY_WALLET_ADDRESS, // base58 encoded string
},
},
],
}
);
console.log(
`Found ${accounts.length} token account(s) for wallet ${MY_WALLET_ADDRESS}: `
);
accounts.forEach((account, i) => {
console.log(
`-- Token Account Address ${i 1}: ${account.pubkey.toString()} --`
);
console.log(`Mint: ${(account.account.data as ParsedAccountData)["parsed"]["info"]["mint"]}`);
console.log(
`Amount: ${(account.account.data as ParsedAccountData)["parsed"]["info"]["tokenAmount"]["uiAmount"]}`
);
});
/*
// Output
Found 1 token account(s) for wallet FriELggez2Dy3phZeHHAdpcoEXkKQVkv6tx3zDtCVP8T:
-- Token Account Address 1: Et3bNDxe2wP1yE5ao6mMvUByQUHg8nZTndpJNvfKLdCb --
Mint: BUGuuhPsHpk8YZrL2GctsCtXGneL1gmT5zYb7eMHZDWf
Amount: 3
*/
})();