added `token` program structs and decoding code

This commit is contained in:
billettc 2020-11-23 11:12:02 -05:00
parent 31f1d42a77
commit 58bade271a
6 changed files with 199 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/dfuse-io/solana-go/rpc" "github.com/dfuse-io/solana-go/rpc"
_ "github.com/dfuse-io/solana-go/serum" _ "github.com/dfuse-io/solana-go/serum"
_ "github.com/dfuse-io/solana-go/system" _ "github.com/dfuse-io/solana-go/system"
_ "github.com/dfuse-io/solana-go/token"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -41,7 +42,7 @@ var getTransactionsCmd = &cobra.Command{
errorCheck("public key", err) errorCheck("public key", err)
csList, err := client.GetConfirmedSignaturesForAddress2(ctx, pubKey, &rpc.GetConfirmedSignaturesForAddress2Opts{ csList, err := client.GetConfirmedSignaturesForAddress2(ctx, pubKey, &rpc.GetConfirmedSignaturesForAddress2Opts{
Limit: 100, Limit: 1,
Before: "", Before: "",
Until: "", Until: "",
}) })
@ -87,6 +88,7 @@ var getTransactionsCmd = &cobra.Command{
} }
decoded, err := decoder(ct.Transaction.AccountMetaList(), &i) decoded, err := decoder(ct.Transaction.AccountMetaList(), &i)
errorCheck("bin decode", err)
err = text.NewEncoder(os.Stdout).Encode(decoded, nil) err = text.NewEncoder(os.Stdout).Encode(decoded, nil)
errorCheck("textEncoding", err) errorCheck("textEncoding", err)
} }

3
go.mod
View File

@ -4,7 +4,7 @@ go 1.14
require ( require (
github.com/GeertJohan/go.rice v1.0.0 github.com/GeertJohan/go.rice v1.0.0
github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309 github.com/dfuse-io/binary v0.0.0-20201123150056-096380ef3e5d
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79
github.com/fatih/color v1.7.0 github.com/fatih/color v1.7.0
github.com/google/go-cmp v0.4.1 // indirect github.com/google/go-cmp v0.4.1 // indirect
@ -27,3 +27,4 @@ require (
golang.org/x/tools v0.0.0-20200601175630-2caf76543d99 // indirect golang.org/x/tools v0.0.0-20200601175630-2caf76543d99 // indirect
google.golang.org/api v0.15.0 google.golang.org/api v0.15.0
) )

2
go.sum
View File

@ -51,6 +51,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309 h1:GO2JfJeCROHoV48h+1ioDCTW2sqeu7exi3cZrzIak+g= github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309 h1:GO2JfJeCROHoV48h+1ioDCTW2sqeu7exi3cZrzIak+g=
github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/binary v0.0.0-20201123150056-096380ef3e5d h1:GcxIdxuJIC7dhfU8Dh5VuJte9QC+JaWV8/v9D6kshzg=
github.com/dfuse-io/binary v0.0.0-20201123150056-096380ef3e5d/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 h1:+HRtcJejUYA/2rnyTMbOaZ4g7f4aVuFduTV/03dbpLY= github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 h1:+HRtcJejUYA/2rnyTMbOaZ4g7f4aVuFduTV/03dbpLY=
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79/go.mod h1:V+ED4kT/t/lKtH99JQmKIb0v9WL3VaYkJ36CfHlVECI= github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79/go.mod h1:V+ED4kT/t/lKtH99JQmKIb0v9WL3VaYkJ36CfHlVECI=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

View File

@ -1,6 +1,7 @@
package serum package serum
import ( import (
"encoding/binary"
"fmt" "fmt"
"github.com/dfuse-io/solana-go/text" "github.com/dfuse-io/solana-go/text"
@ -62,7 +63,7 @@ func (i *Instruction) MarshalBinary(encoder *bin.Encoder) error {
return fmt.Errorf("unable to write instruction version: %w", err) return fmt.Errorf("unable to write instruction version: %w", err)
} }
err = encoder.WriteUint32(i.TypeID) err = encoder.WriteUint32(i.TypeID, binary.LittleEndian)
if err != nil { if err != nil {
return fmt.Errorf("unable to write variant type: %w", err) return fmt.Errorf("unable to write variant type: %w", err)
} }

View File

@ -15,6 +15,7 @@ package serum
import ( import (
"encoding/base64" "encoding/base64"
"encoding/binary"
"fmt" "fmt"
"math/big" "math/big"
@ -118,7 +119,7 @@ func (s *Slab) UnmarshalBinary(decoder *bin.Decoder) error {
return s.BaseVariant.UnmarshalBinaryVariant(decoder, SlabFactoryImplDef) return s.BaseVariant.UnmarshalBinaryVariant(decoder, SlabFactoryImplDef)
} }
func (s *Slab) MarshalBinary(encoder *bin.Encoder) error { func (s *Slab) MarshalBinary(encoder *bin.Encoder) error {
err := encoder.WriteUint32(s.TypeID) err := encoder.WriteUint32(s.TypeID, binary.LittleEndian)
if err != nil { if err != nil {
return err return err
} }

View File

@ -13,3 +13,191 @@
// limitations under the License. // limitations under the License.
package token package token
import (
"fmt"
"github.com/dfuse-io/solana-go/text"
bin "github.com/dfuse-io/binary"
"github.com/dfuse-io/solana-go"
)
var TOKEN_PROGRAM_ID = solana.MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
func init() {
solana.RegisterInstructionDecoder(TOKEN_PROGRAM_ID, registryDecodeInstruction)
}
func registryDecodeInstruction(accounts []*solana.AccountMeta, rawInstruction *solana.CompiledInstruction) (interface{}, error) {
inst, err := DecodeInstruction(accounts, rawInstruction)
if err != nil {
return nil, err
}
return inst, nil
}
func DecodeInstruction(accounts []*solana.AccountMeta, compiledInstruction *solana.CompiledInstruction) (*Instruction, error) {
var inst Instruction
if err := bin.NewDecoder(compiledInstruction.Data).Decode(&inst); err != nil {
return nil, fmt.Errorf("unable to decode instruction for serum program: %w", err)
}
if v, ok := inst.Impl.(solana.AccountSettable); ok {
err := v.SetAccounts(accounts, compiledInstruction.Accounts)
if err != nil {
return nil, fmt.Errorf("unable to set accounts for instruction: %w", err)
}
}
return &inst, nil
}
var InstructionDefVariant = bin.NewVariantDefinition(bin.Uint8TypeIDEncoding, []bin.VariantType{
{"initialize_mint", (*InitializeMint)(nil)},
{"initialize_account", (*InitializeAccount)(nil)},
{"InitializeMultisig", (*InitializeMultisig)(nil)},
{"Transfer", (*Transfer)(nil)},
{"Approve", (*Approve)(nil)},
{"Revoke", (*Revoke)(nil)},
{"SetAuthority", (*SetAuthority)(nil)},
{"MintTo", (*MintTo)(nil)},
{"Burn", (*Burn)(nil)},
{"CloseAccount", (*CloseAccount)(nil)},
{"FreezeAccount", (*FreezeAccount)(nil)},
{"ThawAccount", (*ThawAccount)(nil)},
{"TransferChecked", (*TransferChecked)(nil)},
{"ApproveChecked", (*ApproveChecked)(nil)},
{"MintToChecked", (*MintToChecked)(nil)},
{"BurnChecked", (*BurnChecked)(nil)},
})
type Instruction struct {
bin.BaseVariant
}
func (i *Instruction) UnmarshalBinary(decoder *bin.Decoder) (err error) {
return i.BaseVariant.UnmarshalBinaryVariant(decoder, InstructionDefVariant)
}
func (i *Instruction) MarshalBinary(encoder *bin.Encoder) error {
err := encoder.WriteUint8(uint8(i.TypeID))
if err != nil {
return fmt.Errorf("unable to write variant type: %w", err)
}
return encoder.Encode(i.Impl)
}
func (i *Instruction) TextEncode(encoder *text.Encoder, option *text.Option) error {
return encoder.Encode(i.Impl, option)
}
type InitializeMultisigAccounts struct {
}
type InitializeMultisig struct {
Accounts *InitializeMultisigAccounts
}
type InitializeMintAccounts struct {
}
type InitializeMint struct {
Accounts *InitializeMintAccounts
}
type TransferAccounts struct {
}
type Transfer struct {
Accounts *TransferAccounts
}
type ApproveAccounts struct {
}
type Approve struct {
Accounts *ApproveAccounts
}
type RevokeAccounts struct {
}
type Revoke struct {
Accounts *RevokeAccounts
}
type SetAuthorityAccounts struct {
}
type SetAuthority struct {
Accounts *SetAuthorityAccounts
}
type MintToAccounts struct {
}
type MintTo struct {
Accounts *MintToAccounts
}
type BurnAccounts struct {
}
type Burn struct {
Accounts *BurnAccounts
}
type CloseAccountAccounts struct {
}
type CloseAccount struct {
Accounts *CloseAccountAccounts
}
type FreezeAccountAccounts struct {
}
type FreezeAccount struct {
Accounts *FreezeAccountAccounts
}
type ThawAccountAccounts struct {
}
type ThawAccount struct {
Accounts *ThawAccountAccounts
}
type TransferCheckedAccounts struct {
}
type TransferChecked struct {
Accounts *TransferCheckedAccounts
}
type ApproveCheckedAccounts struct {
}
type ApproveChecked struct {
Accounts *ApproveCheckedAccounts
}
type MintToCheckedAccounts struct {
}
type MintToChecked struct {
Accounts *MintToCheckedAccounts
}
type BurnCheckedAccounts struct {
}
type BurnChecked struct {
Accounts *BurnCheckedAccounts
}
type InitializeAccountAccounts struct {
Account *solana.AccountMeta `text:"linear,notype"`
Mint *solana.AccountMeta `text:"linear,notype"`
Owner *solana.AccountMeta `text:"linear,notype"`
RentSysvar *solana.AccountMeta `text:"linear,notype"`
}
type InitializeAccount struct {
Accounts *InitializeAccountAccounts `bin:"-"`
}
func (i *InitializeAccount) SetAccounts(accounts []*solana.AccountMeta, instructionActIdx []uint8) error {
i.Accounts = &InitializeAccountAccounts{
Account: accounts[instructionActIdx[0]],
Mint: accounts[instructionActIdx[1]],
Owner: accounts[instructionActIdx[2]],
RentSysvar: accounts[instructionActIdx[3]],
}
return nil
}