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