@t2000/sdk 0.1.7 → 0.1.8

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.cjs CHANGED
@@ -47,8 +47,6 @@ var DEFAULT_RPC_URL = "https://fullnode.mainnet.sui.io:443";
47
47
  var DEFAULT_KEY_PATH = "~/.t2000/wallet.key";
48
48
  var API_BASE_URL = process.env.T2000_API_URL ?? "https://api.t2000.ai";
49
49
  var CETUS_USDC_SUI_POOL = "0x51e883ba7c0b566a26cbc8a94cd33eb0abd418a77cc1e60ad22fd9b1f29cd2ab";
50
- var CETUS_GLOBAL_CONFIG = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
51
- var CETUS_PACKAGE = "0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb";
52
50
 
53
51
  // src/errors.ts
54
52
  var T2000Error = class extends Error {
@@ -550,7 +548,7 @@ function getCetusSDK() {
550
548
  return _cetusSDK;
551
549
  }
552
550
  async function buildSwapTx(params) {
553
- const { address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
551
+ const { client, address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
554
552
  const a2b = isA2B(fromAsset);
555
553
  const fromInfo = SUPPORTED_ASSETS[fromAsset];
556
554
  const toInfo = SUPPORTED_ASSETS[toAsset];
@@ -791,51 +789,17 @@ async function shouldAutoTopUp(client, address) {
791
789
  }
792
790
  async function executeAutoTopUp(client, keypair) {
793
791
  const address = keypair.getPublicKey().toSuiAddress();
794
- const tx = new transactions.Transaction();
795
- tx.setSender(address);
796
- const usdcCoins = await client.getCoins({
797
- owner: address,
798
- coinType: SUPPORTED_ASSETS.USDC.type
792
+ const topupAmountHuman = Number(AUTO_TOPUP_AMOUNT) / 1e6;
793
+ const { tx } = await buildSwapTx({
794
+ client,
795
+ address,
796
+ fromAsset: "USDC",
797
+ toAsset: "SUI",
798
+ amount: topupAmountHuman
799
799
  });
800
- if (usdcCoins.data.length === 0) {
801
- throw new T2000Error("AUTO_TOPUP_FAILED", "No USDC coins available for auto-topup");
802
- }
803
- const coinIds = usdcCoins.data.map((c) => c.coinObjectId);
804
- let usdcCoin;
805
- if (coinIds.length === 1) {
806
- usdcCoin = tx.splitCoins(tx.object(coinIds[0]), [AUTO_TOPUP_AMOUNT]);
807
- } else {
808
- const primary = tx.object(coinIds[0]);
809
- if (coinIds.length > 1) {
810
- tx.mergeCoins(primary, coinIds.slice(1).map((id) => tx.object(id)));
811
- }
812
- usdcCoin = tx.splitCoins(primary, [AUTO_TOPUP_AMOUNT]);
813
- }
814
- const MIN_SQRT_PRICE = "4295048016";
815
- const [receivedCoin, returnedCoin] = tx.moveCall({
816
- target: `${CETUS_PACKAGE}::pool_script::swap_a2b`,
817
- arguments: [
818
- tx.object(CETUS_GLOBAL_CONFIG),
819
- tx.object(CETUS_USDC_SUI_POOL),
820
- usdcCoin,
821
- tx.pure.bool(true),
822
- // by_amount_in
823
- tx.pure.u64(AUTO_TOPUP_AMOUNT),
824
- tx.pure.u128(MIN_SQRT_PRICE),
825
- tx.object(CLOCK_ID)
826
- ],
827
- typeArguments: [SUPPORTED_ASSETS.USDC.type, SUPPORTED_ASSETS.SUI.type]
828
- });
829
- tx.transferObjects([receivedCoin], address);
830
- tx.transferObjects([returnedCoin], address);
831
800
  const txBytes = await tx.build({ client, onlyTransactionKind: true });
832
801
  const txBytesBase64 = Buffer.from(txBytes).toString("base64");
833
- let sponsoredResult;
834
- try {
835
- sponsoredResult = await requestGasSponsorship(txBytesBase64, address, "auto-topup");
836
- } catch {
837
- throw new T2000Error("AUTO_TOPUP_FAILED", "Gas station unavailable for auto-topup sponsorship");
838
- }
802
+ const sponsoredResult = await requestGasSponsorship(txBytesBase64, address, "auto-topup");
839
803
  const sponsoredTxBytes = Buffer.from(sponsoredResult.txBytes, "base64");
840
804
  const { signature: agentSig } = await keypair.signTransaction(sponsoredTxBytes);
841
805
  const result = await client.executeTransactionBlock({
@@ -856,7 +820,7 @@ async function executeAutoTopUp(client, keypair) {
856
820
  return {
857
821
  success: true,
858
822
  tx: result.digest,
859
- usdcSpent: Number(AUTO_TOPUP_AMOUNT) / 1e6,
823
+ usdcSpent: topupAmountHuman,
860
824
  suiReceived: Math.abs(suiReceived)
861
825
  };
862
826
  }