Milkncookiez
Milkncookiez

Reputation: 7407

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?

Upvotes: 4

Views: 8831

Answers (2)

Milkncookiez
Milkncookiez

Reputation: 7407

I was redirected to solana.stackexchange.com and there I found the answer. Basically, I could use getParsedTransaction instead of just getTransaction.

The parsed function would give me exactly what I need, since the instructions prop is an array containing one object (in my case), which looks like this:

{
  parsed: {
    info: {
      destination: 'foo',
      lamports: 100000000,
      source: 'bar'
    },
    type: 'transfer'
  },
  // ... other props
}

Upvotes: 1

Frank C.
Frank C.

Reputation: 8098

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.

Upvotes: 1

Related Questions