My background is in Ethereum, where it is easy to get the full list of owners for a particular token (e.g. USDC) using The Graph or a node (by filtering Transfer
events).
How can I do this with an SPL token?
I'm not sure you can
This is supposed to do the job :
let filters = {filters: [{memcmp:{
bytes: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
offset: 0,
}}]};
let result = await connectionMainnet.getParsedProgramAccounts(TOKEN_PROGRAM_ID, filters);
BUT, you will probably get an error like "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA excluded from account secondary indexes" (that's what i'm getting from quicknode). Token program id is one of the program that has the largest set of accounts, hence, some (a lot ?) rpcs disabled it for this rpc method.
FYI, EPjF... = USDC
TOKEN_PROGRAM_ID is the publicKey object of the token program id (you can import it from @solana/spl-token)
You can check doc here : https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getParsedProgramAccounts
Also, there is another method available, getLargestAccounts (https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getLargestAccounts), and, as its name implies, it will not return ALL accounts, just the largest ones.
In any case, getting all USDC token accounts must be like thousands of accounts...
also, might be interested with this topic: Error: failed to get accounts owned by program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA excluded from account secondary indexes; this RPC (it's about the said error above)
The only solution i can see is by parsing all transactions linked to usdc, but that must be a heavy work since there must be millions of transactions... not even sure you can get them all.
getProgramAccounts
. Would this work? (I'm very, very new to Solana)
Commented
Sep 18, 2023 at 9:54