@t2000/sdk 0.1.7 → 0.1.9

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