@t2000/sdk 0.16.24 → 0.16.26

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
@@ -402,6 +402,7 @@ async function buildSendTx({
402
402
  }
403
403
 
404
404
  // src/wallet/balance.ts
405
+ var SUI_PRICE_FALLBACK = 1;
405
406
  var _cachedSuiPrice = 0;
406
407
  var _priceLastFetched = 0;
407
408
  var PRICE_CACHE_TTL_MS = 6e4;
@@ -431,15 +432,15 @@ async function fetchSuiPrice(client) {
431
432
  }
432
433
  } catch {
433
434
  }
434
- return _cachedSuiPrice;
435
+ return _cachedSuiPrice || SUI_PRICE_FALLBACK;
435
436
  }
436
437
  async function queryBalance(client, address) {
437
438
  const stableBalancePromises = STABLE_ASSETS.map(
438
- (asset) => client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** SUPPORTED_ASSETS[asset].decimals }))
439
+ (asset) => client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** SUPPORTED_ASSETS[asset].decimals })).catch(() => ({ asset, amount: 0 }))
439
440
  );
440
441
  const nonSuiInvestmentAssets = Object.keys(INVESTMENT_ASSETS).filter((a) => a !== "SUI");
441
442
  const investBalancePromises = nonSuiInvestmentAssets.map(
442
- (asset) => client.getBalance({ owner: address, coinType: INVESTMENT_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** INVESTMENT_ASSETS[asset].decimals }))
443
+ (asset) => client.getBalance({ owner: address, coinType: INVESTMENT_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** INVESTMENT_ASSETS[asset].decimals })).catch(() => ({ asset, amount: 0 }))
443
444
  );
444
445
  const [suiBalance, suiPriceUsd, ...rest] = await Promise.all([
445
446
  client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
@@ -1134,7 +1135,7 @@ async function maxWithdrawAmount(client, addressOrKeypair) {
1134
1135
  maxAmount = Math.max(0, hf.supplied - hf.borrowed * MIN_HEALTH_FACTOR / ltv);
1135
1136
  }
1136
1137
  const remainingSupply = hf.supplied - maxAmount;
1137
- const hfAfter = hf.borrowed > 0 ? remainingSupply / hf.borrowed : Infinity;
1138
+ const hfAfter = hf.borrowed > 0 ? remainingSupply * ltv / hf.borrowed : Infinity;
1138
1139
  return { maxAmount, healthFactorAfter: hfAfter, currentHF: hf.healthFactor };
1139
1140
  }
1140
1141
  async function maxBorrowAmount(client, addressOrKeypair) {
@@ -2933,8 +2934,9 @@ var PortfolioManager = class {
2933
2934
  throw new T2000Error("INSUFFICIENT_INVESTMENT", `No ${trade.asset} position to sell`);
2934
2935
  }
2935
2936
  const sellAmount = Math.min(trade.amount, pos.totalAmount);
2937
+ const effectiveUsdValue = trade.amount > 0 && sellAmount < trade.amount ? trade.usdValue * (sellAmount / trade.amount) : trade.usdValue;
2936
2938
  const costOfSold = pos.avgPrice * sellAmount;
2937
- const realizedPnL = trade.usdValue - costOfSold;
2939
+ const realizedPnL = effectiveUsdValue - costOfSold;
2938
2940
  pos.totalAmount -= sellAmount;
2939
2941
  pos.costBasis -= costOfSold;
2940
2942
  if (pos.totalAmount < 1e-6) {
@@ -3032,8 +3034,9 @@ var PortfolioManager = class {
3032
3034
  throw new T2000Error("INSUFFICIENT_INVESTMENT", `No ${trade.asset} position in strategy '${strategyKey}'`);
3033
3035
  }
3034
3036
  const sellAmount = Math.min(trade.amount, pos.totalAmount);
3037
+ const effectiveUsdValue = trade.amount > 0 && sellAmount < trade.amount ? trade.usdValue * (sellAmount / trade.amount) : trade.usdValue;
3035
3038
  const costOfSold = pos.avgPrice * sellAmount;
3036
- const realizedPnL = trade.usdValue - costOfSold;
3039
+ const realizedPnL = effectiveUsdValue - costOfSold;
3037
3040
  pos.totalAmount -= sellAmount;
3038
3041
  pos.costBasis -= costOfSold;
3039
3042
  if (pos.totalAmount < 1e-6) {
@@ -3665,7 +3668,9 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3665
3668
  for (const pos of allPositions) {
3666
3669
  if (params.protocol && pos.protocolId !== params.protocol) continue;
3667
3670
  for (const s of pos.positions.supplies) {
3668
- if (s.amount > 1e-3) supplies.push({ protocolId: pos.protocolId, asset: s.asset, amount: s.amount, apy: s.apy });
3671
+ if (s.amount > 1e-3 && !(s.asset in INVESTMENT_ASSETS)) {
3672
+ supplies.push({ protocolId: pos.protocolId, asset: s.asset, amount: s.amount, apy: s.apy });
3673
+ }
3669
3674
  }
3670
3675
  }
3671
3676
  if (supplies.length === 0) {
@@ -3747,7 +3752,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3747
3752
  const withdrawable = [];
3748
3753
  for (const pos of allPositions) {
3749
3754
  for (const supply of pos.positions.supplies) {
3750
- if (supply.amount > 0.01 && !earningAssets.has(supply.asset)) {
3755
+ if (supply.amount > 0.01 && !earningAssets.has(supply.asset) && !(supply.asset in INVESTMENT_ASSETS)) {
3751
3756
  withdrawable.push({ protocolId: pos.protocolId, asset: supply.asset, amount: supply.amount });
3752
3757
  }
3753
3758
  }