@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.js CHANGED
@@ -575,6 +575,20 @@ function addOracleUpdate(tx, config, pool) {
575
575
  ]
576
576
  });
577
577
  }
578
+ function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
579
+ const updated = /* @__PURE__ */ new Set();
580
+ addOracleUpdate(tx, config, primaryPool);
581
+ updated.add(primaryPool.id);
582
+ for (const state of states) {
583
+ if (updated.has(state.assetId)) continue;
584
+ const pool = pools.find((p) => p.id === state.assetId);
585
+ if (!pool) continue;
586
+ const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
587
+ if (!feed) continue;
588
+ addOracleUpdate(tx, config, pool);
589
+ updated.add(pool.id);
590
+ }
591
+ }
578
592
  function rateToApy(rawRate) {
579
593
  if (!rawRate || rawRate === "0") return 0;
580
594
  return Number(BigInt(rawRate)) / 10 ** RATE_DECIMALS * 100;
@@ -687,7 +701,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
687
701
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
688
702
  const tx = new Transaction();
689
703
  tx.setSender(address);
690
- addOracleUpdate(tx, config, pool);
704
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
691
705
  const [balance] = tx.moveCall({
692
706
  target: `${config.package}::incentive_v3::withdraw_v2`,
693
707
  arguments: [
@@ -718,10 +732,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
718
732
  const asset = options.asset ?? "USDC";
719
733
  const assetInfo = SUPPORTED_ASSETS[asset];
720
734
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
721
- const [config, pool] = await Promise.all([getConfig(), getPool(asset)]);
735
+ const [config, pool, pools, states] = await Promise.all([
736
+ getConfig(),
737
+ getPool(asset),
738
+ getPools(),
739
+ getUserState(client, address)
740
+ ]);
722
741
  const tx = new Transaction();
723
742
  tx.setSender(address);
724
- addOracleUpdate(tx, config, pool);
743
+ addOracleUpdatesForPositions(tx, config, pools, states, pool);
725
744
  const [balance] = tx.moveCall({
726
745
  target: `${config.package}::incentive_v3::borrow_v2`,
727
746
  arguments: [
@@ -2774,6 +2793,22 @@ var T2000 = class _T2000 extends EventEmitter {
2774
2793
  const current = savePositions.reduce(
2775
2794
  (worst, p) => p.apy < worst.apy ? p : worst
2776
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
+ }
2777
2812
  const apyDiff = bestRate.rates.saveApy - current.apy;
2778
2813
  const isSameProtocol = current.protocolId === bestRate.protocolId;
2779
2814
  const isSameAsset = current.asset === bestRate.asset;
@@ -2885,7 +2920,6 @@ var T2000 = class _T2000 extends EventEmitter {
2885
2920
  }
2886
2921
  const txDigests = [];
2887
2922
  let totalGasCost = 0;
2888
- const withdrawAdapter = this.registry.getLending(current.protocolId);
2889
2923
  if (!withdrawAdapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${current.protocolId} not found`);
2890
2924
  const withdrawResult = await executeWithGas(this.client, this.keypair, async () => {
2891
2925
  const built = await withdrawAdapter.buildWithdrawTx(this._address, current.amount, current.asset);