@scallop-io/sui-kit 0.38.2 → 0.42.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { TransactionBlock, SUI_CLOCK_OBJECT_ID, SUI_SYSTEM_STATE_OBJECT_ID, } from '@mysten/sui.js';
1
+ export * from '@mysten/sui.js/utils';
2
+ export * from '@mysten/sui.js/transactions';
2
3
  export { SuiKit } from './suiKit';
3
4
  export { SuiAccountManager } from './libs/suiAccountManager';
4
5
  export { SuiTxBlock } from './libs/suiTxBuilder';
package/dist/index.js CHANGED
@@ -15,29 +15,29 @@ var __copyProps = (to, from, except, desc) => {
15
15
  }
16
16
  return to;
17
17
  };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
20
 
20
21
  // src/index.ts
21
22
  var src_exports = {};
22
23
  __export(src_exports, {
23
- SUI_CLOCK_OBJECT_ID: () => import_sui10.SUI_CLOCK_OBJECT_ID,
24
- SUI_SYSTEM_STATE_OBJECT_ID: () => import_sui10.SUI_SYSTEM_STATE_OBJECT_ID,
25
24
  SuiAccountManager: () => SuiAccountManager,
26
25
  SuiKit: () => SuiKit,
27
- SuiTxBlock: () => SuiTxBlock,
28
- TransactionBlock: () => import_sui10.TransactionBlock
26
+ SuiTxBlock: () => SuiTxBlock
29
27
  });
30
28
  module.exports = __toCommonJS(src_exports);
31
- var import_sui10 = require("@mysten/sui.js");
29
+ __reExport(src_exports, require("@mysten/sui.js/utils"), module.exports);
30
+ __reExport(src_exports, require("@mysten/sui.js/transactions"), module.exports);
32
31
 
33
32
  // src/suiKit.ts
34
- var import_sui9 = require("@mysten/sui.js");
33
+ var import_client2 = require("@mysten/sui.js/client");
34
+ var import_transactions2 = require("@mysten/sui.js/transactions");
35
35
 
36
36
  // src/libs/suiAccountManager/index.ts
37
- var import_sui3 = require("@mysten/sui.js");
37
+ var import_ed255192 = require("@mysten/sui.js/keypairs/ed25519");
38
38
 
39
39
  // src/libs/suiAccountManager/keypair.ts
40
- var import_sui = require("@mysten/sui.js");
40
+ var import_ed25519 = require("@mysten/sui.js/keypairs/ed25519");
41
41
  var getDerivePathForSUI = (derivePathParams = {}) => {
42
42
  const {
43
43
  accountIndex = 0,
@@ -48,11 +48,11 @@ var getDerivePathForSUI = (derivePathParams = {}) => {
48
48
  };
49
49
  var getKeyPair = (mnemonics, derivePathParams = {}) => {
50
50
  const derivePath = getDerivePathForSUI(derivePathParams);
51
- return import_sui.Ed25519Keypair.deriveKeypair(mnemonics, derivePath);
51
+ return import_ed25519.Ed25519Keypair.deriveKeypair(mnemonics, derivePath);
52
52
  };
53
53
 
54
54
  // src/libs/suiAccountManager/util.ts
55
- var import_sui2 = require("@mysten/sui.js");
55
+ var import_utils = require("@mysten/sui.js/utils");
56
56
  var isHex = (str) => /^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(str);
57
57
  var isBase64 = (str) => /^[a-zA-Z0-9+/]+={0,2}$/g.test(str);
58
58
  var fromHEX = (hexStr) => {
@@ -69,7 +69,7 @@ var hexOrBase64ToUint8Array = (str) => {
69
69
  if (isHex(str)) {
70
70
  return fromHEX(str);
71
71
  } else if (isBase64(str)) {
72
- return (0, import_sui2.fromB64)(str);
72
+ return (0, import_utils.fromB64)(str);
73
73
  } else {
74
74
  throw new Error("The string is not a valid hex or base64 string.");
75
75
  }
@@ -112,7 +112,7 @@ var SuiAccountManager = class {
112
112
  if (!this.mnemonics && !this.secretKey) {
113
113
  this.mnemonics = generateMnemonic(24);
114
114
  }
115
- this.currentKeyPair = this.secretKey ? import_sui3.Ed25519Keypair.fromSecretKey(
115
+ this.currentKeyPair = this.secretKey ? import_ed255192.Ed25519Keypair.fromSecretKey(
116
116
  normalizePrivateKey(hexOrBase64ToUint8Array(this.secretKey))
117
117
  ) : getKeyPair(this.mnemonics);
118
118
  this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
@@ -150,10 +150,11 @@ var SuiAccountManager = class {
150
150
  };
151
151
 
152
152
  // src/libs/suiTxBuilder/index.ts
153
- var import_sui5 = require("@mysten/sui.js");
153
+ var import_transactions = require("@mysten/sui.js/transactions");
154
+ var import_utils3 = require("@mysten/sui.js/utils");
154
155
 
155
156
  // src/libs/suiTxBuilder/util.ts
156
- var import_sui4 = require("@mysten/sui.js");
157
+ var import_utils2 = require("@mysten/sui.js/utils");
157
158
  var getDefaultSuiInputType = (value) => {
158
159
  if (typeof value === "string" && value.startsWith("0x")) {
159
160
  return "object";
@@ -171,7 +172,7 @@ function makeVecParam(txBlock, args, type) {
171
172
  const defaultSuiType = getDefaultSuiInputType(args[0]);
172
173
  if (type === "object" || !type && defaultSuiType === "object") {
173
174
  const objects = args.map(
174
- (arg) => typeof arg === "string" ? txBlock.object((0, import_sui4.normalizeSuiObjectId)(arg)) : arg
175
+ (arg) => typeof arg === "string" ? txBlock.object((0, import_utils2.normalizeSuiObjectId)(arg)) : arg
175
176
  );
176
177
  return txBlock.makeMoveVec({ objects });
177
178
  } else {
@@ -187,7 +188,7 @@ function isMoveVecArg(arg) {
187
188
  function convertArgs(txBlock, args) {
188
189
  return args.map((arg) => {
189
190
  if (typeof arg === "string" && arg.startsWith("0x")) {
190
- return txBlock.object((0, import_sui4.normalizeSuiObjectId)(arg));
191
+ return txBlock.object((0, import_utils2.normalizeSuiObjectId)(arg));
191
192
  } else if (isMoveVecArg(arg)) {
192
193
  const vecType = arg.vecType || void 0;
193
194
  return vecType ? makeVecParam(txBlock, arg.value, vecType) : makeVecParam(txBlock, arg);
@@ -202,9 +203,15 @@ function convertArgs(txBlock, args) {
202
203
  // src/libs/suiTxBuilder/index.ts
203
204
  var SuiTxBlock = class {
204
205
  constructor(transaction) {
205
- this.txBlock = new import_sui5.TransactionBlock(transaction);
206
+ this.txBlock = new import_transactions.TransactionBlock(transaction);
207
+ }
208
+ /* Directly wrap methods and properties of TransactionBlock */
209
+ get gas() {
210
+ return this.txBlock.gas;
211
+ }
212
+ get blockData() {
213
+ return this.txBlock.blockData;
206
214
  }
207
- //======== override methods of TransactionBlock ============
208
215
  address(value) {
209
216
  return this.txBlock.pure(value, "address");
210
217
  }
@@ -241,24 +248,28 @@ var SuiTxBlock = class {
241
248
  setGasPayment(payments) {
242
249
  return this.txBlock.setGasPayment(payments);
243
250
  }
244
- add(transaction) {
245
- return this.txBlock.add(transaction);
246
- }
247
251
  serialize() {
248
252
  return this.txBlock.serialize();
249
253
  }
250
- build(params = {}) {
254
+ build(params) {
251
255
  return this.txBlock.build(params);
252
256
  }
253
- getDigest({ provider } = {}) {
254
- return this.txBlock.getDigest({ provider });
257
+ getDigest(params = {}) {
258
+ return this.txBlock.getDigest(params);
255
259
  }
256
- get gas() {
257
- return this.txBlock.gas;
260
+ add(...args) {
261
+ return this.txBlock.add(...args);
258
262
  }
259
- get blockData() {
260
- return this.txBlock.blockData;
263
+ publish(...args) {
264
+ return this.txBlock.publish(...args);
265
+ }
266
+ upgrade(...args) {
267
+ return this.txBlock.upgrade(...args);
261
268
  }
269
+ makeMoveVec(...args) {
270
+ return this.txBlock.makeMoveVec(...args);
271
+ }
272
+ /* Override methods of TransactionBlock */
262
273
  transferObjects(objects, recipient) {
263
274
  const tx = this.txBlock;
264
275
  tx.transferObjects(convertArgs(this.txBlock, objects), tx.pure(recipient));
@@ -278,15 +289,6 @@ var SuiTxBlock = class {
278
289
  const sourceObjects = convertArgs(this.txBlock, sources);
279
290
  return this.txBlock.mergeCoins(destinationObject, sourceObjects);
280
291
  }
281
- publish(...args) {
282
- return this.txBlock.publish(...args);
283
- }
284
- upgrade(...args) {
285
- return this.txBlock.upgrade(...args);
286
- }
287
- makeMoveVec(...args) {
288
- return this.txBlock.makeMoveVec(...args);
289
- }
290
292
  /**
291
293
  * @description Move call
292
294
  * @param target `${string}::${string}::${string}`, e.g. `0x3::sui_system::request_add_stake`
@@ -308,7 +310,7 @@ var SuiTxBlock = class {
308
310
  typeArguments: typeArgs
309
311
  });
310
312
  }
311
- //======== enhance methods ============
313
+ /* Enhance methods of TransactionBlock */
312
314
  transferSuiToMany(recipients, amounts) {
313
315
  if (recipients.length !== amounts.length) {
314
316
  throw new Error(
@@ -384,7 +386,7 @@ var SuiTxBlock = class {
384
386
  tx.moveCall({
385
387
  target: "0x3::sui_system::request_add_stake",
386
388
  arguments: [
387
- tx.object(import_sui5.SUI_SYSTEM_STATE_OBJECT_ID),
389
+ tx.object(import_utils3.SUI_SYSTEM_STATE_OBJECT_ID),
388
390
  stakeCoin,
389
391
  tx.pure(validatorAddr)
390
392
  ]
@@ -394,10 +396,9 @@ var SuiTxBlock = class {
394
396
  };
395
397
 
396
398
  // src/libs/suiInteractor/suiInteractor.ts
397
- var import_sui7 = require("@mysten/sui.js");
399
+ var import_client = require("@mysten/sui.js/client");
398
400
 
399
401
  // src/libs/suiModel/suiOwnedObject.ts
400
- var import_sui6 = require("@mysten/sui.js");
401
402
  var SuiOwnedObject = class {
402
403
  constructor(param) {
403
404
  this.objectId = param.objectId;
@@ -431,7 +432,7 @@ var SuiOwnedObject = class {
431
432
  * @param txResponse
432
433
  */
433
434
  updateFromTxResponse(txResponse) {
434
- const changes = (0, import_sui6.getObjectChanges)(txResponse);
435
+ const changes = txResponse.objectChanges;
435
436
  if (!changes) {
436
437
  throw new Error("Bad transaction response!");
437
438
  }
@@ -476,14 +477,15 @@ var SuiInteractor = class {
476
477
  constructor(fullNodeUrls) {
477
478
  if (fullNodeUrls.length === 0)
478
479
  throw new Error("fullNodeUrls must not be empty");
479
- this.providers = fullNodeUrls.map(
480
- (url) => new import_sui7.JsonRpcProvider(new import_sui7.Connection({ fullnode: url }))
481
- );
482
- this.currentProvider = this.providers[0];
480
+ this.fullNodes = fullNodeUrls;
481
+ this.clients = fullNodeUrls.map((url) => new import_client.SuiClient({ url }));
482
+ this.currentFullNode = fullNodeUrls[0];
483
+ this.currentClient = this.clients[0];
483
484
  }
484
- switchToNextProvider() {
485
- const currentProviderIdx = this.providers.indexOf(this.currentProvider);
486
- this.currentProvider = this.providers[(currentProviderIdx + 1) % this.providers.length];
485
+ switchToNextClient() {
486
+ const currentClientIdx = this.clients.indexOf(this.currentClient);
487
+ this.currentClient = this.clients[(currentClientIdx + 1) % this.clients.length];
488
+ this.currentFullNode = this.fullNodes[(currentClientIdx + 1) % this.clients.length];
487
489
  }
488
490
  async sendTx(transactionBlock, signature) {
489
491
  const txResOptions = {
@@ -492,63 +494,50 @@ var SuiInteractor = class {
492
494
  showObjectChanges: true,
493
495
  showBalanceChanges: true
494
496
  };
495
- for (const provider of this.providers) {
497
+ for (const clientIdx in this.clients) {
496
498
  try {
497
- const res = await provider.executeTransactionBlock({
499
+ return await this.clients[clientIdx].executeTransactionBlock({
498
500
  transactionBlock,
499
501
  signature,
500
502
  options: txResOptions
501
503
  });
502
- return res;
503
504
  } catch (err) {
504
505
  console.warn(
505
- `Failed to send transaction with fullnode ${provider.connection.fullnode}: ${err}`
506
+ `Failed to send transaction with fullnode ${this.fullNodes[clientIdx]}: ${err}`
506
507
  );
507
508
  await delay(2e3);
508
509
  }
509
510
  }
510
511
  throw new Error("Failed to send transaction with all fullnodes");
511
512
  }
512
- async getObjects(ids) {
513
- const options = {
513
+ async getObjects(ids, options) {
514
+ const opts = options ?? {
514
515
  showContent: true,
515
516
  showDisplay: true,
516
517
  showType: true,
517
518
  showOwner: true
518
519
  };
519
- for (const provider of this.providers) {
520
+ for (const clientIdx in this.clients) {
520
521
  try {
521
- const objects = await provider.multiGetObjects({ ids, options });
522
- const parsedObjects = objects.map((object) => {
523
- const objectId = (0, import_sui7.getObjectId)(object);
524
- const objectType = (0, import_sui7.getObjectType)(object);
525
- const objectVersion = (0, import_sui7.getObjectVersion)(object);
526
- const objectDigest = object.data ? object.data.digest : void 0;
527
- const initialSharedVersion = (0, import_sui7.getSharedObjectInitialVersion)(object);
528
- const objectFields = (0, import_sui7.getObjectFields)(object);
529
- const objectDisplay = (0, import_sui7.getObjectDisplay)(object);
530
- return {
531
- objectId,
532
- objectType,
533
- objectVersion,
534
- objectDigest,
535
- objectFields,
536
- objectDisplay,
537
- initialSharedVersion
538
- };
522
+ const objects = await this.clients[clientIdx].multiGetObjects({
523
+ ids,
524
+ options: opts
539
525
  });
526
+ const parsedObjects = objects.map((object) => {
527
+ return object.data;
528
+ }).filter((object) => object !== null && object !== void 0);
540
529
  return parsedObjects;
541
530
  } catch (err) {
542
531
  await delay(2e3);
543
532
  console.warn(
544
- `Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
533
+ `Failed to get objects with fullnode ${this.fullNodes[clientIdx]}: ${err}`
545
534
  );
546
535
  }
547
536
  }
548
537
  throw new Error("Failed to get objects with all fullnodes");
549
538
  }
550
- async getObject(id) {
551
- const objects = await this.getObjects([id]);
539
+ async getObject(id, options) {
540
+ const objects = await this.getObjects([id], options);
552
541
  return objects[0];
553
542
  }
554
543
  /**
@@ -560,13 +549,17 @@ var SuiInteractor = class {
560
549
  const objects = await this.getObjects(objectIds);
561
550
  for (const object of objects) {
562
551
  const suiObject = suiObjects.find(
563
- (obj) => obj.objectId === object.objectId
552
+ (obj) => obj.objectId === object?.objectId
564
553
  );
565
554
  if (suiObject instanceof SuiSharedObject) {
566
- suiObject.initialSharedVersion = object.initialSharedVersion;
555
+ if (object.owner && typeof object.owner === "object" && "Shared" in object.owner) {
556
+ suiObject.initialSharedVersion = object.owner.Shared.initial_shared_version;
557
+ } else {
558
+ suiObject.initialSharedVersion = void 0;
559
+ }
567
560
  } else if (suiObject instanceof SuiOwnedObject) {
568
- suiObject.version = object.objectVersion;
569
- suiObject.digest = object.objectDigest;
561
+ suiObject.version = object?.version;
562
+ suiObject.digest = object?.digest;
570
563
  }
571
564
  }
572
565
  }
@@ -581,7 +574,7 @@ var SuiInteractor = class {
581
574
  let totalAmount = 0;
582
575
  let hasNext = true, nextCursor = null;
583
576
  while (hasNext && totalAmount < amount) {
584
- const coins = await this.currentProvider.getCoins({
577
+ const coins = await this.currentClient.getCoins({
585
578
  owner: addr,
586
579
  coinType,
587
580
  cursor: nextCursor
@@ -608,24 +601,6 @@ var SuiInteractor = class {
608
601
  }
609
602
  };
610
603
 
611
- // src/libs/suiInteractor/defaultConfig.ts
612
- var import_sui8 = require("@mysten/sui.js");
613
- var defaultGasBudget = 10 ** 8;
614
- var getDefaultConnection = (networkType = "devnet") => {
615
- switch (networkType) {
616
- case "localnet":
617
- return import_sui8.localnetConnection;
618
- case "devnet":
619
- return import_sui8.devnetConnection;
620
- case "testnet":
621
- return import_sui8.testnetConnection;
622
- case "mainnet":
623
- return import_sui8.mainnetConnection;
624
- default:
625
- return import_sui8.devnetConnection;
626
- }
627
- };
628
-
629
604
  // src/suiKit.ts
630
605
  var SuiKit = class {
631
606
  /**
@@ -636,7 +611,7 @@ var SuiKit = class {
636
611
  *
637
612
  * @param mnemonics, 12 or 24 mnemonics words, separated by space
638
613
  * @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
639
- * @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
614
+ * @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'mainnet'
640
615
  * @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
641
616
  */
642
617
  constructor({
@@ -646,18 +621,17 @@ var SuiKit = class {
646
621
  fullnodeUrls
647
622
  } = {}) {
648
623
  this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
649
- fullnodeUrls = fullnodeUrls || [getDefaultConnection(networkType).fullnode];
624
+ fullnodeUrls = fullnodeUrls || [(0, import_client2.getFullnodeUrl)(networkType ?? "mainnet")];
650
625
  this.suiInteractor = new SuiInteractor(fullnodeUrls);
651
626
  }
652
627
  /**
653
- * if derivePathParams is not provided or mnemonics is empty, it will return the currentSigner.
628
+ * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
654
629
  * else:
655
630
  * it will generate signer from the mnemonic with the given derivePathParams.
656
631
  * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
657
632
  */
658
- getSigner(derivePathParams) {
659
- const keyPair = this.accountManager.getKeyPair(derivePathParams);
660
- return new import_sui9.RawSigner(keyPair, this.suiInteractor.currentProvider);
633
+ getKeypair(derivePathParams) {
634
+ return this.accountManager.getKeyPair(derivePathParams);
661
635
  }
662
636
  /**
663
637
  * @description Switch the current account with the given derivePathParams
@@ -676,15 +650,15 @@ var SuiKit = class {
676
650
  currentAddress() {
677
651
  return this.accountManager.currentAddress;
678
652
  }
679
- provider() {
680
- return this.suiInteractor.currentProvider;
681
- }
682
653
  async getBalance(coinType, derivePathParams) {
683
654
  const owner = this.accountManager.getAddress(derivePathParams);
684
- return this.suiInteractor.currentProvider.getBalance({ owner, coinType });
655
+ return this.suiInteractor.currentClient.getBalance({ owner, coinType });
656
+ }
657
+ client() {
658
+ return this.suiInteractor.currentClient;
685
659
  }
686
- async getObjects(objectIds) {
687
- return this.suiInteractor.getObjects(objectIds);
660
+ async getObjects(objectIds, options) {
661
+ return this.suiInteractor.getObjects(objectIds, options);
688
662
  }
689
663
  /**
690
664
  * @description Update objects in a batch
@@ -694,16 +668,14 @@ var SuiKit = class {
694
668
  return this.suiInteractor.updateObjects(suiObjects);
695
669
  }
696
670
  async signTxn(tx, derivePathParams) {
697
- tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
698
- const signer = this.getSigner(derivePathParams);
699
- return signer.signTransactionBlock({ transactionBlock: tx });
671
+ const txBlock = tx instanceof SuiTxBlock ? tx.txBlock : tx;
672
+ const txBytes = txBlock instanceof import_transactions2.TransactionBlock ? await txBlock.build({ client: this.client() }) : txBlock;
673
+ const keyPair = this.getKeypair(derivePathParams);
674
+ return await keyPair.signTransactionBlock(txBytes);
700
675
  }
701
676
  async signAndSendTxn(tx, derivePathParams) {
702
- const { transactionBlockBytes, signature } = await this.signTxn(
703
- tx,
704
- derivePathParams
705
- );
706
- return this.suiInteractor.sendTx(transactionBlockBytes, signature);
677
+ const { bytes, signature } = await this.signTxn(tx, derivePathParams);
678
+ return this.suiInteractor.sendTx(bytes, signature);
707
679
  }
708
680
  /**
709
681
  * Transfer the given amount of SUI to the recipient
@@ -805,20 +777,19 @@ var SuiKit = class {
805
777
  * @returns the effects and events of the transaction, such as object changes, gas cost, event emitted.
806
778
  */
807
779
  async inspectTxn(tx, derivePathParams) {
808
- tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
809
- return this.suiInteractor.currentProvider.devInspectTransactionBlock({
810
- transactionBlock: tx,
780
+ const txBlock = tx instanceof SuiTxBlock ? tx.txBlock : tx;
781
+ return this.suiInteractor.currentClient.devInspectTransactionBlock({
782
+ transactionBlock: txBlock,
811
783
  sender: this.getAddress(derivePathParams)
812
784
  });
813
785
  }
814
786
  };
815
787
  // Annotate the CommonJS export names for ESM import in node:
816
788
  0 && (module.exports = {
817
- SUI_CLOCK_OBJECT_ID,
818
- SUI_SYSTEM_STATE_OBJECT_ID,
819
789
  SuiAccountManager,
820
790
  SuiKit,
821
791
  SuiTxBlock,
822
- TransactionBlock
792
+ ...require("@mysten/sui.js/utils"),
793
+ ...require("@mysten/sui.js/transactions")
823
794
  });
824
795
  //# sourceMappingURL=index.js.map