2

I'm trying to get a historical daily balances of holders for certain tokens. To do this, I'm calling getSignaturesForAddress for the token address, then going through each holder and calling getSignaturesForAddress for each account address. Then for each signature I'm calling getTransaction and am using the postTokenBalances list to populate the holdings of the owner address.

I noticed for this one specific transaction (here: https://solscan.io/tx/2gdEfvZPiqpjLo8hqpijvnDZKbMAmwpXEUVtfqguJmpyBjNjZT4rj4rNqUGj7GBQkyjttc2f65FWcqzfukiBdixp#tokenBalanceChange) that there was a preTokenBalance for 2NPjGB24MKxewy4VJDfJKZV83A6tqSv5o5mvL4YJGFi8 but there was no entry in the postTokenBalances.

What's the best way to get postTokenBalances for these kinds of transactions? Can it be assumed that if there is no postTokenBalance, but there is a preTokenBalance for an address, the postTokenBalance value is 0?

Or is the best way to get the postTokenBalance to get the accountIndex from the preTokenBalance and then index into the postBalances? I'm unsure if that will give me the value I want as I noticed that the accountIndex + value in postTokenBalances don't always match the postBalances values.

1 Answer 1

2

There's a few things to unpack here, and unfortunately some explorers can make things more confusing to users.

To start, every account will have a preBalance and a postBalance, since those report the amount of SOL in the account before and after the transaction, and all accounts can hold SOL. In this case, that 2NP... address has some SOL at the start and gets emptied to 0 during the transaction.

Not every account will have a preTokenBalance, since not every account is an SPL Token account. In this case, the first entry in both preTokenBalances and postTokenBalances reports an index of 2, whereas the 2NP... address has an index of 1.

That 2NP... address is not a token account, and so it doesn't have any pre or post token balance, but it does have a preBalance of SOL.

So now to answer your specific questions: if a token account no longer has a token balance reported, then that means the token account no longer exists, and can safely be assumed to be 0.

Feel free to read up on the format of the getTransaction response at https://solana.com/docs/rpc/http/gettransaction which explains every single field

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.