0

How do I get the lamports sent in each transaction and why are there multiple transaction accounts on each message? This is the code I have, but 'parsed' is typed as any in the SDK

const getTransactionSummary = (
  fromAddress: ParsedMessageAccount,
  transaction: ParsedTransactionWithMeta
) => {
  const {
    blockTime,
    transaction: { message },
  } = transaction;

  const parsedLamports = message.instructions
    .filter(isParsedInstruction)
    .map(({ parsed }) => parsed.info.lamports);

  return {
    address: fromAddress.pubkey.toBase58(),
    time: blockTime ? new Date(blockTime * 1000).toISOString() : null,
    amount: parsedLamports.reduce(add, 0),
    meta: transaction.meta?.err ?? null,
  };
};```

1 Answer 1

1

I think you can use getParsedTransaction function to get preBalance and postBalance of accounts in a transaction. From that you can calculate lamports sent.

  const connection = new Connection(clusterApiUrl('devnet'), { commitment: 'finalized' });

  const signature = "signature of the transaction you want to calculate";
  const txDetail = await connection.getParsedTransaction(signature, {
    commitment: "finalized",
  });
  console.log("account array: ", txDetail.transaction.message.accountKeys)
  console.log("post balances: ", txDetail.meta.postBalances)
  console.log("pre balances: ", txDetail.meta.preBalances)
2
  • What about the account array, is that just the two accounts? Sorry I haven't been able to try it out. Just doesn't make sense why accountKeys is an array to me Commented Apr 11 at 23:37
  • Array accounts is all account is read or written in the transaction.These account can be System account, PDAs, user accounts, another program accounts,... Commented Apr 15 at 10:03

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.