4

This is my Code, the output is 1000, I tried with another address which had 18 TXNs & the output was 18 , the wallet below has more than a million transactions, Solana/web3js is limited to fetch 1000 Txns , please let me know if there is any other way to get the rest and exact number

const SrchAddress = "5HcS2Qej4uPKop4pNaDHnVywx42Y2qUhceUYmbFKPG8g";
const endpoint = "https://api.mainnet-beta.solana.com";

const SolanaConnection = new solanaweb3.Connection(endpoint);
const getTransactions = async (address) => {
    const pubkey = new solanaweb3.PublicKey(address);
    let transactionList = await SolanaConnection.getSignaturesForAddress(pubkey);
    console.log(transactionList.length);
};

getTransactions(SrchAddress);
1
  • hey, if you found either of the answers useful, could you please mark the question as answered?
    – NHeinDev
    Commented Aug 4, 2022 at 17:15

2 Answers 2

10

Since getSignaturesForAddress returns 1000 objects at the maximum, you will need to add a recursive loop that leverages the {before: "signature"} option to get all of the signatures that you need.

This is going to be an incredibly slow call, but I added a bit of logging so you can see that the list of transactions is growing.

const SrchAddress = "5HcS2Qej4uPKop4pNaDHnVywx42Y2qUhceUYmbFKPG8g";
const endpoint = "https://api.mainnet-beta.solana.com";

const SolanaConnection = new solanaweb3.Connection(endpoint);
const getTransactions = async (address) => {
    let transactionListLength = 1000
    const allTransactions = []
    const pubkey = new solanaweb3.PublicKey(address);
    let transactionList = await SolanaConnection.getSignaturesForAddress(pubkey);
    allTransactions.push(transactionList)
    while (transactionListLength >= 1000) {
      const lastSignature = transactionList[transactionList.length - 1];
      const nextSignatures = await SolanaConnection.getSignaturesForAddress(pubkey, { before: lastSignature.signature });
      allTransactions.push(nextSignatures)
      transactionList = nextSignatures
      transactionListLength = nextSignatures.length;
      console.log('current:',transactionList.length);
      console.log('total:',allTransactions.length)
    }
  return allTransactions.flat()
};

getTransactions(SrchAddress).then((res) => console.log(res))

This loop will keep going until the returned object's length is less than the 1000 transactions, meaning you've reached the end of the list.

1
4

The getSignatureForAddresses method returns a maximum of 1000 signatures.

To retrieve signatures older than your initial query, you can pass in an optional before signature. https://solana-labs.github.io/solana-web3.js/modules.html#SignaturesForAddressOptions

Start searching backwards from this transaction signature.

let transactionList = await SolanaConnection.getSignaturesForAddress(pubkey);
const lastSignature = transactionList[transactionList.lenght - 1];
const nextSingatures = await SolanaConnection.getSignaturesForAddress(pubkey, { before: lastSignature });
2
  • 1
    So that helped me with getting another 1000 of those TXNs, though my main concern is to get all the TXNs of a wallet
    – Kid Knot
    Commented Jul 15, 2022 at 8:15
  • You'll have to call this in a recursive manner. Continue to call the getSignatureForAddress method with the last signature from the previous return until getSignatureForAddress doesn't return any more results. Recursion: freecodecamp.org/news/quick-intro-to-recursion
    – C.OG
    Commented Jul 15, 2022 at 13: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.