2020-11-06 13:52:19 -08:00
|
|
|
package serum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-11-06 14:18:33 -08:00
|
|
|
"encoding/json"
|
2020-11-06 13:52:19 -08:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-06 14:02:52 -08:00
|
|
|
"github.com/dfuse-io/solana-go/token"
|
|
|
|
|
2020-11-06 13:52:19 -08:00
|
|
|
"github.com/dfuse-io/solana-go"
|
|
|
|
"github.com/dfuse-io/solana-go/rpc"
|
|
|
|
"github.com/lunixbochs/struc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SerumClient struct {
|
|
|
|
rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSerumClient(endpoint string) *SerumClient {
|
|
|
|
endpoint = "http://api.mainnet-beta.solana.com:80/rpc"
|
|
|
|
return &SerumClient{
|
|
|
|
*rpc.NewClient(endpoint),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SerumClient) FetchMarket(ctx context.Context, marketAddr solana.PublicKey) (*MarketMeta, error) {
|
|
|
|
accInfo, err := s.GetAccountInfo(ctx, marketAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get market account:%w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountData, err := accInfo.Value.DataToBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve account data byte: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m MarketV2
|
2020-11-06 14:02:52 -08:00
|
|
|
err = struc.Unpack(bytes.NewReader(accountData), &m)
|
2020-11-06 13:52:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode market: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:18:33 -08:00
|
|
|
fmt.Println("market addr", marketAddr)
|
|
|
|
cnt, _ := json.MarshalIndent(m, "", " ")
|
|
|
|
fmt.Println(string(cnt))
|
|
|
|
|
2020-11-06 14:02:52 -08:00
|
|
|
baseMint, err := s.getToken(ctx, m.BaseMint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve base token: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:54:26 -08:00
|
|
|
cnt, _ = json.MarshalIndent(baseMint, "", " ")
|
|
|
|
fmt.Println(string(cnt))
|
|
|
|
|
2020-11-06 14:02:52 -08:00
|
|
|
quoteMint, err := s.getToken(ctx, m.QuoteMint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve base token: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:54:26 -08:00
|
|
|
cnt, _ = json.MarshalIndent(quoteMint, "", " ")
|
|
|
|
fmt.Println(string(cnt))
|
|
|
|
|
2020-11-06 13:52:19 -08:00
|
|
|
return &MarketMeta{
|
2020-11-06 14:18:33 -08:00
|
|
|
Address: marketAddr,
|
|
|
|
MarketV2: &m,
|
2020-11-06 14:02:52 -08:00
|
|
|
QuoteMint: quoteMint,
|
|
|
|
BaseMint: baseMint,
|
2020-11-06 13:52:19 -08:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2020-11-06 14:02:52 -08:00
|
|
|
|
|
|
|
func (s *SerumClient) getToken(ctx context.Context, mintPubKey solana.PublicKey) (*token.Mint, error) {
|
|
|
|
accInfo, err := s.GetAccountInfo(ctx, mintPubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve base mint: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:18:33 -08:00
|
|
|
fmt.Println(accInfo, mintPubKey)
|
|
|
|
|
2020-11-06 14:02:52 -08:00
|
|
|
accountData, err := accInfo.Value.DataToBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve account data byte: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m token.Mint
|
|
|
|
err = struc.Unpack(bytes.NewReader(accountData), &m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode market: %w", err)
|
|
|
|
}
|
|
|
|
return &m, nil
|
|
|
|
}
|