@0xobelisk/sui-client 1.0.3 → 1.0.5

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.mjs CHANGED
@@ -608,6 +608,27 @@ var SuiInteractor = class {
608
608
  }
609
609
  throw new Error("Failed to send transaction with all fullnodes");
610
610
  }
611
+ async waitForTransaction({
612
+ digest,
613
+ timeout = 60 * 1e3,
614
+ pollInterval = 2 * 1e3
615
+ }) {
616
+ for (const clientIdx in this.clients) {
617
+ try {
618
+ return await this.clients[clientIdx].waitForTransaction({
619
+ digest,
620
+ timeout,
621
+ pollInterval
622
+ });
623
+ } catch (err) {
624
+ console.warn(
625
+ `Failed to wait for transaction with fullnode ${this.fullNodes[clientIdx]}: ${err}`
626
+ );
627
+ await delay(2e3);
628
+ }
629
+ }
630
+ throw new Error("Failed to wait for transaction with all fullnodes");
631
+ }
611
632
  async getObjects(ids, options) {
612
633
  const opts = options ?? {
613
634
  showContent: true,
@@ -820,6 +841,56 @@ var SuiInteractor = class {
820
841
  }
821
842
  };
822
843
 
844
+ // src/libs/suiInteractor/defaultConfig.ts
845
+ var getDefaultURL = (networkType = "testnet") => {
846
+ switch (networkType) {
847
+ case "localnet":
848
+ return {
849
+ fullNode: "http://127.0.0.1:9000",
850
+ graphql: "http://127.0.0.1:9125",
851
+ network: "localnet",
852
+ txExplorer: "https://explorer.polymedia.app/txblock/:txHash?network=local",
853
+ accountExplorer: "https://explorer.polymedia.app/address/:address?network=local",
854
+ explorer: "https://explorer.polymedia.app?network=local"
855
+ };
856
+ case "devnet":
857
+ return {
858
+ fullNode: "https://fullnode.devnet.sui.io:443",
859
+ network: "devnet",
860
+ txExplorer: "https://suiscan.xyz/devnet/tx/:txHash",
861
+ accountExplorer: "https://suiscan.xyz/devnet/address/:address",
862
+ explorer: "https://suiscan.xyz/devnet"
863
+ };
864
+ case "testnet":
865
+ return {
866
+ fullNode: "https://fullnode.testnet.sui.io:443",
867
+ graphql: "https://sui-testnet.mystenlabs.com/graphql",
868
+ network: "testnet",
869
+ txExplorer: "https://suiscan.xyz/testnet/tx/:txHash",
870
+ accountExplorer: "https://suiscan.xyz/testnet/address/:address",
871
+ explorer: "https://suiscan.xyz/testnet"
872
+ };
873
+ case "mainnet":
874
+ return {
875
+ fullNode: "https://fullnode.mainnet.sui.io:443",
876
+ graphql: "https://sui-mainnet.mystenlabs.com/graphql",
877
+ network: "mainnet",
878
+ txExplorer: "https://suiscan.xyz/mainnet/tx/:txHash",
879
+ accountExplorer: "https://suiscan.xyz/mainnet/address/:address",
880
+ explorer: "https://suiscan.xyz/mainnet"
881
+ };
882
+ default:
883
+ return {
884
+ fullNode: "https://fullnode.testnet.sui.io:443",
885
+ graphql: "https://sui-testnet.mystenlabs.com/graphql",
886
+ network: "testnet",
887
+ txExplorer: "https://suiscan.xyz/testnet/tx/:txHash",
888
+ accountExplorer: "https://suiscan.xyz/testnet/address/:address",
889
+ explorer: "https://suiscan.xyz/testnet"
890
+ };
891
+ }
892
+ };
893
+
823
894
  // src/libs/suiContractFactory/index.ts
824
895
  var SuiContractFactory = class {
825
896
  // readonly #query: MapMessageQuery<ApiTypes> = {};
@@ -915,6 +986,12 @@ function numberToAddressHex(num) {
915
986
  const paddedHex = "0x" + hex.padStart(64, "0");
916
987
  return paddedHex;
917
988
  }
989
+ function normalizePackageId(input) {
990
+ const withPrefix = input.startsWith("0x") ? input : "0x" + input;
991
+ const withoutPrefix = withPrefix.slice(2);
992
+ const normalized = withoutPrefix.replace(/^0+/, "");
993
+ return "0x" + normalized;
994
+ }
918
995
 
919
996
  // src/dubhe.ts
920
997
  import { bcs as bcs2, fromHEX as fromHEX2, toHEX } from "@mysten/bcs";
@@ -977,7 +1054,7 @@ function createTx(meta, fn) {
977
1054
  }
978
1055
  );
979
1056
  }
980
- var _query, _tx, _object, _exec, _read, _bcs, _processKeyParameter, processKeyParameter_fn;
1057
+ var _query, _tx, _object, _exec, _read, _getVectorDepth, _bcs, _bcsenum, _processKeyParameter, processKeyParameter_fn;
981
1058
  var Dubhe = class {
982
1059
  /**
983
1060
  * Support the following ways to init the DubheClient:
@@ -1120,6 +1197,12 @@ var Dubhe = class {
1120
1197
  });
1121
1198
  return await this.inspectTxn(tx);
1122
1199
  });
1200
+ __privateAdd(this, _getVectorDepth, (field) => {
1201
+ if (typeof field === "object" && "Vector" in field) {
1202
+ return 1 + __privateGet(this, _getVectorDepth).call(this, field.Vector);
1203
+ }
1204
+ return 0;
1205
+ });
1123
1206
  __privateAdd(this, _bcs, (bcsmeta) => {
1124
1207
  let loopFlag = false;
1125
1208
  const bcsJson = {};
@@ -1197,39 +1280,64 @@ var Dubhe = class {
1197
1280
  }
1198
1281
  return;
1199
1282
  case "Vector":
1200
- switch (value) {
1201
- case "U8":
1202
- bcsJson[objName] = bcs2.vector(bcs2.u8());
1203
- return;
1204
- case "U16":
1205
- bcsJson[objName] = bcs2.vector(bcs2.u16());
1206
- return;
1207
- case "U32":
1208
- bcsJson[objName] = bcs2.vector(bcs2.u32());
1209
- return;
1210
- case "U64":
1211
- bcsJson[objName] = bcs2.vector(bcs2.u64());
1212
- return;
1213
- case "U128":
1214
- bcsJson[objName] = bcs2.vector(bcs2.u128());
1215
- return;
1216
- case "U256":
1217
- bcsJson[objName] = bcs2.vector(bcs2.u256());
1218
- return;
1219
- case "Bool":
1220
- bcsJson[objName] = bcs2.vector(bcs2.bool());
1221
- return;
1222
- case "Address":
1223
- bcsJson[objName] = bcs2.vector(
1224
- bcs2.bytes(32).transform({
1225
- // To change the input type, you need to provide a type definition for the input
1226
- input: (val) => fromHEX2(val),
1227
- output: (val) => toHEX(val)
1228
- })
1229
- );
1283
+ if (typeof value === "string") {
1284
+ switch (value) {
1285
+ case "U8":
1286
+ bcsJson[objName] = bcs2.vector(bcs2.u8());
1287
+ return;
1288
+ case "U16":
1289
+ bcsJson[objName] = bcs2.vector(bcs2.u16());
1290
+ return;
1291
+ case "U32":
1292
+ bcsJson[objName] = bcs2.vector(bcs2.u32());
1293
+ return;
1294
+ case "U64":
1295
+ bcsJson[objName] = bcs2.vector(bcs2.u64());
1296
+ return;
1297
+ case "U128":
1298
+ bcsJson[objName] = bcs2.vector(bcs2.u128());
1299
+ return;
1300
+ case "U256":
1301
+ bcsJson[objName] = bcs2.vector(bcs2.u256());
1302
+ return;
1303
+ case "Bool":
1304
+ bcsJson[objName] = bcs2.vector(bcs2.bool());
1305
+ return;
1306
+ case "Address":
1307
+ bcsJson[objName] = bcs2.vector(
1308
+ bcs2.bytes(32).transform({
1309
+ // To change the input type, you need to provide a type definition for the input
1310
+ input: (val) => fromHEX2(val),
1311
+ output: (val) => toHEX(val)
1312
+ })
1313
+ );
1314
+ return;
1315
+ default:
1316
+ }
1317
+ }
1318
+ if (typeof value === "object") {
1319
+ const vectorDepth = __privateGet(this, _getVectorDepth).call(this, value);
1320
+ let innerType = value;
1321
+ for (let i = 0; i < vectorDepth; i++) {
1322
+ innerType = innerType.Vector;
1323
+ }
1324
+ if ("Struct" in innerType) {
1325
+ const structType2 = innerType.Struct;
1326
+ const structId = `${structType2.address}::${structType2.module}::${structType2.name}`;
1327
+ let bcsType = __privateGet(this, _object)[structId];
1328
+ if (!bcsType) {
1329
+ loopFlag = true;
1330
+ return;
1331
+ }
1332
+ let baseType = bcsType;
1333
+ for (let i = 0; i <= vectorDepth; i++) {
1334
+ baseType = bcs2.vector(baseType);
1335
+ }
1336
+ bcsJson[objName] = baseType;
1230
1337
  return;
1231
- default:
1338
+ }
1232
1339
  }
1340
+ return;
1233
1341
  case "TypeParameter":
1234
1342
  bcsJson[objName] = bcs2.u128();
1235
1343
  return;
@@ -1280,10 +1388,178 @@ var Dubhe = class {
1280
1388
  loopFlag
1281
1389
  };
1282
1390
  });
1391
+ __privateAdd(this, _bcsenum, (bcsmeta) => {
1392
+ let loopFlag = false;
1393
+ const variantJson = {};
1394
+ Object.entries(bcsmeta.objectType.variants).forEach(([name, type]) => {
1395
+ if (type.length > 0) {
1396
+ Object.entries(type).forEach(([index, value]) => {
1397
+ const objType = value.type;
1398
+ const objName = value.name;
1399
+ switch (typeof objType) {
1400
+ case "object":
1401
+ for (const [key, value2] of Object.entries(objType)) {
1402
+ switch (key) {
1403
+ case "Struct":
1404
+ const structType = value2;
1405
+ if (structType.address === "0x1" && structType.module === "ascii" && structType.name === "String") {
1406
+ variantJson[objName] = bcs2.string();
1407
+ return;
1408
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "UID") {
1409
+ variantJson[objName] = bcs2.fixedArray(32, bcs2.u8()).transform({
1410
+ input: (id) => fromHEX2(id),
1411
+ output: (id) => toHEX(Uint8Array.from(id))
1412
+ });
1413
+ return;
1414
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "ID") {
1415
+ variantJson[objName] = bcs2.fixedArray(32, bcs2.u8()).transform({
1416
+ input: (id) => fromHEX2(id),
1417
+ output: (id) => toHEX(Uint8Array.from(id))
1418
+ });
1419
+ return;
1420
+ } else if (structType.address === "0x2" && structType.module === "bag" && structType.name === "Bag") {
1421
+ variantJson[objName] = bcs2.fixedArray(32, bcs2.u8()).transform({
1422
+ input: (id) => fromHEX2(id),
1423
+ output: (id) => toHEX(Uint8Array.from(id))
1424
+ });
1425
+ return;
1426
+ } else if (structType.address === "0x1" && structType.module === "option" && structType.name === "Option") {
1427
+ switch (structType.typeArguments[0]) {
1428
+ case "U8":
1429
+ variantJson[objName] = bcs2.option(bcs2.u8());
1430
+ return;
1431
+ case "U16":
1432
+ variantJson[objName] = bcs2.option(bcs2.u16());
1433
+ return;
1434
+ case "U32":
1435
+ variantJson[objName] = bcs2.option(bcs2.u32());
1436
+ return;
1437
+ case "U64":
1438
+ variantJson[objName] = bcs2.option(bcs2.u64());
1439
+ return;
1440
+ case "U128":
1441
+ variantJson[objName] = bcs2.option(bcs2.u128());
1442
+ return;
1443
+ case "U256":
1444
+ variantJson[objName] = bcs2.option(bcs2.u256());
1445
+ return;
1446
+ case "Bool":
1447
+ variantJson[objName] = bcs2.option(bcs2.bool());
1448
+ return;
1449
+ case "Address":
1450
+ variantJson[objName] = bcs2.option(
1451
+ bcs2.bytes(32).transform({
1452
+ // To change the input type, you need to provide a type definition for the input
1453
+ input: (val) => fromHEX2(val),
1454
+ output: (val) => toHEX(val)
1455
+ })
1456
+ );
1457
+ return;
1458
+ default:
1459
+ }
1460
+ } else {
1461
+ if (this.object[`${structType.address}::${structType.module}::${structType.name}`] === void 0) {
1462
+ loopFlag = true;
1463
+ } else {
1464
+ variantJson[objName] = this.object[`${structType.address}::${structType.module}::${structType.name}`];
1465
+ return;
1466
+ }
1467
+ }
1468
+ return;
1469
+ case "Vector":
1470
+ if (typeof value2 === "string") {
1471
+ switch (value2) {
1472
+ case "U8":
1473
+ variantJson[objName] = bcs2.vector(bcs2.u8());
1474
+ return;
1475
+ case "U16":
1476
+ variantJson[objName] = bcs2.vector(bcs2.u16());
1477
+ return;
1478
+ case "U32":
1479
+ variantJson[objName] = bcs2.vector(bcs2.u32());
1480
+ return;
1481
+ case "U64":
1482
+ variantJson[objName] = bcs2.vector(bcs2.u64());
1483
+ return;
1484
+ case "U128":
1485
+ variantJson[objName] = bcs2.vector(bcs2.u128());
1486
+ return;
1487
+ case "U256":
1488
+ variantJson[objName] = bcs2.vector(bcs2.u256());
1489
+ return;
1490
+ case "Bool":
1491
+ variantJson[objName] = bcs2.vector(bcs2.bool());
1492
+ return;
1493
+ case "Address":
1494
+ variantJson[objName] = bcs2.vector(
1495
+ bcs2.bytes(32).transform({
1496
+ // To change the input type, you need to provide a type definition for the input
1497
+ input: (val) => fromHEX2(val),
1498
+ output: (val) => toHEX(val)
1499
+ })
1500
+ );
1501
+ return;
1502
+ default:
1503
+ }
1504
+ }
1505
+ case "TypeParameter":
1506
+ variantJson[objName] = bcs2.u128();
1507
+ return;
1508
+ default:
1509
+ throw new Error("Unsupported type");
1510
+ }
1511
+ }
1512
+ return;
1513
+ case "string":
1514
+ switch (objType) {
1515
+ case "U8":
1516
+ variantJson[objName] = bcs2.u8();
1517
+ return;
1518
+ case "U16":
1519
+ variantJson[objName] = bcs2.u16();
1520
+ return;
1521
+ case "U32":
1522
+ variantJson[objName] = bcs2.u32();
1523
+ return;
1524
+ case "U64":
1525
+ variantJson[objName] = bcs2.u64();
1526
+ return;
1527
+ case "U128":
1528
+ variantJson[objName] = bcs2.u128();
1529
+ return;
1530
+ case "U256":
1531
+ variantJson[objName] = bcs2.u256();
1532
+ return;
1533
+ case "Bool":
1534
+ variantJson[objName] = bcs2.bool();
1535
+ return;
1536
+ case "Address":
1537
+ variantJson[objName] = bcs2.bytes(32).transform({
1538
+ // To change the input type, you need to provide a type definition for the input
1539
+ input: (val) => fromHEX2(val),
1540
+ output: (val) => toHEX(val)
1541
+ });
1542
+ return;
1543
+ default:
1544
+ return;
1545
+ }
1546
+ default:
1547
+ throw new Error("Unsupported type");
1548
+ }
1549
+ });
1550
+ } else {
1551
+ variantJson[name] = null;
1552
+ }
1553
+ });
1554
+ return {
1555
+ bcs: bcs2.enum(bcsmeta.objectName, variantJson),
1556
+ loopFlag
1557
+ };
1558
+ });
1283
1559
  this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
1284
1560
  fullnodeUrls = fullnodeUrls || [getFullnodeUrl(networkType ?? "mainnet")];
1285
1561
  this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
1286
- this.packageId = packageId;
1562
+ this.packageId = packageId ? normalizePackageId(packageId) : void 0;
1287
1563
  if (metadata !== void 0) {
1288
1564
  this.metadata = metadata;
1289
1565
  const maxLoopNum = 5;
@@ -1296,6 +1572,30 @@ var Dubhe = class {
1296
1572
  const data = moudlevalue;
1297
1573
  const moduleName = data.name;
1298
1574
  const objMoudleId = `${packageId}::${moduleName}`;
1575
+ if (data.enums) {
1576
+ Object.entries(data.enums).forEach(([enumName, enumType]) => {
1577
+ const objectId = `${objMoudleId}::${enumName}`;
1578
+ const bcsmeta = {
1579
+ objectId,
1580
+ objectName: enumName,
1581
+ objectType: enumType
1582
+ };
1583
+ let bcsObj = __privateGet(this, _bcsenum).call(this, bcsmeta);
1584
+ if (bcsObj.loopFlag === true) {
1585
+ loopFlag = bcsObj.loopFlag;
1586
+ }
1587
+ if (__privateGet(this, _object)[objectId] === void 0) {
1588
+ __privateGet(this, _object)[objectId] = bcsObj.bcs;
1589
+ __privateGet(this, _object)[`vector<${objectId}>`] = bcs2.vector(bcsObj.bcs);
1590
+ __privateGet(this, _object)[`vector<vector<${objectId}>>`] = bcs2.vector(
1591
+ bcs2.vector(bcsObj.bcs)
1592
+ );
1593
+ __privateGet(this, _object)[`0x1::option::Option<${objectId}>`] = bcs2.option(
1594
+ bcsObj.bcs
1595
+ );
1596
+ }
1597
+ });
1598
+ }
1299
1599
  Object.entries(data.structs).forEach(([objectName, objectType]) => {
1300
1600
  const objectId = `${objMoudleId}::${objectName}`;
1301
1601
  const bcsmeta = {
@@ -1469,15 +1769,33 @@ var Dubhe = class {
1469
1769
  }
1470
1770
  }
1471
1771
  async state({
1772
+ tx,
1773
+ schema,
1774
+ params
1775
+ }) {
1776
+ const moduleName = `schema`;
1777
+ const functionName = `get_${schema}`;
1778
+ let queryResponse = void 0;
1779
+ try {
1780
+ queryResponse = await this.query[moduleName][functionName]({
1781
+ tx,
1782
+ params
1783
+ });
1784
+ if (queryResponse.effects.status.status !== "success") {
1785
+ return void 0;
1786
+ }
1787
+ } catch {
1788
+ return void 0;
1789
+ }
1790
+ return this.view(queryResponse);
1791
+ }
1792
+ async parseState({
1472
1793
  schema,
1473
- field,
1474
1794
  objectId,
1475
1795
  storageType,
1476
1796
  params
1477
1797
  }) {
1478
1798
  const tx = new Transaction2();
1479
- const moduleName = `${schema}_schema`;
1480
- const functionName = `get_${field}`;
1481
1799
  const schemaObject = tx.object(objectId);
1482
1800
  const storageValueMatch = storageType.match(/^StorageValue<(.+)>$/);
1483
1801
  const storageMapMatch = storageType.match(/^StorageMap<(.+),\s*(.+)>$/);
@@ -1512,19 +1830,18 @@ var Dubhe = class {
1512
1830
  `Invalid storage type: ${storageType}. Must be StorageValue<V>, StorageMap<K,V>, or StorageDoubleMap<K1,K2,V>`
1513
1831
  );
1514
1832
  }
1515
- const queryResponse = await this.query[moduleName][functionName]({
1833
+ return this.state({
1516
1834
  tx,
1835
+ schema,
1517
1836
  params: processedParams
1518
1837
  });
1519
- return this.view(queryResponse);
1520
1838
  }
1521
1839
  /**
1522
- * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
1523
1840
  * else:
1524
1841
  * it will generate signer from the mnemonic with the given derivePathParams.
1525
1842
  * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
1526
1843
  */
1527
- getKeypair(derivePathParams) {
1844
+ getSigner(derivePathParams) {
1528
1845
  return this.accountManager.getKeyPair(derivePathParams);
1529
1846
  }
1530
1847
  /**
@@ -1553,6 +1870,18 @@ var Dubhe = class {
1553
1870
  getNetwork() {
1554
1871
  return this.suiInteractor.network;
1555
1872
  }
1873
+ getNetworkConfig() {
1874
+ return getDefaultURL(this.getNetwork());
1875
+ }
1876
+ getTxExplorerUrl(txHash) {
1877
+ return this.getNetworkConfig().txExplorer.replace(":txHash", txHash);
1878
+ }
1879
+ getAccountExplorerUrl(address) {
1880
+ return this.getNetworkConfig().accountExplorer.replace(":address", address);
1881
+ }
1882
+ getExplorerUrl() {
1883
+ return this.getNetworkConfig().explorer;
1884
+ }
1556
1885
  /**
1557
1886
  * Request some SUI from faucet
1558
1887
  * @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
@@ -1592,15 +1921,18 @@ var Dubhe = class {
1592
1921
  }
1593
1922
  const txBlock = tx instanceof SuiTx ? tx.tx : tx;
1594
1923
  const txBytes = txBlock instanceof Transaction2 ? await txBlock.build({ client: this.client() }) : txBlock;
1595
- const keyPair = this.getKeypair(derivePathParams);
1924
+ const keyPair = this.getSigner(derivePathParams);
1596
1925
  return await keyPair.signTransaction(txBytes);
1597
1926
  }
1598
1927
  async signAndSendTxn(tx, derivePathParams) {
1599
1928
  const { bytes, signature } = await this.signTxn(tx, derivePathParams);
1600
- return this.suiInteractor.sendTx(bytes, signature);
1929
+ return this.sendTx(bytes, signature);
1930
+ }
1931
+ async sendTx(transaction, signature) {
1932
+ return this.suiInteractor.sendTx(transaction, signature);
1601
1933
  }
1602
- async sendTxn(transactionBlock, signature) {
1603
- return this.suiInteractor.sendTx(transactionBlock, signature);
1934
+ async waitForTransaction(digest) {
1935
+ return this.suiInteractor.waitForTransaction({ digest });
1604
1936
  }
1605
1937
  /**
1606
1938
  * Transfer the given amount of SUI to the recipient
@@ -1784,7 +2116,9 @@ _tx = new WeakMap();
1784
2116
  _object = new WeakMap();
1785
2117
  _exec = new WeakMap();
1786
2118
  _read = new WeakMap();
2119
+ _getVectorDepth = new WeakMap();
1787
2120
  _bcs = new WeakMap();
2121
+ _bcsenum = new WeakMap();
1788
2122
  _processKeyParameter = new WeakSet();
1789
2123
  processKeyParameter_fn = function(tx, keyType, value) {
1790
2124
  switch (keyType.toLowerCase()) {