@0xobelisk/sui-client 0.5.19 → 0.5.20

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.20",
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,64 @@ 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
+ let stillNeedFormat = true;
185
+ while (stillNeedFormat === true) {
186
+ let loopFlag = false;
187
+ Object.values(metadata as SuiMoveNormalizedModules).forEach(
188
+ (moudlevalue) => {
189
+ const data = moudlevalue as SuiMoveMoudleValueType;
190
+ const moduleName = data.name;
191
+ const objMoudleId = `${packageId}::${moduleName}`;
192
+
193
+ Object.entries(data.structs).forEach(([objectName, objectType]) => {
194
+ const objId = `${objMoudleId}::${objectName}`;
195
+ const bcsmeta: MoveStructType = {
196
+ objectName,
197
+ objectType,
198
+ };
199
+ // if (isUndefined(this.#object[objId])) {
200
+ let bcsObj = this.#bcs(bcsmeta);
201
+ if (bcsObj.loopFlag === true) {
202
+ loopFlag = bcsObj.loopFlag;
164
203
  }
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
- );
204
+ this.#object[objId] = bcsObj.bcs;
205
+ // }
206
+ });
207
+
208
+ Object.entries(data.exposedFunctions).forEach(
209
+ ([funcName, funcvalue]) => {
210
+ const meta = funcvalue as SuiMoveMoudleFuncType;
211
+ meta.moduleName = moduleName;
212
+ meta.funcName = funcName;
213
+ if (isUndefined(this.#query[moduleName])) {
214
+ this.#query[moduleName] = {};
215
+ }
216
+ if (isUndefined(this.#query[moduleName][funcName])) {
217
+ this.#query[moduleName][funcName] = createQuery(
218
+ meta,
219
+ (tx, p, typeArguments, isRaw) =>
220
+ this.#read(meta, tx, p, typeArguments, isRaw)
221
+ );
222
+ }
223
+
224
+ if (isUndefined(this.#tx[moduleName])) {
225
+ this.#tx[moduleName] = {};
226
+ }
227
+ if (isUndefined(this.#tx[moduleName][funcName])) {
228
+ this.#tx[moduleName][funcName] = createTx(
229
+ meta,
230
+ (tx, p, typeArguments, isRaw) =>
231
+ this.#exec(meta, tx, p, typeArguments, isRaw)
232
+ );
233
+ }
171
234
  }
172
- }
173
- );
174
- }
175
- );
235
+ );
236
+ }
237
+ );
238
+
239
+ stillNeedFormat = loopFlag;
240
+ }
176
241
  }
177
242
  this.contractFactory = new SuiContractFactory({
178
243
  packageId,
@@ -188,8 +253,8 @@ export class Obelisk {
188
253
  return this.#tx;
189
254
  }
190
255
 
191
- public get struct(): MapMoudleStruct {
192
- return this.#struct;
256
+ public get object(): MapObjectStruct {
257
+ return this.#object;
193
258
  }
194
259
 
195
260
  #exec = async (
@@ -239,6 +304,227 @@ export class Obelisk {
239
304
  return await this.inspectTxn(tx);
240
305
  };
241
306
 
307
+ #bcs = (bcsmeta: MoveStructType) => {
308
+ let loopFlag = false;
309
+ const bcsJson: Record<string, BcsType<any, any>> = {};
310
+ Object.entries(bcsmeta.objectType.fields).forEach(([index, type]) => {
311
+ const objName = type.name;
312
+ const objType: SuiMoveNormalizedType = type.type;
313
+ switch (typeof objType) {
314
+ case 'object':
315
+ for (const [key, value] of Object.entries(objType)) {
316
+ switch (key) {
317
+ case 'Struct':
318
+ const structType = value as {
319
+ address: string;
320
+ module: string;
321
+ name: string;
322
+ typeArguments: SuiMoveNormalizedType[];
323
+ };
324
+ if (
325
+ structType.address === '0x1' &&
326
+ structType.module === 'ascii' &&
327
+ structType.name === 'String'
328
+ ) {
329
+ bcsJson[objName] = bcs.string();
330
+ return;
331
+ } else if (
332
+ structType.address === '0x2' &&
333
+ structType.module === 'object' &&
334
+ structType.name === 'UID'
335
+ ) {
336
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
337
+ input: (id: string) => fromHEX(id),
338
+ output: (id) => toHEX(Uint8Array.from(id)),
339
+ });
340
+ return;
341
+ } else if (
342
+ structType.address === '0x2' &&
343
+ structType.module === 'object' &&
344
+ structType.name === 'ID'
345
+ ) {
346
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
347
+ input: (id: string) => fromHEX(id),
348
+ output: (id) => toHEX(Uint8Array.from(id)),
349
+ });
350
+ return;
351
+ } else if (
352
+ structType.address === '0x2' &&
353
+ structType.module === 'bag' &&
354
+ structType.name === 'Bag'
355
+ ) {
356
+ bcsJson[objName] = bcs.fixedArray(32, bcs.u8()).transform({
357
+ input: (id: string) => fromHEX(id),
358
+ output: (id) => toHEX(Uint8Array.from(id)),
359
+ });
360
+ return;
361
+ } else if (
362
+ structType.address === '0x1' &&
363
+ structType.module === 'option' &&
364
+ structType.name === 'Option'
365
+ ) {
366
+ switch (structType.typeArguments[0]) {
367
+ case 'U8':
368
+ bcsJson[objName] = bcs.option(bcs.u8());
369
+ return;
370
+ case 'U16':
371
+ bcsJson[objName] = bcs.option(bcs.u16());
372
+ return;
373
+ case 'U32':
374
+ bcsJson[objName] = bcs.option(bcs.u32());
375
+ return;
376
+ case 'U64':
377
+ bcsJson[objName] = bcs.option(bcs.u64());
378
+ return;
379
+ case 'U128':
380
+ bcsJson[objName] = bcs.option(bcs.u128());
381
+ return;
382
+ case 'U256':
383
+ bcsJson[objName] = bcs.option(bcs.u256());
384
+ return;
385
+ case 'Bool':
386
+ bcsJson[objName] = bcs.option(bcs.bool());
387
+ return;
388
+ case 'Address':
389
+ bcsJson[objName] = bcs.option(
390
+ bcs.bytes(32).transform({
391
+ // To change the input type, you need to provide a type definition for the input
392
+ input: (val: string) => fromHEX(val),
393
+ output: (val) => toHEX(val),
394
+ })
395
+ );
396
+ return;
397
+ default:
398
+ // throw new Error('Unsupported type');
399
+ }
400
+ } else {
401
+ if (
402
+ this.object[
403
+ `${structType.address}::${structType.module}::${structType.name}`
404
+ ] === undefined
405
+ ) {
406
+ loopFlag = true;
407
+ } else {
408
+ bcsJson[objName] =
409
+ this.object[
410
+ `${structType.address}::${structType.module}::${structType.name}`
411
+ ];
412
+ return;
413
+ }
414
+ }
415
+ return;
416
+ case 'Vector':
417
+ switch (value) {
418
+ case 'U8':
419
+ bcsJson[objName] = bcs.vector(bcs.u8());
420
+ return;
421
+ case 'U16':
422
+ bcsJson[objName] = bcs.vector(bcs.u16());
423
+ return;
424
+ case 'U32':
425
+ bcsJson[objName] = bcs.vector(bcs.u32());
426
+ return;
427
+ case 'U64':
428
+ bcsJson[objName] = bcs.vector(bcs.u64());
429
+ return;
430
+ case 'U128':
431
+ bcsJson[objName] = bcs.vector(bcs.u128());
432
+ return;
433
+ case 'U256':
434
+ bcsJson[objName] = bcs.vector(bcs.u256());
435
+ return;
436
+ case 'Bool':
437
+ bcsJson[objName] = bcs.vector(bcs.bool());
438
+ return;
439
+ case 'Address':
440
+ bcsJson[objName] = bcs.vector(
441
+ bcs.bytes(32).transform({
442
+ // To change the input type, you need to provide a type definition for the input
443
+ input: (val: string) => fromHEX(val),
444
+ output: (val) => toHEX(val),
445
+ })
446
+ );
447
+ return;
448
+ default:
449
+ // throw new Error('Unsupported type');
450
+ }
451
+
452
+ case 'TypeParameter':
453
+ bcsJson[objName] = bcs.u128();
454
+ return;
455
+ // case 'Reference':
456
+
457
+ // case 'MutableReference':
458
+
459
+ default:
460
+ throw new Error('Unsupported type');
461
+ }
462
+ }
463
+ return;
464
+ case 'string':
465
+ switch (objType) {
466
+ case 'U8':
467
+ bcsJson[objName] = bcs.u8();
468
+ return;
469
+ case 'U16':
470
+ bcsJson[objName] = bcs.u16();
471
+ return;
472
+ case 'U32':
473
+ bcsJson[objName] = bcs.u32();
474
+ return;
475
+ case 'U64':
476
+ bcsJson[objName] = bcs.u64();
477
+ return;
478
+ case 'U128':
479
+ bcsJson[objName] = bcs.u128();
480
+ return;
481
+ case 'U256':
482
+ bcsJson[objName] = bcs.u256();
483
+ return;
484
+ case 'Bool':
485
+ bcsJson[objName] = bcs.bool();
486
+ return;
487
+ case 'Address':
488
+ bcsJson[objName] = bcs.bytes(32).transform({
489
+ // To change the input type, you need to provide a type definition for the input
490
+ input: (val: string) => fromHEX(val),
491
+ output: (val) => toHEX(val),
492
+ });
493
+ return;
494
+ default:
495
+ return;
496
+ }
497
+ default:
498
+ throw new Error('Unsupported type');
499
+ }
500
+ });
501
+
502
+ return {
503
+ bcs: bcs.struct(bcsmeta.objectName, bcsJson),
504
+ loopFlag,
505
+ };
506
+ };
507
+
508
+ view(dryResult: DevInspectResults) {
509
+ let returnValues = [];
510
+
511
+ // "success" | "failure";
512
+ if (dryResult.effects.status.status === 'success') {
513
+ const resultList = dryResult.results![0].returnValues!;
514
+
515
+ for (const res of resultList) {
516
+ let baseValue = res[0];
517
+ let baseType = res[1];
518
+
519
+ const value = Uint8Array.from(baseValue);
520
+ returnValues.push(this.object[baseType].parse(value));
521
+ }
522
+ return returnValues;
523
+ } else {
524
+ return undefined;
525
+ }
526
+ }
527
+
242
528
  /**
243
529
  * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
244
530
  * else:
@@ -549,54 +835,13 @@ export class Obelisk {
549
835
  params.push(tx.pure.address(entityId));
550
836
  }
551
837
 
552
- const getResult = (await this.query[schemaModuleName].get(
838
+ const dryResult = (await this.query[schemaModuleName].get(
553
839
  tx,
554
840
  params
555
841
  )) as DevInspectResults;
556
- let returnValues = [];
557
842
 
558
843
  // "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
- }
844
+ return this.view(dryResult);
600
845
  }
601
846
 
602
847
  async containEntity(
@@ -612,21 +857,12 @@ export class Obelisk {
612
857
  params.push(tx.pure.address(entityId));
613
858
  }
614
859
 
615
- const getResult = (await this.query[schemaModuleName].contains(
860
+ const dryResult = (await this.query[schemaModuleName].contains(
616
861
  tx,
617
862
  params
618
863
  )) as DevInspectResults;
619
864
 
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
- }
865
+ return this.view(dryResult) as boolean | undefined;
630
866
  }
631
867
 
632
868
  // async getEntities(
@@ -740,50 +976,4 @@ export class Obelisk {
740
976
  // const u8Value = Uint8Array.from(value);
741
977
  // return bcs.de(type, u8Value);
742
978
  // }
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
979
  }
@@ -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>>;