solana-go/serum/rpc.go

50 lines
1.1 KiB
Go
Raw Normal View History

2020-11-06 13:52:19 -08:00
package serum
import (
"bytes"
"context"
"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"
)
2020-11-06 17:03:49 -08:00
func FetchMarket(ctx context.Context, rpcCli *rpc.Client, marketAddr solana.PublicKey) (*MarketMeta, error) {
accInfo, err := rpcCli.GetAccountInfo(ctx, marketAddr)
2020-11-06 13:52:19 -08:00
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 17:03:49 -08:00
baseMint, err := token.GetMint(ctx, rpcCli, m.BaseMint)
2020-11-06 14:02:52 -08:00
if err != nil {
return nil, fmt.Errorf("unable to retrieve base token: %w", err)
}
2020-11-06 17:03:49 -08:00
quoteMint, err := token.GetMint(ctx, rpcCli, m.QuoteMint)
2020-11-06 14:02:52 -08:00
if err != nil {
return nil, fmt.Errorf("unable to retrieve base token: %w", err)
}
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
}