Get all transactions for an NFT on Solana

Viewed 417
1 Answers

You can use the getSignaturesForAddress RPC method on the mint address and walk backward to get all the transactions.

Here is an example in JS:

import {
  Connection,
  clusterApiUrl,
  ConfirmedSignatureInfo,
  PublicKey,
} from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("mainnet-beta"));

export const getTxs = async (connection: Connection, pubkey: PublicKey) => {
  const txs: ConfirmedSignatureInfo[] = [];

  // Walk backward
  let lastTransactions = await connection.getConfirmedSignaturesForAddress2(
    pubkey
  );
  let before = lastTransactions[lastTransactions.length - 1].signature;
  txs.push(...lastTransactions);

  while (true) {
    const newTransactions = await connection.getConfirmedSignaturesForAddress2(
      pubkey,
      {
        before,
      }
    );
    if (newTransactions.length === 0) break;
    txs.push(...newTransactions);
    before = newTransactions[newTransactions.length - 1].signature;
  }

  return txs;
};

getTxs(
  connection,
  new PublicKey("2Nzt8TYeAfgJDftKzkb7rgYShVvyXTR7cPVvpqaZ2a4V")
);

The equivalent method in Solana.py is this one https://michaelhly.github.io/solana-py/rpc/api/#solana.rpc.api.Client.get_signatures_for_address

Related