@t2000/sdk 0.18.5 → 0.18.7

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
@@ -3854,48 +3854,47 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3854
3854
  }
3855
3855
  async balance() {
3856
3856
  const bal = await queryBalance(this.client, this._address);
3857
+ const portfolioPositions = this.portfolio.getPositions();
3857
3858
  const earningAssets = new Set(
3858
- this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
3859
+ portfolioPositions.filter((p) => p.earning).map((p) => p.asset)
3859
3860
  );
3860
- try {
3861
- const positions = await this.positions();
3862
- const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).reduce((sum, p) => sum + p.amount, 0);
3863
- const debt = positions.positions.filter((p) => p.type === "borrow").reduce((sum, p) => sum + p.amount, 0);
3864
- bal.savings = savings;
3865
- bal.debt = debt;
3866
- } catch {
3867
- }
3868
- try {
3869
- const portfolioPositions = this.portfolio.getPositions();
3870
- const suiPrice = bal.gasReserve.sui > 0 ? bal.gasReserve.usdEquiv / bal.gasReserve.sui : 0;
3871
- const assetPrices = { SUI: suiPrice };
3872
- const swapAdapter = this.registry.listSwap()[0];
3873
- const investedAssets = /* @__PURE__ */ new Set();
3874
- for (const pos of portfolioPositions) {
3875
- if (pos.asset in INVESTMENT_ASSETS) investedAssets.add(pos.asset);
3876
- }
3877
- for (const key of this.portfolio.getAllStrategyKeys()) {
3878
- for (const sp of this.portfolio.getStrategyPositions(key)) {
3879
- if (sp.asset in INVESTMENT_ASSETS) investedAssets.add(sp.asset);
3861
+ const suiPrice = bal.gasReserve.sui > 0 ? bal.gasReserve.usdEquiv / bal.gasReserve.sui : 0;
3862
+ const assetPrices = { SUI: suiPrice };
3863
+ const swapAdapter = this.registry.listSwap()[0];
3864
+ for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3865
+ if (asset === "SUI") continue;
3866
+ try {
3867
+ if (swapAdapter) {
3868
+ const quote = await swapAdapter.getQuote("USDC", asset, 1);
3869
+ assetPrices[asset] = quote.expectedOutput > 0 ? 1 / quote.expectedOutput : 0;
3880
3870
  }
3871
+ } catch {
3872
+ assetPrices[asset] = 0;
3881
3873
  }
3882
- for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3883
- if ((bal.assets[asset] ?? 0) > 0) investedAssets.add(asset);
3884
- }
3885
- for (const asset of investedAssets) {
3886
- if (asset === "SUI" || asset in assetPrices) continue;
3887
- try {
3888
- if (swapAdapter) {
3889
- const quote = await swapAdapter.getQuote("USDC", asset, 1);
3890
- assetPrices[asset] = quote.expectedOutput > 0 ? 1 / quote.expectedOutput : 0;
3874
+ }
3875
+ const toUsd = (asset, amount) => asset in INVESTMENT_ASSETS ? amount * (assetPrices[asset] ?? 0) : amount;
3876
+ let chainTotal = bal.available + bal.gasReserve.usdEquiv;
3877
+ for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3878
+ if (asset === "SUI") continue;
3879
+ chainTotal += (bal.assets[asset] ?? 0) * (assetPrices[asset] ?? 0);
3880
+ }
3881
+ try {
3882
+ const positions = await this.positions();
3883
+ for (const pos of positions.positions) {
3884
+ const usdValue = toUsd(pos.asset, pos.amount);
3885
+ if (pos.type === "save") {
3886
+ chainTotal += usdValue;
3887
+ if (!earningAssets.has(pos.asset)) {
3888
+ bal.savings += usdValue;
3891
3889
  }
3892
- } catch {
3893
- assetPrices[asset] = 0;
3890
+ } else if (pos.type === "borrow") {
3891
+ chainTotal -= usdValue;
3892
+ bal.debt += usdValue;
3894
3893
  }
3895
3894
  }
3896
- let investmentValue = 0;
3897
- let investmentCostBasis = 0;
3898
- let trackedValue = 0;
3895
+ } catch {
3896
+ }
3897
+ try {
3899
3898
  const trackedAmounts = {};
3900
3899
  const trackedCostBasis = {};
3901
3900
  const earningAssetSet = /* @__PURE__ */ new Set();
@@ -3912,6 +3911,9 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3912
3911
  trackedCostBasis[sp.asset] = (trackedCostBasis[sp.asset] ?? 0) + sp.costBasis;
3913
3912
  }
3914
3913
  }
3914
+ let investmentValue = 0;
3915
+ let investmentCostBasis = 0;
3916
+ let trackedValue = 0;
3915
3917
  for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3916
3918
  const price = assetPrices[asset] ?? 0;
3917
3919
  const tracked = trackedAmounts[asset] ?? 0;
@@ -3949,7 +3951,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3949
3951
  } catch {
3950
3952
  bal.pendingRewards = 0;
3951
3953
  }
3952
- bal.total = bal.available + bal.savings - bal.debt + bal.investment + bal.gasReserve.usdEquiv;
3954
+ bal.total = chainTotal;
3953
3955
  return bal;
3954
3956
  }
3955
3957
  async history(params) {
@@ -4846,7 +4848,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4846
4848
  });
4847
4849
  const walletAmount = Number(assetBalance.totalBalance) / 10 ** assetInfo.decimals;
4848
4850
  const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
4849
- const depositAmount = Math.max(0, walletAmount - gasReserve);
4851
+ const depositAmount = Math.min(pos.totalAmount, Math.max(0, walletAmount - gasReserve));
4850
4852
  if (pos.earning && depositAmount <= 0) {
4851
4853
  return {
4852
4854
  success: true,
@@ -5767,7 +5769,12 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5767
5769
  }
5768
5770
  }
5769
5771
  }
5770
- const bestRate = allRates.reduce(
5772
+ const stableSet = new Set(STABLE_ASSETS);
5773
+ const stableRates = allRates.filter((r) => stableSet.has(r.asset));
5774
+ if (stableRates.length === 0) {
5775
+ throw new T2000Error("PROTOCOL_UNAVAILABLE", "No stablecoin lending rates available for rebalance");
5776
+ }
5777
+ const bestRate = stableRates.reduce(
5771
5778
  (best, r) => r.rates.saveApy > best.rates.saveApy ? r : best
5772
5779
  );
5773
5780
  const current = savePositions.reduce(