/
read_transaction_api.go
297 lines (282 loc) · 12.2 KB
/
read_transaction_api.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package sui
import (
"context"
"encoding/json"
"errors"
"github.com/shoshinsquare/sui-go-sdk/common/rpc_client"
"github.com/shoshinsquare/sui-go-sdk/models"
"github.com/tidwall/gjson"
)
type IReadTransactionFromSuiAPI interface {
GetRecentTransactions(ctx context.Context, req models.GetRecentTransactionRequest, opts ...interface{}) (models.GetRecentTransactionResponse, error)
GetTotalTransactionNumber(ctx context.Context, req models.GetTotalTransactionNumberRequest, opts ...interface{}) (models.GetTotalTransactionNumberResponse, error)
GetTransaction(ctx context.Context, req models.GetTransactionRequest, opts ...interface{}) (models.GetTransactionResponse, error)
GetTransactionsByInputObject(ctx context.Context, req models.GetTransactionsByInputObjectRequest, opts ...interface{}) (models.GetTransactionsByInputObjectResponse, error)
GetTransactionsByMoveFunction(ctx context.Context, req models.GetTransactionsByMoveFunctionRequest, opts ...interface{}) (models.GetTransactionsByMoveFunctionResponse, error)
GetTransactionsByMutatedObject(ctx context.Context, req models.GetTransactionsByMutatedObjectRequest, opts ...interface{}) (models.GetTransactionsByMutatedObjectResponse, error)
GetTransactionsFromAddress(ctx context.Context, req models.GetTransactionsFromAddressRequest, opts ...interface{}) (models.GetTransactionsFromAddressResponse, error)
GetTransactionsInRange(ctx context.Context, req models.GetTransactionsInRangeRequest, opts ...interface{}) (models.GetTransactionsInRangeResponse, error)
GetTransactionsToAddress(ctx context.Context, req models.GetTransactionsToAddressRequest, opts ...interface{}) (models.GetTransactionsToAddressResponse, error)
GetTransactionAuthSigners(ctx context.Context, req models.GetTransactionAuthSignersRequest, opts ...interface{}) (models.GetTransactionAuthSignersResponse, error)
}
type suiReadTransactionFromSuiImpl struct {
cli *rpc_client.RPCClient
}
// GetRecentTransactions implements method `sui_getRecentTransactions`.
// Returns an array of transactions' metadata
func (s *suiReadTransactionFromSuiImpl) GetRecentTransactions(ctx context.Context, req models.GetRecentTransactionRequest, opts ...interface{}) (models.GetRecentTransactionResponse, error) {
var rsp models.GetRecentTransactionResponse
reqList := make([]interface{}, 0)
reqList = append(reqList, req.Count)
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getRecentTransactions",
Params: reqList,
})
if err != nil {
return models.GetRecentTransactionResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetRecentTransactionResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTotalTransactionNumber implements method `sui_getTotalTransactionNumber`
// Returns a number of total transactions
func (s *suiReadTransactionFromSuiImpl) GetTotalTransactionNumber(ctx context.Context, req models.GetTotalTransactionNumberRequest, opts ...interface{}) (models.GetTotalTransactionNumberResponse, error) {
var rsp models.GetTotalTransactionNumberResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTotalTransactionNumber",
Params: []interface{}{},
})
if err != nil {
return models.GetTotalTransactionNumberResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTotalTransactionNumberResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
rsp.TotalNumberOfTransaction = gjson.ParseBytes(respBytes).Get("result").Uint()
return rsp, nil
}
// GetTransaction implements method `sui_getTransaction`
// Returns detail info of the transaction
func (s *suiReadTransactionFromSuiImpl) GetTransaction(ctx context.Context, req models.GetTransactionRequest, opts ...interface{}) (models.GetTransactionResponse, error) {
var rsp models.GetTransactionResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransaction",
Params: []interface{}{
req.Digest,
},
})
if err != nil {
return models.GetTransactionResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
err = json.Unmarshal([]byte(gjson.ParseBytes(respBytes).Get("result").Raw), &rsp)
if err != nil {
return models.GetTransactionResponse{}, err
}
return rsp, nil
}
// GetTransactionsByInputObject implements method `sui_getTransactionsByInputObject`.
// Returns an array of transactions' metadata
func (s *suiReadTransactionFromSuiImpl) GetTransactionsByInputObject(ctx context.Context, req models.GetTransactionsByInputObjectRequest, opts ...interface{}) (models.GetTransactionsByInputObjectResponse, error) {
var rsp models.GetTransactionsByInputObjectResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsByInputObject",
Params: []interface{}{
req.ObjectID,
},
})
if err != nil {
return models.GetTransactionsByInputObjectResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsByInputObjectResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTransactionsByMoveFunction implements method `sui_getTransactionsByInputObject`.
// Returns an array of transactions' metadata.
func (s *suiReadTransactionFromSuiImpl) GetTransactionsByMoveFunction(ctx context.Context, req models.GetTransactionsByMoveFunctionRequest, opts ...interface{}) (models.GetTransactionsByMoveFunctionResponse, error) {
var rsp models.GetTransactionsByMoveFunctionResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsByInputObject",
Params: []interface{}{
req.Package,
req.Module,
req.Function,
},
})
if err != nil {
return models.GetTransactionsByMoveFunctionResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsByMoveFunctionResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTransactionsByMutatedObject implements method `sui_getTransactionsByMutatedObject`.
// Returns an array of transactions' metadata.
func (s *suiReadTransactionFromSuiImpl) GetTransactionsByMutatedObject(ctx context.Context, req models.GetTransactionsByMutatedObjectRequest, opts ...interface{}) (models.GetTransactionsByMutatedObjectResponse, error) {
var rsp models.GetTransactionsByMutatedObjectResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsByMutatedObject",
Params: []interface{}{
req.ObjectID,
},
})
if err != nil {
return models.GetTransactionsByMutatedObjectResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsByMutatedObjectResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTransactionsFromAddress implements method `sui_getTransactionsFromAddress`.
// Returns an array of transactions' metadata.
func (s *suiReadTransactionFromSuiImpl) GetTransactionsFromAddress(ctx context.Context, req models.GetTransactionsFromAddressRequest, opts ...interface{}) (models.GetTransactionsFromAddressResponse, error) {
var rsp models.GetTransactionsFromAddressResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsFromAddress",
Params: []interface{}{
req.Addr,
},
})
if err != nil {
return models.GetTransactionsFromAddressResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsFromAddressResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTransactionsInRange implements method `sui_getTransactionsInRange`.
// Returns an array of transactions' metadata.
func (s *suiReadTransactionFromSuiImpl) GetTransactionsInRange(ctx context.Context, req models.GetTransactionsInRangeRequest, opts ...interface{}) (models.GetTransactionsInRangeResponse, error) {
var rsp models.GetTransactionsInRangeResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsInRange",
Params: []interface{}{
req.Start,
req.End,
},
})
if err != nil {
return models.GetTransactionsInRangeResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsInRangeResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
// GetTransactionsToAddress implements method `sui_getTransactionsToAddress`.
// Returns an array of transactions' metadata.
func (s *suiReadTransactionFromSuiImpl) GetTransactionsToAddress(ctx context.Context, req models.GetTransactionsToAddressRequest, opts ...interface{}) (models.GetTransactionsToAddressResponse, error) {
var rsp models.GetTransactionsToAddressResponse
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionsToAddress",
Params: []interface{}{
req.Addr,
},
})
if err != nil {
return models.GetTransactionsToAddressResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionsToAddressResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
results := gjson.ParseBytes(respBytes).Get("result").Array()
for i := range results {
if len(results[i].Array()) < 2 {
continue
}
rsp.Result = append(rsp.Result, models.GetTransactionMetaData{
GatewayTxSeqNumber: results[i].Array()[0].Uint(),
TransactionDigest: results[i].Array()[1].String(),
})
}
return rsp, nil
}
func (s *suiReadTransactionFromSuiImpl) GetTransactionAuthSigners(ctx context.Context, req models.GetTransactionAuthSignersRequest, opts ...interface{}) (models.GetTransactionAuthSignersResponse, error) {
respBytes, err := s.cli.Request(ctx, models.Operation{
Method: "sui_getTransactionAuthSigners",
Params: []interface{}{
req.Digest,
},
})
if err != nil {
return models.GetTransactionAuthSignersResponse{}, err
}
if gjson.ParseBytes(respBytes).Get("error").Exists() {
return models.GetTransactionAuthSignersResponse{}, errors.New(gjson.ParseBytes(respBytes).Get("error").String())
}
var rsp models.GetTransactionAuthSignersResponse
err = json.Unmarshal([]byte(gjson.ParseBytes(respBytes).Get("result").String()), &rsp)
if err != nil {
return models.GetTransactionAuthSignersResponse{}, err
}
return rsp, nil
}