@1delta/margin-fetcher 0.0.321 → 0.0.323

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
@@ -6,7 +6,7 @@ export { isAaveType, isAaveV2Type, isAaveV32Type, isAaveV3Type, isCompoundV3, is
6
6
  import { aavePools, compoundV3Pools, initConfig, morphoPools, compoundV2Pools, eulerVaults, siloMarkets, siloV2LenderKey, siloMarketsV3, siloV3LenderKey, aaveV4Spokes, aaveV4SpokeLenderKey, fluidResolvers, gearboxResolvers, dolomiteConfigs, midnightConfig, aaveReserves, compoundV3Reserves, morphoTypeMarkets, getSiloV2MarketEntry, getSiloV3MarketEntry, dolomiteEmode, aaveTokens, morphoTypeVaults, eulerConfigs, gearboxMarketCompressor, gearboxMarketConfigurators, gearboxV3LenderKey, midnightMarketsByChain, parseAaveV4SpokeLenderKey, gearboxAccountCompressor, compoundV2Tokens, aaveOracles, aaveOraclesConfig, compoundV2Oracles, compoundV2TokenArray, compoundV2OraclesConfig, compoundV3OraclesData, compoundV3BaseData, compoundV2Reserves, fetchTokenLists, fetchTokenList, aaveV4Oracles, resolveMidnightApiBase } from '@1delta/data-sdk';
7
7
  import lodash from 'lodash';
8
8
  import { getEvmClient, getEvmChain, getEvmClientUniversal, multicallRetryUniversal } from '@1delta/providers';
9
- import { MetaMorphoAbi, FluidLendingResolverAbi, FluidVaultResolverAbi, MoolahVaultAbi, MorphoLensAbi, AaveV4SpokeAbi, AaveV4OracleAbi, AaveV4HubAbi, DolomiteMarginAbi, GearboxMarketCompressorV310Abi, MorphoBlueAbi, MidnightAbi, GearboxCreditAccountCompressorV310Abi } from '@1delta/abis';
9
+ import { MetaMorphoAbi, FluidLendingResolverAbi, FluidVaultResolverAbi, FluidLiquidityResolverAbi, MoolahVaultAbi, MorphoLensAbi, AaveV4SpokeAbi, AaveV4OracleAbi, AaveV4HubAbi, DolomiteMarginAbi, GearboxMarketCompressorV310Abi, MorphoBlueAbi, MidnightAbi, GearboxCreditAccountCompressorV310Abi } from '@1delta/abis';
10
10
  export { MorphoLensAbi } from '@1delta/abis';
11
11
  import { prepareDebitDataMulticall, prepareLenderDebitMulticall, parseDebitDataResult, parseLenderDebitResult, getPermit2ContractAddress, getCompoundV3CometAddress as getCompoundV3CometAddress$1, getMorphoAddress, getAaveCollateralTokenAddress, getSiloHalfForUnderlying, InitMarginAddresses, getLstAcceptedInputs } from '@1delta/calldata-sdk';
12
12
  import { proxyNativeFetch } from '@1delta/proxy-fetch';
@@ -19013,7 +19013,15 @@ function normalizeAaveV4(spokeDataList, chainId, spokeLenderKeyArg, prices, addi
19013
19013
  const hubTotalOwed = toTokenNumber(hubTotalOwedRaw, hubDecimals);
19014
19014
  const hubDepositsUSD = hubTotalSupply * price2;
19015
19015
  const hubBorrowsUSD = hubTotalOwed * price2;
19016
- const hubUtilization = hubTotalSupply > 0 ? hubTotalOwed / hubTotalSupply : 0;
19016
+ const drawnRaw = (reserve.hubAsset.drawnShares ?? 0n) * (reserve.hubAsset.drawnIndex ?? 0n) / RAY_BIGINT;
19017
+ const irmDenominatorRaw = (reserve.hubAsset.liquidity ?? 0n) + drawnRaw + (reserve.hubAsset.swept ?? 0n);
19018
+ let irmTotalDebt = toTokenNumber(drawnRaw, hubDecimals);
19019
+ let irmTotalDeposits = toTokenNumber(irmDenominatorRaw, hubDecimals);
19020
+ if (drawnRaw === 0n && hubTotalOwed > 0) {
19021
+ irmTotalDebt = hubTotalOwed;
19022
+ irmTotalDeposits = hubTotalSupply;
19023
+ }
19024
+ const hubUtilization = irmTotalDeposits > 0 ? irmTotalDebt / irmTotalDeposits : 0;
19017
19025
  const feeRate = reserve.hubAsset.liquidityFee ? reserve.hubAsset.liquidityFee / BPS : 0;
19018
19026
  const depositRate = variableBorrowRate * hubUtilization * (1 - feeRate);
19019
19027
  const collateralRiskBps = reserve.config.collateralRisk || reserve.reserve.collateralRisk || 0;
@@ -19095,6 +19103,10 @@ function normalizeAaveV4(spokeDataList, chainId, spokeLenderKeyArg, prices, addi
19095
19103
  variableBorrowRate,
19096
19104
  stableBorrowRate: 0,
19097
19105
  utilization: hubUtilization,
19106
+ // Totals in the hub IRM's own utilization basis — rate-impact
19107
+ // simulations must shift these, not the spoke-local totals.
19108
+ irmTotalDeposits,
19109
+ irmTotalDebt,
19098
19110
  intrinsicYield: additionalYields?.intrinsicYields?.[oracleKey] ?? 0,
19099
19111
  rewards: [],
19100
19112
  decimals,
@@ -19891,6 +19903,11 @@ var buildFluidCall = (chainId, _lender) => {
19891
19903
  address: resolvers.vaultResolver,
19892
19904
  name: "getVaultsEntireData",
19893
19905
  params: []
19906
+ },
19907
+ {
19908
+ address: resolvers.liquidityResolver,
19909
+ name: "getAllOverallTokensData",
19910
+ params: []
19894
19911
  }
19895
19912
  ];
19896
19913
  };
@@ -19903,12 +19920,36 @@ var scaleFluidRate = (raw) => {
19903
19920
  if (raw === void 0 || raw === null) return 0;
19904
19921
  return Number(raw) / FLUID_RATE_SCALE;
19905
19922
  };
19923
+ var parseOverallTokensData = (overallTokensData) => {
19924
+ const out = {};
19925
+ if (!Array.isArray(overallTokensData)) return out;
19926
+ for (const td of overallTokensData) {
19927
+ const rateData = td?.rateData;
19928
+ if (!rateData) continue;
19929
+ const version = Number(rateData.version ?? 1);
19930
+ const rawToken = ((version === 2 ? rateData.rateDataV2?.token : rateData.rateDataV1?.token) ?? "").toString().toLowerCase();
19931
+ if (!rawToken || rawToken === zeroAddress) continue;
19932
+ const token = normalizeUnderlying(rawToken);
19933
+ const maxUtilizationRaw = Number(td.maxUtilization ?? 0);
19934
+ out[token] = {
19935
+ utilization: Number(td.lastStoredUtilization ?? 0) / BPS,
19936
+ fee: Number(td.fee ?? 0) / BPS,
19937
+ maxUtilization: maxUtilizationRaw > 0 ? maxUtilizationRaw / BPS : 1,
19938
+ totalSupply: (td.totalSupply ?? 0n).toString(),
19939
+ totalBorrow: (td.totalBorrow ?? 0n).toString(),
19940
+ borrowRate: scaleFluidRate(td.borrowRate),
19941
+ supplyRate: scaleFluidRate(td.supplyRate)
19942
+ };
19943
+ }
19944
+ return out;
19945
+ };
19906
19946
  var getFluidPublicDataConverter = (_lender, chainId, prices, additionalYields, tokenList = {}) => {
19907
- const expectedNumberOfCalls = 1;
19947
+ const expectedNumberOfCalls = 2;
19908
19948
  return [
19909
19949
  (data) => {
19910
19950
  if (data.length !== expectedNumberOfCalls) return void 0;
19911
- const [vaultEntireData] = data;
19951
+ const [vaultEntireData, overallTokensData] = data;
19952
+ const liquidityByToken = parseOverallTokensData(overallTokensData);
19912
19953
  const out = {};
19913
19954
  for (const vault of vaultEntireData ?? []) {
19914
19955
  const parsed = parseVault(
@@ -19916,7 +19957,8 @@ var getFluidPublicDataConverter = (_lender, chainId, prices, additionalYields, t
19916
19957
  chainId,
19917
19958
  prices,
19918
19959
  additionalYields,
19919
- tokenList
19960
+ tokenList,
19961
+ liquidityByToken
19920
19962
  );
19921
19963
  if (parsed) {
19922
19964
  out[parsed.lender] = {
@@ -19931,7 +19973,7 @@ var getFluidPublicDataConverter = (_lender, chainId, prices, additionalYields, t
19931
19973
  expectedNumberOfCalls
19932
19974
  ];
19933
19975
  };
19934
- function parseVault(vault, chainId, prices, additionalYields, tokenList) {
19976
+ function parseVault(vault, chainId, prices, additionalYields, tokenList, liquidityByToken = {}) {
19935
19977
  const vaultAddress = (vault.vault ?? "").toLowerCase();
19936
19978
  const constants = vault.constantVariables;
19937
19979
  const configs = vault.configs;
@@ -19981,9 +20023,18 @@ function parseVault(vault, chainId, prices, additionalYields, tokenList) {
19981
20023
  const liquidationPenalty = Number(configs.liquidationPenalty ?? 0) / BPS;
19982
20024
  const supplyRate = scaleFluidRate(rates.supplyRateVault);
19983
20025
  const borrowRate = scaleFluidRate(rates.borrowRateVault);
19984
- const utilization = totalSupply > 0 ? totalBorrow / totalSupply : 0;
20026
+ const collateralLiquidity = liquidityByToken[collateralAddress];
20027
+ const loanLiquidity = liquidityByToken[loanAddress];
20028
+ const irmTotals = (state, decimals) => state ? {
20029
+ irmTotalDeposits: Number(parseRawAmount(state.totalSupply, decimals)),
20030
+ irmTotalDebt: Number(parseRawAmount(state.totalBorrow, decimals))
20031
+ } : {};
19985
20032
  const data = {};
19986
- const collateralMarketUid = createMarketUid(chainId, lender, collateralAddress);
20033
+ const collateralMarketUid = createMarketUid(
20034
+ chainId,
20035
+ lender,
20036
+ collateralAddress
20037
+ );
19987
20038
  data[collateralMarketUid] = {
19988
20039
  marketUid: collateralMarketUid,
19989
20040
  name: "Collateral " + (collateralMeta?.symbol ?? ""),
@@ -20000,7 +20051,13 @@ function parseVault(vault, chainId, prices, additionalYields, tokenList) {
20000
20051
  totalDebtUSD: 0,
20001
20052
  totalLiquidityUSD: totalSupply * collateralPrice,
20002
20053
  borrowLiquidityUSD: 0,
20003
- utilization: lockupRatio,
20054
+ // IRM x-coordinate: the collateral token's Liquidity-layer utilization —
20055
+ // it drives the supply rate (borrowRate × utilization × (1 - fee)).
20056
+ utilization: collateralLiquidity?.utilization ?? 0,
20057
+ ...irmTotals(collateralLiquidity, colDecimals),
20058
+ // Share of this vault's collateral currently locked below the LL
20059
+ // withdrawal limit (withdrawLimit / totalSupplyVault).
20060
+ lockupRatio,
20004
20061
  depositRate: supplyRate,
20005
20062
  // vault collateral earns the supply side rate
20006
20063
  variableBorrowRate: 0,
@@ -20046,7 +20103,10 @@ function parseVault(vault, chainId, prices, additionalYields, tokenList) {
20046
20103
  totalDebtUSD: totalBorrow * loanPrice,
20047
20104
  totalLiquidityUSD: borrowable * loanPrice,
20048
20105
  borrowLiquidityUSD: borrowable * loanPrice,
20049
- utilization,
20106
+ // IRM x-coordinate: the loan token's Liquidity-layer utilization — the
20107
+ // exact input the current borrow rate was computed from.
20108
+ utilization: loanLiquidity?.utilization ?? 0,
20109
+ ...irmTotals(loanLiquidity, borrowDecimals),
20050
20110
  depositRate: 0,
20051
20111
  variableBorrowRate: borrowRate,
20052
20112
  stableBorrowRate: 0,
@@ -20083,6 +20143,15 @@ function parseVault(vault, chainId, prices, additionalYields, tokenList) {
20083
20143
  expandDuration: Number(llSupply.expandDuration ?? 0),
20084
20144
  lastUpdateTimestamp: Number(llSupply.lastUpdateTimestamp ?? 0)
20085
20145
  } : void 0;
20146
+ const formatLiquidity = (state, decimals) => state ? {
20147
+ utilization: state.utilization,
20148
+ fee: state.fee,
20149
+ maxUtilization: state.maxUtilization,
20150
+ totalSupply: Number(parseRawAmount(state.totalSupply, decimals)),
20151
+ totalBorrow: Number(parseRawAmount(state.totalBorrow, decimals)),
20152
+ borrowRate: state.borrowRate,
20153
+ supplyRate: state.supplyRate
20154
+ } : void 0;
20086
20155
  return {
20087
20156
  lender,
20088
20157
  data,
@@ -20104,7 +20173,11 @@ function parseVault(vault, chainId, prices, additionalYields, tokenList) {
20104
20173
  oraclePriceLiquidate: configs.oraclePriceLiquidate?.toString(),
20105
20174
  isSmartCol: !!vault.isSmartCol,
20106
20175
  isSmartDebt: !!vault.isSmartDebt,
20107
- liquidityUserSupply
20176
+ liquidityUserSupply,
20177
+ liquidity: {
20178
+ supplyToken: formatLiquidity(collateralLiquidity, colDecimals),
20179
+ borrowToken: formatLiquidity(loanLiquidity, borrowDecimals)
20180
+ }
20108
20181
  }
20109
20182
  }
20110
20183
  };
@@ -20358,10 +20431,17 @@ var getGearboxV3PublicDataConverter = (_lender, chainId, prices, additionalYield
20358
20431
  underlyingDecimals
20359
20432
  )
20360
20433
  );
20361
- const poolTotalBorrowed = Number(
20362
- parseRawAmount(pool.totalBorrowed?.toString(), underlyingDecimals)
20434
+ const poolAvailableLiquidity = Number(
20435
+ parseRawAmount(
20436
+ pool.availableLiquidity?.toString(),
20437
+ underlyingDecimals
20438
+ )
20439
+ );
20440
+ const poolIrmDebt = Math.max(
20441
+ poolExpectedLiquidity - poolAvailableLiquidity,
20442
+ 0
20363
20443
  );
20364
- const poolUtilization = poolExpectedLiquidity > 0 ? poolTotalBorrowed / poolExpectedLiquidity : 0;
20444
+ const poolUtilization = poolExpectedLiquidity > 0 ? poolIrmDebt / poolExpectedLiquidity : 0;
20365
20445
  for (const cm of m.creditManagers ?? []) {
20366
20446
  const facade = cm?.creditFacade;
20367
20447
  const manager = cm?.creditManager;
@@ -20413,6 +20493,10 @@ var getGearboxV3PublicDataConverter = (_lender, chainId, prices, additionalYield
20413
20493
  totalLiquidityUSD: availableToBorrow * underlyingPrice,
20414
20494
  borrowLiquidityUSD: availableToBorrow * underlyingPrice,
20415
20495
  utilization: poolUtilization,
20496
+ // Pool-level totals — the basis the IRM's utilization is defined
20497
+ // over (shared across all credit managers on this pool).
20498
+ irmTotalDeposits: poolExpectedLiquidity,
20499
+ irmTotalDebt: poolIrmDebt,
20416
20500
  depositRate: supplyRate,
20417
20501
  variableBorrowRate: borrowRate,
20418
20502
  stableBorrowRate: 0,
@@ -20693,16 +20777,16 @@ var getAbi = (lender) => {
20693
20777
  if (isInit(lender)) return InitLensAbi;
20694
20778
  if (isMorphoType(lender)) return MorphoLensAbi;
20695
20779
  if (isEulerType(lender)) return vaultLensAbi;
20696
- if (isAaveV4Type(lender)) return [...AaveV4SpokeAbi, ...AaveV4OracleAbi, ...AaveV4HubAbi];
20780
+ if (isAaveV4Type(lender))
20781
+ return [...AaveV4SpokeAbi, ...AaveV4OracleAbi, ...AaveV4HubAbi];
20697
20782
  if (isSiloV2Type(lender))
20698
20783
  return [...SiloAbi, ...SiloLensAbi, ...InterestRateModelV2Abi];
20699
20784
  if (isSiloV3Type(lender))
20700
20785
  return [...SiloAbi, ...SiloLensAbi, ...InterestRateModelV2Abi];
20701
20786
  if (isFluid(lender))
20702
- return [...FluidVaultResolverAbi];
20787
+ return [...FluidVaultResolverAbi, ...FluidLiquidityResolverAbi];
20703
20788
  if (isDolomite(lender)) return [...DolomiteMarginAbi];
20704
- if (isGearboxV3(lender))
20705
- return [...GearboxMarketCompressorV310Abi];
20789
+ if (isGearboxV3(lender)) return [...GearboxMarketCompressorV310Abi];
20706
20790
  if (isSumerType(lender)) return [...SumerLensAbi, ...SumerComptrollerAbi];
20707
20791
  if (lender === Lender.TAKARA) return [...TakaraMarketStateAbi];
20708
20792
  if (isCompoundV2Type(lender)) return VenusLensAbi;
@@ -37206,7 +37290,12 @@ async function fetchFluidExposures(chainId, multicallRetry, prices, tokenList) {
37206
37290
  const rawVaults = await multicallRetry({
37207
37291
  chain: chainId,
37208
37292
  calls: vaultCalls,
37209
- abi: vaultCalls.map(() => FluidVaultResolverAbi)
37293
+ // buildFluidCall mixes vault-resolver and liquidity-resolver calls; the
37294
+ // merged ABI lets viem resolve each call by function name.
37295
+ abi: vaultCalls.map(() => [
37296
+ ...FluidVaultResolverAbi,
37297
+ ...FluidLiquidityResolverAbi
37298
+ ])
37210
37299
  });
37211
37300
  const [convert] = getFluidPublicDataConverter(
37212
37301
  "FLUID",