@0xobelisk/sui-client 1.0.3 → 1.0.4

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> = {};
@@ -977,7 +1048,7 @@ function createTx(meta, fn) {
977
1048
  }
978
1049
  );
979
1050
  }
980
- var _query, _tx, _object, _exec, _read, _bcs, _processKeyParameter, processKeyParameter_fn;
1051
+ var _query, _tx, _object, _exec, _read, _getVectorDepth, _bcs, _bcsenum, _processKeyParameter, processKeyParameter_fn;
981
1052
  var Dubhe = class {
982
1053
  /**
983
1054
  * Support the following ways to init the DubheClient:
@@ -1120,6 +1191,12 @@ var Dubhe = class {
1120
1191
  });
1121
1192
  return await this.inspectTxn(tx);
1122
1193
  });
1194
+ __privateAdd(this, _getVectorDepth, (field) => {
1195
+ if (typeof field === "object" && "Vector" in field) {
1196
+ return 1 + __privateGet(this, _getVectorDepth).call(this, field.Vector);
1197
+ }
1198
+ return 0;
1199
+ });
1123
1200
  __privateAdd(this, _bcs, (bcsmeta) => {
1124
1201
  let loopFlag = false;
1125
1202
  const bcsJson = {};
@@ -1197,39 +1274,64 @@ var Dubhe = class {
1197
1274
  }
1198
1275
  return;
1199
1276
  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
- );
1277
+ if (typeof value === "string") {
1278
+ switch (value) {
1279
+ case "U8":
1280
+ bcsJson[objName] = bcs2.vector(bcs2.u8());
1281
+ return;
1282
+ case "U16":
1283
+ bcsJson[objName] = bcs2.vector(bcs2.u16());
1284
+ return;
1285
+ case "U32":
1286
+ bcsJson[objName] = bcs2.vector(bcs2.u32());
1287
+ return;
1288
+ case "U64":
1289
+ bcsJson[objName] = bcs2.vector(bcs2.u64());
1290
+ return;
1291
+ case "U128":
1292
+ bcsJson[objName] = bcs2.vector(bcs2.u128());
1293
+ return;
1294
+ case "U256":
1295
+ bcsJson[objName] = bcs2.vector(bcs2.u256());
1296
+ return;
1297
+ case "Bool":
1298
+ bcsJson[objName] = bcs2.vector(bcs2.bool());
1299
+ return;
1300
+ case "Address":
1301
+ bcsJson[objName] = bcs2.vector(
1302
+ bcs2.bytes(32).transform({
1303
+ // To change the input type, you need to provide a type definition for the input
1304
+ input: (val) => fromHEX2(val),
1305
+ output: (val) => toHEX(val)
1306
+ })
1307
+ );
1308
+ return;
1309
+ default:
1310
+ }
1311
+ }
1312
+ if (typeof value === "object") {
1313
+ const vectorDepth = __privateGet(this, _getVectorDepth).call(this, value);
1314
+ let innerType = value;
1315
+ for (let i = 0; i < vectorDepth; i++) {
1316
+ innerType = innerType.Vector;
1317
+ }
1318
+ if ("Struct" in innerType) {
1319
+ const structType2 = innerType.Struct;
1320
+ const structId = `${structType2.address}::${structType2.module}::${structType2.name}`;
1321
+ let bcsType = __privateGet(this, _object)[structId];
1322
+ if (!bcsType) {
1323
+ loopFlag = true;
1324
+ return;
1325
+ }
1326
+ let baseType = bcsType;
1327
+ for (let i = 0; i <= vectorDepth; i++) {
1328
+ baseType = bcs2.vector(baseType);
1329
+ }
1330
+ bcsJson[objName] = baseType;
1230
1331
  return;
1231
- default:
1332
+ }
1232
1333
  }
1334
+ return;
1233
1335
  case "TypeParameter":
1234
1336
  bcsJson[objName] = bcs2.u128();
1235
1337
  return;
@@ -1280,6 +1382,174 @@ var Dubhe = class {
1280
1382
  loopFlag
1281
1383
  };
1282
1384
  });
1385
+ __privateAdd(this, _bcsenum, (bcsmeta) => {
1386
+ let loopFlag = false;
1387
+ const variantJson = {};
1388
+ Object.entries(bcsmeta.objectType.variants).forEach(([name, type]) => {
1389
+ if (type.length > 0) {
1390
+ Object.entries(type).forEach(([index, value]) => {
1391
+ const objType = value.type;
1392
+ const objName = value.name;
1393
+ switch (typeof objType) {
1394
+ case "object":
1395
+ for (const [key, value2] of Object.entries(objType)) {
1396
+ switch (key) {
1397
+ case "Struct":
1398
+ const structType = value2;
1399
+ if (structType.address === "0x1" && structType.module === "ascii" && structType.name === "String") {
1400
+ variantJson[objName] = bcs2.string();
1401
+ return;
1402
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "UID") {
1403
+ variantJson[objName] = bcs2.fixedArray(32, bcs2.u8()).transform({
1404
+ input: (id) => fromHEX2(id),
1405
+ output: (id) => toHEX(Uint8Array.from(id))
1406
+ });
1407
+ return;
1408
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "ID") {
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 === "bag" && structType.name === "Bag") {
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 === "0x1" && structType.module === "option" && structType.name === "Option") {
1421
+ switch (structType.typeArguments[0]) {
1422
+ case "U8":
1423
+ variantJson[objName] = bcs2.option(bcs2.u8());
1424
+ return;
1425
+ case "U16":
1426
+ variantJson[objName] = bcs2.option(bcs2.u16());
1427
+ return;
1428
+ case "U32":
1429
+ variantJson[objName] = bcs2.option(bcs2.u32());
1430
+ return;
1431
+ case "U64":
1432
+ variantJson[objName] = bcs2.option(bcs2.u64());
1433
+ return;
1434
+ case "U128":
1435
+ variantJson[objName] = bcs2.option(bcs2.u128());
1436
+ return;
1437
+ case "U256":
1438
+ variantJson[objName] = bcs2.option(bcs2.u256());
1439
+ return;
1440
+ case "Bool":
1441
+ variantJson[objName] = bcs2.option(bcs2.bool());
1442
+ return;
1443
+ case "Address":
1444
+ variantJson[objName] = bcs2.option(
1445
+ bcs2.bytes(32).transform({
1446
+ // To change the input type, you need to provide a type definition for the input
1447
+ input: (val) => fromHEX2(val),
1448
+ output: (val) => toHEX(val)
1449
+ })
1450
+ );
1451
+ return;
1452
+ default:
1453
+ }
1454
+ } else {
1455
+ if (this.object[`${structType.address}::${structType.module}::${structType.name}`] === void 0) {
1456
+ loopFlag = true;
1457
+ } else {
1458
+ variantJson[objName] = this.object[`${structType.address}::${structType.module}::${structType.name}`];
1459
+ return;
1460
+ }
1461
+ }
1462
+ return;
1463
+ case "Vector":
1464
+ if (typeof value2 === "string") {
1465
+ switch (value2) {
1466
+ case "U8":
1467
+ variantJson[objName] = bcs2.vector(bcs2.u8());
1468
+ return;
1469
+ case "U16":
1470
+ variantJson[objName] = bcs2.vector(bcs2.u16());
1471
+ return;
1472
+ case "U32":
1473
+ variantJson[objName] = bcs2.vector(bcs2.u32());
1474
+ return;
1475
+ case "U64":
1476
+ variantJson[objName] = bcs2.vector(bcs2.u64());
1477
+ return;
1478
+ case "U128":
1479
+ variantJson[objName] = bcs2.vector(bcs2.u128());
1480
+ return;
1481
+ case "U256":
1482
+ variantJson[objName] = bcs2.vector(bcs2.u256());
1483
+ return;
1484
+ case "Bool":
1485
+ variantJson[objName] = bcs2.vector(bcs2.bool());
1486
+ return;
1487
+ case "Address":
1488
+ variantJson[objName] = bcs2.vector(
1489
+ bcs2.bytes(32).transform({
1490
+ // To change the input type, you need to provide a type definition for the input
1491
+ input: (val) => fromHEX2(val),
1492
+ output: (val) => toHEX(val)
1493
+ })
1494
+ );
1495
+ return;
1496
+ default:
1497
+ }
1498
+ }
1499
+ case "TypeParameter":
1500
+ variantJson[objName] = bcs2.u128();
1501
+ return;
1502
+ default:
1503
+ throw new Error("Unsupported type");
1504
+ }
1505
+ }
1506
+ return;
1507
+ case "string":
1508
+ switch (objType) {
1509
+ case "U8":
1510
+ variantJson[objName] = bcs2.u8();
1511
+ return;
1512
+ case "U16":
1513
+ variantJson[objName] = bcs2.u16();
1514
+ return;
1515
+ case "U32":
1516
+ variantJson[objName] = bcs2.u32();
1517
+ return;
1518
+ case "U64":
1519
+ variantJson[objName] = bcs2.u64();
1520
+ return;
1521
+ case "U128":
1522
+ variantJson[objName] = bcs2.u128();
1523
+ return;
1524
+ case "U256":
1525
+ variantJson[objName] = bcs2.u256();
1526
+ return;
1527
+ case "Bool":
1528
+ variantJson[objName] = bcs2.bool();
1529
+ return;
1530
+ case "Address":
1531
+ variantJson[objName] = bcs2.bytes(32).transform({
1532
+ // To change the input type, you need to provide a type definition for the input
1533
+ input: (val) => fromHEX2(val),
1534
+ output: (val) => toHEX(val)
1535
+ });
1536
+ return;
1537
+ default:
1538
+ return;
1539
+ }
1540
+ default:
1541
+ throw new Error("Unsupported type");
1542
+ }
1543
+ });
1544
+ } else {
1545
+ variantJson[name] = null;
1546
+ }
1547
+ });
1548
+ return {
1549
+ bcs: bcs2.enum(bcsmeta.objectName, variantJson),
1550
+ loopFlag
1551
+ };
1552
+ });
1283
1553
  this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
1284
1554
  fullnodeUrls = fullnodeUrls || [getFullnodeUrl(networkType ?? "mainnet")];
1285
1555
  this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
@@ -1296,6 +1566,30 @@ var Dubhe = class {
1296
1566
  const data = moudlevalue;
1297
1567
  const moduleName = data.name;
1298
1568
  const objMoudleId = `${packageId}::${moduleName}`;
1569
+ if (data.enums) {
1570
+ Object.entries(data.enums).forEach(([enumName, enumType]) => {
1571
+ const objectId = `${objMoudleId}::${enumName}`;
1572
+ const bcsmeta = {
1573
+ objectId,
1574
+ objectName: enumName,
1575
+ objectType: enumType
1576
+ };
1577
+ let bcsObj = __privateGet(this, _bcsenum).call(this, bcsmeta);
1578
+ if (bcsObj.loopFlag === true) {
1579
+ loopFlag = bcsObj.loopFlag;
1580
+ }
1581
+ if (__privateGet(this, _object)[objectId] === void 0) {
1582
+ __privateGet(this, _object)[objectId] = bcsObj.bcs;
1583
+ __privateGet(this, _object)[`vector<${objectId}>`] = bcs2.vector(bcsObj.bcs);
1584
+ __privateGet(this, _object)[`vector<vector<${objectId}>>`] = bcs2.vector(
1585
+ bcs2.vector(bcsObj.bcs)
1586
+ );
1587
+ __privateGet(this, _object)[`0x1::option::Option<${objectId}>`] = bcs2.option(
1588
+ bcsObj.bcs
1589
+ );
1590
+ }
1591
+ });
1592
+ }
1299
1593
  Object.entries(data.structs).forEach(([objectName, objectType]) => {
1300
1594
  const objectId = `${objMoudleId}::${objectName}`;
1301
1595
  const bcsmeta = {
@@ -1469,15 +1763,33 @@ var Dubhe = class {
1469
1763
  }
1470
1764
  }
1471
1765
  async state({
1766
+ tx,
1767
+ schema,
1768
+ params
1769
+ }) {
1770
+ const moduleName = `schema`;
1771
+ const functionName = `get_${schema}`;
1772
+ let queryResponse = void 0;
1773
+ try {
1774
+ queryResponse = await this.query[moduleName][functionName]({
1775
+ tx,
1776
+ params
1777
+ });
1778
+ if (queryResponse.effects.status.status !== "success") {
1779
+ return void 0;
1780
+ }
1781
+ } catch {
1782
+ return void 0;
1783
+ }
1784
+ return this.view(queryResponse);
1785
+ }
1786
+ async parseState({
1472
1787
  schema,
1473
- field,
1474
1788
  objectId,
1475
1789
  storageType,
1476
1790
  params
1477
1791
  }) {
1478
1792
  const tx = new Transaction2();
1479
- const moduleName = `${schema}_schema`;
1480
- const functionName = `get_${field}`;
1481
1793
  const schemaObject = tx.object(objectId);
1482
1794
  const storageValueMatch = storageType.match(/^StorageValue<(.+)>$/);
1483
1795
  const storageMapMatch = storageType.match(/^StorageMap<(.+),\s*(.+)>$/);
@@ -1512,19 +1824,18 @@ var Dubhe = class {
1512
1824
  `Invalid storage type: ${storageType}. Must be StorageValue<V>, StorageMap<K,V>, or StorageDoubleMap<K1,K2,V>`
1513
1825
  );
1514
1826
  }
1515
- const queryResponse = await this.query[moduleName][functionName]({
1827
+ return this.state({
1516
1828
  tx,
1829
+ schema,
1517
1830
  params: processedParams
1518
1831
  });
1519
- return this.view(queryResponse);
1520
1832
  }
1521
1833
  /**
1522
- * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
1523
1834
  * else:
1524
1835
  * it will generate signer from the mnemonic with the given derivePathParams.
1525
1836
  * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
1526
1837
  */
1527
- getKeypair(derivePathParams) {
1838
+ getSigner(derivePathParams) {
1528
1839
  return this.accountManager.getKeyPair(derivePathParams);
1529
1840
  }
1530
1841
  /**
@@ -1553,6 +1864,18 @@ var Dubhe = class {
1553
1864
  getNetwork() {
1554
1865
  return this.suiInteractor.network;
1555
1866
  }
1867
+ getNetworkConfig() {
1868
+ return getDefaultURL(this.getNetwork());
1869
+ }
1870
+ getTxExplorerUrl(txHash) {
1871
+ return this.getNetworkConfig().txExplorer.replace(":txHash", txHash);
1872
+ }
1873
+ getAccountExplorerUrl(address) {
1874
+ return this.getNetworkConfig().accountExplorer.replace(":address", address);
1875
+ }
1876
+ getExplorerUrl() {
1877
+ return this.getNetworkConfig().explorer;
1878
+ }
1556
1879
  /**
1557
1880
  * Request some SUI from faucet
1558
1881
  * @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
@@ -1592,15 +1915,18 @@ var Dubhe = class {
1592
1915
  }
1593
1916
  const txBlock = tx instanceof SuiTx ? tx.tx : tx;
1594
1917
  const txBytes = txBlock instanceof Transaction2 ? await txBlock.build({ client: this.client() }) : txBlock;
1595
- const keyPair = this.getKeypair(derivePathParams);
1918
+ const keyPair = this.getSigner(derivePathParams);
1596
1919
  return await keyPair.signTransaction(txBytes);
1597
1920
  }
1598
1921
  async signAndSendTxn(tx, derivePathParams) {
1599
1922
  const { bytes, signature } = await this.signTxn(tx, derivePathParams);
1600
- return this.suiInteractor.sendTx(bytes, signature);
1923
+ return this.sendTx(bytes, signature);
1924
+ }
1925
+ async sendTx(transaction, signature) {
1926
+ return this.suiInteractor.sendTx(transaction, signature);
1601
1927
  }
1602
- async sendTxn(transactionBlock, signature) {
1603
- return this.suiInteractor.sendTx(transactionBlock, signature);
1928
+ async waitForTransaction(digest) {
1929
+ return this.suiInteractor.waitForTransaction({ digest });
1604
1930
  }
1605
1931
  /**
1606
1932
  * Transfer the given amount of SUI to the recipient
@@ -1784,7 +2110,9 @@ _tx = new WeakMap();
1784
2110
  _object = new WeakMap();
1785
2111
  _exec = new WeakMap();
1786
2112
  _read = new WeakMap();
2113
+ _getVectorDepth = new WeakMap();
1787
2114
  _bcs = new WeakMap();
2115
+ _bcsenum = new WeakMap();
1788
2116
  _processKeyParameter = new WeakSet();
1789
2117
  processKeyParameter_fn = function(tx, keyType, value) {
1790
2118
  switch (keyType.toLowerCase()) {