Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Solana transaction data from transaction object

I'm doing a simple transaction with a single transfer instruction for 0,1 SOL from one account to another. Then I want to get the transaction data and use it to verify the data it carries - in this case that a transfer has been made for 0,1 SOL.

I use getTransaction with the tx signature and get a response like this:

{
  message: Message {
    header: {
      numReadonlySignedAccounts: 0,
      numReadonlyUnsignedAccounts: 1,
      numRequiredSignatures: 1
    },
    accountKeys: [ [PublicKey], [PublicKey], [PublicKey] ],
    recentBlockhash: '9S44wiNxXZSdP5VTG6nvaumUJBiUW1DGUXmhVfuhwTMh',
    instructions: [ [Object] ],
    indexToProgramIds: Map(1) { 2 => [PublicKey] }
  },
  signatures: [
    '8ykRq1XtgrtymXVkVhsWjaDrid5FkKzRPJrarzJX9a6EArbEUYMrst6vVC6TydDRG4sagSciK6pP5Lw9ZDnt3RD'
  ]
}

So, I dig into message.instructions and find the following object:

{ accounts: [ 0, 1 ], data: '3Bxs411Dtc7pkFQj', programIdIndex: 2 }

Ok, so data is the base58-encoded string '3Bxs411Dtc7pkFQj'. I decode that from base58 (using bs58), but that only gives me a Uint8Array, which I am not really sure how to convert into a JS object.

I can see in the tx in Solscan that the data information is decoded into hex:

enter image description here

And I can also get this info in my script:

---> Instructions:
{ accounts: [ 0, 1 ], data: '3Bxs411Dtc7pkFQj', programIdIndex: 2 }

---> Instructions Data:
3Bxs411Dtc7pkFQj
0200000000e1f50500000000
-----

But not sure what to do next. How do I get the actual amount data out of it.

So, I guess the question is: How to decode the instruction data into a JS object?

like image 738
Milkncookiez Avatar asked Oct 23 '25 15:10

Milkncookiez


1 Answers

So, for your second question:

To know how to deserialize the array returned from the bs58 decoding you need to know how the sender serialized the instruction.

like image 140
Frank C. Avatar answered Oct 26 '25 11:10

Frank C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!