@t2000/sdk 0.17.11 → 0.17.12

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
@@ -4346,7 +4346,10 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4346
4346
  }
4347
4347
  const didAutoWithdraw = !!(pos.earning && pos.earningProtocol);
4348
4348
  if (didAutoWithdraw) {
4349
- await this.investUnearn({ asset: params.asset });
4349
+ const unearnResult = await this.investUnearn({ asset: params.asset });
4350
+ if (unearnResult.tx) {
4351
+ await this.client.waitForTransaction({ digest: unearnResult.tx, options: { showEffects: true } });
4352
+ }
4350
4353
  }
4351
4354
  const assetInfo = SUPPORTED_ASSETS[params.asset];
4352
4355
  const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
@@ -4357,8 +4360,8 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4357
4360
  coinType: assetInfo.type
4358
4361
  });
4359
4362
  walletAmount = Number(assetBalance.totalBalance) / 10 ** assetInfo.decimals;
4360
- if (!didAutoWithdraw || walletAmount > gasReserve || attempt >= 3) break;
4361
- await new Promise((r) => setTimeout(r, 1e3));
4363
+ if (!didAutoWithdraw || walletAmount > gasReserve || attempt >= 5) break;
4364
+ await new Promise((r) => setTimeout(r, 1500));
4362
4365
  }
4363
4366
  const maxSellable = Math.max(0, walletAmount - gasReserve);
4364
4367
  let sellAmountAsset;
@@ -4629,8 +4632,15 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4629
4632
  if (!swapAdapter?.addSwapToTx) {
4630
4633
  throw new T2000Error("PROTOCOL_UNAVAILABLE", "Swap adapter does not support composable PTB");
4631
4634
  }
4635
+ for (const pos of stratPositions) {
4636
+ const directPos = this.portfolio.getPosition(pos.asset);
4637
+ if (directPos?.earning && directPos.earningProtocol) {
4638
+ await this.investUnearn({ asset: pos.asset });
4639
+ await new Promise((r) => setTimeout(r, 1500));
4640
+ }
4641
+ }
4632
4642
  let swapMetas = [];
4633
- const gasResult = await executeWithGas(this.client, this.keypair, async () => {
4643
+ const buildSellPtb = async () => {
4634
4644
  swapMetas = [];
4635
4645
  const tx = new transactions.Transaction();
4636
4646
  tx.setSender(this._address);
@@ -4665,12 +4675,31 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4665
4675
  usdcOutputs.push(outputCoin);
4666
4676
  swapMetas.push({ asset: pos.asset, amount: sellAmount, estimatedOut, toDecimals });
4667
4677
  }
4678
+ if (usdcOutputs.length === 0) {
4679
+ throw new T2000Error("INSUFFICIENT_BALANCE", "No assets available to sell");
4680
+ }
4668
4681
  if (usdcOutputs.length > 1) {
4669
4682
  tx.mergeCoins(usdcOutputs[0], usdcOutputs.slice(1));
4670
4683
  }
4671
4684
  tx.transferObjects([usdcOutputs[0]], this._address);
4672
4685
  return tx;
4673
- });
4686
+ };
4687
+ let gasResult;
4688
+ const MAX_RETRIES = 3;
4689
+ for (let attempt = 0; ; attempt++) {
4690
+ try {
4691
+ gasResult = await executeWithGas(this.client, this.keypair, buildSellPtb);
4692
+ break;
4693
+ } catch (err) {
4694
+ const msg = err instanceof Error ? err.message : String(err);
4695
+ const isSlippage = msg.includes("slippage") || msg.includes("amount_out_slippage");
4696
+ if (isSlippage && attempt < MAX_RETRIES) {
4697
+ await new Promise((r) => setTimeout(r, 2e3 * (attempt + 1)));
4698
+ continue;
4699
+ }
4700
+ throw err;
4701
+ }
4702
+ }
4674
4703
  const digest = gasResult.digest;
4675
4704
  const now = (/* @__PURE__ */ new Date()).toISOString();
4676
4705
  const sells = [];
@@ -4705,6 +4734,9 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4705
4734
  totalProceeds += usdValue;
4706
4735
  totalPnL += pnl;
4707
4736
  }
4737
+ if (this.portfolio.hasStrategyPositions(params.strategy)) {
4738
+ this.portfolio.clearStrategy(params.strategy);
4739
+ }
4708
4740
  return {
4709
4741
  success: true,
4710
4742
  strategy: params.strategy,