Comments (6)
NOTE: updated example.
from solana-go.
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.
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.
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.
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.
Thanks a lot for the example, super helpful. Closing the issue
from solana-go.
Related Issues (20)
- General Question: Is there any example on submitting txs with priority fees? HOT 2
- Cannot build account meta during transaction decoding HOT 2
- Pls help, dont knpw how to create trans for launchmynft HOT 2
- GetMultipleAccountsWithOpts doesn't honor MinContextSlot
- WSS method blockSubscribe throws "Invalid/Unspported params for blockSubscribe." HOT 2
- Deserialise of uint64 in rentEpoch field is overflow HOT 3
- [HELP] - Always getting a versioned Transaction Too large error
- 文档里面能不能写一个token 兑换的例子?
- WebSocket handle function missing error handling HOT 1
- View all ATA accounts under the account
- serious bug: message version `Legacy` and `0` is not mapped to `0` and `1` HOT 2
- The Instructions field in the Transaction.Message is displayed in the wrong position by solana.Transaction's EncodeToTree() HOT 3
- invalid transaction: Transaction failed to sanitize accounts offsets correctly HOT 1
- panic: runtime error: makeslice: len out of range HOT 6
- unable to decode instruction: no known type for type [12 0 225 245 0 0 0 0] HOT 3
- Pls help me, how to get token metadata? HOT 1
- UiTokenAmount data type HOT 1
- "AccountSubscribe method is invalid" HOT 5
- Priority Fee during this congested period. HOT 4
- Create associated token account idempotent instruction
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from solana-go.