@t2000/sdk 0.16.23 → 0.16.24

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
@@ -437,11 +437,18 @@ async function queryBalance(client, address) {
437
437
  const stableBalancePromises = STABLE_ASSETS.map(
438
438
  (asset) => client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** SUPPORTED_ASSETS[asset].decimals }))
439
439
  );
440
- const [suiBalance, suiPriceUsd, ...stableResults] = await Promise.all([
440
+ const nonSuiInvestmentAssets = Object.keys(INVESTMENT_ASSETS).filter((a) => a !== "SUI");
441
+ 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
+ );
444
+ const [suiBalance, suiPriceUsd, ...rest] = await Promise.all([
441
445
  client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
442
446
  fetchSuiPrice(client),
443
- ...stableBalancePromises
447
+ ...stableBalancePromises,
448
+ ...investBalancePromises
444
449
  ]);
450
+ const stableResults = rest.slice(0, STABLE_ASSETS.length);
451
+ const investResults = rest.slice(STABLE_ASSETS.length);
445
452
  const stables = {};
446
453
  let totalStables = 0;
447
454
  for (const { asset, amount } of stableResults) {
@@ -452,6 +459,13 @@ async function queryBalance(client, address) {
452
459
  const savings = 0;
453
460
  const usdEquiv = suiAmount * suiPriceUsd;
454
461
  const total = totalStables + savings + usdEquiv;
462
+ const assets = {
463
+ USDC: stables.USDC ?? 0,
464
+ SUI: suiAmount
465
+ };
466
+ for (const { asset, amount } of investResults) {
467
+ assets[asset] = amount;
468
+ }
455
469
  return {
456
470
  available: totalStables,
457
471
  savings,
@@ -464,10 +478,7 @@ async function queryBalance(client, address) {
464
478
  },
465
479
  total,
466
480
  stables,
467
- assets: {
468
- USDC: stables.USDC ?? 0,
469
- SUI: suiAmount
470
- }
481
+ assets
471
482
  };
472
483
  }
473
484
 
@@ -694,12 +705,17 @@ function normalizeHealthFactor(raw) {
694
705
  const v = raw / 10 ** RATE_DECIMALS;
695
706
  return v > 1e5 ? Infinity : v;
696
707
  }
697
- function compoundBalance(rawBalance, currentIndex) {
708
+ function naviStorageDecimals(poolId, tokenDecimals) {
709
+ if (poolId <= 10) return NAVI_BALANCE_DECIMALS;
710
+ return tokenDecimals;
711
+ }
712
+ function compoundBalance(rawBalance, currentIndex, pool) {
698
713
  if (!rawBalance || !currentIndex || currentIndex === "0") return 0;
699
714
  const scale = BigInt("1" + "0".repeat(RATE_DECIMALS));
700
715
  const half = scale / 2n;
701
716
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
702
- return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
717
+ const decimals = pool ? naviStorageDecimals(pool.id, pool.token.decimals) : NAVI_BALANCE_DECIMALS;
718
+ return Number(result) / 10 ** decimals;
703
719
  }
704
720
  async function getUserState(client, address) {
705
721
  const config = await getConfig();
@@ -783,7 +799,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
783
799
  getUserState(client, address)
784
800
  ]);
785
801
  const assetState = states.find((s) => s.assetId === pool.id);
786
- const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
802
+ const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
787
803
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
788
804
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
789
805
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
@@ -826,7 +842,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
826
842
  getUserState(client, address)
827
843
  ]);
828
844
  const assetState = states.find((s) => s.assetId === pool.id);
829
- const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
845
+ const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
830
846
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
831
847
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
832
848
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
@@ -997,8 +1013,8 @@ async function getHealthFactor(client, addressOrKeypair) {
997
1013
  for (const state of states) {
998
1014
  const pool = pools.find((p) => p.id === state.assetId);
999
1015
  if (!pool) continue;
1000
- const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
1001
- const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
1016
+ const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
1017
+ const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
1002
1018
  const price = pool.token?.price ?? 1;
1003
1019
  supplied += supplyBal * price;
1004
1020
  borrowed += borrowBal * price;
@@ -1085,8 +1101,8 @@ async function getPositions(client, addressOrKeypair) {
1085
1101
  const pool = pools.find((p) => p.id === state.assetId);
1086
1102
  if (!pool) continue;
1087
1103
  const symbol = resolvePoolSymbol(pool);
1088
- const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
1089
- const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
1104
+ const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
1105
+ const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
1090
1106
  if (supplyBal > 1e-4) {
1091
1107
  positions.push({
1092
1108
  protocol: "navi",
@@ -3466,45 +3482,44 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3466
3482
  }
3467
3483
  let investmentValue = 0;
3468
3484
  let investmentCostBasis = 0;
3485
+ const trackedAmounts = {};
3486
+ const trackedCostBasis = {};
3487
+ const earningAssetSet = /* @__PURE__ */ new Set();
3469
3488
  for (const pos of portfolioPositions) {
3470
3489
  if (!(pos.asset in INVESTMENT_ASSETS)) continue;
3471
- const price = assetPrices[pos.asset] ?? 0;
3472
- if (pos.earning) {
3473
- investmentValue += pos.totalAmount * price;
3474
- investmentCostBasis += pos.costBasis;
3475
- if (pos.asset === "SUI") {
3476
- const gasSui = Math.max(0, bal.gasReserve.sui);
3477
- bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
3478
- }
3479
- } else if (pos.asset === "SUI") {
3480
- const actualHeld = Math.min(pos.totalAmount, bal.gasReserve.sui);
3481
- investmentValue += actualHeld * price;
3482
- if (actualHeld < pos.totalAmount && pos.totalAmount > 0) {
3483
- investmentCostBasis += pos.costBasis * (actualHeld / pos.totalAmount);
3484
- } else {
3485
- investmentCostBasis += pos.costBasis;
3486
- }
3487
- const gasSui = Math.max(0, bal.gasReserve.sui - pos.totalAmount);
3488
- bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
3489
- } else {
3490
- investmentValue += pos.totalAmount * price;
3491
- investmentCostBasis += pos.costBasis;
3492
- }
3490
+ trackedAmounts[pos.asset] = (trackedAmounts[pos.asset] ?? 0) + pos.totalAmount;
3491
+ trackedCostBasis[pos.asset] = (trackedCostBasis[pos.asset] ?? 0) + pos.costBasis;
3492
+ if (pos.earning) earningAssetSet.add(pos.asset);
3493
3493
  }
3494
- let strategySuiTotal = 0;
3495
3494
  for (const key of this.portfolio.getAllStrategyKeys()) {
3496
3495
  for (const sp of this.portfolio.getStrategyPositions(key)) {
3497
3496
  if (!(sp.asset in INVESTMENT_ASSETS)) continue;
3498
- const price = assetPrices[sp.asset] ?? 0;
3499
- investmentValue += sp.totalAmount * price;
3500
- investmentCostBasis += sp.costBasis;
3501
- if (sp.asset === "SUI") strategySuiTotal += sp.totalAmount;
3497
+ trackedAmounts[sp.asset] = (trackedAmounts[sp.asset] ?? 0) + sp.totalAmount;
3498
+ trackedCostBasis[sp.asset] = (trackedCostBasis[sp.asset] ?? 0) + sp.costBasis;
3502
3499
  }
3503
3500
  }
3504
- if (strategySuiTotal > 0) {
3505
- const suiPrice2 = assetPrices["SUI"] ?? 0;
3506
- const gasSui = Math.max(0, bal.gasReserve.sui - strategySuiTotal);
3507
- bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * suiPrice2 };
3501
+ for (const asset of Object.keys(INVESTMENT_ASSETS)) {
3502
+ const price = assetPrices[asset] ?? 0;
3503
+ const tracked = trackedAmounts[asset] ?? 0;
3504
+ const costBasis = trackedCostBasis[asset] ?? 0;
3505
+ if (asset === "SUI") {
3506
+ const actualSui = earningAssetSet.has("SUI") ? tracked : Math.min(tracked, bal.gasReserve.sui);
3507
+ investmentValue += actualSui * price;
3508
+ if (actualSui < tracked && tracked > 0) {
3509
+ investmentCostBasis += costBasis * (actualSui / tracked);
3510
+ } else {
3511
+ investmentCostBasis += costBasis;
3512
+ }
3513
+ if (!earningAssetSet.has("SUI")) {
3514
+ const gasSui = Math.max(0, bal.gasReserve.sui - tracked);
3515
+ bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
3516
+ }
3517
+ } else {
3518
+ const onChainAmount = bal.assets[asset] ?? 0;
3519
+ const effectiveAmount = Math.max(tracked, onChainAmount);
3520
+ investmentValue += effectiveAmount * price;
3521
+ investmentCostBasis += costBasis;
3522
+ }
3508
3523
  }
3509
3524
  bal.investment = investmentValue;
3510
3525
  bal.investmentPnL = investmentValue - investmentCostBasis;