@t2000/sdk 0.8.4 → 0.8.5

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
@@ -2793,6 +2793,22 @@ var T2000 = class _T2000 extends EventEmitter {
2793
2793
  const current = savePositions.reduce(
2794
2794
  (worst, p) => p.apy < worst.apy ? p : worst
2795
2795
  );
2796
+ const withdrawAdapter = this.registry.getLending(current.protocolId);
2797
+ if (withdrawAdapter) {
2798
+ try {
2799
+ const maxResult = await withdrawAdapter.maxWithdraw(this._address, current.asset);
2800
+ if (maxResult.maxAmount < current.amount) {
2801
+ current.amount = Math.max(0, maxResult.maxAmount - 0.01);
2802
+ }
2803
+ } catch {
2804
+ }
2805
+ }
2806
+ if (current.amount <= 0.01) {
2807
+ throw new T2000Error(
2808
+ "HEALTH_FACTOR_TOO_LOW",
2809
+ "Cannot rebalance \u2014 active borrows prevent safe withdrawal. Repay some debt first."
2810
+ );
2811
+ }
2796
2812
  const apyDiff = bestRate.rates.saveApy - current.apy;
2797
2813
  const isSameProtocol = current.protocolId === bestRate.protocolId;
2798
2814
  const isSameAsset = current.asset === bestRate.asset;
@@ -2904,7 +2920,6 @@ var T2000 = class _T2000 extends EventEmitter {
2904
2920
  }
2905
2921
  const txDigests = [];
2906
2922
  let totalGasCost = 0;
2907
- const withdrawAdapter = this.registry.getLending(current.protocolId);
2908
2923
  if (!withdrawAdapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${current.protocolId} not found`);
2909
2924
  const withdrawResult = await executeWithGas(this.client, this.keypair, async () => {
2910
2925
  const built = await withdrawAdapter.buildWithdrawTx(this._address, current.amount, current.asset);