forked from blocto/solana-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
223 lines (204 loc) · 6.52 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package client
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"github.com/olegfomenko/solana-go-sdk/client/rpc"
"github.com/olegfomenko/solana-go-sdk/common"
"github.com/olegfomenko/solana-go-sdk/types"
)
type Client struct {
rpc.RpcClient
}
func NewClient(endpoint string) *Client {
return &Client{rpc.NewRpcClient(endpoint)}
}
// GetBalance fetch users lamports(SOL) balance
func (c *Client) GetBalance(ctx context.Context, base58Addr string) (uint64, error) {
res, err := c.RpcClient.GetBalance(ctx, base58Addr)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, err
}
return res.Result.Value, nil
}
// GetBalance fetch users lamports(SOL) balance with specific commitment
func (c *Client) GetBalanceWithCfg(ctx context.Context, base58Addr string, cfg rpc.GetBalanceConfig) (uint64, error) {
res, err := c.RpcClient.GetBalanceWithCfg(ctx, base58Addr, cfg)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, err
}
return res.Result.Value, nil
}
// GetTokenAccountBalance returns the token balance of an SPL Token account
func (c *Client) GetTokenAccountBalance(ctx context.Context, base58Addr string) (uint64, uint8, error) {
res, err := c.RpcClient.GetTokenAccountBalance(ctx, base58Addr)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, 0, err
}
balance, err := strconv.ParseUint(res.Result.Value.Amount, 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("failed to cast token amount, err: %v", err)
}
return balance, res.Result.Value.Decimals, nil
}
// GetTokenAccountBalance returns the token balance of an SPL Token account
func (c *Client) GetTokenAccountBalanceWithCfg(ctx context.Context, base58Addr string, cfg rpc.GetTokenAccountBalanceConfig) (uint64, uint8, error) {
res, err := c.RpcClient.GetTokenAccountBalanceWithCfg(ctx, base58Addr, cfg)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, 0, err
}
balance, err := strconv.ParseUint(res.Result.Value.Amount, 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("failed to cast token amount, err: %v", err)
}
return balance, res.Result.Value.Decimals, nil
}
type AccountInfo struct {
Lamports uint64
Owner string
Excutable bool
RentEpoch uint64
Data []byte
}
// GetAccountInfo return account's info
func (c *Client) GetAccountInfo(ctx context.Context, base58Addr string) (AccountInfo, error) {
res, err := c.RpcClient.GetAccountInfoWithCfg(ctx, base58Addr, rpc.GetAccountInfoConfig{
Encoding: rpc.GetAccountInfoConfigEncodingBase64,
})
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return AccountInfo{}, err
}
if res.Result.Value == (rpc.GetAccountInfoResultValue{}) {
return AccountInfo{}, nil
}
data, ok := res.Result.Value.Data.([]interface{})
if !ok {
return AccountInfo{}, fmt.Errorf("failed to cast raw response to []interface{}")
}
if data[1] != string(rpc.GetAccountInfoConfigEncodingBase64) {
return AccountInfo{}, fmt.Errorf("encoding mistmatch")
}
rawData, err := base64.StdEncoding.DecodeString(data[0].(string))
if err != nil {
return AccountInfo{}, fmt.Errorf("failed to base64 decode data")
}
return AccountInfo{
Lamports: res.Result.Value.Lamports,
Owner: res.Result.Value.Owner,
Excutable: res.Result.Value.Excutable,
RentEpoch: res.Result.Value.RentEpoch,
Data: rawData,
}, nil
}
// GetRecentBlockhash return recent blockhash information
func (c *Client) GetRecentBlockhash(ctx context.Context) (rpc.GetRecentBlockHashResultValue, error) {
res, err := c.RpcClient.GetRecentBlockhash(ctx)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return rpc.GetRecentBlockHashResultValue{}, err
}
return res.Result.Value, nil
}
// SendRawTransaction will send your raw tx
func (c *Client) SendRawTransaction(ctx context.Context, tx []byte) (string, error) {
res, err := c.RpcClient.SendTransactionWithConfig(
ctx,
base64.StdEncoding.EncodeToString(tx),
rpc.SendTransactionConfig{
Encoding: rpc.SendTransactionConfigEncodingBase64,
},
)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return "", err
}
return res.Result, nil
}
type SendTransactionParam struct {
Instructions []types.Instruction
Signers []types.Account
FeePayer common.PublicKey
}
// SendTransaction is a quick way to send tx
func (c *Client) SendTransaction(ctx context.Context, param SendTransactionParam) (string, error) {
recentBlockhashRes, err := c.GetRecentBlockhash(ctx)
if err != nil {
return "", fmt.Errorf("failed to get recent blockhash, err: %v", err)
}
rawTx, err := types.CreateRawTransaction(types.CreateRawTransactionParam{
Instructions: param.Instructions,
Signers: param.Signers,
FeePayer: param.FeePayer,
RecentBlockHash: recentBlockhashRes.Blockhash,
})
if err != nil {
return "", fmt.Errorf("failed to build tx, err: %v", err)
}
res, err := c.RpcClient.SendTransactionWithConfig(
ctx,
base64.StdEncoding.EncodeToString(rawTx),
rpc.SendTransactionConfig{Encoding: rpc.SendTransactionConfigEncodingBase64},
)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return "", err
}
return res.Result, nil
}
// SendTransaction2 send transaction struct directly
func (c *Client) SendTransaction2(ctx context.Context, tx types.Transaction) (string, error) {
rawTx, err := tx.Serialize()
if err != nil {
return "", fmt.Errorf("failed to serialize tx, err: %v", err)
}
res, err := c.RpcClient.SendTransactionWithConfig(
ctx,
base64.StdEncoding.EncodeToString(rawTx),
rpc.SendTransactionConfig{
Encoding: rpc.SendTransactionConfigEncodingBase64,
},
)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return "", err
}
return res.Result, nil
}
// GetSlot get current slot (finalized)
func (c *Client) GetSlot(ctx context.Context) (uint64, error) {
res, err := c.RpcClient.GetSlot(ctx)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, err
}
return res.Result, nil
}
// GetSlotWithCfg get slot by commitment
func (c *Client) GetSlotWithCfg(ctx context.Context, cfg rpc.GetSlotConfig) (uint64, error) {
res, err := c.RpcClient.GetSlotWithCfg(ctx, cfg)
err = checkRpcResult(res.GeneralResponse, err)
if err != nil {
return 0, err
}
return res.Result, nil
}
func checkRpcResult(res rpc.GeneralResponse, err error) error {
if err != nil {
return err
}
if res.Error != nil {
errRes, err := json.Marshal(res.Error)
if err != nil {
return fmt.Errorf("rpc response error: %v", res.Error)
}
return fmt.Errorf("rpc response error: %v", string(errRes))
}
return nil
}