Skip to content

Commit

Permalink
added get transaction cli cmd with decoded instruction for serum and …
Browse files Browse the repository at this point in the history
…system.
  • Loading branch information
billettc committed Nov 18, 2020
1 parent 57107bb commit 965d5a6
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 80 deletions.
83 changes: 83 additions & 0 deletions cmd/slnc/cmd/get_transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"context"
"fmt"

"github.com/dfuse-io/solana-go"
"github.com/dfuse-io/solana-go/rpc"
_ "github.com/dfuse-io/solana-go/serum"
_ "github.com/dfuse-io/solana-go/system"
"github.com/spf13/cobra"
)

var getTransactionsCmd = &cobra.Command{
Use: "transactions {account}",
Short: "Retrieve transaction for a specific account",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getClient()
ctx := context.Background()

address := args[0]
pubKey, err := solana.PublicKeyFromBase58(address)
errorCheck("public key", err)

csList, err := client.GetConfirmedSignaturesForAddress2(ctx, pubKey, &rpc.GetConfirmedSignaturesForAddress2Opts{
Limit: 10,
Before: "",
Until: "",
})

errorCheck("getting confirm transaction:", err)

for _, cs := range csList {
fmt.Println("-----------------------------------------------------------------------------------------------")
fmt.Println("Transaction: ", cs.Signature)
fmt.Println("Slot: ", cs.Slot)
fmt.Println("Memo: ", cs.Memo)

ct, err := client.GetConfirmedTransaction(ctx, cs.Signature)
errorCheck("confirm transaction", err)
if ct.Meta.Err != nil {
fmt.Println("ERROR:", ct.Meta.Err)
// for k, _ := range ct.Meta.Err
}
fmt.Println("account count:", len(ct.Transaction.Message.AccountKeys))

fmt.Print("\nInstructions:\n-------------\n\n")
for _, i := range ct.Transaction.Message.Instructions {
id, err := ct.Transaction.ResolveProgramIDIndex(i.ProgramIDIndex)
errorCheck("resolving programID", err)
decoder := solana.InstructionDecoderRegistry[id.String()]
if decoder == nil {
continue
}

decoded, err := decoder(ct.Transaction.Message.AccountKeys, &i)
fmt.Printf("%s\n\n", decoded)
}
fmt.Print("End of transaction\n\n")
}

return nil
},
}

func init() {
getCmd.AddCommand(getTransactionsCmd)
}
5 changes: 5 additions & 0 deletions instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package solana

import (
"bytes"
"fmt"

bin "github.com/dfuse-io/binary"
)
Expand All @@ -26,6 +27,10 @@ type AccountMeta struct {
IsWritable bool
}

func (a *AccountMeta) String() string {
return fmt.Sprintf("%s Signer: %t Writable: %t", a.PublicKey.String(), a.IsSigner, a.IsWritable)
}

type Instruction struct {
ProgramID PublicKey
Accounts []AccountMeta
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package solana

type AccountSettable interface {
SetAccounts(accounts []PublicKey)
SetAccounts(accounts []PublicKey) error
}
12 changes: 7 additions & 5 deletions registry.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package solana

import "fmt"
import (
"fmt"
)

type InstructionDecoder func([]PublicKey, *CompiledInstruction) (interface{}, error)

var instructionDecoderRegistry = map[string]InstructionDecoder{}
var InstructionDecoderRegistry = map[string]InstructionDecoder{}

func RegisterInstructionDecoder(programID PublicKey, decoder InstructionDecoder) {
p := programID.String()
if _, found := instructionDecoderRegistry[p]; found {
if _, found := InstructionDecoderRegistry[p]; found {
panic(fmt.Sprintf("unable to re-register instruction decoder for program %q", p))
}
instructionDecoderRegistry[p] = decoder
InstructionDecoderRegistry[p] = decoder
}

func DecodeInstruction(programID PublicKey, accounts []PublicKey, inst *CompiledInstruction) (interface{}, error) {
p := programID.String()

decoder, found := instructionDecoderRegistry[p]
decoder, found := InstructionDecoderRegistry[p]
if !found {
return nil, fmt.Errorf("unknown programID, cannot find any instruction decoder %q", p)
}
Expand Down
Loading

0 comments on commit 965d5a6

Please sign in to comment.