oijafoijf asnjksdjn
oijafoijf asnjksdjn

Reputation: 1225

How to filter getSignaturesForAddress by token transfer

When a user sends a spl token to my wallet, i want amount of money in my app increases. The way I thought of is to continuously load the transaction list of a specific wallet and compare it with the value in the DB to apply the change.

Loading transaction list was successful. But among these, how to filter the transfer of the spl Token?

import * as web3 from '@solana/web3.js';


const solanaConnection = new web3.Connection(web3.clusterApiUrl("mainnet-beta"));
const getTransactions = async(address,lastTransaction) => {
  const pubKey = new web3.PublicKey(address);
  let transactionList = await solanaConnection.getSignaturesForAddress(pubKey,{until:lastTransaction});
  let signatureList = transactionList.map(transaction=>transaction.signature);
  console.log(signatureList);
  for await (const sig of signatureList) {
    console.log(await solanaConnection.getParsedTransaction(sig));
  }
}

getTransactions(myAddress,lastTransaction);

Should I parse LogMessage to parsedTransaction to filter it?

and I want to know how many splTokens have been sent.

Upvotes: 0

Views: 2038

Answers (1)

Jon C
Jon C

Reputation: 8472

The easiest way is to fetch those transactions with jsonParsed encoding with getTransaction https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction

Once you have the transaction, you can compare the preTokenBalances and postTokenBalances to see how much has been moved in the transaction.

However, if you're just giving the wallet address, it might not be part of the transaction. If I send you USDC from my wallet into your USDC account, your wallet is not included in that transaction! Because of that, for total coverage, you'll need to poll for the signatures for all of your spl token accounts.

Upvotes: 1

Related Questions