Hello Im trying to get the metadata for the latest 5 newly minted tokens on the blockchain, I keep getting the error
const args = this._buildArgsAtLeastConfirmed([address.toBase58()], commitment, undefined, options);
^
TypeError: Cannot read properties of undefined (reading 'toBase58') at Connection.getSignaturesForAddress (C:\Users\user\node_modules@solana\web3.js\lib\index.cjs.js:7691:59) at main (C:\Users\user\OneDrive\Desktop\Solana\solana.js:20:45) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Here is my code
const { Connection, clusterApiUrl, PublicKey } = require('@solana/web3.js');
const { TOKEN_PROGRAM_ID, Token } = require("@solana/spl-token");
async function fetchTokenMetadata(connection, tokenMintAddress) {
const token = new Token(connection, tokenMintAddress, TOKEN_PROGRAM_ID, null);
const tokenInfo = await token.getMintInfo();
return tokenInfo;
}
async function main() {
const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');
// Fetch the latest blockhash
const latestBlockhash = await connection.getLatestBlockhash();
// Fetch the latest slot
const latestSlot = latestBlockhash.blockhash.slot;
// Fetch the most recent signatures
const recentSignatures = await connection.getSignaturesForAddress(
connection.publicKey,
{ limit: 100, before: null },
'confirmed'
);
const filteredSignatures = recentSignatures.filter(signature => signature.slot <= latestSlot);
// Extract token accounts from signatures
const tokenAccountAddresses = filteredSignatures
.flatMap(signature => {
const transaction = connection.current.rpc.getTransaction(signature.signature, 'confirmed');
return transaction.transaction.message.instructions
.filter(instruction => instruction.programId.equals(TOKEN_PROGRAM_ID))
.flatMap(instruction => instruction.accounts
.filter(account => account.isSigner || account.isWritable)
.map(account => account.toString())
);
})
.slice(0, 5); // Limits to the 5 latest token accounts can change later
const promises = tokenAccountAddresses.map(async (mintAddress) => {
return fetchTokenMetadata(connection, new PublicKey(mintAddress));
});
const tokenInfos = await Promise.all(promises);
tokenInfos.forEach((tokenInfo, index) => {
console.log(`Token Metadata for ${tokenAccountAddresses[index]}:`);
console.log(tokenInfo);
});
}
main();
I would appreciate if anyone could help me with this as its been really hard so far to learn and use solanas tools.
Thanks!