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