@easysui/sdk 1.0.1 → 1.2.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.mjs CHANGED
@@ -2,12 +2,13 @@ import path from 'path';
2
2
  import * as fs3 from 'fs';
3
3
  import fs3__default from 'fs';
4
4
  import dotenv from 'dotenv';
5
- import { getJsonRpcFullnodeUrl, SuiJsonRpcClient } from '@mysten/sui/jsonRpc';
5
+ import { getJsonRpcFullnodeUrl } from '@mysten/sui/jsonRpc';
6
6
  import { decodeSuiPrivateKey } from '@mysten/sui/cryptography';
7
7
  import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
8
8
  import { Secp256k1Keypair } from '@mysten/sui/keypairs/secp256k1';
9
9
  import { Secp256r1Keypair } from '@mysten/sui/keypairs/secp256r1';
10
10
  import { SUI_CLOCK_OBJECT_ID, fromHex, fromBase64, toBase64, normalizeStructTag, deriveObjectID, toHex } from '@mysten/sui/utils';
11
+ import { SuiGrpcClient } from '@mysten/sui/grpc';
11
12
  import { Transaction, coinWithBalance } from '@mysten/sui/transactions';
12
13
  import { bcs } from '@mysten/sui/bcs';
13
14
  import { spawn } from 'child_process';
@@ -1540,6 +1541,7 @@ var Config = class _Config {
1540
1541
  NETWORK,
1541
1542
  RPC: getJsonRpcFullnodeUrl(NETWORK),
1542
1543
  PACKAGE_PATH: process.env.PACKAGE_PATH || "",
1544
+ PUBFILE_PATH: process.env.PUBFILE_PATH || "",
1543
1545
  PACKAGE_ID: process.env.PACKAGE_ID || "",
1544
1546
  UPGRADE_CAP_ID: process.env.UPGRADE_CAP_ID || "",
1545
1547
  USDC_TREASURY_CAP: process.env.USDC_TREASURY_CAP,
@@ -1634,16 +1636,11 @@ var MoveType = /* @__PURE__ */ ((MoveType2) => {
1634
1636
  MoveType2[MoveType2["vec_u64"] = 14] = "vec_u64";
1635
1637
  return MoveType2;
1636
1638
  })(MoveType || {});
1637
- var txOptions = {
1638
- showEffects: true,
1639
- showObjectChanges: true,
1640
- showBalanceChanges: true
1641
- };
1642
1639
  var SuiClient = class _SuiClient {
1643
1640
  static instance = null;
1644
1641
  client;
1645
1642
  constructor() {
1646
- this.client = new SuiJsonRpcClient({ url: Config.vars.RPC, network: Config.vars.NETWORK });
1643
+ this.client = new SuiGrpcClient({ baseUrl: Config.vars.RPC, network: Config.vars.NETWORK });
1647
1644
  }
1648
1645
  static getInstance() {
1649
1646
  if (!_SuiClient.instance) {
@@ -1655,20 +1652,31 @@ var SuiClient = class _SuiClient {
1655
1652
  return this.getInstance().client;
1656
1653
  }
1657
1654
  static async waitForTransaction(ptb, resp) {
1658
- await _SuiClient.client.waitForTransaction({ digest: resp.digest });
1659
- if (resp.effects?.status.status !== "success") {
1655
+ await _SuiClient.client.core.waitForTransaction({ digest: resp.digest });
1656
+ if (!resp.status.success) {
1660
1657
  throw new Error(JSON.stringify(resp));
1661
1658
  }
1662
1659
  analyze_cost(ptb, resp);
1663
1660
  return resp;
1664
1661
  }
1665
1662
  static async signAndExecute(ptb, signer) {
1666
- const resp = await _SuiClient.client.signAndExecuteTransaction({
1663
+ const result = await _SuiClient.client.core.signAndExecuteTransaction({
1667
1664
  transaction: ptb,
1668
1665
  signer,
1669
- options: txOptions
1666
+ include: {
1667
+ effects: true,
1668
+ events: true,
1669
+ balanceChanges: true,
1670
+ objectTypes: true,
1671
+ transaction: true,
1672
+ bcs: true
1673
+ }
1670
1674
  });
1671
- return _SuiClient.waitForTransaction(ptb, resp);
1675
+ const tx = result.Transaction ?? result.FailedTransaction;
1676
+ if (!tx) {
1677
+ throw new Error("Transaction failed with no response");
1678
+ }
1679
+ return _SuiClient.waitForTransaction(ptb, tx);
1672
1680
  }
1673
1681
  static toMoveArg(ptb, value, type3) {
1674
1682
  if (typeof value === "object" && !Array.isArray(value)) {
@@ -1759,11 +1767,14 @@ var SuiClient = class _SuiClient {
1759
1767
  gasOwnerSignature = await this.getSignature(gasOwnerSignature, transactionBlock);
1760
1768
  signature.push(gasOwnerSignature);
1761
1769
  }
1762
- const resp = await _SuiClient.client.executeTransactionBlock({
1763
- transactionBlock: toBase64(transactionBlock),
1764
- signature,
1765
- options: txOptions
1770
+ const result = await _SuiClient.client.core.executeTransaction({
1771
+ transaction: transactionBlock,
1772
+ signatures: signature
1766
1773
  });
1774
+ const resp = result.Transaction ?? result.FailedTransaction;
1775
+ if (!resp) {
1776
+ throw new Error("Transaction execution failed with no response");
1777
+ }
1767
1778
  const ptb = Transaction.from(toBase64(transactionBlock));
1768
1779
  return _SuiClient.waitForTransaction(ptb, resp);
1769
1780
  }
@@ -1785,14 +1796,20 @@ var SuiClient = class _SuiClient {
1785
1796
  return await _SuiClient.signAndExecute(tx, from);
1786
1797
  }
1787
1798
  static async devInspect(ptb, sender) {
1788
- return await _SuiClient.client.devInspectTransactionBlock({
1789
- transactionBlock: ptb,
1790
- sender
1799
+ ptb.setSender(sender);
1800
+ return await _SuiClient.client.core.simulateTransaction({
1801
+ transaction: ptb,
1802
+ checksEnabled: false,
1803
+ include: { commandResults: true }
1791
1804
  });
1792
1805
  }
1793
1806
  static async devInspectRaw(ptb, sender) {
1794
1807
  const result = await this.devInspect(ptb, sender);
1795
- return result.results?.[0].returnValues?.[0]?.[0];
1808
+ const commandResult = result.commandResults?.[0];
1809
+ if (!commandResult || !commandResult.returnValues?.[0]) {
1810
+ return void 0;
1811
+ }
1812
+ return commandResult.returnValues[0];
1796
1813
  }
1797
1814
  static async devInspectBool(ptb, sender) {
1798
1815
  const result = await this.devInspectRaw(ptb, sender);
@@ -1818,24 +1835,25 @@ var SuiClient = class _SuiClient {
1818
1835
  return bcs.string().parse(new Uint8Array(value));
1819
1836
  }
1820
1837
  static async getObject(id) {
1821
- return _SuiClient.client.getObject({
1822
- id,
1823
- options: {
1824
- showContent: true,
1825
- showType: true,
1826
- showDisplay: true,
1827
- showBcs: true
1838
+ return _SuiClient.client.core.getObject({
1839
+ objectId: id,
1840
+ include: {
1841
+ content: true,
1842
+ type: true,
1843
+ display: true,
1844
+ bcs: true
1828
1845
  }
1829
1846
  });
1830
1847
  }
1831
1848
  static async getObjectsByType(owner, type3) {
1832
- const res = await _SuiClient.client.getOwnedObjects({
1849
+ const res = await _SuiClient.client.core.listOwnedObjects({
1833
1850
  owner,
1834
- filter: {
1835
- StructType: type3
1851
+ type: type3,
1852
+ include: {
1853
+ content: true
1836
1854
  }
1837
1855
  });
1838
- return res.data.map((o) => o.data?.objectId).filter((o) => o);
1856
+ return res.objects.map((o) => o.objectId).filter((o) => o);
1839
1857
  }
1840
1858
  };
1841
1859
 
@@ -16235,24 +16253,27 @@ var Coin = class {
16235
16253
  throw new Error("`coinType` getter must be implemented !");
16236
16254
  }
16237
16255
  static async getBalance(owner) {
16238
- const result = await SuiClient.client.getBalance({
16239
- owner,
16240
- coinType: this.coinType
16256
+ const result = await SuiClient.client.core.listBalances({
16257
+ owner
16241
16258
  });
16242
- return BigInt(result.totalBalance);
16259
+ const balance = result.balances.find((b) => b.coinType === this.coinType);
16260
+ return BigInt(balance?.balance || 0);
16243
16261
  }
16244
- static async getCoin(owner, amount) {
16245
- const balance = amount || await this.getBalance(owner.toSuiAddress());
16246
- const tx = new Transaction();
16247
- const coinSplit = coinWithBalance({
16262
+ static coinWithBalance(balance) {
16263
+ return coinWithBalance({
16248
16264
  balance,
16249
16265
  useGasCoin: false,
16250
16266
  type: this.coinType
16251
16267
  });
16268
+ }
16269
+ static async getCoin(owner, amount) {
16270
+ const balance = amount || await this.getBalance(owner.toSuiAddress());
16271
+ const tx = new Transaction();
16272
+ const coinSplit = this.coinWithBalance(balance);
16252
16273
  tx.transferObjects([coinSplit], owner.toSuiAddress());
16253
16274
  const result = await SuiClient.signAndExecute(tx, owner);
16254
- const coin = result.objectChanges?.find(
16255
- (o) => o.type === "created" && o.objectType === `0x2::coin::Coin<${this.coinType}>`
16275
+ const coin = result.effects?.changedObjects?.find(
16276
+ (o) => o.idOperation === "Created"
16256
16277
  );
16257
16278
  return coin?.objectId;
16258
16279
  }
@@ -16281,9 +16302,8 @@ var Coin = class {
16281
16302
  });
16282
16303
  }
16283
16304
  static async send(amount, from, to) {
16284
- const coin = await this.getCoin(from, amount);
16285
16305
  const ptb = new Transaction();
16286
- ptb.transferObjects([ptb.object(coin)], to);
16306
+ ptb.transferObjects([this.coinWithBalance(amount)], to);
16287
16307
  await SuiClient.signAndExecute(ptb, from);
16288
16308
  }
16289
16309
  static async assertBalance(wallet, amount) {
@@ -16380,11 +16400,21 @@ var PublishSingleton = class _PublishSingleton {
16380
16400
  }
16381
16401
  return packagePath;
16382
16402
  }
16383
- static async publish(signer, packagePath) {
16403
+ static getPubFilePath(pubFilePath) {
16404
+ pubFilePath ??= Config.vars.PUBFILE_PATH;
16405
+ if (!pubFilePath) {
16406
+ throw new Error(
16407
+ `You must set the \`PUBFILE_PATH\` environment variable to your Pub.${Config.vars.NETWORK}.toml path.`
16408
+ );
16409
+ }
16410
+ return pubFilePath;
16411
+ }
16412
+ static async publish(signer, packagePath, pubFilePath) {
16384
16413
  signer ??= ADMIN_KEYPAIR;
16385
16414
  const _packagePath = this.getPackagePath(packagePath);
16415
+ const _pubFilePath = this.getPubFilePath(pubFilePath);
16386
16416
  if (!_PublishSingleton.instance) {
16387
- const publishResp = await _PublishSingleton.publishPackage(signer, _packagePath);
16417
+ const publishResp = await _PublishSingleton.publishPackage(signer, _packagePath, _pubFilePath);
16388
16418
  _PublishSingleton.instance = new _PublishSingleton(publishResp);
16389
16419
  }
16390
16420
  }
@@ -16420,7 +16450,7 @@ var PublishSingleton = class _PublishSingleton {
16420
16450
  false
16421
16451
  );
16422
16452
  }
16423
- static getPublishCmd(packagePath, sender, inBytes = false) {
16453
+ static getPublishCmd(packagePath, sender, pubFilePath, inBytes = false) {
16424
16454
  const network = Config.vars.NETWORK;
16425
16455
  if (!fs3__default.existsSync(packagePath)) {
16426
16456
  throw new Error(`Package doesn't exist under: ${packagePath}`);
@@ -16435,17 +16465,21 @@ var PublishSingleton = class _PublishSingleton {
16435
16465
  if (isEphemeralChain) {
16436
16466
  buildCommand += " --publish-unpublished-deps";
16437
16467
  }
16468
+ if (pubFilePath) {
16469
+ buildCommand += ` --pubfile-path ${pubFilePath}`;
16470
+ }
16438
16471
  buildCommand += inBytes ? ` --serialize-unsigned-transaction --sender ${sender}` : " --json";
16439
16472
  return buildCommand;
16440
16473
  }
16441
- static async getPublishBytes(signer, packagePath) {
16474
+ static async getPublishBytes(signer, packagePath, pubFilePath) {
16442
16475
  signer ??= ADMIN_KEYPAIR.toSuiAddress();
16443
16476
  const _packagePath = this.getPackagePath(packagePath);
16444
- const cmd = this.getPublishCmd(_packagePath, signer, true);
16477
+ const _pubFilePath = this.getPubFilePath(pubFilePath);
16478
+ const cmd = this.getPublishCmd(_packagePath, signer, _pubFilePath, true);
16445
16479
  return execCmd(cmd);
16446
16480
  }
16447
- static async publishPackage(signer, packagePath) {
16448
- const cmd = this.getPublishCmd(packagePath, signer.toSuiAddress());
16481
+ static async publishPackage(signer, packagePath, pubFilePath) {
16482
+ const cmd = this.getPublishCmd(packagePath, signer.toSuiAddress(), pubFilePath);
16449
16483
  const res = await execCmd(cmd);
16450
16484
  const match = res.match(/\{[\s\S]*\}/);
16451
16485
  if (!match) {
@@ -16473,9 +16507,9 @@ var PublishSingleton = class _PublishSingleton {
16473
16507
  };
16474
16508
 
16475
16509
  // src/utils/deploy.ts
16476
- async function deploy(ConfigClass = Config, packagePath) {
16510
+ async function deploy(ConfigClass = Config, packagePath, pubFilePath) {
16477
16511
  const vars = ConfigClass.vars;
16478
- await PublishSingleton.publish(ADMIN_KEYPAIR, packagePath);
16512
+ await PublishSingleton.publish(ADMIN_KEYPAIR, packagePath, pubFilePath);
16479
16513
  const newConfig = {
16480
16514
  ...vars,
16481
16515
  PACKAGE_ID: PublishSingleton.packageId,
@@ -16520,12 +16554,14 @@ function sleep(ms) {
16520
16554
  return new Promise((resolve3) => setTimeout(resolve3, ms));
16521
16555
  }
16522
16556
  async function waitForNextEpoch(timeoutMs = 5 * 60 * 1e3, pollIntervalMs = 2e3) {
16523
- const startEpoch = (await SuiClient.client.getLatestSuiSystemState()).epoch;
16557
+ const startEpochData = await SuiClient.client.core.getCurrentSystemState();
16558
+ const startEpoch = BigInt(startEpochData.systemState.epoch);
16524
16559
  const startTime = Date.now();
16525
16560
  while (true) {
16526
- const { epoch } = await SuiClient.client.getLatestSuiSystemState();
16527
- if (Number(epoch) > Number(startEpoch)) {
16528
- return epoch;
16561
+ const currentEpochData = await SuiClient.client.core.getCurrentSystemState();
16562
+ const currentEpoch = BigInt(currentEpochData.systemState.epoch);
16563
+ if (currentEpoch > startEpoch) {
16564
+ return currentEpoch.toString();
16529
16565
  }
16530
16566
  if (Date.now() - startTime > timeoutMs) {
16531
16567
  throw new Error("Timeout waiting for next epoch.");