@0xobelisk/sui-client 0.5.19 → 0.5.21

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xobelisk/sui-client",
3
- "version": "0.5.19",
3
+ "version": "0.5.21",
4
4
  "description": "Tookit for interacting with move eps framework",
5
5
  "keywords": [
6
6
  "sui",
package/src/obelisk.ts CHANGED
@@ -1,19 +1,19 @@
1
1
  import keccak256 from 'keccak256';
2
2
  import { getFullnodeUrl } from '@mysten/sui/client';
3
3
  import { Transaction, TransactionResult } from '@mysten/sui/transactions';
4
- import type { BcsType, SerializedBcs } from '@mysten/bcs';
4
+ import { BcsType, SerializedBcs } from '@mysten/bcs';
5
5
  import type { TransactionArgument } from '@mysten/sui/transactions';
6
6
  import type {
7
7
  SuiTransactionBlockResponse,
8
8
  DevInspectResults,
9
9
  SuiMoveNormalizedModules,
10
+ SuiMoveNormalizedType,
10
11
  SuiObjectData,
11
12
  } from '@mysten/sui/client';
12
13
  import { SuiAccountManager } from './libs/suiAccountManager';
13
14
  import { SuiTx } from './libs/suiTxBuilder';
14
15
  import { SuiInteractor } from './libs/suiInteractor';
15
-
16
- import { MapMoudleStruct, NetworkType, ObeliskObjectContent } from './types';
16
+ import { MapObjectStruct, MoveStructType, ObeliskObjectContent } from './types';
17
17
  import { SuiContractFactory } from './libs/suiContractFactory';
18
18
  import {
19
19
  SuiMoveMoudleFuncType,
@@ -107,7 +107,50 @@ export class Obelisk {
107
107
 
108
108
  readonly #query: MapMoudleFuncQuery = {};
109
109
  readonly #tx: MapMoudleFuncTx = {};
110
- readonly #struct: MapMoudleStruct = {};
110
+ readonly #object: MapObjectStruct = {
111
+ address: bcs.bytes(32).transform({
112
+ // To change the input type, you need to provide a type definition for the input
113
+ input: (val: string) => fromHEX(val),
114
+ output: (val) => toHEX(val),
115
+ }),
116
+ u8: bcs.u8(),
117
+ u16: bcs.u16(),
118
+ u32: bcs.u32(),
119
+ u64: bcs.u64(),
120
+ u128: bcs.u128(),
121
+ u256: bcs.u256(),
122
+ bool: bcs.bool(),
123
+ '0x1::ascii::String': bcs.string(),
124
+ '0x1::option::Option<address>': bcs.option(
125
+ bcs.bytes(32).transform({
126
+ // To change the input type, you need to provide a type definition for the input
127
+ input: (val: string) => fromHEX(val),
128
+ output: (val) => toHEX(val),
129
+ })
130
+ ),
131
+ '0x1::option::Option<u8>': bcs.option(bcs.u8()),
132
+ '0x1::option::Option<u16>': bcs.option(bcs.u16()),
133
+ '0x1::option::Option<u32>': bcs.option(bcs.u32()),
134
+ '0x1::option::Option<u64>': bcs.option(bcs.u64()),
135
+ '0x1::option::Option<u128>': bcs.option(bcs.u128()),
136
+ '0x1::option::Option<u256>': bcs.option(bcs.u256()),
137
+ '0x1::option::Option<bool>': bcs.option(bcs.bool()),
138
+ 'vector<address>': bcs.vector(
139
+ bcs.bytes(32).transform({
140
+ // To change the input type, you need to provide a type definition for the input
141
+ input: (val: string) => fromHEX(val),
142
+ output: (val) => toHEX(val),
143
+ })
144
+ ),
145
+ 'vector<u8>': bcs.vector(bcs.u8()),
146
+ 'vector<u16>': bcs.vector(bcs.u16()),
147
+ 'vector<u32>': bcs.vector(bcs.u32()),
148
+ 'vector<u64>': bcs.vector(bcs.u64()),
149
+ 'vector<u128>': bcs.vector(bcs.u128()),
150
+ 'vector<u256>': bcs.vector(bcs.u256()),
151
+ 'vector<bool>': bcs.vector(bcs.bool()),
152
+ };
153
+
111
154
  /**
112
155
  * Support the following ways to init the ObeliskClient:
113
156
  * 1. mnemonics
@@ -137,42 +180,67 @@ export class Obelisk {
137
180
  this.packageId = packageId;
138
181
  if (metadata !== undefined) {
139
182
  this.metadata = metadata as SuiMoveNormalizedModules;
140
- Object.values(metadata as SuiMoveNormalizedModules).forEach(
141
- (moudlevalue) => {
142
- const data = moudlevalue as SuiMoveMoudleValueType;
143
- const moduleName = data.name;
144
-
145
- Object.entries(data.exposedFunctions).forEach(
146
- ([funcName, funcvalue]) => {
147
- const meta = funcvalue as SuiMoveMoudleFuncType;
148
- meta.moduleName = moduleName;
149
- meta.funcName = funcName;
150
- // console.log(JSON.stringify(funcvalue));
151
- if (isUndefined(this.#query[moduleName])) {
152
- this.#query[moduleName] = {};
153
- }
154
- if (isUndefined(this.#query[moduleName][funcName])) {
155
- this.#query[moduleName][funcName] = createQuery(
156
- meta,
157
- (tx, p, typeArguments, isRaw) =>
158
- this.#read(meta, tx, p, typeArguments, isRaw)
159
- );
160
- }
161
183
 
162
- if (isUndefined(this.#tx[moduleName])) {
163
- this.#tx[moduleName] = {};
184
+ const maxLoopNum = 5;
185
+ let loopNum = 0;
186
+ let stillNeedFormat = true;
187
+ while (stillNeedFormat === true && loopNum <= maxLoopNum) {
188
+ let loopFlag = false;
189
+ Object.values(metadata as SuiMoveNormalizedModules).forEach(
190
+ (moudlevalue) => {
191
+ const data = moudlevalue as SuiMoveMoudleValueType;
192
+ const moduleName = data.name;
193
+ const objMoudleId = `${packageId}::${moduleName}`;
194
+
195
+ Object.entries(data.structs).forEach(([objectName, objectType]) => {
196
+ const objId = `${objMoudleId}::${objectName}`;
197
+ const bcsmeta: MoveStructType = {
198
+ objectName,
199
+ objectType,
200
+ };
201
+ // if (isUndefined(this.#object[objId])) {
202
+ let bcsObj = this.#bcs(bcsmeta);
203
+ if (bcsObj.loopFlag === true) {
204
+ loopFlag = bcsObj.loopFlag;
164
205
  }
165
- if (isUndefined(this.#tx[moduleName][funcName])) {
166
- this.#tx[moduleName][funcName] = createTx(
167
- meta,
168
- (tx, p, typeArguments, isRaw) =>
169
- this.#exec(meta, tx, p, typeArguments, isRaw)
170
- );
206
+ this.#object[objId] = bcsObj.bcs;
207
+ // }
208
+ });
209
+
210
+ Object.entries(data.exposedFunctions).forEach(
211
+ ([funcName, funcvalue]) => {
212
+ const meta = funcvalue as SuiMoveMoudleFuncType;
213
+ meta.moduleName = moduleName;
214
+ meta.funcName = funcName;
215
+ if (isUndefined(this.#query[moduleName])) {
216
+ this.#query[moduleName] = {};
217
+ }
218
+ if (isUndefined(this.#query[moduleName][funcName])) {
219
+ this.#query[moduleName][funcName] = createQuery(
220
+ meta,
221
+ (tx, p, typeArguments, isRaw) =>
222
+ this.#read(meta, tx, p, typeArguments, isRaw)
223
+ );
224
+ }
225
+
226
+ if (isUndefined(this.#tx[moduleName])) {
227
+ this.#tx[moduleName] = {};
228
+ }
229
+ if (isUndefined(this.#tx[moduleName][funcName])) {
230
+ this.#tx[moduleName][funcName] = createTx(
231
+ meta,
232
+ (tx, p, typeArguments, isRaw) =>
233
+ this.#exec(meta, tx, p, typeArguments, isRaw)
234
+ );
235
+ }
171
236
  }
172
- }
173
- );
174
- }
175
- );
237
+ );
238
+ }
239
+ );
240
+
241
+ stillNeedFormat = loopFlag;
242
+ loopNum++;
243
+ }
176
244
  }
177
245
  this.contractFactory = new SuiContractFactory({
178
246
  packageId,
@@ -188,8 +256,8 @@ export class Obelisk {
188
256
  return this.#tx;
189
257
  }
190
258
 
191
- public get struct(): MapMoudleStruct {
192
- return this.#struct;
259
+ public get object(): MapObjectStruct {
260
+ return this.#object;
193
261
  }
194
262
 
195
263
  #exec = async (
@@ -239,6 +307,227 @@ export class Obelisk {
239
307
  return await this.inspectTxn(tx);
240
308
  };
241
309
 
310
+ #bcs = (bcsmeta: MoveStructType) => {
311
+ let loopFlag = false;
312
+ const bcsJson: Record<string, BcsType<any, any>> = {};
313
+ Object.entries(bcsmeta.objectType.fields).forEach(([index, type]) => {
314
+ const objName = type.name;
315
+ const objType: SuiMoveNormalizedType = type.type;
316
+ switch (typeof objType) {
317
+ case 'object':
318
+ for (const [key, value] of Object.entries(objType)) {
319
+ switch (key) {
320
+ case 'Struct':
321
+ const structType = value as {
322
+ address: string;
323
+ module: string;
324
+ name: string;
325
+ typeArguments: SuiMoveNormalizedType[];
326
+ };
327
+ if (
328
+ structType.address === '0x1' &&
329
+ structType.module === 'ascii' &&
330
+ structType.name === 'String'
331
+ ) {
332
+ bcsJson[objName] = bcs.string();
333
+ return;
334
+ } else if (
335
+ structType.address === '0x2' &&
336
+ structType.module === 'object' &&
337
+ structType.name === 'UID'
338
+ ) {
339
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
340
+ input: (id: string) => fromHEX(id),
341
+ output: (id) => toHEX(Uint8Array.from(id)),
342
+ });
343
+ return;
344
+ } else if (
345
+ structType.address === '0x2' &&
346
+ structType.module === 'object' &&
347
+ structType.name === 'ID'
348
+ ) {
349
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
350
+ input: (id: string) => fromHEX(id),
351
+ output: (id) => toHEX(Uint8Array.from(id)),
352
+ });
353
+ return;
354
+ } else if (
355
+ structType.address === '0x2' &&
356
+ structType.module === 'bag' &&
357
+ structType.name === 'Bag'
358
+ ) {
359
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
360
+ input: (id: string) => fromHEX(id),
361
+ output: (id) => toHEX(Uint8Array.from(id)),
362
+ });
363
+ return;
364
+ } else if (
365
+ structType.address === '0x1' &&
366
+ structType.module === 'option' &&
367
+ structType.name === 'Option'
368
+ ) {
369
+ switch (structType.typeArguments[0]) {
370
+ case 'U8':
371
+ bcsJson[objName] = bcs.option(bcs.u8());
372
+ return;
373
+ case 'U16':
374
+ bcsJson[objName] = bcs.option(bcs.u16());
375
+ return;
376
+ case 'U32':
377
+ bcsJson[objName] = bcs.option(bcs.u32());
378
+ return;
379
+ case 'U64':
380
+ bcsJson[objName] = bcs.option(bcs.u64());
381
+ return;
382
+ case 'U128':
383
+ bcsJson[objName] = bcs.option(bcs.u128());
384
+ return;
385
+ case 'U256':
386
+ bcsJson[objName] = bcs.option(bcs.u256());
387
+ return;
388
+ case 'Bool':
389
+ bcsJson[objName] = bcs.option(bcs.bool());
390
+ return;
391
+ case 'Address':
392
+ bcsJson[objName] = bcs.option(
393
+ bcs.bytes(32).transform({
394
+ // To change the input type, you need to provide a type definition for the input
395
+ input: (val: string) => fromHEX(val),
396
+ output: (val) => toHEX(val),
397
+ })
398
+ );
399
+ return;
400
+ default:
401
+ // throw new Error('Unsupported type');
402
+ }
403
+ } else {
404
+ if (
405
+ this.object[
406
+ `${structType.address}::${structType.module}::${structType.name}`
407
+ ] === undefined
408
+ ) {
409
+ loopFlag = true;
410
+ } else {
411
+ bcsJson[objName] =
412
+ this.object[
413
+ `${structType.address}::${structType.module}::${structType.name}`
414
+ ];
415
+ return;
416
+ }
417
+ }
418
+ return;
419
+ case 'Vector':
420
+ switch (value) {
421
+ case 'U8':
422
+ bcsJson[objName] = bcs.vector(bcs.u8());
423
+ return;
424
+ case 'U16':
425
+ bcsJson[objName] = bcs.vector(bcs.u16());
426
+ return;
427
+ case 'U32':
428
+ bcsJson[objName] = bcs.vector(bcs.u32());
429
+ return;
430
+ case 'U64':
431
+ bcsJson[objName] = bcs.vector(bcs.u64());
432
+ return;
433
+ case 'U128':
434
+ bcsJson[objName] = bcs.vector(bcs.u128());
435
+ return;
436
+ case 'U256':
437
+ bcsJson[objName] = bcs.vector(bcs.u256());
438
+ return;
439
+ case 'Bool':
440
+ bcsJson[objName] = bcs.vector(bcs.bool());
441
+ return;
442
+ case 'Address':
443
+ bcsJson[objName] = bcs.vector(
444
+ bcs.bytes(32).transform({
445
+ // To change the input type, you need to provide a type definition for the input
446
+ input: (val: string) => fromHEX(val),
447
+ output: (val) => toHEX(val),
448
+ })
449
+ );
450
+ return;
451
+ default:
452
+ // throw new Error('Unsupported type');
453
+ }
454
+
455
+ case 'TypeParameter':
456
+ bcsJson[objName] = bcs.u128();
457
+ return;
458
+ // case 'Reference':
459
+
460
+ // case 'MutableReference':
461
+
462
+ default:
463
+ throw new Error('Unsupported type');
464
+ }
465
+ }
466
+ return;
467
+ case 'string':
468
+ switch (objType) {
469
+ case 'U8':
470
+ bcsJson[objName] = bcs.u8();
471
+ return;
472
+ case 'U16':
473
+ bcsJson[objName] = bcs.u16();
474
+ return;
475
+ case 'U32':
476
+ bcsJson[objName] = bcs.u32();
477
+ return;
478
+ case 'U64':
479
+ bcsJson[objName] = bcs.u64();
480
+ return;
481
+ case 'U128':
482
+ bcsJson[objName] = bcs.u128();
483
+ return;
484
+ case 'U256':
485
+ bcsJson[objName] = bcs.u256();
486
+ return;
487
+ case 'Bool':
488
+ bcsJson[objName] = bcs.bool();
489
+ return;
490
+ case 'Address':
491
+ bcsJson[objName] = bcs.bytes(32).transform({
492
+ // To change the input type, you need to provide a type definition for the input
493
+ input: (val: string) => fromHEX(val),
494
+ output: (val) => toHEX(val),
495
+ });
496
+ return;
497
+ default:
498
+ return;
499
+ }
500
+ default:
501
+ throw new Error('Unsupported type');
502
+ }
503
+ });
504
+
505
+ return {
506
+ bcs: bcs.struct(bcsmeta.objectName, bcsJson),
507
+ loopFlag,
508
+ };
509
+ };
510
+
511
+ view(dryResult: DevInspectResults) {
512
+ let returnValues = [];
513
+
514
+ // "success" | "failure";
515
+ if (dryResult.effects.status.status === 'success') {
516
+ const resultList = dryResult.results![0].returnValues!;
517
+
518
+ for (const res of resultList) {
519
+ let baseValue = res[0];
520
+ let baseType = res[1];
521
+
522
+ const value = Uint8Array.from(baseValue);
523
+ returnValues.push(this.object[baseType].parse(value));
524
+ }
525
+ return returnValues;
526
+ } else {
527
+ return undefined;
528
+ }
529
+ }
530
+
242
531
  /**
243
532
  * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
244
533
  * else:
@@ -549,54 +838,13 @@ export class Obelisk {
549
838
  params.push(tx.pure.address(entityId));
550
839
  }
551
840
 
552
- const getResult = (await this.query[schemaModuleName].get(
841
+ const dryResult = (await this.query[schemaModuleName].get(
553
842
  tx,
554
843
  params
555
844
  )) as DevInspectResults;
556
- let returnValues = [];
557
845
 
558
846
  // "success" | "failure";
559
- if (getResult.effects.status.status === 'success') {
560
- const resultList = getResult.results![0].returnValues!;
561
-
562
- for (const res of resultList) {
563
- let baseValue = res[0];
564
- let baseType = res[1];
565
-
566
- const value = Uint8Array.from(baseValue);
567
- if (baseType === 'address') {
568
- const Address = bcs.bytes(32).transform({
569
- // To change the input type, you need to provide a type definition for the input
570
- input: (val: string) => fromHEX(val),
571
- output: (val) => toHEX(val),
572
- });
573
- returnValues.push(Address.parse(value));
574
- } else if (baseType === 'u8') {
575
- returnValues.push(bcs.u8().parse(value));
576
- } else if (baseType === 'u16') {
577
- returnValues.push(bcs.u16().parse(value));
578
- } else if (baseType === 'u32') {
579
- returnValues.push(bcs.u32().parse(value));
580
- } else if (baseType === 'u64') {
581
- returnValues.push(bcs.u64().parse(value));
582
- } else if (baseType === 'u128') {
583
- returnValues.push(bcs.u128().parse(value));
584
- } else if (baseType === 'u256') {
585
- returnValues.push(bcs.u256().parse(value));
586
- } else if (baseType === 'bool') {
587
- returnValues.push(bcs.bool().parse(value));
588
- } else if (baseType === '0x1::ascii::String') {
589
- returnValues.push(bcs.string().parse(value));
590
- } else if (baseType === 'vector<u8>') {
591
- returnValues.push(bcs.vector(bcs.u8()).parse(value));
592
- } else if (baseType === '0x1::option::Option<u8>') {
593
- returnValues.push(bcs.option(bcs.u8()).parse(value));
594
- }
595
- }
596
- return returnValues;
597
- } else {
598
- return undefined;
599
- }
847
+ return this.view(dryResult);
600
848
  }
601
849
 
602
850
  async containEntity(
@@ -612,21 +860,12 @@ export class Obelisk {
612
860
  params.push(tx.pure.address(entityId));
613
861
  }
614
862
 
615
- const getResult = (await this.query[schemaModuleName].contains(
863
+ const dryResult = (await this.query[schemaModuleName].contains(
616
864
  tx,
617
865
  params
618
866
  )) as DevInspectResults;
619
867
 
620
- // "success" | "failure";
621
- if (getResult.effects.status.status === 'success') {
622
- const res = getResult.results![0].returnValues![0];
623
- let baseValue = res[0];
624
-
625
- const value = Uint8Array.from(baseValue);
626
- return bcs.bool().parse(value);
627
- } else {
628
- return undefined;
629
- }
868
+ return this.view(dryResult) as boolean | undefined;
630
869
  }
631
870
 
632
871
  // async getEntities(
@@ -740,50 +979,4 @@ export class Obelisk {
740
979
  // const u8Value = Uint8Array.from(value);
741
980
  // return bcs.de(type, u8Value);
742
981
  // }
743
-
744
- async autoFormatDryValue(value: DevInspectResults) {
745
- let returnValues = [];
746
-
747
- // "success" | "failure";
748
- if (value.effects.status.status === 'success') {
749
- const resultList = value.results![0].returnValues!;
750
-
751
- for (const res of resultList) {
752
- let baseValue = res[0];
753
- let baseType = res[1];
754
- const value = Uint8Array.from(baseValue);
755
- if (baseType === 'address') {
756
- const Address = bcs.bytes(32).transform({
757
- // To change the input type, you need to provide a type definition for the input
758
- input: (val: string) => fromHEX(val),
759
- output: (val) => toHEX(val),
760
- });
761
- returnValues.push(Address.parse(value));
762
- } else if (baseType === 'u8') {
763
- returnValues.push(bcs.u8().parse(value));
764
- } else if (baseType === 'u16') {
765
- returnValues.push(bcs.u16().parse(value));
766
- } else if (baseType === 'u32') {
767
- returnValues.push(bcs.u32().parse(value));
768
- } else if (baseType === 'u64') {
769
- returnValues.push(bcs.u64().parse(value));
770
- } else if (baseType === 'u128') {
771
- returnValues.push(bcs.u128().parse(value));
772
- } else if (baseType === 'u256') {
773
- returnValues.push(bcs.u256().parse(value));
774
- } else if (baseType === 'bool') {
775
- returnValues.push(bcs.bool().parse(value));
776
- } else if (baseType === '0x1::ascii::String') {
777
- returnValues.push(bcs.string().parse(value));
778
- } else if (baseType === 'vector<u8>') {
779
- returnValues.push(bcs.vector(bcs.u8()).parse(value));
780
- } else if (baseType === '0x1::option::Option<u8>') {
781
- returnValues.push(bcs.option(bcs.u8()).parse(value));
782
- }
783
- }
784
- return returnValues;
785
- } else {
786
- return undefined;
787
- }
788
- }
789
982
  }
@@ -109,35 +109,27 @@ export type MapMessageQuery = Record<string, ContractQuery>;
109
109
  export type MapMoudleFuncTx = Record<string, MapMessageTx>;
110
110
  export type MapMoudleFuncQuery = Record<string, MapMessageQuery>;
111
111
 
112
- type MoveStructType = {
113
- struct: Record<
114
- string,
115
- {
116
- fields: {
117
- type: SuiMoveNormalizedType;
118
- name: string;
119
- }[];
120
- abilities: {
121
- abilities: string[];
122
- };
123
- typeParameters: {
124
- constraints: {
125
- abilities: string[];
126
- };
127
- isPhantom: boolean;
128
- }[];
129
- }
130
- >;
131
- bcs: BcsType<
132
- {
133
- [x: string]: any;
134
- },
135
- {
136
- [x: string]: any;
137
- }
138
- >;
112
+ export type MoveStructValueType = {
113
+ fields: {
114
+ type: SuiMoveNormalizedType;
115
+ name: string;
116
+ }[];
117
+ abilities: {
118
+ abilities: string[];
119
+ };
120
+ typeParameters: {
121
+ constraints: {
122
+ abilities: string[];
123
+ };
124
+ isPhantom: boolean;
125
+ }[];
139
126
  };
140
- export type MapMoudleStruct = Record<string, MoveStructType>;
127
+ export type MoveStructType = {
128
+ objectType: MoveStructValueType;
129
+ objectName: string;
130
+ };
131
+
132
+ export type MapObjectStruct = Record<string, BcsType<any, any>>;
141
133
 
142
134
  export type MapMoudleFuncTest = Record<string, Record<string, string>>;
143
135
  export type MapMoudleFuncQueryTest = Record<string, Record<string, string>>;