@1delta/margin-fetcher 0.0.179 → 0.0.181

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
@@ -1,7 +1,7 @@
1
1
  import { parseAbi, formatEther, BaseError, encodeFunctionData, formatUnits, decodeFunctionResult, decodeAbiParameters, AbiEncodingLengthMismatchError, concatHex, isAddress, InvalidAddressError, pad, stringToHex, boolToHex, integerRegex, numberToHex, bytesRegex, BytesSizeMismatchError, arrayRegex, UnsupportedPackedAbiType } from './chunk-ZVIJSUIM.js';
2
2
  import './chunk-BYTNVMX7.js';
3
3
  import './chunk-PR4QN5HX.js';
4
- import { Lender, isAaveType, isCompoundV3, isMultiMarket, isInit, isCompoundV2Type, isVenusType, isMorphoType, isSumerType, AAVE_V3_LENDERS, AAVE_V2_LENDERS, isAaveV2Type, isAaveV32Type, isAaveV3Type, isEulerType, isYLDR, isCompoundV3Type, isTectonicType, isBenqiType, isLista } from '@1delta/lender-registry';
4
+ import { Lender, isAaveType, isCompoundV3, isMultiMarket, isInit, isMorphoType, isCompoundV2Type, isVenusType, isSumerType, AAVE_V3_LENDERS, AAVE_V2_LENDERS, isAaveV2Type, isAaveV32Type, isAaveV3Type, isEulerType, isYLDR, isCompoundV3Type, isTectonicType, isBenqiType, isLista } from '@1delta/lender-registry';
5
5
  export { isAaveType, isAaveV2Type, isAaveV32Type, isAaveV3Type, isCompoundV3, isCompoundV3Type, isInit, isMorphoType, isMultiMarket, isYLDR } from '@1delta/lender-registry';
6
6
  import lodash from 'lodash';
7
7
  import { getEvmChain, multicallRetry, getEvmClient, getEvmClientUniversal } from '@1delta/providers';
@@ -7486,7 +7486,8 @@ var DISABLED_COMPOUNDS = {
7486
7486
  };
7487
7487
  var ENABLED_EULER_V2_CHAINS = [
7488
7488
  Chain.BNB_SMART_CHAIN_MAINNET,
7489
- Chain.PLASMA_MAINNET
7489
+ Chain.PLASMA_MAINNET,
7490
+ Chain.BERACHAIN
7490
7491
  ];
7491
7492
  var getLendersForChain = (c) => {
7492
7493
  let lenders = [];
@@ -10908,7 +10909,7 @@ var apyToApr2 = (apy) => {
10908
10909
 
10909
10910
  // src/lending/user-data/utils/oraclePrice.ts
10910
10911
  function getOraclePrice(meta) {
10911
- const oracleUSD = meta?.oraclePrice?.oraclePriceUSD;
10912
+ const oracleUSD = meta?.oraclePrice?.oraclePriceUsd;
10912
10913
  if (oracleUSD != null && oracleUSD > 0) return oracleUSD;
10913
10914
  return meta?.price?.priceUsd ?? 1;
10914
10915
  }
@@ -10917,12 +10918,19 @@ function getDisplayPrice(meta) {
10917
10918
  }
10918
10919
 
10919
10920
  // src/lending/user-data/utils/types.ts
10921
+ function buildUnderlyingInfo(meta) {
10922
+ return {
10923
+ asset: meta.asset,
10924
+ oraclePrice: meta.oraclePrice,
10925
+ prices: meta.price
10926
+ };
10927
+ }
10920
10928
  function getMarketUidsFromMeta(meta) {
10921
10929
  return Object.keys(meta);
10922
10930
  }
10923
10931
 
10924
10932
  // src/lending/user-data/utils/createGeneralUserState.ts
10925
- function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, totalDebt24h = 0) {
10933
+ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, totalDebt24h = 0, lender) {
10926
10934
  let assetKeys = getMarketUidsFromMeta(lenderData);
10927
10935
  const { chainId, account } = payload;
10928
10936
  const mode = String(payload.userEMode ?? 0);
@@ -11042,7 +11050,8 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11042
11050
  Math.min(withdrawableUSD / price, Number(pos.deposits))
11043
11051
  );
11044
11052
  }
11045
- if (withdrawLiquidity != null) {
11053
+ const isCollateralOnlyMorpho = lender && isMorphoType(lender) && !flags?.borrowingEnabled;
11054
+ if (withdrawLiquidity != null && !isCollateralOnlyMorpho) {
11046
11055
  withdrawable = String(Math.min(Number(withdrawable), withdrawLiquidity));
11047
11056
  }
11048
11057
  let borrowable;
@@ -11057,6 +11066,7 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11057
11066
  }
11058
11067
  pos.withdrawable = withdrawable;
11059
11068
  pos.borrowable = borrowable;
11069
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11060
11070
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11061
11071
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11062
11072
  if (borrowLiquidity != null) {
@@ -11073,7 +11083,8 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11073
11083
  collateralEnabled: false,
11074
11084
  claimableRewards: 0,
11075
11085
  withdrawable: "0",
11076
- borrowable
11086
+ borrowable,
11087
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11077
11088
  };
11078
11089
  }
11079
11090
  }
@@ -11813,6 +11824,7 @@ function createSumerUserState(payload, lenderData, totalDeposits24h = 0, totalDe
11813
11824
  }
11814
11825
  pos.withdrawable = withdrawable;
11815
11826
  pos.borrowable = borrowable;
11827
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11816
11828
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11817
11829
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11818
11830
  if (borrowLiquidity != null) {
@@ -11829,7 +11841,8 @@ function createSumerUserState(payload, lenderData, totalDeposits24h = 0, totalDe
11829
11841
  collateralEnabled: false,
11830
11842
  claimableRewards: 0,
11831
11843
  withdrawable: "0",
11832
- borrowable
11844
+ borrowable,
11845
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11833
11846
  };
11834
11847
  }
11835
11848
  }
@@ -11968,6 +11981,7 @@ function createMultiAccountTypeUserState(payload, lenderData, histData) {
11968
11981
  }
11969
11982
  pos.withdrawable = withdrawable;
11970
11983
  pos.borrowable = borrowable;
11984
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11971
11985
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11972
11986
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11973
11987
  if (borrowLiquidity != null) {
@@ -11982,7 +11996,8 @@ function createMultiAccountTypeUserState(payload, lenderData, histData) {
11982
11996
  collateralEnabled: false,
11983
11997
  claimableRewards: 0,
11984
11998
  withdrawable: "0",
11985
- borrowable
11999
+ borrowable,
12000
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11986
12001
  };
11987
12002
  }
11988
12003
  }
@@ -12071,7 +12086,8 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12071
12086
  oracleAddress,
12072
12087
  loanAsset,
12073
12088
  collateralAsset,
12074
- state
12089
+ state,
12090
+ whitelisted
12075
12091
  } = market;
12076
12092
  if (collateralAsset && loanAsset && oracleAddress && oracleAddress !== zeroAddress) {
12077
12093
  const m = "MORPHO_BLUE_" + uniqueKey.slice(2).toUpperCase();
@@ -12186,7 +12202,8 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12186
12202
  oracle: oracleAddress,
12187
12203
  irm: irmAddress,
12188
12204
  collateralAddress: collateralAssetAddress,
12189
- loanAddress: loanAssetAddress
12205
+ loanAddress: loanAssetAddress,
12206
+ isListed: whitelisted
12190
12207
  }
12191
12208
  };
12192
12209
  data[m].chainId = chainId;
@@ -12196,13 +12213,12 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12196
12213
  }
12197
12214
 
12198
12215
  // src/lending/public-data/morpho/fetchPublic.ts
12199
- var query = (first, skip, chainId) => `
12216
+ var query = (first, skip, chainId, includeUnlisted = false) => `
12200
12217
  query GetMarkets {
12201
12218
  markets(first: ${first}, skip: ${skip}, where: {
12202
- chainId_in: [${chainId}],
12203
- whitelisted: true
12219
+ chainId_in: [${chainId}]${includeUnlisted ? "" : ",\n whitelisted: true"}
12204
12220
  },
12205
- orderBy: SupplyAssetsUsd,
12221
+ orderBy: SupplyAssetsUsd,
12206
12222
  orderDirection: Desc
12207
12223
  ) {
12208
12224
  items {
@@ -12210,6 +12226,7 @@ query GetMarkets {
12210
12226
  irmAddress
12211
12227
  oracleAddress
12212
12228
  lltv
12229
+ whitelisted
12213
12230
  loanAsset {
12214
12231
  address
12215
12232
  name
@@ -12247,10 +12264,10 @@ query GetMarkets {
12247
12264
  }
12248
12265
  `;
12249
12266
  var BASE_URL = "https://blue-api.morpho.org/graphql";
12250
- async function fetchMorphoMarkets(chainId) {
12267
+ async function fetchMorphoMarkets(chainId, includeUnlisted = false) {
12251
12268
  if (chainId !== Chain.ETHEREUM_MAINNET) {
12252
12269
  const requestBody = {
12253
- query: query(200, 0, chainId),
12270
+ query: query(200, 0, chainId, includeUnlisted),
12254
12271
  variables: {}
12255
12272
  };
12256
12273
  const response = await fetch(BASE_URL, {
@@ -12269,11 +12286,11 @@ async function fetchMorphoMarkets(chainId) {
12269
12286
  return data.data;
12270
12287
  }
12271
12288
  const requestBody0 = {
12272
- query: query(200, 0, chainId),
12289
+ query: query(200, 0, chainId, includeUnlisted),
12273
12290
  variables: {}
12274
12291
  };
12275
12292
  const requestBody1 = {
12276
- query: query(200, 200, chainId),
12293
+ query: query(200, 200, chainId, includeUnlisted),
12277
12294
  variables: {}
12278
12295
  };
12279
12296
  const [data0, data1] = await Promise.all([
@@ -17347,7 +17364,7 @@ var buildEulerV2LenderReserveCall = (chainId, lender) => {
17347
17364
  // src/lending/public-data/euler/fetcher/cluster.ts
17348
17365
  function getActiveCollaterals(ltvInfo) {
17349
17366
  const active = /* @__PURE__ */ new Set();
17350
- for (const entry of ltvInfo) {
17367
+ for (const entry of ltvInfo ?? []) {
17351
17368
  if (entry.borrowLTV > 0n) {
17352
17369
  active.add(entry.collateral.toLowerCase());
17353
17370
  }
@@ -17411,12 +17428,12 @@ function findConnectedComponents(adj) {
17411
17428
  }
17412
17429
  function deriveGovernor(vaultAddresses, vaultData) {
17413
17430
  const counts = /* @__PURE__ */ new Map();
17414
- const zeroNorm = zeroAddress.toLowerCase();
17431
+ const zeroNorm = zeroAddress;
17415
17432
  for (const addr of vaultAddresses) {
17416
17433
  const info = findVaultInfo(addr, vaultData);
17417
17434
  if (!info) continue;
17418
- const gov = info.governorAdmin.toLowerCase();
17419
- if (gov === zeroNorm) continue;
17435
+ const gov = info.governorAdmin?.toLowerCase();
17436
+ if (!gov || gov === zeroNorm) continue;
17420
17437
  counts.set(gov, (counts.get(gov) ?? 0) + 1);
17421
17438
  }
17422
17439
  let best = zeroAddress;
@@ -17476,8 +17493,8 @@ function toTokenAmount(raw, decimals) {
17476
17493
  function identifyBorrowVaults(cluster) {
17477
17494
  const borrowVaults = /* @__PURE__ */ new Set();
17478
17495
  for (const [addr, info] of cluster.vaultData) {
17479
- const hasIRM = info.interestRateModel.toLowerCase() !== zeroAddress.toLowerCase();
17480
- const hasActiveCollateral = info.collateralLTVInfo.some(
17496
+ const hasIRM = info.interestRateModel && info.interestRateModel.toLowerCase() !== zeroAddress;
17497
+ const hasActiveCollateral = (info.collateralLTVInfo ?? []).some(
17481
17498
  (ltv) => ltv.borrowLTV > 0n
17482
17499
  );
17483
17500
  if (hasIRM && hasActiveCollateral) {
@@ -17490,7 +17507,7 @@ function buildReverseLTVIndex(cluster) {
17490
17507
  const index = /* @__PURE__ */ new Map();
17491
17508
  for (const [addr, info] of cluster.vaultData) {
17492
17509
  const borrowVault = addr.toLowerCase();
17493
- for (const ltv of info.collateralLTVInfo) {
17510
+ for (const ltv of info.collateralLTVInfo ?? []) {
17494
17511
  if (ltv.borrowLTV <= 0n) continue;
17495
17512
  const collateral = ltv.collateral.toLowerCase();
17496
17513
  let entries = index.get(collateral);
@@ -17563,6 +17580,7 @@ function buildMetadata(info) {
17563
17580
  };
17564
17581
  }
17565
17582
  function buildTokenEntry(info, config, collateralActive, borrowVaults, opts) {
17583
+ if (!info.asset) return void 0;
17566
17584
  const addr = info.asset.toLowerCase();
17567
17585
  const tokenMeta = opts.tokenList?.[addr];
17568
17586
  const asset = {
@@ -17651,13 +17669,14 @@ function normalizeCluster(cluster, opts) {
17651
17669
  borrowVaults,
17652
17670
  cluster.vaultData
17653
17671
  );
17654
- data[norm] = buildTokenEntry(
17672
+ const entry = buildTokenEntry(
17655
17673
  info,
17656
17674
  config,
17657
17675
  collateralActiveVaults.has(norm),
17658
17676
  borrowVaults,
17659
17677
  opts
17660
17678
  );
17679
+ if (entry) data[norm] = entry;
17661
17680
  }
17662
17681
  return {
17663
17682
  data,
@@ -17951,8 +17970,8 @@ var getLenderPublicData = async (chainId, lenders, prices, additionalYields, mul
17951
17970
  }
17952
17971
  return lenderData;
17953
17972
  };
17954
- async function getLenderDataFromApi(lender, chainId, prices, additionalYields) {
17955
- if (isMorphoType(lender)) return await fetchMorphoMarkets(chainId);
17973
+ async function getLenderDataFromApi(lender, chainId, prices, additionalYields, includeUnlisted = false) {
17974
+ if (isMorphoType(lender)) return await fetchMorphoMarkets(chainId, includeUnlisted);
17956
17975
  return {};
17957
17976
  }
17958
17977
  function convertLenderDataFromApi(lender, chainId, data, prices, additionalYields, list = {}) {
@@ -17962,11 +17981,11 @@ function convertLenderDataFromApi(lender, chainId, data, prices, additionalYield
17962
17981
  }
17963
17982
  var getLenderPublicDataViaApi = async (chainId, lenders, prices, additionalYields, tokenList = async () => {
17964
17983
  return {};
17965
- }) => {
17984
+ }, includeUnlisted = false) => {
17966
17985
  let promises = [];
17967
17986
  for (const lender of lenders) {
17968
17987
  promises.push(
17969
- getLenderDataFromApi(lender, chainId)
17988
+ getLenderDataFromApi(lender, chainId, prices, additionalYields, includeUnlisted)
17970
17989
  );
17971
17990
  }
17972
17991
  const [list, ...results] = await Promise.all([tokenList(), ...promises]);
@@ -18014,7 +18033,7 @@ function lenderCanUseApi(lender, chainId) {
18014
18033
  }
18015
18034
  return false;
18016
18035
  }
18017
- var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields, multicallRetry3, tokenList) => {
18036
+ var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields, multicallRetry3, tokenList, includeUnlistedMorphoMarkets = false) => {
18018
18037
  const lendersApi = lenders.filter((l) => lenderCanUseApi(l, chainId));
18019
18038
  const lendersOnChain = lenders.filter((l) => !lenderCanUseApi(l, chainId));
18020
18039
  const onChain = getLenderPublicData(
@@ -18030,7 +18049,8 @@ var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields,
18030
18049
  lendersApi,
18031
18050
  prices,
18032
18051
  additionalYields,
18033
- tokenList
18052
+ tokenList,
18053
+ includeUnlistedMorphoMarkets
18034
18054
  );
18035
18055
  const [onChainRes, apiRes] = await Promise.all([onChain, api]);
18036
18056
  return { ...onChainRes, ...apiRes };
@@ -19096,7 +19116,8 @@ var getMorphoUserDataConverterWithlens = (lender, chainId, account, markets, met
19096
19116
  payload,
19097
19117
  metaMap?.[lenderKey],
19098
19118
  addedDeposits,
19099
- addedDebt
19119
+ addedDebt,
19120
+ lenderKey
19100
19121
  );
19101
19122
  datas[lenderKey] = userData;
19102
19123
  });
@@ -19159,7 +19180,8 @@ var getListaUserDataConverterWithlens = (lender, chainId, account, markets, meta
19159
19180
  payload,
19160
19181
  metaMap?.[lenderKey],
19161
19182
  addedDeposits,
19162
- addedDebt
19183
+ addedDebt,
19184
+ lenderKey
19163
19185
  );
19164
19186
  datas[lenderKey] = {
19165
19187
  ...payload,
@@ -19366,6 +19388,7 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19366
19388
  const price = getOraclePrice(lenderData[marketUid]);
19367
19389
  const bcf = config?.borrowCollateralFactor ?? 1;
19368
19390
  const bf = config?.borrowFactor ?? 1;
19391
+ const vaultAddr = lenderData[marketUid]?.params?.metadata?.vault;
19369
19392
  const pos = payload.lendingPositions[subAccountIndex]?.[marketUid];
19370
19393
  if (pos) {
19371
19394
  let withdrawable;
@@ -19387,6 +19410,8 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19387
19410
  let borrowable;
19388
19411
  if (!flags?.borrowingEnabled || flags?.isFrozen || config?.debtDisabled) {
19389
19412
  borrowable = "0";
19413
+ } else if (debt > 0 && vaultAddr !== controller) {
19414
+ borrowable = "0";
19390
19415
  } else {
19391
19416
  const borrowableUSD = Math.max(creditLine / bf, 0);
19392
19417
  borrowable = String(borrowableUSD / price);
@@ -19396,7 +19421,10 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19396
19421
  }
19397
19422
  pos.withdrawable = withdrawable;
19398
19423
  pos.borrowable = borrowable;
19399
- } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
19424
+ pos.underlyingInfo = buildUnderlyingInfo(
19425
+ lenderData[marketUid]
19426
+ );
19427
+ } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled && (debt === 0 || vaultAddr === controller)) {
19400
19428
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
19401
19429
  if (borrowLiquidity != null) {
19402
19430
  borrowable = String(Math.min(Number(borrowable), borrowLiquidity));
@@ -19416,18 +19444,21 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19416
19444
  collateralEnabled: false,
19417
19445
  isAllowed: false,
19418
19446
  withdrawable: "0",
19419
- borrowable
19447
+ borrowable,
19448
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
19420
19449
  };
19421
19450
  }
19422
19451
  }
19423
19452
  data.push({
19424
19453
  accountId: subAccountIndex,
19425
- health: balanceData2.debt === 0 ? null : balanceData2.adjustedDebt > 0 ? balanceData2.borrowDiscountedCollateral / balanceData2.adjustedDebt : balanceData2.collateral / balanceData2.debt,
19454
+ health: balanceData2.debt === 0 ? null : balanceData2.adjustedDebt > 0 ? balanceData2.collateral / balanceData2.adjustedDebt : balanceData2.collateral / balanceData2.debt,
19426
19455
  borrowCapacityUSD: creditLine,
19427
19456
  userConfig,
19428
19457
  balanceData: balanceData2,
19429
19458
  aprData: aprData2,
19430
- positions: Object.values(payload.lendingPositions[subAccountIndex])
19459
+ positions: Object.values(
19460
+ payload.lendingPositions[subAccountIndex]
19461
+ )
19431
19462
  });
19432
19463
  }
19433
19464
  return {
@@ -24649,7 +24680,7 @@ function resolveDebitDataKey(chainId, lender, tokenAddress, cToken) {
24649
24680
  function needsLenderApproval(params) {
24650
24681
  const { lender, lenderDebitData, tokenAddress, amount, chainId, cToken } = params;
24651
24682
  if (!lenderDebitData) return true;
24652
- if (isInit(lender)) {
24683
+ if (isInit(lender) || isMorphoType(lender)) {
24653
24684
  const entry2 = Object.values(lenderDebitData)[0];
24654
24685
  if (!entry2 || entry2.amount === void 0) return true;
24655
24686
  return entry2.amount === 0n;
@@ -24661,7 +24692,7 @@ function needsLenderApproval(params) {
24661
24692
  const requiredShares = toCompoundV2Shares(entry, amount);
24662
24693
  return entry.amount < requiredShares;
24663
24694
  }
24664
- if (isCompoundV3(lender) || isMorphoType(lender)) {
24695
+ if (isCompoundV3(lender)) {
24665
24696
  return entry.amount === 0n;
24666
24697
  }
24667
24698
  return entry.amount < amount;