0

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!

1 Answer 1

1

The Connection object doesn't have a publicKey field, which is why you're getting an undefined address. You can see all its fields/methods here: https://solana-labs.github.io/solana-web3.js/classes/Connection.html

I'm not sure which address you're expecting that to be. But you probably want something like this:

// use your address
const myPublicKey = new PublicKey('B556Hnif217eTSdyTFkwvVXAWftfDXFkxrAeKGfpHEQ');

const recentSignatures = await connection.getSignaturesForAddress(
    myPublicKey,
    { limit: 100, before: null },
    'confirmed'
);
4
  • Honestly these docs dont make any sense to me they are so confusing idk what to do
    – Golden
    Commented May 9 at 18:10
  • What address are you trying to get transactions for? And which doc did you get connection.publicKey from, we shouldn't have that documented anywhere because it doesn't exist so I think wherever you got that is likely the source of confusion here.
    – Callum M
    Commented May 9 at 18:52
  • I found it in the libraries source code, in this code I was trying to get the transactions related to the mainnet by using its public key
    – Golden
    Commented May 9 at 21:36
  • The objective was for me to get metadata for all newly minted tokens, the 5 latest ones at least
    – Golden
    Commented May 9 at 21:36

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.