From b124787015efc244081a98329ad03cea11416ec9 Mon Sep 17 00:00:00 2001 From: Slavomir Date: Mon, 6 Sep 2021 17:58:08 +0200 Subject: [PATCH] Improve naming --- registry.go | 8 +-- rpc/sendAndConfirmTransaction.go | 52 ++++++++++++++++++ .../sendAndConfirmTransaction.go | 53 +++++++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 rpc/sendAndConfirmTransaction.go create mode 100644 rpc/sendAndConfirmTransaction/sendAndConfirmTransaction.go diff --git a/registry.go b/registry.go index 8d68489..a3a0ae8 100644 --- a/registry.go +++ b/registry.go @@ -12,12 +12,12 @@ type InstructionDecoder func(instructionAccounts []*AccountMeta, data []byte) (i var InstructionDecoderRegistry = map[string]InstructionDecoder{} func RegisterInstructionDecoder(programID PublicKey, decoder InstructionDecoder) { - p := programID.String() - if _, found := InstructionDecoderRegistry[p]; found { - panic(fmt.Sprintf("unable to re-register instruction decoder for program %q", p)) + pid := programID.String() + if _, found := InstructionDecoderRegistry[pid]; found { + panic(fmt.Sprintf("unable to re-register instruction decoder for program %q", pid)) } - InstructionDecoderRegistry[p] = decoder + InstructionDecoderRegistry[pid] = decoder } func DecodeInstruction(programID PublicKey, accounts []*AccountMeta, data []byte) (interface{}, error) { diff --git a/rpc/sendAndConfirmTransaction.go b/rpc/sendAndConfirmTransaction.go new file mode 100644 index 0000000..ee39146 --- /dev/null +++ b/rpc/sendAndConfirmTransaction.go @@ -0,0 +1,52 @@ +package rpc + +import ( + "context" + + "github.com/davecgh/go-spew/spew" + "github.com/gagliardetto/solana-go" + "github.com/gagliardetto/solana-go/rpc/ws" +) + +func (cl *Client) SendAndConfirmTransactionWithOpts( + ctx context.Context, + transaction *solana.Transaction, + skipPreflight bool, // if true, skip the preflight transaction checks (default: false) + preflightCommitment CommitmentType, // optional; Commitment level to use for preflight (default: "finalized"). +) (signature solana.Signature, err error) { + + sig, err := cl.SendTransactionWithOpts( + ctx, + transaction, + skipPreflight, + preflightCommitment, + ) + if err != nil { + return sig, err + } + + client, err := ws.Connect(context.Background(), TestNet_WS) + if err != nil { + panic(err) + } + + txSig := solana.MustSignatureFromBase58("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") + + sub, err := client.SignatureSubscribe( + txSig, + "", + ) + if err != nil { + panic(err) + } + defer sub.Unsubscribe() + + for { + got, err := sub.Recv() + if err != nil { + panic(err) + } + spew.Dump(got) + } + return +} diff --git a/rpc/sendAndConfirmTransaction/sendAndConfirmTransaction.go b/rpc/sendAndConfirmTransaction/sendAndConfirmTransaction.go new file mode 100644 index 0000000..0942fd8 --- /dev/null +++ b/rpc/sendAndConfirmTransaction/sendAndConfirmTransaction.go @@ -0,0 +1,53 @@ +package sendandconfirmtransaction + +import ( + "context" + "fmt" + + "github.com/davecgh/go-spew/spew" + "github.com/gagliardetto/solana-go" + "github.com/gagliardetto/solana-go/rpc" + "github.com/gagliardetto/solana-go/rpc/ws" +) + +func SendAndConfirmTransactionWithOpts( + ctx context.Context, + rpcClient *rpc.Client, + wsClient *ws.Client, + transaction *solana.Transaction, + skipPreflight bool, // if true, skip the preflight transaction checks (default: false) + preflightCommitment rpc.CommitmentType, // optional; Commitment level to use for preflight (default: "finalized"). +) (signature solana.Signature, err error) { + + sig, err := rpcClient.SendTransactionWithOpts( + ctx, + transaction, + skipPreflight, + preflightCommitment, + ) + if err != nil { + return sig, err + } + + sub, err := wsClient.SignatureSubscribe( + sig, + rpc.CommitmentConfirmed, + ) + if err != nil { + panic(err) + } + defer sub.Unsubscribe() + + for { + got, err := sub.Recv() + if err != nil { + return sig, err + } + spew.Dump(got) + if got.Value.Err != nil { + return sig, fmt.Errorf("confirmation error: %v", got.Value.Err) + } else { + return sig, nil + } + } +}