-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
159 lines (132 loc) · 3.64 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gosol
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/mitchellh/mapstructure"
)
// NewClient creates a new JSON-RPC client with the specified base URL.
func NewClient(baseURL string, opts *ClientOptions) *Client {
if opts == nil {
opts = DefaultClientOptions
}
return &Client{BaseURL: baseURL, HttpClient: opts.HttpClient}
}
// makeRequest is a private method to send a JSON-RPC request.
func (c *Client) makeRequest(method string, params interface{}) (map[string]interface{}, error) {
requestPayload := RPCRequest{
JSONRPC: JSONRPCVersion,
ID: 1,
Method: method,
Params: params,
}
requestPayloadBytes, err := json.Marshal(requestPayload)
if err != nil {
return nil, err
}
resp, err := c.HttpClient.Post(c.BaseURL, "application/json", bytes.NewBuffer(requestPayloadBytes))
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println("Error closing response body:", err)
}
}(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("HTTP request failed with status code: %d", resp.StatusCode)
}
var response map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response, nil
}
// GetAccountBalance retrieves the account balance for a given public key.
func (c *Client) GetAccountBalance(publicKey string) (*RPCResponse[Result[int64]], error) {
params := []interface{}{
publicKey,
map[string]string{
"encoding": "base58",
},
}
response, err := c.makeRequest("getBalance", params)
if err != nil {
return nil, err
}
var rpcResponse RPCResponse[Result[int64]]
err = mapstructure.Decode(response, &rpcResponse)
if err != nil {
return nil, err
}
return &rpcResponse, nil
}
func (c *Client) GetAccountInfo(publicKey string) (*RPCResponse[Result[AccountInfo]], error) {
params := []interface{}{
publicKey,
map[string]string{
"encoding": "base58",
},
}
response, err := c.makeRequest("getAccountInfo", params)
if err != nil {
return nil, err
}
var rpcResponse RPCResponse[Result[AccountInfo]]
err = mapstructure.Decode(response, &rpcResponse)
if err != nil {
return nil, err
}
return &rpcResponse, nil
}
func (c *Client) GetBlockHeight() (*RPCResponse[int64], error) {
response, err := c.makeRequest("getBlockHeight", nil)
if err != nil {
return nil, err
}
var rpcResponse RPCResponse[int64]
err = mapstructure.Decode(response, &rpcResponse)
if err != nil {
return nil, err
}
return &rpcResponse, nil
}
func (c *Client) GetBlock(slotNumber int64, getBlockProps *GetBlockProps) (*RPCResponse[Result[BlockResult]], error) {
if getBlockProps == nil {
getBlockProps = &GetBlockProps{
Encoding: "json",
MaxSupportedTransactionVersion: 0,
TransactionDetails: "full",
Rewards: false,
}
}
params := []interface{}{
slotNumber,
getBlockProps,
}
response, err := c.makeRequest("getBlock", params)
if err != nil {
return nil, err
}
var rpcResponse RPCResponse[Result[BlockResult]]
err = mapstructure.Decode(response, &rpcResponse)
if err != nil {
return nil, err
}
return &rpcResponse, nil
}
// GetBlockProduction retrieves the block production information from the Solana RPC endpoint.
func (c *Client) GetBlockProduction() (*RPCResponse[Result[GetBlockProdResponse]], error) {
response, err := c.makeRequest("getBlockProduction", nil)
if err != nil {
return nil, err
}
var rpcResponse RPCResponse[Result[GetBlockProdResponse]]
err = mapstructure.Decode(response, &rpcResponse)
if err != nil {
return nil, err
}
return &rpcResponse, nil
}