-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathclient_call.go
420 lines (376 loc) · 13.9 KB
/
client_call.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package client
import (
"context"
"errors"
"strings"
"github.com/coming-chat/go-sui/v2/lib"
"github.com/coming-chat/go-sui/v2/sui_types"
"github.com/coming-chat/go-sui/v2/types"
)
// NOTE: This copys the query limit from our Rust JSON RPC backend, this needs to be kept in sync!
const QUERY_MAX_RESULT_LIMIT = 1000
type suiAddress = sui_types.SuiAddress
type suiObjectID = sui_types.ObjectID
type suiDigest = sui_types.TransactionDigest
type suiBase64Data = lib.Base64Data
// MARK - Getter Function
// GetBalance to use default sui coin(0x2::sui::SUI) when coinType is empty
func (c *Client) GetBalance(ctx context.Context, owner suiAddress, coinType string) (*types.Balance, error) {
resp := types.Balance{}
if coinType == "" {
return &resp, c.CallContext(ctx, &resp, getBalance, owner)
} else {
return &resp, c.CallContext(ctx, &resp, getBalance, owner, coinType)
}
}
func (c *Client) GetAllBalances(ctx context.Context, owner suiAddress) ([]types.Balance, error) {
var resp []types.Balance
return resp, c.CallContext(ctx, &resp, getAllBalances, owner)
}
// GetSuiCoinsOwnedByAddress This function will retrieve a maximum of 200 coins.
func (c *Client) GetSuiCoinsOwnedByAddress(ctx context.Context, address suiAddress) (types.Coins, error) {
coinType := types.SuiCoinType
page, err := c.GetCoins(ctx, address, &coinType, nil, 200)
if err != nil {
return nil, err
}
return page.Data, nil
}
// GetCoins to use default sui coin(0x2::sui::SUI) when coinType is nil
// start with the first object when cursor is nil
func (c *Client) GetCoins(
ctx context.Context,
owner suiAddress,
coinType *string,
cursor *suiObjectID,
limit uint,
) (*types.CoinPage, error) {
var resp types.CoinPage
return &resp, c.CallContext(ctx, &resp, getCoins, owner, coinType, cursor, limit)
}
// GetAllCoins
// start with the first object when cursor is nil
func (c *Client) GetAllCoins(
ctx context.Context,
owner suiAddress,
cursor *suiObjectID,
limit uint,
) (*types.CoinPage, error) {
var resp types.CoinPage
return &resp, c.CallContext(ctx, &resp, getAllCoins, owner, cursor, limit)
}
func (c *Client) GetCoinMetadata(ctx context.Context, coinType string) (*types.SuiCoinMetadata, error) {
var resp types.SuiCoinMetadata
return &resp, c.CallContext(ctx, &resp, getCoinMetadata, coinType)
}
func (c *Client) GetObject(
ctx context.Context,
objID suiObjectID,
options *types.SuiObjectDataOptions,
) (*types.SuiObjectResponse, error) {
var resp types.SuiObjectResponse
return &resp, c.CallContext(ctx, &resp, getObject, objID, options)
}
func (c *Client) MultiGetObjects(
ctx context.Context,
objIDs []suiObjectID,
options *types.SuiObjectDataOptions,
) ([]types.SuiObjectResponse, error) {
var resp []types.SuiObjectResponse
return resp, c.CallContext(ctx, &resp, multiGetObjects, objIDs, options)
}
// address : <SuiAddress> - the owner's Sui address
// query : <ObjectResponseQuery> - the objects query criteria.
// cursor : <CheckpointedObjectID> - An optional paging cursor. If provided, the query will start from the next item after the specified cursor. Default to start from the first item if not specified.
// limit : <uint> - Max number of items returned per page, default to [QUERY_MAX_RESULT_LIMIT_OBJECTS] if is 0
func (c *Client) GetOwnedObjects(
ctx context.Context,
address suiAddress,
query *types.SuiObjectResponseQuery,
cursor *types.CheckpointedObjectId,
limit *uint,
) (*types.ObjectsPage, error) {
var resp types.ObjectsPage
return &resp, c.CallContext(ctx, &resp, getOwnedObjects, address, query, cursor, limit)
}
func (c *Client) GetTotalSupply(ctx context.Context, coinType string) (*types.Supply, error) {
var resp types.Supply
return &resp, c.CallContext(ctx, &resp, getTotalSupply, coinType)
}
func (c *Client) GetTotalTransactionBlocks(ctx context.Context) (string, error) {
var resp string
return resp, c.CallContext(ctx, &resp, getTotalTransactionBlocks)
}
func (c *Client) GetLatestCheckpointSequenceNumber(ctx context.Context) (string, error) {
var resp string
return resp, c.CallContext(ctx, &resp, getLatestCheckpointSequenceNumber)
}
// BatchGetObjectsOwnedByAddress @param filterType You can specify filtering out the specified resources, this will fetch all resources if it is not empty ""
func (c *Client) BatchGetObjectsOwnedByAddress(
ctx context.Context,
address suiAddress,
options types.SuiObjectDataOptions,
filterType string,
) ([]types.SuiObjectResponse, error) {
filterType = strings.TrimSpace(filterType)
return c.BatchGetFilteredObjectsOwnedByAddress(
ctx, address, options, func(sod *types.SuiObjectData) bool {
return filterType == "" || filterType == *sod.Type
},
)
}
func (c *Client) BatchGetFilteredObjectsOwnedByAddress(
ctx context.Context,
address suiAddress,
options types.SuiObjectDataOptions,
filter func(*types.SuiObjectData) bool,
) ([]types.SuiObjectResponse, error) {
query := types.SuiObjectResponseQuery{
Options: &types.SuiObjectDataOptions{
ShowType: true,
},
}
filteringObjs, err := c.GetOwnedObjects(ctx, address, &query, nil, nil)
if err != nil {
return nil, err
}
objIds := make([]suiObjectID, 0)
for _, obj := range filteringObjs.Data {
if obj.Data == nil {
continue // error obj
}
if filter != nil && filter(obj.Data) == false {
continue // ignore objects if non-specified type
}
objIds = append(objIds, obj.Data.ObjectId)
}
return c.MultiGetObjects(ctx, objIds, &options)
}
func (c *Client) GetTransactionBlock(
ctx context.Context,
digest suiDigest,
options types.SuiTransactionBlockResponseOptions,
) (*types.SuiTransactionBlockResponse, error) {
resp := types.SuiTransactionBlockResponse{}
return &resp, c.CallContext(ctx, &resp, getTransactionBlock, digest, options)
}
func (c *Client) GetReferenceGasPrice(ctx context.Context) (*types.SafeSuiBigInt[uint64], error) {
var resp types.SafeSuiBigInt[uint64]
return &resp, c.CallContext(ctx, &resp, getReferenceGasPrice)
}
func (c *Client) GetEvents(ctx context.Context, digest suiDigest) ([]types.SuiEvent, error) {
var resp []types.SuiEvent
return resp, c.CallContext(ctx, &resp, getEvents, digest)
}
func (c *Client) TryGetPastObject(
ctx context.Context,
objectId suiObjectID,
version uint64,
options *types.SuiObjectDataOptions,
) (*types.SuiPastObjectResponse, error) {
var resp types.SuiPastObjectResponse
return &resp, c.CallContext(ctx, &resp, tryGetPastObject, objectId, version, options)
}
func (c *Client) DevInspectTransactionBlock(
ctx context.Context,
senderAddress suiAddress,
txByte suiBase64Data,
gasPrice *types.SafeSuiBigInt[uint64],
epoch *uint64,
) (*types.DevInspectResults, error) {
var resp types.DevInspectResults
return &resp, c.CallContext(ctx, &resp, devInspectTransactionBlock, senderAddress, txByte, gasPrice, epoch)
}
func (c *Client) DryRunTransaction(
ctx context.Context,
txBytes suiBase64Data,
) (*types.DryRunTransactionBlockResponse, error) {
var resp types.DryRunTransactionBlockResponse
return &resp, c.CallContext(ctx, &resp, dryRunTransactionBlock, txBytes)
}
func (c *Client) ExecuteTransactionBlock(
ctx context.Context, txBytes suiBase64Data, signatures []any,
options *types.SuiTransactionBlockResponseOptions, requestType types.ExecuteTransactionRequestType,
) (*types.SuiTransactionBlockResponse, error) {
resp := types.SuiTransactionBlockResponse{}
return &resp, c.CallContext(ctx, &resp, executeTransactionBlock, txBytes, signatures, options, requestType)
}
// TransferObject Create an unsigned transaction to transfer an object from one address to another. The object's type must allow public transfers
func (c *Client) TransferObject(
ctx context.Context,
signer, recipient suiAddress,
objID suiObjectID,
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, transferObject, signer, objID, gas, gasBudget, recipient)
}
// TransferSui Create an unsigned transaction to send SUI coin object to a Sui address. The SUI object is also used as the gas object.
func (c *Client) TransferSui(
ctx context.Context, signer, recipient suiAddress, suiObjID suiObjectID, amount,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, transferSui, signer, suiObjID, gasBudget, recipient, amount)
}
// PayAllSui Create an unsigned transaction to send all SUI coins to one recipient.
func (c *Client) PayAllSui(
ctx context.Context,
signer, recipient suiAddress,
inputCoins []suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, payAllSui, signer, inputCoins, recipient, gasBudget)
}
func (c *Client) Pay(
ctx context.Context,
signer suiAddress,
inputCoins []suiObjectID,
recipients []suiAddress,
amount []types.SafeSuiBigInt[uint64],
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, pay, signer, inputCoins, recipients, amount, gas, gasBudget)
}
func (c *Client) PaySui(
ctx context.Context,
signer suiAddress,
inputCoins []suiObjectID,
recipients []suiAddress,
amount []types.SafeSuiBigInt[uint64],
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, paySui, signer, inputCoins, recipients, amount, gasBudget)
}
// SplitCoin Create an unsigned transaction to split a coin object into multiple coins.
func (c *Client) SplitCoin(
ctx context.Context,
signer suiAddress,
Coin suiObjectID,
splitAmounts []types.SafeSuiBigInt[uint64],
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, splitCoin, signer, Coin, splitAmounts, gas, gasBudget)
}
// SplitCoinEqual Create an unsigned transaction to split a coin object into multiple equal-size coins.
func (c *Client) SplitCoinEqual(
ctx context.Context,
signer suiAddress,
Coin suiObjectID,
splitCount types.SafeSuiBigInt[uint64],
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, splitCoinEqual, signer, Coin, splitCount, gas, gasBudget)
}
// MergeCoins Create an unsigned transaction to merge multiple coins into one coin.
func (c *Client) MergeCoins(
ctx context.Context,
signer suiAddress,
primaryCoin, coinToMerge suiObjectID,
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, mergeCoins, signer, primaryCoin, coinToMerge, gas, gasBudget)
}
func (c *Client) Publish(
ctx context.Context,
sender suiAddress,
compiledModules []*suiBase64Data,
dependencies []suiObjectID,
gas suiObjectID,
gasBudget uint,
) (*types.TransactionBytes, error) {
var resp types.TransactionBytes
return &resp, c.CallContext(ctx, &resp, publish, sender, compiledModules, dependencies, gas, gasBudget)
}
// MoveCall Create an unsigned transaction to execute a Move call on the network, by calling the specified function in the module of a given package.
// TODO: not support param `typeArguments` yet.
// So now only methods with `typeArguments` are supported
// TODO: execution_mode : <SuiTransactionBlockBuilderMode>
func (c *Client) MoveCall(
ctx context.Context,
signer suiAddress,
packageId suiObjectID,
module, function string,
typeArgs []string,
arguments []any,
gas *suiObjectID,
gasBudget types.SafeSuiBigInt[uint64],
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(
ctx,
&resp,
moveCall,
signer,
packageId,
module,
function,
typeArgs,
arguments,
gas,
gasBudget,
)
}
// TODO: execution_mode : <SuiTransactionBlockBuilderMode>
func (c *Client) BatchTransaction(
ctx context.Context,
signer suiAddress,
txnParams []map[string]interface{},
gas *suiObjectID,
gasBudget uint64,
) (*types.TransactionBytes, error) {
resp := types.TransactionBytes{}
return &resp, c.CallContext(ctx, &resp, batchTransaction, signer, txnParams, gas, gasBudget)
}
func (c *Client) QueryTransactionBlocks(
ctx context.Context, query types.SuiTransactionBlockResponseQuery,
cursor *suiDigest, limit *uint, descendingOrder bool,
) (*types.TransactionBlocksPage, error) {
resp := types.TransactionBlocksPage{}
return &resp, c.CallContext(ctx, &resp, queryTransactionBlocks, query, cursor, limit, descendingOrder)
}
func (c *Client) QueryEvents(
ctx context.Context, query types.EventFilter, cursor *types.EventId, limit *uint,
descendingOrder bool,
) (*types.EventPage, error) {
var resp types.EventPage
return &resp, c.CallContext(ctx, &resp, queryEvents, query, cursor, limit, descendingOrder)
}
func (c *Client) ResolveNameServiceAddress(ctx context.Context, suiName string) (*suiAddress, error) {
var resp suiAddress
err := c.CallContext(ctx, &resp, resolveNameServiceAddress, suiName)
if err != nil && err.Error() == "nil address" {
return nil, errors.New("sui name not found")
}
return &resp, nil
}
func (c *Client) ResolveNameServiceNames(ctx context.Context,
owner suiAddress, cursor *suiObjectID, limit *uint) (*types.SuiNamePage, error) {
var resp types.SuiNamePage
return &resp, c.CallContext(ctx, &resp, resolveNameServiceNames, owner, cursor, limit)
}
func (c *Client) GetDynamicFields(
ctx context.Context, parentObjectId suiObjectID, cursor *suiObjectID,
limit *uint,
) (*types.DynamicFieldPage, error) {
var resp types.DynamicFieldPage
return &resp, c.CallContext(ctx, &resp, getDynamicFields, parentObjectId, cursor, limit)
}
func (c *Client) GetDynamicFieldObject(
ctx context.Context, parentObjectId suiObjectID,
name sui_types.DynamicFieldName,
) (*types.SuiObjectResponse, error) {
var resp types.SuiObjectResponse
return &resp, c.CallContext(ctx, &resp, getDynamicFieldObject, parentObjectId, name)
}