GithubHelp home page GithubHelp logo

Comments (6)

gagliardetto avatar gagliardetto commented on July 17, 2024 1

NOTE: updated example.

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

Hi @Lazar955

solana.Transaction is the representation of a transaction you send to be processed on the blockchain; Meta is not part of that object, and is not user-definable.

Meta is the object that contains the outcome of the transaction processing (and that's why that's something you can't send).

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

In GetTransaction RPC method, the Meta is not part of the transaction:

https://github.com/gagliardetto/solana-go#index--rpc--gettransaction

package main

import (
  "context"

  "github.com/davecgh/go-spew/spew"
  "github.com/gagliardetto/solana-go"
  "github.com/gagliardetto/solana-go/rpc"
)

func main() {
  endpoint := rpc.TestNet_RPC
  client := rpc.New(endpoint)

  txSig := solana.MustSignatureFromBase58("4bjVLV1g9SAfv7BSAdNnuSPRbSscADHFe4HegL6YVcuEBMY83edLEvtfjE4jfr6rwdLwKBQbaFiGgoLGtVicDzHq")
  {
    out, err := client.GetTransaction(
      context.TODO(),
      txSig,
      &rpc.GetTransactionOpts{
        Encoding: solana.EncodingBase64,
      },
    )
    if err != nil {
      panic(err)
    }
    spew.Dump(out)
    spew.Dump(out.Transaction.GetBinary())

    decodedTx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(out.Transaction.GetBinary()))
    if err != nil {
      panic(err)
    }
    spew.Dump(decodedTx)
  }
}

decodedTx is a solana.Transaction type.

from solana-go.

Lazar955 avatar Lazar955 commented on July 17, 2024

Hey @gagliardetto,

Thanks for the reply.

That makes sense. But how would I go about decoding a transaction that has already been submitted?
Maybe I am misunderstanding as I am new to Solana, if I create a transaction transferring sol from one account to another, how would I decode the instructions for that?

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

A transaction contains a list of base64-encoded instructions data.

To decode a specific instruction, you can do something like this:

https://github.com/gagliardetto/solana-go#parsedecode-an-instruction-from-a-transaction

package main

import (
	"context"
	"encoding/base64"
	"os"
	"reflect"

	"github.com/davecgh/go-spew/spew"
	bin "github.com/gagliardetto/binary"
	"github.com/gagliardetto/solana-go"
	"github.com/gagliardetto/solana-go/programs/system"
	"github.com/gagliardetto/solana-go/rpc"
	"github.com/gagliardetto/solana-go/text"
)

func main() {
	exampleFromGetTransaction()
}

func exampleFromBase64() {
	encoded := "AfjEs3XhTc3hrxEvlnMPkm/cocvAUbFNbCl00qKnrFue6J53AhEqIFmcJJlJW3EDP5RmcMz+cNTTcZHW/WJYwAcBAAEDO8hh4VddzfcO5jbCt95jryl6y8ff65UcgukHNLWH+UQGgxCGGpgyfQVQV02EQYqm4QwzUt2qf9f1gVLM7rI4hwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA6ANIF55zOZWROWRkeh+lExxZBnKFqbvIxZDLE7EijjoBAgIAAQwCAAAAOTAAAAAAAAA="

	data, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		panic(err)
	}

	// parse transaction:
	tx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(data))
	if err != nil {
		panic(err)
	}

	decodeSystemTransfer(tx)
}

func exampleFromGetTransaction() {
	endpoint := rpc.TestNet_RPC
	client := rpc.New(endpoint)

	txSig := solana.MustSignatureFromBase58("3pByJJ2ff7EQANKd2bgetmnYQxknk3QUib1xLMnrg6aCvg5hS78peaGMoceC9AFckomqrsgo38DpzrG2LPW9zj3g")
	{
		out, err := client.GetTransaction(
			context.TODO(),
			txSig,
			&rpc.GetTransactionOpts{
				Encoding: solana.EncodingBase64,
			},
		)
		if err != nil {
			panic(err)
		}

		tx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(out.Transaction.GetBinary()))
		if err != nil {
			panic(err)
		}

		decodeSystemTransfer(tx)
	}
}

func decodeSystemTransfer(tx *solana.Transaction) {
	spew.Dump(tx)

	// we know that the first instruction of the transaction is a `system` program instruction:
	i0 := tx.Message.Instructions[0]

	// parse a system program instruction:
	inst, err := system.DecodeInstruction(i0.ResolveInstructionAccounts(&tx.Message), i0.Data)
	if err != nil {
		panic(err)
	}
	// inst.Impl contains the specific instruction type (in this case, `inst.Impl` is a `*system.Transfer`)
	spew.Dump(inst)

	// OR
	{
		// There is a more general instruction decoder;
		// before you can use you `solana.DecodeInstruction`,
		// you must to register a decoder for each program ID beforehand
		// by using `solana.RegisterInstructionDecoder`.
		decodedInstruction, err := solana.DecodeInstruction(
			system.ProgramID,
			i0.ResolveInstructionAccounts(&tx.Message),
			tx.Message.Instructions[0].Data,
		)
		if err != nil {
			panic(err)
		}
		spew.Dump(decodedInstruction)

		// decodedInstruction == inst
		if !reflect.DeepEqual(inst, decodedInstruction) {
			panic("they are NOT equal (this would never happen)")
		}

		// To register other (not yet registered decoders), you can add them with
		// `solana.RegisterInstructionDecoder` function.
	}

	{
		// pretty-print whole transaction:
		_, err := tx.EncodeTree(text.NewTreeEncoder(os.Stdout, text.Bold("TEST TRANSACTION")))
		if err != nil {
			panic(err)
		}
	}
}

from solana-go.

Lazar955 avatar Lazar955 commented on July 17, 2024

Thanks a lot for the example, super helpful. Closing the issue

from solana-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.