3

I need to extract this info from a transaction:

  • from, to wallet addresses
  • asset: SOL or a token (what token?)
  • amount transfered
  • block_timestamp

I know how to get info of a transaction - https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction

{
  "jsonrpc": "2.0",
  "result": {
    "meta": {
      "err": null,
      "fee": 5000,
      "innerInstructions": [],
      "postBalances": [499998932500, 26858640, 1, 1, 1],
      "postTokenBalances": [],
      "preBalances": [499998937500, 26858640, 1, 1, 1],
      "preTokenBalances": [],
      "rewards": [],
      "status": {
        "Ok": null
      }
    },
    "slot": 430,
    "transaction": {
      "message": {
        "accountKeys": [
          "3UVYmECPPMZSCqWKfENfuoTv51fTDTWicX9xmBD2euKe",
          "AjozzgE83A3x1sHNUR64hfH7zaEBWeMaFuAN9kQgujrc",
          "SysvarS1otHashes111111111111111111111111111",
          "SysvarC1ock11111111111111111111111111111111",
          "Vote111111111111111111111111111111111111111"
        ],
        "header": {
          "numReadonlySignedAccounts": 0,
          "numReadonlyUnsignedAccounts": 3,
          "numRequiredSignatures": 1
        },
        "instructions": [
          {
            "accounts": [1, 2, 3, 0],
            "data": "37u9WtQpcm6ULa3WRQHmj49EPs4if7o9f1jSRVZpm2dvihR9C8jY4NqEwXUbLwx15HBSNcP1",
            "programIdIndex": 4
          }
        ],
        "recentBlockhash": "mfcyqEXB3DnHXki6KjjmZck6YjmZLvpAByy2fj4nh6B"
      },
      "signatures": [
        "2nBhEBYYvfaAe16UMNqRHre4YNSskvuYgx3M6E4JP1oDYvZEJHvoPzyUidNgNX5r9sTyN1J9UxtbCXy2rqYcuyuv"
      ]
    }
  },
  "blockTime": null,
  "id": 1
}

However, how to extract all the information above?

I see blockTime which may be it.

Also, preBalances and postBalances - are they the ones to calculate the balances by? Why are they arrays? What each digit correspond to? Are the digits supposed to be subtracted from as in postBalances[N] - preBalances[N]?

All in all, how to extract all the information I need?

P.S. At the moment I'm only interested in SOL and USDT/USDC assets.

1
  • If your question is answered, please mark this as answered. Thank you.
    – vicyyn
    Dec 30, 2022 at 15:51

1 Answer 1

3

In transaction.message you can find accountKeys those are the accounts for the postBalance and preBalance in order. This means the first preBalance and postBalance are for the first account in accountKeys.

You can get the from field by calculating postBalance - preBalance. If it's negative then the account tokens were deducted which means it's from. if it's zero then it's unchanged. If it's positive then it was transferred to (to you are looking for).

example

doing this in rust, but it's gonna be similar in typescript since they both invoke the Solana JSON RPC API.

{
account_keys: [
    ParsedAccount {
        pubkey:  "9FunJaYzY97hcTNN95mAn8iEhXQjw9NpKDmng8NYLAbt",
        writable: true,
        signer: true 
    },
    ParsedAccount {
        pubkey: "CXFCTHyuhcJL3fiFR5eRochz4k9dwzZWfLMU7knzaCp",
        writable: true,
        signer: false
    }],
}

One way to know which token is transferred is to compare the accounts to a mint you know beforehand. But make sure the transaction doesn't have multiple mints.

For the signer, a transaction can have multiple signers, they can sign a transaction of a token transfer from one token account to another. use getParsedTransaction to get the accounts that are signers or not.

12
  • 1
    How about the from field?
    – Kum
    Dec 30, 2022 at 12:57
  • You can get the from field by calculating postBalance - preBalance. If it's negative then the account tokens were deducted which means it's from. if it's zero then it's unchanged. If it's positive then it was transferred to.
    – vicyyn
    Dec 30, 2022 at 13:45
  • The from means a wallet address or account a TX was sent from
    – Kum
    Dec 30, 2022 at 20:50
  • the sender...
    – Kum
    Dec 31, 2022 at 0:10
  • That's the signer, a transaction can have multiple signers, they can sign a transaction of a token transfer from a token account to another.
    – vicyyn
    Dec 31, 2022 at 6:31

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.