From 7a8d2663be6de3ab5575fe58d02a2aa9320f743d Mon Sep 17 00:00:00 2001 From: billettc Date: Tue, 24 Nov 2020 09:58:28 -0500 Subject: [PATCH] added token-meta to slnc. still missing the program ID to be completed --- cmd/slnc/cmd/get_token_meta.go | 56 ++++++++++++ {serum => programs/serum}/data/markets.json | 0 {serum => programs/serum}/init_test.go | 0 {serum => programs/serum}/instruction.go | 0 {serum => programs/serum}/instruction_test.go | 0 {serum => programs/serum}/logging.go | 4 +- {serum => programs/serum}/market.go | 0 {serum => programs/serum}/math.go | 0 {serum => programs/serum}/rice-box.go | 0 {serum => programs/serum}/rpc.go | 0 {serum => programs/serum}/rpc_test.go | 0 .../serum}/testdata/orderbook.hex | 0 {serum => programs/serum}/types.go | 0 {serum => programs/serum}/types_test.go | 0 {system => programs/system}/init_test.go | 0 {system => programs/system}/instructions.go | 0 .../system}/instructions_test.go | 0 {system => programs/system}/logging.go | 0 {token => programs/token}/instructions.go | 0 .../token}/mints-data/mainnet-tokens.json | 0 {token => programs/token}/rice-box.go | 0 {token => programs/token}/rpc.go | 0 {token => programs/token}/types.go | 0 {token => programs/token}/types_test.go | 0 programs/tokenregistry/instruction.go | 91 +++++++++++++++++++ programs/tokenregistry/logging.go | 27 ++++++ programs/tokenregistry/types.go | 20 ++++ 27 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 cmd/slnc/cmd/get_token_meta.go rename {serum => programs/serum}/data/markets.json (100%) rename {serum => programs/serum}/init_test.go (100%) rename {serum => programs/serum}/instruction.go (100%) rename {serum => programs/serum}/instruction_test.go (100%) rename {serum => programs/serum}/logging.go (87%) rename {serum => programs/serum}/market.go (100%) rename {serum => programs/serum}/math.go (100%) rename {serum => programs/serum}/rice-box.go (100%) rename {serum => programs/serum}/rpc.go (100%) rename {serum => programs/serum}/rpc_test.go (100%) rename {serum => programs/serum}/testdata/orderbook.hex (100%) rename {serum => programs/serum}/types.go (100%) rename {serum => programs/serum}/types_test.go (100%) rename {system => programs/system}/init_test.go (100%) rename {system => programs/system}/instructions.go (100%) rename {system => programs/system}/instructions_test.go (100%) rename {system => programs/system}/logging.go (100%) rename {token => programs/token}/instructions.go (100%) rename {token => programs/token}/mints-data/mainnet-tokens.json (100%) rename {token => programs/token}/rice-box.go (100%) rename {token => programs/token}/rpc.go (100%) rename {token => programs/token}/types.go (100%) rename {token => programs/token}/types_test.go (100%) create mode 100644 programs/tokenregistry/instruction.go create mode 100644 programs/tokenregistry/logging.go create mode 100644 programs/tokenregistry/types.go diff --git a/cmd/slnc/cmd/get_token_meta.go b/cmd/slnc/cmd/get_token_meta.go new file mode 100644 index 0000000..55c53f9 --- /dev/null +++ b/cmd/slnc/cmd/get_token_meta.go @@ -0,0 +1,56 @@ +// 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" + "os" + + bin "github.com/dfuse-io/binary" + "github.com/dfuse-io/solana-go" + "github.com/dfuse-io/solana-go/programs/tokenregistry" + _ "github.com/dfuse-io/solana-go/programs/tokenregistry" + "github.com/dfuse-io/solana-go/text" + "github.com/spf13/cobra" +) + +var getTokenMetaCmd = &cobra.Command{ + Use: "token-meta {account}", + Short: "Retrieve token meta 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) + + accountInfo, err := client.GetAccountInfo(ctx, pubKey) + + var tm *tokenregistry.TokenMeta + err = bin.NewDecoder(accountInfo.Value.Data).Decode(&tm) + errorCheck("decode", err) + + err = text.NewEncoder(os.Stdout).Encode(tm, nil) + errorCheck("textEncoding", err) + + return nil + }, +} + +func init() { + getCmd.AddCommand(getTransactionsCmd) +} diff --git a/serum/data/markets.json b/programs/serum/data/markets.json similarity index 100% rename from serum/data/markets.json rename to programs/serum/data/markets.json diff --git a/serum/init_test.go b/programs/serum/init_test.go similarity index 100% rename from serum/init_test.go rename to programs/serum/init_test.go diff --git a/serum/instruction.go b/programs/serum/instruction.go similarity index 100% rename from serum/instruction.go rename to programs/serum/instruction.go diff --git a/serum/instruction_test.go b/programs/serum/instruction_test.go similarity index 100% rename from serum/instruction_test.go rename to programs/serum/instruction_test.go diff --git a/serum/logging.go b/programs/serum/logging.go similarity index 87% rename from serum/logging.go rename to programs/serum/logging.go index b2767eb..c3b7c0b 100644 --- a/serum/logging.go +++ b/programs/serum/logging.go @@ -20,8 +20,8 @@ import ( ) var zlog = zap.NewNop() -var traceEnabled = logging.IsTraceEnabled("solana-go", "github.com/dfuse-io/solana-go/serum") +var traceEnabled = logging.IsTraceEnabled("solana-go", "github.com/dfuse-io/solana-go/program/serum") func init() { - logging.Register("github.com/dfuse-io/solana-go/serum", &zlog) + logging.Register("github.com/dfuse-io/solana-go/program/serum", &zlog) } diff --git a/serum/market.go b/programs/serum/market.go similarity index 100% rename from serum/market.go rename to programs/serum/market.go diff --git a/serum/math.go b/programs/serum/math.go similarity index 100% rename from serum/math.go rename to programs/serum/math.go diff --git a/serum/rice-box.go b/programs/serum/rice-box.go similarity index 100% rename from serum/rice-box.go rename to programs/serum/rice-box.go diff --git a/serum/rpc.go b/programs/serum/rpc.go similarity index 100% rename from serum/rpc.go rename to programs/serum/rpc.go diff --git a/serum/rpc_test.go b/programs/serum/rpc_test.go similarity index 100% rename from serum/rpc_test.go rename to programs/serum/rpc_test.go diff --git a/serum/testdata/orderbook.hex b/programs/serum/testdata/orderbook.hex similarity index 100% rename from serum/testdata/orderbook.hex rename to programs/serum/testdata/orderbook.hex diff --git a/serum/types.go b/programs/serum/types.go similarity index 100% rename from serum/types.go rename to programs/serum/types.go diff --git a/serum/types_test.go b/programs/serum/types_test.go similarity index 100% rename from serum/types_test.go rename to programs/serum/types_test.go diff --git a/system/init_test.go b/programs/system/init_test.go similarity index 100% rename from system/init_test.go rename to programs/system/init_test.go diff --git a/system/instructions.go b/programs/system/instructions.go similarity index 100% rename from system/instructions.go rename to programs/system/instructions.go diff --git a/system/instructions_test.go b/programs/system/instructions_test.go similarity index 100% rename from system/instructions_test.go rename to programs/system/instructions_test.go diff --git a/system/logging.go b/programs/system/logging.go similarity index 100% rename from system/logging.go rename to programs/system/logging.go diff --git a/token/instructions.go b/programs/token/instructions.go similarity index 100% rename from token/instructions.go rename to programs/token/instructions.go diff --git a/token/mints-data/mainnet-tokens.json b/programs/token/mints-data/mainnet-tokens.json similarity index 100% rename from token/mints-data/mainnet-tokens.json rename to programs/token/mints-data/mainnet-tokens.json diff --git a/token/rice-box.go b/programs/token/rice-box.go similarity index 100% rename from token/rice-box.go rename to programs/token/rice-box.go diff --git a/token/rpc.go b/programs/token/rpc.go similarity index 100% rename from token/rpc.go rename to programs/token/rpc.go diff --git a/token/types.go b/programs/token/types.go similarity index 100% rename from token/types.go rename to programs/token/types.go diff --git a/token/types_test.go b/programs/token/types_test.go similarity index 100% rename from token/types_test.go rename to programs/token/types_test.go diff --git a/programs/tokenregistry/instruction.go b/programs/tokenregistry/instruction.go new file mode 100644 index 0000000..69ca37a --- /dev/null +++ b/programs/tokenregistry/instruction.go @@ -0,0 +1,91 @@ +package tokenregistry + +import ( + "encoding/binary" + "fmt" + + "github.com/dfuse-io/solana-go/text" + + bin "github.com/dfuse-io/binary" + "github.com/dfuse-io/solana-go" +) + +var PROGRAM_ID = solana.MustPublicKeyFromBase58("ask julien for program id") + +func init() { + solana.RegisterInstructionDecoder(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 +} + +type Instruction struct { + bin.BaseVariant +} + +var InstructionDefVariant = bin.NewVariantDefinition(bin.Uint32TypeIDEncoding, []bin.VariantType{ + {"register_token", (*RegisterToken)(nil)}, +}) + +func (i *Instruction) TextEncode(encoder *text.Encoder, option *text.Option) error { + return encoder.Encode(i.Impl, option) +} + +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.WriteUint32(i.TypeID, binary.LittleEndian) + if err != nil { + return fmt.Errorf("unable to write variant type: %w", err) + } + return encoder.Encode(i.Impl) +} + +type RegisterTokenAccounts struct { + MintMeta *solana.AccountMeta `text:"linear,notype"` + Owner *solana.AccountMeta `text:"linear,notype"` + Mint *solana.AccountMeta `text:"linear,notype"` +} + +type RegisterToken struct { + Logo [32]byte + Name [32]byte + Symbol [12]byte + Accounts *RegisterTokenAccounts `bin:"-"` +} + +func (i *RegisterToken) SetAccounts(accounts []*solana.AccountMeta, instructionActIdx []uint8) error { + if len(instructionActIdx) < 9 { + return fmt.Errorf("insuficient account") + } + i.Accounts = &RegisterTokenAccounts{ + MintMeta: accounts[instructionActIdx[0]], + Owner: accounts[instructionActIdx[1]], + Mint: accounts[instructionActIdx[2]], + } + + return nil +} diff --git a/programs/tokenregistry/logging.go b/programs/tokenregistry/logging.go new file mode 100644 index 0000000..8dc8e42 --- /dev/null +++ b/programs/tokenregistry/logging.go @@ -0,0 +1,27 @@ +// 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 tokenregistry + +import ( + "github.com/dfuse-io/logging" + "go.uber.org/zap" +) + +var zlog = zap.NewNop() +var traceEnabled = logging.IsTraceEnabled("solana-go", "github.com/dfuse-io/solana-go/program/tokenregistry") + +func init() { + logging.Register("github.com/dfuse-io/solana-go/program/tokenregistry", &zlog) +} diff --git a/programs/tokenregistry/types.go b/programs/tokenregistry/types.go new file mode 100644 index 0000000..fdba445 --- /dev/null +++ b/programs/tokenregistry/types.go @@ -0,0 +1,20 @@ +// 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 tokenregistry + +type TokenMeta struct { + Logo [32]byte + Name [32]byte + Symbol [12]byte +}