Start printing the orderbook.

This commit is contained in:
Alexandre Bourget 2020-11-06 16:55:52 -05:00
parent b88026d792
commit 708f8646c6
3 changed files with 71 additions and 3 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/mr-tron/base58 v1.2.0 github.com/mr-tron/base58 v1.2.0
github.com/onsi/gomega v1.10.1 // indirect github.com/onsi/gomega v1.10.1 // indirect
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0 github.com/spf13/viper v1.7.0

1
go.sum
View File

@ -201,6 +201,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f h1:UFr9zpz4xgTnIE5yIMtWAMngCdZ9p/+q6lTbgelo80M=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=

View File

@ -1,18 +1,84 @@
package cmd package cmd
import ( import (
"bytes"
"context"
"strings"
"github.com/dfuse-io/solana-go/serum"
"github.com/lunixbochs/struc"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var serumOrderbookCmd = &cobra.Command{ var serumOrderbookCmd = &cobra.Command{
Use: "orderbook {market_addr}", Use: "orderbook {market_addr}",
Short: "Get serum orderbook for a market", Short: "Get Serum orderbook for a given market",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
pubKey, err := solana.PublicKeyFromBase58(args[0])
if err != nil {
return fmt.Errorf("decoding market addr: %w", err)
}
ctx := context.Background()
bids, err := rpcClient.GetProgramAccounts(ctx, pubKey, &rpc.GetProgramAccountsOpts{
Encoding: "base64",
Filters: []rpc.RPCFilter{
{Memcmp: &rpc.RPCFilterMemcmp{Bytes: []byte{0x73, 0x65, 0x72, 0x75, 0x6d, 0x21}}}
},
})
if err != nil {
return fmt.Errorf("failed query: %w", err)
}
fmt.Println("Query done")
for _, bid := range bids {
fmt.Println("Big", bid.Pubkey)
data, err := bid.Account.DataToBytes()
if err != nil {
return fmt.Errorf("decoding bid account data: %w", err)
}
var o serum.Orderbook
if err := struc.Unpack(bytes.NewReader(data), &o); err != nil {
return fmt.Errorf("decoding bid orderbook data: %w", err)
}
output := []string{
"Price | Quantity | Depth",
}
var highestQuantity uint64
o.Items(true, func(node *serum.SlabLeafNode) error {
if uint64(node.Quantity) > highestQuantity {
highestQuantity = uint64(node.Quantity)
}
return nil
})
o.Items(true, func(node *serum.SlabLeafNode) error {
// TODO: compute the actual price and lots size?
price := node.KeyPrice
qty := node.Quantity
percent := uint64(qty) * 100 / highestQuantity
output = append(output,
fmt.Sprintf("%d | %d | %s",
price,
qty,
strings.Repeat("-", int(percent/10))+strings.Repeat("#", int(10-(percent/10))),
),
)
return nil
})
fmt.Println(columnize.Format(output, nil))
}
return nil return nil
}, },
} }
func init() { func init() {
serumCmd.AddCommand(serumMarketsCmd) serumCmd.AddCommand(serumOrderbookCmd)
} }