@scallop-io/sui-kit 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,69 +1,69 @@
1
- import {
2
- normalizeSuiObjectId,
3
- normalizeSuiAddress,
4
- isValidSuiObjectId,
5
- isValidSuiAddress,
6
- } from '@mysten/sui/utils';
7
- import { Inputs, getPureBcsSchema } from '@mysten/sui/transactions';
8
- import { SerializedBcs, bcs, isSerializedBcs } from '@mysten/bcs';
1
+ import { bcs, isSerializedBcs, SerializedBcs } from "@mysten/bcs";
2
+ import type { SuiClientTypes } from "@mysten/sui/client";
9
3
  import type {
10
- TransactionArgument,
11
- Transaction,
12
- TransactionObjectArgument,
13
- } from '@mysten/sui/transactions';
14
- import type { SuiClientTypes } from '@mysten/sui/client';
4
+ Transaction,
5
+ TransactionArgument,
6
+ TransactionObjectArgument,
7
+ } from "@mysten/sui/transactions";
8
+ import { getPureBcsSchema, Inputs } from "@mysten/sui/transactions";
9
+ import {
10
+ isValidSuiAddress,
11
+ isValidSuiObjectId,
12
+ normalizeSuiAddress,
13
+ normalizeSuiObjectId,
14
+ } from "@mysten/sui/utils";
15
15
  import type {
16
- SuiObjectArg,
17
- SuiAddressArg,
18
- SuiTxArg,
19
- SuiVecTxArg,
20
- SuiInputTypes,
21
- SuiAmountsArg,
22
- } from '../../types/index.js';
16
+ SuiAddressArg,
17
+ SuiAmountsArg,
18
+ SuiInputTypes,
19
+ SuiObjectArg,
20
+ SuiTxArg,
21
+ SuiVecTxArg,
22
+ } from "../../types/index.js";
23
23
 
24
24
  // Object reference type
25
25
  interface SuiObjectRef {
26
- objectId: string;
27
- version: number | string;
28
- digest: string;
26
+ objectId: string;
27
+ version: number | string;
28
+ digest: string;
29
29
  }
30
30
 
31
31
  // Simple types that can be converted to OpenSignatureBody
32
32
  const SIMPLE_BCS_TYPES = [
33
- 'u8',
34
- 'u16',
35
- 'u32',
36
- 'u64',
37
- 'u128',
38
- 'u256',
39
- 'bool',
40
- 'address',
33
+ "u8",
34
+ "u16",
35
+ "u32",
36
+ "u64",
37
+ "u128",
38
+ "u256",
39
+ "bool",
40
+ "address",
41
41
  ] as const;
42
42
 
43
43
  type SimpleBcsType = (typeof SIMPLE_BCS_TYPES)[number];
44
44
 
45
45
  // Convert simple type string to OpenSignatureBody
46
46
  function toOpenSignatureBody(type: string): SuiClientTypes.OpenSignatureBody {
47
- if (!SIMPLE_BCS_TYPES.includes(type as SimpleBcsType)) {
48
- throw new Error(`Invalid SimpleBcsType: ${type}`);
49
- }
50
- return { $kind: type } as SuiClientTypes.OpenSignatureBody;
47
+ if (!SIMPLE_BCS_TYPES.includes(type as SimpleBcsType)) {
48
+ throw new Error(`Invalid SimpleBcsType: ${type}`);
49
+ }
50
+ return { $kind: type } as SuiClientTypes.OpenSignatureBody;
51
51
  }
52
52
 
53
53
  // TODO: unclear why we need this function and types
54
54
  export const getDefaultSuiInputType = (
55
- value: SuiTxArg
56
- ): 'u64' | 'bool' | 'object' | undefined => {
57
- if (typeof value === 'string' && isValidSuiObjectId(value)) {
58
- return 'object';
59
- }
60
- if (typeof value === 'number' || typeof value === 'bigint') {
61
- return 'u64';
62
- }
63
- if (typeof value === 'boolean') {
64
- return 'bool';
65
- }
66
- return undefined;
55
+ value: SuiTxArg,
56
+ ): "u64" | "bool" | "object" | undefined => {
57
+ if (typeof value === "string" && isValidSuiObjectId(value)) {
58
+ return "object";
59
+ }
60
+ if (typeof value === "number" || typeof value === "bigint") {
61
+ return "u64";
62
+ }
63
+ if (typeof value === "boolean") {
64
+ return "bool";
65
+ }
66
+ return undefined;
67
67
  };
68
68
 
69
69
  // =========== TYPE GUARD ============
@@ -74,11 +74,13 @@ export const getDefaultSuiInputType = (
74
74
  * @returns boolean.
75
75
  */
76
76
  function isAmountArg(arg: any): arg is bigint | number | string {
77
- return (
78
- typeof arg === 'number' ||
79
- typeof arg === 'bigint' ||
80
- (typeof arg === 'string' && !isValidSuiAddress(arg) && !isNaN(Number(arg)))
81
- );
77
+ return (
78
+ typeof arg === "number" ||
79
+ typeof arg === "bigint" ||
80
+ (typeof arg === "string" &&
81
+ !isValidSuiAddress(arg) &&
82
+ !Number.isNaN(Number(arg)))
83
+ );
82
84
  }
83
85
 
84
86
  /**
@@ -88,19 +90,19 @@ function isAmountArg(arg: any): arg is bigint | number | string {
88
90
  * @returns boolean.
89
91
  */
90
92
  function isMoveVecArg(
91
- arg: SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg
93
+ arg: SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg,
92
94
  ): arg is SuiVecTxArg {
93
- if (
94
- arg !== null &&
95
- typeof arg === 'object' &&
96
- 'vecType' in arg &&
97
- 'value' in arg
98
- ) {
99
- return true;
100
- } else if (Array.isArray(arg)) {
101
- return true;
102
- }
103
- return false;
95
+ if (
96
+ arg !== null &&
97
+ typeof arg === "object" &&
98
+ "vecType" in arg &&
99
+ "value" in arg
100
+ ) {
101
+ return true;
102
+ } else if (Array.isArray(arg)) {
103
+ return true;
104
+ }
105
+ return false;
104
106
  }
105
107
 
106
108
  /**
@@ -109,12 +111,12 @@ function isMoveVecArg(
109
111
  * @returns boolean
110
112
  */
111
113
  function isObjectRef(arg: SuiObjectArg): arg is SuiObjectRef {
112
- return (
113
- typeof arg === 'object' &&
114
- 'digest' in arg &&
115
- 'version' in arg &&
116
- 'objectId' in arg
117
- );
114
+ return (
115
+ typeof arg === "object" &&
116
+ "digest" in arg &&
117
+ "version" in arg &&
118
+ "objectId" in arg
119
+ );
118
120
  }
119
121
 
120
122
  /**
@@ -123,14 +125,14 @@ function isObjectRef(arg: SuiObjectArg): arg is SuiObjectRef {
123
125
  * @returns
124
126
  */
125
127
  function isSharedObjectRef(
126
- arg: SuiObjectArg
128
+ arg: SuiObjectArg,
127
129
  ): arg is Parameters<typeof Inputs.SharedObjectRef>[0] {
128
- return (
129
- typeof arg === 'object' &&
130
- 'objectId' in arg &&
131
- 'initialSharedVersion' in arg &&
132
- 'mutable' in arg
133
- );
130
+ return (
131
+ typeof arg === "object" &&
132
+ "objectId" in arg &&
133
+ "initialSharedVersion" in arg &&
134
+ "mutable" in arg
135
+ );
134
136
  }
135
137
  // ===================================
136
138
 
@@ -148,45 +150,45 @@ function isSharedObjectRef(
148
150
  * @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'signer' | 'object' | string
149
151
  */
150
152
  export function makeVecParam(
151
- txBlock: Transaction,
152
- args: SuiTxArg[],
153
- type?: SuiInputTypes
153
+ txBlock: Transaction,
154
+ args: SuiTxArg[],
155
+ type?: SuiInputTypes,
154
156
  ): TransactionArgument {
155
- if (args.length === 0)
156
- throw new Error('Transaction builder error: Empty array is not allowed');
157
- // Using first element value as default type
158
- // TODO: unclear why we need this function and types
159
- const defaultSuiType = getDefaultSuiInputType(args[0]);
160
- const VECTOR_REGEX = /^vector<(.+)>$/;
161
- const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;
157
+ if (args.length === 0)
158
+ throw new Error("Transaction builder error: Empty array is not allowed");
159
+ // Using first element value as default type
160
+ // TODO: unclear why we need this function and types
161
+ const defaultSuiType = getDefaultSuiInputType(args[0]);
162
+ const VECTOR_REGEX = /^vector<(.+)>$/;
163
+ const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;
162
164
 
163
- type = type || defaultSuiType;
165
+ type = type || defaultSuiType;
164
166
 
165
- if (type === 'object') {
166
- const elements = args.map((arg) =>
167
- typeof arg === 'string' && isValidSuiObjectId(arg)
168
- ? txBlock.object(normalizeSuiObjectId(arg))
169
- : convertObjArg(txBlock, arg as SuiObjectArg)
170
- );
171
- return txBlock.makeMoveVec({ elements });
172
- } else if (
173
- typeof type === 'string' &&
174
- !VECTOR_REGEX.test(type) &&
175
- !STRUCT_REGEX.test(type)
176
- ) {
177
- // Convert simple type to OpenSignatureBody for BCS schema
178
- const signatureBody = toOpenSignatureBody(type as SimpleBcsType);
179
- const bcsSchema = getPureBcsSchema(signatureBody);
180
- if (!bcsSchema) {
181
- throw new Error(`Unknown type: ${type}`);
182
- }
183
- return txBlock.pure(bcs.vector(bcsSchema).serialize(args));
184
- } else {
185
- const elements = args.map((arg) =>
186
- convertObjArg(txBlock, arg as SuiObjectArg)
187
- );
188
- return txBlock.makeMoveVec({ elements, type: type as string });
189
- }
167
+ if (type === "object") {
168
+ const elements = args.map((arg) =>
169
+ typeof arg === "string" && isValidSuiObjectId(arg)
170
+ ? txBlock.object(normalizeSuiObjectId(arg))
171
+ : convertObjArg(txBlock, arg as SuiObjectArg),
172
+ );
173
+ return txBlock.makeMoveVec({ elements });
174
+ } else if (
175
+ typeof type === "string" &&
176
+ !VECTOR_REGEX.test(type) &&
177
+ !STRUCT_REGEX.test(type)
178
+ ) {
179
+ // Convert simple type to OpenSignatureBody for BCS schema
180
+ const signatureBody = toOpenSignatureBody(type as SimpleBcsType);
181
+ const bcsSchema = getPureBcsSchema(signatureBody);
182
+ if (!bcsSchema) {
183
+ throw new Error(`Unknown type: ${type}`);
184
+ }
185
+ return txBlock.pure(bcs.vector(bcsSchema).serialize(args));
186
+ } else {
187
+ const elements = args.map((arg) =>
188
+ convertObjArg(txBlock, arg as SuiObjectArg),
189
+ );
190
+ return txBlock.makeMoveVec({ elements, type: type as string });
191
+ }
190
192
  }
191
193
 
192
194
  /**
@@ -197,27 +199,27 @@ export function makeVecParam(
197
199
  * @returns The converted array of TransactionArgument.
198
200
  */
199
201
  export function convertArgs(
200
- txBlock: Transaction,
201
- args: (SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg)[]
202
+ txBlock: Transaction,
203
+ args: (SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg)[],
202
204
  ): TransactionArgument[] {
203
- return args.map((arg) => {
204
- if (arg instanceof SerializedBcs || isSerializedBcs(arg)) {
205
- return txBlock.pure(arg);
206
- }
205
+ return args.map((arg) => {
206
+ if (arg instanceof SerializedBcs || isSerializedBcs(arg)) {
207
+ return txBlock.pure(arg);
208
+ }
207
209
 
208
- if (isMoveVecArg(arg)) {
209
- const vecType = 'vecType' in arg;
210
- return vecType
211
- ? makeVecParam(txBlock, arg.value, arg.vecType)
212
- : makeVecParam(txBlock, arg);
213
- }
210
+ if (isMoveVecArg(arg)) {
211
+ const vecType = "vecType" in arg;
212
+ return vecType
213
+ ? makeVecParam(txBlock, arg.value, arg.vecType)
214
+ : makeVecParam(txBlock, arg);
215
+ }
214
216
 
215
- if (isAmountArg(arg)) {
216
- return convertAmounts(txBlock, [arg as unknown as SuiAmountsArg])[0];
217
- }
217
+ if (isAmountArg(arg)) {
218
+ return convertAmounts(txBlock, [arg as unknown as SuiAmountsArg])[0];
219
+ }
218
220
 
219
- return convertObjArg(txBlock, arg as SuiObjectArg);
220
- });
221
+ return convertObjArg(txBlock, arg as SuiObjectArg);
222
+ });
221
223
  }
222
224
 
223
225
  /**
@@ -228,14 +230,14 @@ export function convertArgs(
228
230
  * @returns The converted TransactionArgument.
229
231
  */
230
232
  export function convertAddressArg(
231
- txBlock: Transaction,
232
- arg: SuiAddressArg
233
+ txBlock: Transaction,
234
+ arg: SuiAddressArg,
233
235
  ): SuiTxArg {
234
- if (typeof arg === 'string' && isValidSuiAddress(arg)) {
235
- return txBlock.pure.address(normalizeSuiAddress(arg));
236
- } else {
237
- return convertArgs(txBlock, [arg])[0];
238
- }
236
+ if (typeof arg === "string" && isValidSuiAddress(arg)) {
237
+ return txBlock.pure.address(normalizeSuiAddress(arg));
238
+ } else {
239
+ return convertArgs(txBlock, [arg])[0];
240
+ }
239
241
  }
240
242
 
241
243
  /**
@@ -246,64 +248,64 @@ export function convertAddressArg(
246
248
  * @returns The converted TransactionArgument.
247
249
  */
248
250
  export function convertObjArg(
249
- txb: Transaction,
250
- arg: SuiObjectArg
251
+ txb: Transaction,
252
+ arg: SuiObjectArg,
251
253
  ): TransactionObjectArgument {
252
- if (typeof arg === 'string') {
253
- return txb.object(arg);
254
- }
254
+ if (typeof arg === "string") {
255
+ return txb.object(arg);
256
+ }
255
257
 
256
- if (isObjectRef(arg)) {
257
- return txb.objectRef(arg);
258
- }
258
+ if (isObjectRef(arg)) {
259
+ return txb.objectRef(arg);
260
+ }
259
261
 
260
- if (isSharedObjectRef(arg)) {
261
- return txb.sharedObjectRef(arg);
262
- }
262
+ if (isSharedObjectRef(arg)) {
263
+ return txb.sharedObjectRef(arg);
264
+ }
263
265
 
264
- if ('Object' in arg) {
265
- if ('ImmOrOwnedObject' in arg.Object) {
266
- return txb.object(Inputs.ObjectRef(arg.Object.ImmOrOwnedObject));
267
- } else if ('SharedObject' in arg.Object) {
268
- return txb.object(Inputs.SharedObjectRef(arg.Object.SharedObject));
269
- } else {
270
- throw new Error('Invalid argument type');
271
- }
272
- }
266
+ if ("Object" in arg) {
267
+ if ("ImmOrOwnedObject" in arg.Object) {
268
+ return txb.object(Inputs.ObjectRef(arg.Object.ImmOrOwnedObject));
269
+ } else if ("SharedObject" in arg.Object) {
270
+ return txb.object(Inputs.SharedObjectRef(arg.Object.SharedObject));
271
+ } else {
272
+ throw new Error("Invalid argument type");
273
+ }
274
+ }
273
275
 
274
- if (typeof arg === 'function') {
275
- return arg;
276
- }
276
+ if (typeof arg === "function") {
277
+ return arg;
278
+ }
277
279
 
278
- if (
279
- 'GasCoin' in arg ||
280
- 'Input' in arg ||
281
- 'Result' in arg ||
282
- 'NestedResult' in arg
283
- ) {
284
- return arg;
285
- }
280
+ if (
281
+ "GasCoin" in arg ||
282
+ "Input" in arg ||
283
+ "Result" in arg ||
284
+ "NestedResult" in arg
285
+ ) {
286
+ return arg;
287
+ }
286
288
 
287
- throw new Error('Invalid argument type');
289
+ throw new Error("Invalid argument type");
288
290
  }
289
291
 
290
292
  export function convertAmounts(
291
- txBlock: Transaction,
292
- amounts: SuiAmountsArg[]
293
+ txBlock: Transaction,
294
+ amounts: SuiAmountsArg[],
293
295
  ): TransactionArgument[] {
294
- return amounts.map((amount) => {
295
- if (isAmountArg(amount)) {
296
- return txBlock.pure.u64(amount);
297
- } else {
298
- return convertArgs(txBlock, [amount])[0];
299
- }
300
- });
296
+ return amounts.map((amount) => {
297
+ if (isAmountArg(amount)) {
298
+ return txBlock.pure.u64(amount);
299
+ } else {
300
+ return convertArgs(txBlock, [amount])[0];
301
+ }
302
+ });
301
303
  }
302
304
 
303
305
  export const partitionArray = <T>(array: T[], chunkSize: number) => {
304
- const result: T[][] = [];
305
- for (let i = 0; i < array.length; i += chunkSize) {
306
- result.push(array.slice(i, i + chunkSize));
307
- }
308
- return result;
306
+ const result: T[][] = [];
307
+ for (let i = 0; i < array.length; i += chunkSize) {
308
+ result.push(array.slice(i, i + chunkSize));
309
+ }
310
+ return result;
309
311
  };