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.