@t2000/sdk 0.16.23 → 0.16.25

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.
@@ -408,12 +408,17 @@ function normalizeHealthFactor(raw) {
408
408
  const v = raw / 10 ** RATE_DECIMALS;
409
409
  return v > 1e5 ? Infinity : v;
410
410
  }
411
- function compoundBalance(rawBalance, currentIndex) {
411
+ function naviStorageDecimals(poolId, tokenDecimals) {
412
+ if (poolId <= 10) return NAVI_BALANCE_DECIMALS;
413
+ return tokenDecimals;
414
+ }
415
+ function compoundBalance(rawBalance, currentIndex, pool) {
412
416
  if (!rawBalance || !currentIndex || currentIndex === "0") return 0;
413
417
  const scale = BigInt("1" + "0".repeat(RATE_DECIMALS));
414
418
  const half = scale / 2n;
415
419
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
416
- return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
420
+ const decimals = pool ? naviStorageDecimals(pool.id, pool.token.decimals) : NAVI_BALANCE_DECIMALS;
421
+ return Number(result) / 10 ** decimals;
417
422
  }
418
423
  async function getUserState(client, address) {
419
424
  const config = await getConfig();
@@ -497,7 +502,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
497
502
  getUserState(client, address)
498
503
  ]);
499
504
  const assetState = states.find((s) => s.assetId === pool.id);
500
- const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
505
+ const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
501
506
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
502
507
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
503
508
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
@@ -540,7 +545,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
540
545
  getUserState(client, address)
541
546
  ]);
542
547
  const assetState = states.find((s) => s.assetId === pool.id);
543
- const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
548
+ const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
544
549
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
545
550
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
546
551
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
@@ -711,8 +716,8 @@ async function getHealthFactor(client, addressOrKeypair) {
711
716
  for (const state of states) {
712
717
  const pool = pools.find((p) => p.id === state.assetId);
713
718
  if (!pool) continue;
714
- const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
715
- const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
719
+ const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
720
+ const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
716
721
  const price = pool.token?.price ?? 1;
717
722
  supplied += supplyBal * price;
718
723
  borrowed += borrowBal * price;
@@ -799,8 +804,8 @@ async function getPositions(client, addressOrKeypair) {
799
804
  const pool = pools.find((p) => p.id === state.assetId);
800
805
  if (!pool) continue;
801
806
  const symbol = resolvePoolSymbol(pool);
802
- const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
803
- const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
807
+ const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
808
+ const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
804
809
  if (supplyBal > 1e-4) {
805
810
  positions.push({
806
811
  protocol: "navi",
@@ -832,7 +837,7 @@ async function maxWithdrawAmount(client, addressOrKeypair) {
832
837
  maxAmount = Math.max(0, hf.supplied - hf.borrowed * MIN_HEALTH_FACTOR / ltv);
833
838
  }
834
839
  const remainingSupply = hf.supplied - maxAmount;
835
- const hfAfter = hf.borrowed > 0 ? remainingSupply / hf.borrowed : Infinity;
840
+ const hfAfter = hf.borrowed > 0 ? remainingSupply * ltv / hf.borrowed : Infinity;
836
841
  return { maxAmount, healthFactorAfter: hfAfter, currentHF: hf.healthFactor };
837
842
  }
838
843
  async function maxBorrowAmount(client, addressOrKeypair) {