added token-meta to slnc. still missing the program ID to be completed
This commit is contained in:
parent
d7226cdc7b
commit
7a8d2663be
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue