@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.cjs CHANGED
@@ -1131,11 +1131,19 @@ async function getRates(client) {
1131
1131
  }
1132
1132
  async function getPositions(client, addressOrKeypair) {
1133
1133
  const address = typeof addressOrKeypair === "string" ? addressOrKeypair : addressOrKeypair.getPublicKey().toSuiAddress();
1134
- const [states, pools] = await Promise.all([getUserState(client, address), getPools()]);
1134
+ const [states, cachedPools] = await Promise.all([getUserState(client, address), getPools()]);
1135
+ let pools = cachedPools;
1136
+ const unmatchedIds = states.filter((s) => !pools.find((p) => p.id === s.assetId)).map((s) => s.assetId);
1137
+ if (unmatchedIds.length > 0) {
1138
+ pools = await getPools(true);
1139
+ }
1135
1140
  const positions = [];
1136
1141
  for (const state of states) {
1137
1142
  const pool = pools.find((p) => p.id === state.assetId);
1138
- if (!pool) continue;
1143
+ if (!pool) {
1144
+ console.warn(`[NAVI] No pool found for assetId=${state.assetId} (supply=${state.supplyBalance}, borrow=${state.borrowBalance}) \u2014 funds may be invisible`);
1145
+ continue;
1146
+ }
1139
1147
  const symbol = resolvePoolSymbol(pool);
1140
1148
  const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
1141
1149
  const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
@@ -3853,7 +3861,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3853
3861
  );
3854
3862
  try {
3855
3863
  const positions = await this.positions();
3856
- 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);
3864
+ const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).reduce((sum, p) => sum + p.amount, 0);
3857
3865
  const debt = positions.positions.filter((p) => p.type === "borrow").reduce((sum, p) => sum + p.amount, 0);
3858
3866
  bal.savings = savings;
3859
3867
  bal.debt = debt;
@@ -3873,6 +3881,9 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3873
3881
  if (sp.asset in INVESTMENT_ASSETS) investedAssets.add(sp.asset);
3874
3882
  }
3875
3883
  }
3884
+ for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3885
+ if ((bal.assets[asset] ?? 0) > 0) investedAssets.add(asset);
3886
+ }
3876
3887
  for (const asset of investedAssets) {
3877
3888
  if (asset === "SUI" || asset in assetPrices) continue;
3878
3889
  try {
@@ -4074,11 +4085,14 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4074
4085
  return this.withdrawAllProtocols();
4075
4086
  }
4076
4087
  const allPositions = await this.registry.allPositions(this._address);
4088
+ const earningAssets = new Set(
4089
+ this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4090
+ );
4077
4091
  const supplies = [];
4078
4092
  for (const pos of allPositions) {
4079
4093
  if (params.protocol && pos.protocolId !== params.protocol) continue;
4080
4094
  for (const s of pos.positions.supplies) {
4081
- if (s.amount > 1e-3 && !(s.asset in INVESTMENT_ASSETS)) {
4095
+ if (s.amount > 1e-3 && !earningAssets.has(s.asset)) {
4082
4096
  supplies.push({ protocolId: pos.protocolId, asset: s.asset, amount: s.amount, apy: s.apy });
4083
4097
  }
4084
4098
  }
@@ -4162,7 +4176,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4162
4176
  const withdrawable = [];
4163
4177
  for (const pos of allPositions) {
4164
4178
  for (const supply of pos.positions.supplies) {
4165
- if (supply.amount > 0.01 && !earningAssets.has(supply.asset) && !(supply.asset in INVESTMENT_ASSETS)) {
4179
+ if (supply.amount > 0.01 && !earningAssets.has(supply.asset)) {
4166
4180
  withdrawable.push({ protocolId: pos.protocolId, asset: supply.asset, amount: supply.amount });
4167
4181
  }
4168
4182
  }
@@ -4834,7 +4848,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4834
4848
  });
4835
4849
  const walletAmount = Number(assetBalance.totalBalance) / 10 ** assetInfo.decimals;
4836
4850
  const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
4837
- const depositAmount = Math.max(0, walletAmount - gasReserve);
4851
+ const depositAmount = Math.min(pos.totalAmount, Math.max(0, walletAmount - gasReserve));
4838
4852
  if (pos.earning && depositAmount <= 0) {
4839
4853
  return {
4840
4854
  success: true,
@@ -5755,7 +5769,12 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5755
5769
  }
5756
5770
  }
5757
5771
  }
5758
- 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(
5759
5778
  (best, r) => r.rates.saveApy > best.rates.saveApy ? r : best
5760
5779
  );
5761
5780
  const current = savePositions.reduce(