简体   繁体   中英

How to analyse tokenBalances in a confirmed transaction on the Solana blockchain?

I'm working on a personal project focusing on Solana NFT's, and I'm struggling to wrap my head around how I should approach getting the data regarding buyer/seller accounts for a in a particular transaction from the result of a call to their rpc to get transaction data.

I'm using nodeJS and the @solana/web3js module to get the signatures of all transactions for a particular collection, and then getting the transactions from those signatures using the getConfirmedParsedTransaction(signature) method then attempting to grab the data I need from them.

Taking this signature on explorer.solana.com for example - it is using the data returned by an API call similar to this -

curl 'https://explorer-api.mainnet-beta.solana.com/' \\ -H 'Connection: keep-alive' \\ -H 'content-type: application/json' \\ -H 'Accept: */*' \\ -H 'Origin: https://explorer.solana.com' \\ -H 'Sec-Fetch-Site: same-site' \\ -H 'Sec-Fetch-Mode: cors' \\ -H 'Sec-Fetch-Dest: empty' \\ -H 'Referer: https://explorer.solana.com/' \\ -H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \\ --data-raw '{"method":"getConfirmedTransaction","jsonrpc":"2.0","params":["3sRxEfGD2VmrJmw3YsULDER42HW3W2eiAv74Tn41MoL1YQmtELDcEiRf29cBPfsxRVcqXCEkCmwPJNihxAtfRZ4S",{"encoding":"jsonParsed","commitment":"confirmed"}],"id":"b70247d9-75ef-429a-bf27-ab2519dd75b8"}' \\ --compressed

I could see from the returned JSON that there are multiple arrays with 21 indexes - in these arrays, data for the buyer is at index 0, and data for the seller is at index 3. I know that this will not always be the case due to the fact that there are different marketplaces which may use different instructions, or different tokens having different splits for royalties - as a result I looked for a more logical method.

The first thing I'm currently doing is looking at pre/post token balances to get the actual token (mint property) that was transferred. These objects look something like this :

{"postTokenBalances":[{"accountIndex":1,"mint":"7t2PCc8WAE14dE58yYogBXBspoL8o8PXesAag3zMse3h","uiTokenAmount":{"amount":"1","decimals":0,"uiAmount":1.0,"uiAmountString":"1"}}],"preBalances":[413842981566,0,2039280,22184812910,1398960,13264655885039,5616720,0,212257097061,135958021650,135956740765,599225381740,1000,1461600,1,1089991680,1,11352771574,1141440,898174080,1141440],"preTokenBalances":[{"accountIndex":2,"mint":"7t2PCc8WAE14dE58yYogBXBspoL8o8PXesAag3zMse3h","uiTokenAmount":{"amount":"1","decimals":0,"uiAmount":1.0,"uiAmountString":"1"}}]}

I thought I had figured out an easy way to figure things out based on this - the accountIndex must be the index in the aforementioned arrays, but unfortunately that didn't seem to be correct.

I've looked at the documentation here regarding what the accountIndex actually represents, and it says it represents the index in the array like I thought ('Index of the account in which the token balance is provided for') but this does not appear to be correct :

As I've said, the buyer for this transaction is at index 0 and the accountIndex in the postTokenBalances is 1, while the seller is at index 3, and the accountIndex in preTokenBalances is 2 - so my question is what do these accountIndex properties actually represent?

Sorry for the relatively long question, but I wanted to try and provide a bit of detail!

You've understood almost everything correctly. The last bit to understand is that the buyer / seller are not the same thing as the recipient / sending token account. Tokens (including NFTs) are stored in SPL token accounts, which are separate accounts from the buyer and seller, which are system accounts with SOL.

If you look at the transaction in the explorer: https://explorer.solana.com/tx/3sRxEfGD2VmrJmw3YsULDER42HW3W2eiAv74Tn41MoL1YQmtELDcEiRf29cBPfsxRVcqXCEkCmwPJNihxAtfRZ4S , you'll see that the buyer spending 50 SOL is indeed GWcJPnrAuMqN83D8wDvmtoAdGATJGLsCRvgZ85qxXP3j , but that the recipient token account is 7wuC7oveWkSoYoBR1WT5ZwMn62uQddSebWghQrQdFE72 . GWcJPnrAuMqN83D8wDvmtoAdGATJGLsCRvgZ85qxXP3j cannot hold tokens, but 7wuC7oveWkSoYoBR1WT5ZwMn62uQddSebWghQrQdFE72 can, and is owned by GWcJPnrAuMqN83D8wDvmtoAdGATJGLsCRvgZ85qxXP3j .

You can read more about the Solana account model at https://docs.solana.com/developing/programming-model/accounts and more info about the SPL token program at https://spl.solana.com/token

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM