0

i want to fetch the tokens which are being created by the user. i came across the method .getParsedTokenAccountsByOwner() which is being used to fetch the tokens that is being owned by owner. and what I write the code is

const fetchTokens = async () => {
    const connection = new Connection(
      'https://solana-mainnet.quiknode.pro/86ecc1c13af7833',
      'confirmed'
    );
    const walletAddress = '99q4g7RHKh1eD2w95HFSQdakYC6RqmAqEyzptCLN74E4';
    const walletPublicKey = new PublicKey(walletAddress);
  
    const TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
  
    try {
      const tokenAccounts = await connection.getParsedTokenAccountsByOwner(walletPublicKey, {
        programId: TOKEN_PROGRAM_ID,
      });
  
      console.log(`Found ${tokenAccounts.value.length} token accounts`);
  
      const tokensCreatedByMe = [];
  
      for (const accountInfo of tokenAccounts.value) {
        const mintAddress = accountInfo.account.data.parsed.info.mint;
        const mintPublicKey = new PublicKey(mintAddress);
        // console.log(`Checking token mint: ${mintAddress}`);
  
        try {
          const mintAccountInfo = await connection.getAccountInfo(mintPublicKey);
  
          if (mintAccountInfo) {
            const mintData = Buffer.from(mintAccountInfo.data);
            const mintAuthority = new PublicKey(mintData.slice(0, 32));
  
            // console.log(`Mint authority for ${mintAddress}: ${mintAuthority.toString()}`);
            if(mintAddress.toString().includes('pump'))
              {tokensCreatedByMe.push(mintAddress);
                console.log(mintAccountInfo)
                continue;
              }
  
            if (mintAuthority.equals(walletPublicKey) || mintAuthority.equals(PublicKey.default)) {
              tokensCreatedByMe.push(mintAddress);
              // console.log(`Token ${mintAddress} added as created by me`);
            }
          } else {
            console.warn(`Mint account info not found for ${mintAddress}`);
          }
        } catch (innerError) {
          console.error(`Error processing mint ${mintAddress}:`, innerError);
        }
      }
  
      console.log('Tokens Created by Me:', tokensCreatedByMe);
      return tokensCreatedByMe;
  
    } catch (error) {
      console.error('Error fetching token accounts:', error);
    }
  };

I am not sure if this is the right way to fetch all tokens created by solana wallet? and for the cross verification I am checking pumpfun tokens that's why I includes pump in token info. and if this is the right way then how will I get the token details? do I need to again get to another function? cant we have a single function which will tell use, tokens created by me with token stikr, name, image, address.

1 Answer 1

1

The getParsedTokenAccountsByOwner method will fetch the owner's token accounts.

Alternatively, you could use the getAssetsByCreator method offered by the DAS API and filter using the interface field to only fetch the fungible assets.

Here is a page that lists RPCs that support DAS -> https://developers.metaplex.com/rpc-providers#rpcs-with-das-support

The snippet below makes use of Metaplex UMI, but you can call the RPC directly as demonstrated in the Helius docs -> https://docs.helius.dev/compression-and-das-api/digital-asset-standard-das-api/get-assets-by-creator

async function fetchCreatorAssets() {
    const creator = publicKey("99q4g7RHKh1eD2w95HFSQdakYC6RqmAqEyzptCLN74E4");
    const assets = await (
        await umi.rpc.getAssetsByCreator({
            creator,
            onlyVerified: true,
            limit: 10,
        })
    ).items.filter((asset) => asset.interface === "FungibleAsset");

  // -- todo, filter for `pump`token

    console.log(assets);
}
fetchCreatorAssets();
3
  • hi thanks for the comment, i tried to use getAssetsByCreator using helius but I dint get any output as I get array of 0 items, and if I do searchAssets from helius, I do get array of items, so I wasn't able to use getAssetsByCreator mainly, then how to use?? Commented Nov 29 at 4:12
  • could you create a new question with the configuration and address you used.
    – Jimii
    Commented Nov 29 at 8:25
  • 1
    hi just posted the new question with the configuration solana.stackexchange.com/questions/17888/… can you please check Commented Nov 29 at 8:47

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.