Skip to content

Commit

Permalink
Added hard-coded token mints.
Browse files Browse the repository at this point in the history
Shuffle around the commands on `slnc`.
Better decoding, better reading of data.
Simplified API
  • Loading branch information
abourget committed Nov 9, 2020
1 parent 438d441 commit 0002874
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 130 deletions.
7 changes: 1 addition & 6 deletions cmd/slnc/cmd/get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/dfuse-io/solana-go"
Expand All @@ -24,18 +23,14 @@ var getAccountCmd = &cobra.Command{
return err
}

if resp.Value == nil {
errorCheck("not found", errors.New("account not found"))
}

acct := resp.Value

data, err := json.MarshalIndent(acct, "", " ")
errorCheck("json marshal", err)

fmt.Println(string(data))

obj, err := decode(acct.Owner, acct.MustDataToBytes())
obj, err := decode(acct.Owner, acct.Data)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slnc/cmd/get_program_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var getProgramAccountsCmd = &cobra.Command{
acct := keyedAcct.Account
//fmt.Println("Data len:", len(acct.Data), keyedAcct.Pubkey)

obj, err := decode(acct.Owner, acct.MustDataToBytes())
obj, err := decode(acct.Owner, acct.Data)
if err != nil {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/slnc/cmd/serum_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/spf13/cobra"
)

var serumGetCmd = &cobra.Command{
Use: "get",
Short: "Get Serum objects",
}

func init() {
serumCmd.AddCommand(serumGetCmd)
}
19 changes: 4 additions & 15 deletions cmd/slnc/cmd/serum_market.go → cmd/slnc/cmd/serum_get_market.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package cmd

import (
"bytes"
"context"
"fmt"
"math/big"

"github.com/dfuse-io/solana-go"
"github.com/dfuse-io/solana-go/rpc"
"github.com/dfuse-io/solana-go/serum"
"github.com/lunixbochs/struc"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

var serumMarketCmd = &cobra.Command{
var serumGetMarketCmd = &cobra.Command{
Use: "market {market_addr}",
Short: "Get Serum orderbook for a given market",
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -71,18 +69,9 @@ type orderBookEntry struct {
}

func getOrderBook(ctx context.Context, market *serum.MarketMeta, cli *rpc.Client, address solana.PublicKey, desc bool) (out []*orderBookEntry, err error) {
bids, err := cli.GetAccountInfo(ctx, address)
if err != nil {
return nil, fmt.Errorf("failed retrieving account: %w", err)
}

data, err := bids.Value.DataToBytes()
if err != nil {
return nil, fmt.Errorf("decoding account data: %w", err)
}
var o serum.Orderbook
if err := struc.Unpack(bytes.NewReader(data), &o); err != nil {
return nil, fmt.Errorf("decoding bid orderbook data: %w", err)
if err := cli.GetAccountDataIn(ctx, address, &o); err != nil {
return nil, fmt.Errorf("getting orderbook: %w", err)
}

limit := 20
Expand Down Expand Up @@ -115,5 +104,5 @@ func getOrderBook(ctx context.Context, market *serum.MarketMeta, cli *rpc.Client
}

func init() {
serumCmd.AddCommand(serumMarketCmd)
serumGetCmd.AddCommand(serumGetMarketCmd)
}
14 changes: 14 additions & 0 deletions cmd/slnc/cmd/serum_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/spf13/cobra"
)

var serumListCmd = &cobra.Command{
Use: "list",
Short: "List Serum objects",
}

func init() {
serumCmd.AddCommand(serumListCmd)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"

"github.com/dfuse-io/solana-go/serum"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

var serumMarketsCmd = &cobra.Command{
var serumListMarketsCmd = &cobra.Command{
Use: "markets",
Short: "Get serum markets",
Args: cobra.ExactArgs(0),
Expand All @@ -18,14 +19,18 @@ var serumMarketsCmd = &cobra.Command{
return fmt.Errorf("unable to retrieve markets: %w", err)
}

out := []string{"Pairs | Market Address"}

for _, market := range markets {
fmt.Printf("%s -> %s\n", market.Name, market.Address.String())
out = append(out, fmt.Sprintf("%s | %s ", market.Name, market.Address.String()))
}

fmt.Println(columnize.Format(out, nil))

return nil
},
}

func init() {
serumCmd.AddCommand(serumMarketsCmd)
serumListCmd.AddCommand(serumListMarketsCmd)
}
68 changes: 68 additions & 0 deletions cmd/slnc/cmd/spl_get_mint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"

"github.com/dfuse-io/solana-go"
"github.com/dfuse-io/solana-go/token"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

var splGetMintCmd = &cobra.Command{
Use: "get-mint {mint_addr}",
Short: "Retrieves mint information",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

mintAddr, err := solana.PublicKeyFromBase58(args[0])
if err != nil {
return fmt.Errorf("decoding mint addr: %w", err)
}

client := getClient()

acct, err := client.GetAccountInfo(ctx, mintAddr)
if err != nil {
return fmt.Errorf("couldn't get account data: %w", err)
}

mint, err := token.DecodeMint(acct.Value.Data)
if err != nil {
return fmt.Errorf("unable to retrieve int information: %w", err)
}

if !mint.IsInitialized {
fmt.Println("Uninitialized mint. Data length", len(acct.Value.Data))
return nil
}

var out []string

out = append(out, fmt.Sprintf("Data length | %d", len(acct.Value.Data)))

if mint.MintAuthorityOption != 0 {
out = append(out, fmt.Sprintf("Mint Authority | %s", mint.MintAuthority))
} else {
out = append(out, "No mint authority")
}

out = append(out, fmt.Sprintf("Supply | %d", mint.Supply))
out = append(out, fmt.Sprintf("Decimals | %d", mint.Decimals))

if mint.FreezeAuthorityOption != 0 {
out = append(out, fmt.Sprintf("Freeze Authority | %s", mint.FreezeAuthority))
} else {
out = append(out, "No freeze authority")
}

fmt.Println(columnize.Format(out, nil))

return nil
},
}

func init() {
splCmd.AddCommand(splGetMintCmd)
}
36 changes: 36 additions & 0 deletions cmd/slnc/cmd/spl_list_mints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"

"github.com/dfuse-io/solana-go/token"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

var splListMintsCmd = &cobra.Command{
Use: "list-mints",
Short: "Lists mints",
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: implement a different network argument,
// later. Ultimately, get on chain. We have a database here!

mints, err := token.KnownMints("mainnet")
if err != nil {
return fmt.Errorf("listing mints: %w", err)
}
out := []string{"Symbol | Mint address | Token name"}

for _, m := range mints {
out = append(out, fmt.Sprintf("%s | %s | %s", m.TokenSymbol, m.MintAddress, m.TokenName))
}

fmt.Println(columnize.Format(out, nil))

return nil
},
}

func init() {
splCmd.AddCommand(splListMintsCmd)
}
48 changes: 0 additions & 48 deletions cmd/slnc/cmd/spl_mint.go

This file was deleted.

36 changes: 36 additions & 0 deletions nativetypes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solana

import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
Expand Down Expand Up @@ -139,6 +140,41 @@ func (t Base58) String() string {
return base58.Encode(t)
}

///

type Data []byte

func (t Data) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"data": []byte(t),
"encoding": "base64",
})
}

func (t *Data) UnmarshalJSON(data []byte) (err error) {
var in []string
if err := json.Unmarshal(data, &in); err != nil {
return err
}

if len(in) != 2 {
return fmt.Errorf("invalid length for solana.Data, expected 2, found %d", len(in))
}

if in[1] == "base64" {
*t, err = base64.StdEncoding.DecodeString(in[0])
if err != nil {
return err
}
return nil
}
return fmt.Errorf("unsupported encoding %s", in[1])
}

func (t Data) String() string {
return base64.StdEncoding.EncodeToString(t)
}

///
type U128 big.Int

Expand Down
Loading

0 comments on commit 0002874

Please sign in to comment.