2020-11-06 07:40:28 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2020-11-06 08:20:48 -08:00
|
|
|
"context"
|
|
|
|
"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-06 07:40:28 -08:00
|
|
|
"github.com/ybbus/jsonrpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
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...)
|
|
|
|
return
|
|
|
|
}
|
2020-11-06 07:40:28 -08:00
|
|
|
|
2020-11-06 08:47:26 -08:00
|
|
|
func (c *Client) GetProgramAccounts(ctx context.Context, publicKey solana.PublicKey, opts *GetProgramAccountsOpts) (out *GetProgramAccountsResult, err error) {
|
2020-11-06 08:20:48 -08:00
|
|
|
params := []interface{}{publicKey}
|
|
|
|
if opts != nil {
|
|
|
|
params = append(params, opts)
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 08:20:48 -08:00
|
|
|
err = c.rpcClient.CallFor(&out, "getProgramAccounts", params...)
|
|
|
|
return
|
2020-11-06 07:40:28 -08:00
|
|
|
}
|