@t2000/sdk 0.8.3 → 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.cjs CHANGED
@@ -577,6 +577,20 @@ function addOracleUpdate(tx, config, pool) {
577
577
  ]
578
578
  });
579
579
  }
580
+ function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
581
+ const updated = /* @__PURE__ */ new Set();
582
+ addOracleUpdate(tx, config, primaryPool);
583
+ updated.add(primaryPool.id);
584
+ for (const state of states) {
585
+ if (updated.has(state.assetId)) continue;
586
+ const pool = pools.find((p) => p.id === state.assetId);
587
+ if (!pool) continue;
588
+ const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
589
+ if (!feed) continue;
590
+ addOracleUpdate(tx, config, pool);
591
+ updated.add(pool.id);
592
+ }
593
+ }
580
594
  function rateToApy(rawRate) {
581
595
  if (!rawRate || rawRate === "0") return 0;
582
596
  return Number(BigInt(rawRate)) / 10 ** RATE_DECIMALS * 100;
@@ -689,7 +703,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
689
703
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
690
704
  const tx = new transactions.Transaction();
691
705
  tx.setSender(address);
692
- addOracleUpdate(tx, config, pool);
706
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
693
707
  const [balance] = tx.moveCall({
694
708
  target: `${config.package}::incentive_v3::withdraw_v2`,
695
709
  arguments: [
@@ -720,10 +734,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
720
734
  const asset = options.asset ?? "USDC";
721
735
  const assetInfo = SUPPORTED_ASSETS[asset];
722
736
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
723
- const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
737
+ const [config, pool, pools, states] = await Promise.all([
738
+ getConfig(),
739
+ getPool(asset),
740
+ getPools(),
741
+ getUserState(client, address)
742
+ ]);
724
743
  const tx = new transactions.Transaction();
725
744
  tx.setSender(address);
726
- addOracleUpdate(tx, config, pool);
745
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
727
746
  const [balance] = tx.moveCall({
728
747
  target: `${config.package}::incentive_v3::borrow_v2`,
729
748
  arguments: [
@@ -2776,6 +2795,22 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2776
2795
  const current = savePositions.reduce(
2777
2796
  (worst, p) => p.apy < worst.apy ? p : worst
2778
2797
  );
2798
+ const withdrawAdapter = this.registry.getLending(current.protocolId);
2799
+ if (withdrawAdapter) {
2800
+ try {
2801
+ const maxResult = await withdrawAdapter.maxWithdraw(this._address, current.asset);
2802
+ if (maxResult.maxAmount < current.amount) {
2803
+ current.amount = Math.max(0, maxResult.maxAmount - 0.01);
2804
+ }
2805
+ } catch {
2806
+ }
2807
+ }
2808
+ if (current.amount <= 0.01) {
2809
+ throw new T2000Error(
2810
+ "HEALTH_FACTOR_TOO_LOW",
2811
+ "Cannot rebalance \u2014 active borrows prevent safe withdrawal. Repay some debt first."
2812
+ );
2813
+ }
2779
2814
  const apyDiff = bestRate.rates.saveApy - current.apy;
2780
2815
  const isSameProtocol = current.protocolId === bestRate.protocolId;
2781
2816
  const isSameAsset = current.asset === bestRate.asset;
@@ -2887,7 +2922,6 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2887
2922
  }
2888
2923
  const txDigests = [];
2889
2924
  let totalGasCost = 0;
2890
- const withdrawAdapter = this.registry.getLending(current.protocolId);
2891
2925
  if (!withdrawAdapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${current.protocolId} not found`);
2892
2926
  const withdrawResult = await executeWithGas(this.client, this.keypair, async () => {
2893
2927
  const built = await withdrawAdapter.buildWithdrawTx(this._address, current.amount, current.asset);