@t2000/sdk 0.18.4 → 0.18.6

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
@@ -1129,11 +1129,19 @@ async function getRates(client) {
1129
1129
  }
1130
1130
  async function getPositions(client, addressOrKeypair) {
1131
1131
  const address = typeof addressOrKeypair === "string" ? addressOrKeypair : addressOrKeypair.getPublicKey().toSuiAddress();
1132
- const [states, pools] = await Promise.all([getUserState(client, address), getPools()]);
1132
+ const [states, cachedPools] = await Promise.all([getUserState(client, address), getPools()]);
1133
+ let pools = cachedPools;
1134
+ const unmatchedIds = states.filter((s) => !pools.find((p) => p.id === s.assetId)).map((s) => s.assetId);
1135
+ if (unmatchedIds.length > 0) {
1136
+ pools = await getPools(true);
1137
+ }
1133
1138
  const positions = [];
1134
1139
  for (const state of states) {
1135
1140
  const pool = pools.find((p) => p.id === state.assetId);
1136
- if (!pool) continue;
1141
+ if (!pool) {
1142
+ console.warn(`[NAVI] No pool found for assetId=${state.assetId} (supply=${state.supplyBalance}, borrow=${state.borrowBalance}) \u2014 funds may be invisible`);
1143
+ continue;
1144
+ }
1137
1145
  const symbol = resolvePoolSymbol(pool);
1138
1146
  const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
1139
1147
  const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
@@ -3851,7 +3859,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3851
3859
  );
3852
3860
  try {
3853
3861
  const positions = await this.positions();
3854
- const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).filter((p) => !(p.asset in INVESTMENT_ASSETS)).reduce((sum, p) => sum + p.amount, 0);
3862
+ const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).reduce((sum, p) => sum + p.amount, 0);
3855
3863
  const debt = positions.positions.filter((p) => p.type === "borrow").reduce((sum, p) => sum + p.amount, 0);
3856
3864
  bal.savings = savings;
3857
3865
  bal.debt = debt;
@@ -3871,6 +3879,9 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3871
3879
  if (sp.asset in INVESTMENT_ASSETS) investedAssets.add(sp.asset);
3872
3880
  }
3873
3881
  }
3882
+ for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3883
+ if ((bal.assets[asset] ?? 0) > 0) investedAssets.add(asset);
3884
+ }
3874
3885
  for (const asset of investedAssets) {
3875
3886
  if (asset === "SUI" || asset in assetPrices) continue;
3876
3887
  try {
@@ -4072,11 +4083,14 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4072
4083
  return this.withdrawAllProtocols();
4073
4084
  }
4074
4085
  const allPositions = await this.registry.allPositions(this._address);
4086
+ const earningAssets = new Set(
4087
+ this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4088
+ );
4075
4089
  const supplies = [];
4076
4090
  for (const pos of allPositions) {
4077
4091
  if (params.protocol && pos.protocolId !== params.protocol) continue;
4078
4092
  for (const s of pos.positions.supplies) {
4079
- if (s.amount > 1e-3 && !(s.asset in INVESTMENT_ASSETS)) {
4093
+ if (s.amount > 1e-3 && !earningAssets.has(s.asset)) {
4080
4094
  supplies.push({ protocolId: pos.protocolId, asset: s.asset, amount: s.amount, apy: s.apy });
4081
4095
  }
4082
4096
  }
@@ -4160,7 +4174,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4160
4174
  const withdrawable = [];
4161
4175
  for (const pos of allPositions) {
4162
4176
  for (const supply of pos.positions.supplies) {
4163
- if (supply.amount > 0.01 && !earningAssets.has(supply.asset) && !(supply.asset in INVESTMENT_ASSETS)) {
4177
+ if (supply.amount > 0.01 && !earningAssets.has(supply.asset)) {
4164
4178
  withdrawable.push({ protocolId: pos.protocolId, asset: supply.asset, amount: supply.amount });
4165
4179
  }
4166
4180
  }
@@ -4832,7 +4846,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4832
4846
  });
4833
4847
  const walletAmount = Number(assetBalance.totalBalance) / 10 ** assetInfo.decimals;
4834
4848
  const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
4835
- const depositAmount = Math.max(0, walletAmount - gasReserve);
4849
+ const depositAmount = Math.min(pos.totalAmount, Math.max(0, walletAmount - gasReserve));
4836
4850
  if (pos.earning && depositAmount <= 0) {
4837
4851
  return {
4838
4852
  success: true,
@@ -5753,7 +5767,12 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5753
5767
  }
5754
5768
  }
5755
5769
  }
5756
- const bestRate = allRates.reduce(
5770
+ const stableSet = new Set(STABLE_ASSETS);
5771
+ const stableRates = allRates.filter((r) => stableSet.has(r.asset));
5772
+ if (stableRates.length === 0) {
5773
+ throw new T2000Error("PROTOCOL_UNAVAILABLE", "No stablecoin lending rates available for rebalance");
5774
+ }
5775
+ const bestRate = stableRates.reduce(
5757
5776
  (best, r) => r.rates.saveApy > best.rates.saveApy ? r : best
5758
5777
  );
5759
5778
  const current = savePositions.reduce(