2020-11-06 07:40:28 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2020-11-09 09:59:24 -08:00
|
|
|
"bytes"
|
2020-11-06 08:20:48 -08:00
|
|
|
"context"
|
2020-11-09 09:59:24 -08:00
|
|
|
"errors"
|
2020-11-06 08:20:48 -08:00
|
|
|
"net/http"
|
2020-11-06 07:40:28 -08:00
|
|
|
|
2020-11-06 08:38:43 -08:00
|
|
|
//"github.com/dfuse-io/solana-go"
|
2020-11-06 08:47:26 -08:00
|
|
|
"github.com/dfuse-io/solana-go"
|
2020-11-09 09:59:24 -08:00
|
|
|
"github.com/lunixbochs/struc"
|
2020-11-06 07:40:28 -08:00
|
|
|
"github.com/ybbus/jsonrpc"
|
|
|
|
)
|
|
|
|
|
2020-11-09 09:59:24 -08:00
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
|
2020-11-06 07:40:28 -08:00
|
|
|
type Client struct {
|
2020-11-06 08:20:48 -08:00
|
|
|
rpcURL string
|
|
|
|
rpcClient jsonrpc.RPCClient
|
|
|
|
headers http.Header
|
|
|
|
|
|
|
|
Debug bool
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:20:48 -08:00
|
|
|
func NewClient(rpcURL string) *Client {
|
|
|
|
rpcClient := jsonrpc.NewClient(rpcURL)
|
2020-11-06 07:40:28 -08:00
|
|
|
return &Client{
|
2020-11-06 08:20:48 -08:00
|
|
|
rpcURL: rpcURL,
|
|
|
|
rpcClient: rpcClient,
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:20:48 -08:00
|
|
|
func (c *Client) SetHeader(k, v string) {
|
|
|
|
if c.headers == nil {
|
|
|
|
c.headers = http.Header{}
|
|
|
|
}
|
|
|
|
c.headers.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:38:43 -08:00
|
|
|
func (c *Client) GetBalance(ctx context.Context, publicKey string, commitment CommitmentType) (out *GetBalanceResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
params := []interface{}{publicKey}
|
|
|
|
if commitment != "" {
|
|
|
|
params = append(params, string(commitment))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.rpcClient.CallFor(&out, "getBalance", params...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:38:43 -08:00
|
|
|
func (c *Client) GetRecentBlockhash(ctx context.Context, commitment CommitmentType) (out *GetRecentBlockhashResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
var params []interface{}
|
|
|
|
if commitment != "" {
|
|
|
|
params = append(params, string(commitment))
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:20:48 -08:00
|
|
|
err = c.rpcClient.CallFor(&out, "getRecentBlockhash", params...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-06 08:38:43 -08:00
|
|
|
func (c *Client) GetSlot(ctx context.Context, commitment CommitmentType) (out GetSlotResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
var params []interface{}
|
|
|
|
if commitment != "" {
|
|
|
|
params = append(params, string(commitment))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.rpcClient.CallFor(&out, "getSlot", params...)
|
|
|
|
return
|
|
|
|
}
|
2020-11-06 07:40:28 -08:00
|
|
|
|
2020-11-06 08:38:43 -08:00
|
|
|
func (c *Client) GetConfirmedBlock(ctx context.Context, slot uint64, encoding string) (out *GetConfirmedBlockResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
if encoding == "" {
|
|
|
|
encoding = "json"
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
2020-11-06 08:20:48 -08:00
|
|
|
params := []interface{}{slot, encoding}
|
|
|
|
|
|
|
|
err = c.rpcClient.CallFor(&out, "getConfirmedBlock", params...)
|
|
|
|
return
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:47:26 -08:00
|
|
|
func (c *Client) GetAccountInfo(ctx context.Context, account solana.PublicKey) (out *GetAccountInfoResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
obj := map[string]interface{}{
|
|
|
|
"encoding": "base64",
|
|
|
|
}
|
2020-11-06 08:47:26 -08:00
|
|
|
params := []interface{}{account, obj}
|
2020-11-06 08:20:48 -08:00
|
|
|
|
|
|
|
err = c.rpcClient.CallFor(&out, "getAccountInfo", params...)
|
2020-11-09 09:59:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Value == nil {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetAccountDataIn(ctx context.Context, account solana.PublicKey, inVar interface{}) (err error) {
|
|
|
|
resp, err := c.GetAccountInfo(ctx, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return struc.Unpack(bytes.NewReader(resp.Value.Data), inVar)
|
2020-11-06 08:20:48 -08:00
|
|
|
}
|
2020-11-06 07:40:28 -08:00
|
|
|
|
2020-11-06 13:31:18 -08:00
|
|
|
func (c *Client) GetProgramAccounts(ctx context.Context, publicKey solana.PublicKey, opts *GetProgramAccountsOpts) (out GetProgramAccountsResult, err error) {
|
2020-11-09 08:07:13 -08:00
|
|
|
obj := map[string]interface{}{
|
|
|
|
"encoding": "base64",
|
|
|
|
}
|
2020-11-06 08:20:48 -08:00
|
|
|
if opts != nil {
|
2020-11-09 08:07:13 -08:00
|
|
|
if opts.Commitment != "" {
|
|
|
|
obj["commitment"] = string(opts.Commitment)
|
|
|
|
}
|
|
|
|
if len(opts.Filters) != 0 {
|
|
|
|
obj["filters"] = opts.Filters
|
|
|
|
}
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:07:13 -08:00
|
|
|
params := []interface{}{publicKey, obj}
|
|
|
|
|
2020-11-06 08:20:48 -08:00
|
|
|
err = c.rpcClient.CallFor(&out, "getProgramAccounts", params...)
|
|
|
|
return
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|