@1delta/margin-fetcher 0.0.180 → 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';
@@ -10909,7 +10909,7 @@ var apyToApr2 = (apy) => {
10909
10909
 
10910
10910
  // src/lending/user-data/utils/oraclePrice.ts
10911
10911
  function getOraclePrice(meta) {
10912
- const oracleUSD = meta?.oraclePrice?.oraclePriceUSD;
10912
+ const oracleUSD = meta?.oraclePrice?.oraclePriceUsd;
10913
10913
  if (oracleUSD != null && oracleUSD > 0) return oracleUSD;
10914
10914
  return meta?.price?.priceUsd ?? 1;
10915
10915
  }
@@ -10918,12 +10918,19 @@ function getDisplayPrice(meta) {
10918
10918
  }
10919
10919
 
10920
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
+ }
10921
10928
  function getMarketUidsFromMeta(meta) {
10922
10929
  return Object.keys(meta);
10923
10930
  }
10924
10931
 
10925
10932
  // src/lending/user-data/utils/createGeneralUserState.ts
10926
- function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, totalDebt24h = 0) {
10933
+ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, totalDebt24h = 0, lender) {
10927
10934
  let assetKeys = getMarketUidsFromMeta(lenderData);
10928
10935
  const { chainId, account } = payload;
10929
10936
  const mode = String(payload.userEMode ?? 0);
@@ -11043,7 +11050,8 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11043
11050
  Math.min(withdrawableUSD / price, Number(pos.deposits))
11044
11051
  );
11045
11052
  }
11046
- if (withdrawLiquidity != null) {
11053
+ const isCollateralOnlyMorpho = lender && isMorphoType(lender) && !flags?.borrowingEnabled;
11054
+ if (withdrawLiquidity != null && !isCollateralOnlyMorpho) {
11047
11055
  withdrawable = String(Math.min(Number(withdrawable), withdrawLiquidity));
11048
11056
  }
11049
11057
  let borrowable;
@@ -11058,6 +11066,7 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11058
11066
  }
11059
11067
  pos.withdrawable = withdrawable;
11060
11068
  pos.borrowable = borrowable;
11069
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11061
11070
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11062
11071
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11063
11072
  if (borrowLiquidity != null) {
@@ -11074,7 +11083,8 @@ function createBaseTypeUserState(payload, lenderData, totalDeposits24h = 0, tota
11074
11083
  collateralEnabled: false,
11075
11084
  claimableRewards: 0,
11076
11085
  withdrawable: "0",
11077
- borrowable
11086
+ borrowable,
11087
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11078
11088
  };
11079
11089
  }
11080
11090
  }
@@ -11814,6 +11824,7 @@ function createSumerUserState(payload, lenderData, totalDeposits24h = 0, totalDe
11814
11824
  }
11815
11825
  pos.withdrawable = withdrawable;
11816
11826
  pos.borrowable = borrowable;
11827
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11817
11828
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11818
11829
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11819
11830
  if (borrowLiquidity != null) {
@@ -11830,7 +11841,8 @@ function createSumerUserState(payload, lenderData, totalDeposits24h = 0, totalDe
11830
11841
  collateralEnabled: false,
11831
11842
  claimableRewards: 0,
11832
11843
  withdrawable: "0",
11833
- borrowable
11844
+ borrowable,
11845
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11834
11846
  };
11835
11847
  }
11836
11848
  }
@@ -11969,6 +11981,7 @@ function createMultiAccountTypeUserState(payload, lenderData, histData) {
11969
11981
  }
11970
11982
  pos.withdrawable = withdrawable;
11971
11983
  pos.borrowable = borrowable;
11984
+ pos.underlyingInfo = buildUnderlyingInfo(lenderData[marketUid]);
11972
11985
  } else if ((deposits > 0 || debt > 0) && flags?.borrowingEnabled && !flags?.isFrozen && !config?.debtDisabled) {
11973
11986
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
11974
11987
  if (borrowLiquidity != null) {
@@ -11983,7 +11996,8 @@ function createMultiAccountTypeUserState(payload, lenderData, histData) {
11983
11996
  collateralEnabled: false,
11984
11997
  claimableRewards: 0,
11985
11998
  withdrawable: "0",
11986
- borrowable
11999
+ borrowable,
12000
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
11987
12001
  };
11988
12002
  }
11989
12003
  }
@@ -12072,7 +12086,8 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12072
12086
  oracleAddress,
12073
12087
  loanAsset,
12074
12088
  collateralAsset,
12075
- state
12089
+ state,
12090
+ whitelisted
12076
12091
  } = market;
12077
12092
  if (collateralAsset && loanAsset && oracleAddress && oracleAddress !== zeroAddress) {
12078
12093
  const m = "MORPHO_BLUE_" + uniqueKey.slice(2).toUpperCase();
@@ -12187,7 +12202,8 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12187
12202
  oracle: oracleAddress,
12188
12203
  irm: irmAddress,
12189
12204
  collateralAddress: collateralAssetAddress,
12190
- loanAddress: loanAssetAddress
12205
+ loanAddress: loanAssetAddress,
12206
+ isListed: whitelisted
12191
12207
  }
12192
12208
  };
12193
12209
  data[m].chainId = chainId;
@@ -12197,13 +12213,12 @@ function convertMarketsToMorphoResponse(response, chainId, additionalYields = {
12197
12213
  }
12198
12214
 
12199
12215
  // src/lending/public-data/morpho/fetchPublic.ts
12200
- var query = (first, skip, chainId) => `
12216
+ var query = (first, skip, chainId, includeUnlisted = false) => `
12201
12217
  query GetMarkets {
12202
12218
  markets(first: ${first}, skip: ${skip}, where: {
12203
- chainId_in: [${chainId}],
12204
- whitelisted: true
12219
+ chainId_in: [${chainId}]${includeUnlisted ? "" : ",\n whitelisted: true"}
12205
12220
  },
12206
- orderBy: SupplyAssetsUsd,
12221
+ orderBy: SupplyAssetsUsd,
12207
12222
  orderDirection: Desc
12208
12223
  ) {
12209
12224
  items {
@@ -12211,6 +12226,7 @@ query GetMarkets {
12211
12226
  irmAddress
12212
12227
  oracleAddress
12213
12228
  lltv
12229
+ whitelisted
12214
12230
  loanAsset {
12215
12231
  address
12216
12232
  name
@@ -12248,10 +12264,10 @@ query GetMarkets {
12248
12264
  }
12249
12265
  `;
12250
12266
  var BASE_URL = "https://blue-api.morpho.org/graphql";
12251
- async function fetchMorphoMarkets(chainId) {
12267
+ async function fetchMorphoMarkets(chainId, includeUnlisted = false) {
12252
12268
  if (chainId !== Chain.ETHEREUM_MAINNET) {
12253
12269
  const requestBody = {
12254
- query: query(200, 0, chainId),
12270
+ query: query(200, 0, chainId, includeUnlisted),
12255
12271
  variables: {}
12256
12272
  };
12257
12273
  const response = await fetch(BASE_URL, {
@@ -12270,11 +12286,11 @@ async function fetchMorphoMarkets(chainId) {
12270
12286
  return data.data;
12271
12287
  }
12272
12288
  const requestBody0 = {
12273
- query: query(200, 0, chainId),
12289
+ query: query(200, 0, chainId, includeUnlisted),
12274
12290
  variables: {}
12275
12291
  };
12276
12292
  const requestBody1 = {
12277
- query: query(200, 200, chainId),
12293
+ query: query(200, 200, chainId, includeUnlisted),
12278
12294
  variables: {}
12279
12295
  };
12280
12296
  const [data0, data1] = await Promise.all([
@@ -17954,8 +17970,8 @@ var getLenderPublicData = async (chainId, lenders, prices, additionalYields, mul
17954
17970
  }
17955
17971
  return lenderData;
17956
17972
  };
17957
- async function getLenderDataFromApi(lender, chainId, prices, additionalYields) {
17958
- 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);
17959
17975
  return {};
17960
17976
  }
17961
17977
  function convertLenderDataFromApi(lender, chainId, data, prices, additionalYields, list = {}) {
@@ -17965,11 +17981,11 @@ function convertLenderDataFromApi(lender, chainId, data, prices, additionalYield
17965
17981
  }
17966
17982
  var getLenderPublicDataViaApi = async (chainId, lenders, prices, additionalYields, tokenList = async () => {
17967
17983
  return {};
17968
- }) => {
17984
+ }, includeUnlisted = false) => {
17969
17985
  let promises = [];
17970
17986
  for (const lender of lenders) {
17971
17987
  promises.push(
17972
- getLenderDataFromApi(lender, chainId)
17988
+ getLenderDataFromApi(lender, chainId, prices, additionalYields, includeUnlisted)
17973
17989
  );
17974
17990
  }
17975
17991
  const [list, ...results] = await Promise.all([tokenList(), ...promises]);
@@ -18017,7 +18033,7 @@ function lenderCanUseApi(lender, chainId) {
18017
18033
  }
18018
18034
  return false;
18019
18035
  }
18020
- var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields, multicallRetry3, tokenList) => {
18036
+ var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields, multicallRetry3, tokenList, includeUnlistedMorphoMarkets = false) => {
18021
18037
  const lendersApi = lenders.filter((l) => lenderCanUseApi(l, chainId));
18022
18038
  const lendersOnChain = lenders.filter((l) => !lenderCanUseApi(l, chainId));
18023
18039
  const onChain = getLenderPublicData(
@@ -18033,7 +18049,8 @@ var getLenderPublicDataAll = async (chainId, lenders, prices, additionalYields,
18033
18049
  lendersApi,
18034
18050
  prices,
18035
18051
  additionalYields,
18036
- tokenList
18052
+ tokenList,
18053
+ includeUnlistedMorphoMarkets
18037
18054
  );
18038
18055
  const [onChainRes, apiRes] = await Promise.all([onChain, api]);
18039
18056
  return { ...onChainRes, ...apiRes };
@@ -19099,7 +19116,8 @@ var getMorphoUserDataConverterWithlens = (lender, chainId, account, markets, met
19099
19116
  payload,
19100
19117
  metaMap?.[lenderKey],
19101
19118
  addedDeposits,
19102
- addedDebt
19119
+ addedDebt,
19120
+ lenderKey
19103
19121
  );
19104
19122
  datas[lenderKey] = userData;
19105
19123
  });
@@ -19162,7 +19180,8 @@ var getListaUserDataConverterWithlens = (lender, chainId, account, markets, meta
19162
19180
  payload,
19163
19181
  metaMap?.[lenderKey],
19164
19182
  addedDeposits,
19165
- addedDebt
19183
+ addedDebt,
19184
+ lenderKey
19166
19185
  );
19167
19186
  datas[lenderKey] = {
19168
19187
  ...payload,
@@ -19369,6 +19388,7 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19369
19388
  const price = getOraclePrice(lenderData[marketUid]);
19370
19389
  const bcf = config?.borrowCollateralFactor ?? 1;
19371
19390
  const bf = config?.borrowFactor ?? 1;
19391
+ const vaultAddr = lenderData[marketUid]?.params?.metadata?.vault;
19372
19392
  const pos = payload.lendingPositions[subAccountIndex]?.[marketUid];
19373
19393
  if (pos) {
19374
19394
  let withdrawable;
@@ -19390,6 +19410,8 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19390
19410
  let borrowable;
19391
19411
  if (!flags?.borrowingEnabled || flags?.isFrozen || config?.debtDisabled) {
19392
19412
  borrowable = "0";
19413
+ } else if (debt > 0 && vaultAddr !== controller) {
19414
+ borrowable = "0";
19393
19415
  } else {
19394
19416
  const borrowableUSD = Math.max(creditLine / bf, 0);
19395
19417
  borrowable = String(borrowableUSD / price);
@@ -19399,7 +19421,10 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19399
19421
  }
19400
19422
  pos.withdrawable = withdrawable;
19401
19423
  pos.borrowable = borrowable;
19402
- } 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)) {
19403
19428
  let borrowable = String(Math.max(creditLine / bf, 0) / price);
19404
19429
  if (borrowLiquidity != null) {
19405
19430
  borrowable = String(Math.min(Number(borrowable), borrowLiquidity));
@@ -19419,18 +19444,21 @@ function createEulerMultiAccountTypeUserState(payload, lenderData, histData) {
19419
19444
  collateralEnabled: false,
19420
19445
  isAllowed: false,
19421
19446
  withdrawable: "0",
19422
- borrowable
19447
+ borrowable,
19448
+ underlyingInfo: buildUnderlyingInfo(lenderData[marketUid])
19423
19449
  };
19424
19450
  }
19425
19451
  }
19426
19452
  data.push({
19427
19453
  accountId: subAccountIndex,
19428
- 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,
19429
19455
  borrowCapacityUSD: creditLine,
19430
19456
  userConfig,
19431
19457
  balanceData: balanceData2,
19432
19458
  aprData: aprData2,
19433
- positions: Object.values(payload.lendingPositions[subAccountIndex])
19459
+ positions: Object.values(
19460
+ payload.lendingPositions[subAccountIndex]
19461
+ )
19434
19462
  });
19435
19463
  }
19436
19464
  return {
@@ -24652,7 +24680,7 @@ function resolveDebitDataKey(chainId, lender, tokenAddress, cToken) {
24652
24680
  function needsLenderApproval(params) {
24653
24681
  const { lender, lenderDebitData, tokenAddress, amount, chainId, cToken } = params;
24654
24682
  if (!lenderDebitData) return true;
24655
- if (isInit(lender)) {
24683
+ if (isInit(lender) || isMorphoType(lender)) {
24656
24684
  const entry2 = Object.values(lenderDebitData)[0];
24657
24685
  if (!entry2 || entry2.amount === void 0) return true;
24658
24686
  return entry2.amount === 0n;
@@ -24664,7 +24692,7 @@ function needsLenderApproval(params) {
24664
24692
  const requiredShares = toCompoundV2Shares(entry, amount);
24665
24693
  return entry.amount < requiredShares;
24666
24694
  }
24667
- if (isCompoundV3(lender) || isMorphoType(lender)) {
24695
+ if (isCompoundV3(lender)) {
24668
24696
  return entry.amount === 0n;
24669
24697
  }
24670
24698
  return entry.amount < amount;