1

I have a list of transactions that I periodically fetch via Solana API. Out of those, I need to identify the ones that are:

  • incoming that transfer SOL
  • incoming that transfer a token (USDT, USDC, maybe 1-2 more)

That is, I need to implement a simple way of watching "top up" TX-s, for SOL and the stablecoins, for a list of accounts of mine.

So how to clearly identify the type of transfer of a TX I'm parsing: whether it's EITHER SOL transfer, OR a token transfer? As well as the amount.

Will it be by the presence of the field lamports - SOL transfer? Namely, instructions[X].parsed.info["lamports"] Will be a reliable way?

On the contrary: how to identify if a TX is a pure "token transfer" one? Not the one that may involve "token transfer" among other instructions, but is only "token transfer" one.

I don't expect any other, more complex types of TX in my list of addresses. If such TX-s happen to occur, I'll filter them out during parsing.

2 Answers 2

1

Your best bet is to simply look at the pre and post balances on the transactions that come in.

Quoting the docs:

The preBalances and postBalances fields allow you to track the balance changes in every account without having to parse the entire transaction. They list the starting and ending balances of each account in lamports, indexed to the accountKeys list. For example, if the deposit address of interest is G1wZ113tiUHdSpQEBcid8n1x8BAvcWZoZgxPKxgE5B7o, this transaction represents a transfer of 1040000000 - 1030000000 = 10,000,000 lamports = 0.01 SOL

And there's also preTokenBalances and postTokenBalances for SPL tokens.

You can find more information at https://docs.solana.com/integrations/exchange#block-fetching-tips and https://docs.solana.com/developing/clients/jsonrpc-api#token-balances-structure

4
  • And if preBalances and postBalances are identical, what kind of TX is it?
    – Camila326
    Commented Jan 18, 2023 at 1:26
  • It could be anything! But most importantly, it means that your account is neither credited nor debited.
    – Jon C
    Commented Jan 18, 2023 at 11:43
  • Out of curiosity, why not look at the program and instruction handler? Eg look for system program running the transfer instruction for SOL, the token program running the transfer instruction for SPL tokens?
    – mikemaccana
    Commented Feb 16 at 20:55
  • You certainly can! It's just easier to look at the pre and post balances, since then you don't need to worry about parsing instructions. Also, since it's possible for programs to directly debit / credit lamports without an instruction to the system program, it's possible to miss those transfers if you only look at instructions.
    – Jon C
    Commented Feb 16 at 20:58
0

my approach for this would be to as you say watch for the presence of either mint or lamports in the transfer/transferChecked instructions.

Put simply: -You can have SPL transfers in a "transfer" instruction and/or a "transferChecked" instruction, indicated by the presence of the token mint.

-You wont find a lamports value in a "transferChecked", but you might in a "transfer". If there is no lamports value it must have a mint value. And vice versa.

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.