@defisaver/positions-sdk 2.1.127-midnight-1-dev → 2.1.127-midnight-3-dev

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.
Files changed (76) hide show
  1. package/cjs/aaveV3/index.js +7 -1
  2. package/cjs/aaveV4/lend.js +3 -3
  3. package/cjs/claiming/index.d.ts +2 -1
  4. package/cjs/claiming/index.js +3 -1
  5. package/cjs/claiming/uniswap.d.ts +5 -0
  6. package/cjs/claiming/uniswap.js +75 -0
  7. package/cjs/config/contracts.d.ts +22 -0
  8. package/cjs/config/contracts.js +9 -1
  9. package/cjs/contracts.d.ts +35 -0
  10. package/cjs/contracts.js +2 -1
  11. package/cjs/curveUsd/index.js +1 -1
  12. package/cjs/helpers/morphoBlueHelpers/index.js +1 -0
  13. package/cjs/helpers/morphoMidnightHelpers/index.d.ts +45 -7
  14. package/cjs/helpers/morphoMidnightHelpers/index.js +101 -13
  15. package/cjs/llamaLend/index.js +1 -1
  16. package/cjs/markets/aaveV4/index.d.ts +2 -0
  17. package/cjs/markets/aaveV4/index.js +14 -1
  18. package/cjs/morphoBlue/index.d.ts +8 -6
  19. package/cjs/morphoBlue/index.js +69 -40
  20. package/cjs/portfolio/index.js +31 -2
  21. package/cjs/savings/summerVaults/options.js +10 -10
  22. package/cjs/staking/staking.js +3 -0
  23. package/cjs/types/aaveV4.d.ts +2 -1
  24. package/cjs/types/aaveV4.js +1 -0
  25. package/cjs/types/claiming.d.ts +10 -0
  26. package/cjs/types/claiming.js +2 -0
  27. package/cjs/types/morphoBlue.d.ts +9 -0
  28. package/cjs/types/morphoMidnight.d.ts +9 -0
  29. package/esm/aaveV3/index.js +7 -1
  30. package/esm/aaveV4/lend.js +3 -3
  31. package/esm/claiming/index.d.ts +2 -1
  32. package/esm/claiming/index.js +2 -1
  33. package/esm/claiming/uniswap.d.ts +5 -0
  34. package/esm/claiming/uniswap.js +67 -0
  35. package/esm/config/contracts.d.ts +22 -0
  36. package/esm/config/contracts.js +8 -0
  37. package/esm/contracts.d.ts +35 -0
  38. package/esm/contracts.js +1 -0
  39. package/esm/curveUsd/index.js +1 -1
  40. package/esm/helpers/morphoBlueHelpers/index.js +1 -0
  41. package/esm/helpers/morphoMidnightHelpers/index.d.ts +45 -7
  42. package/esm/helpers/morphoMidnightHelpers/index.js +98 -13
  43. package/esm/llamaLend/index.js +1 -1
  44. package/esm/markets/aaveV4/index.d.ts +2 -0
  45. package/esm/markets/aaveV4/index.js +12 -0
  46. package/esm/morphoBlue/index.d.ts +8 -6
  47. package/esm/morphoBlue/index.js +62 -39
  48. package/esm/portfolio/index.js +32 -3
  49. package/esm/savings/summerVaults/options.js +10 -10
  50. package/esm/staking/staking.js +3 -0
  51. package/esm/types/aaveV4.d.ts +2 -1
  52. package/esm/types/aaveV4.js +1 -0
  53. package/esm/types/claiming.d.ts +10 -0
  54. package/esm/types/claiming.js +2 -0
  55. package/esm/types/morphoBlue.d.ts +9 -0
  56. package/esm/types/morphoMidnight.d.ts +9 -0
  57. package/package.json +2 -2
  58. package/src/aaveV3/index.ts +13 -1
  59. package/src/aaveV4/lend.ts +3 -3
  60. package/src/claiming/index.ts +2 -0
  61. package/src/claiming/uniswap.ts +70 -0
  62. package/src/config/contracts.ts +9 -0
  63. package/src/contracts.ts +2 -0
  64. package/src/curveUsd/index.ts +1 -1
  65. package/src/helpers/morphoBlueHelpers/index.ts +1 -0
  66. package/src/helpers/morphoMidnightHelpers/index.ts +142 -17
  67. package/src/llamaLend/index.ts +1 -1
  68. package/src/markets/aaveV4/index.ts +13 -0
  69. package/src/morphoBlue/index.ts +101 -44
  70. package/src/portfolio/index.ts +32 -3
  71. package/src/savings/summerVaults/options.ts +10 -10
  72. package/src/staking/staking.ts +2 -0
  73. package/src/types/aaveV4.ts +1 -0
  74. package/src/types/claiming.ts +11 -0
  75. package/src/types/morphoBlue.ts +11 -0
  76. package/src/types/morphoMidnight.ts +12 -0
@@ -7,7 +7,7 @@ export const SUMMER_VAULT_USDC_MAINNET_LR = {
7
7
  asset: 'USDC',
8
8
  network: NetworkNumber.Eth,
9
9
  deploymentBlock: 21795049,
10
- isLegacy: false,
10
+ isLegacy: true,
11
11
  };
12
12
  export const SUMMER_VAULT_USDC_MAINNET_HR = {
13
13
  type: SummerVaultType.SummerVaultUSDCMainnetHR,
@@ -16,7 +16,7 @@ export const SUMMER_VAULT_USDC_MAINNET_HR = {
16
16
  asset: 'USDC',
17
17
  network: NetworkNumber.Eth,
18
18
  deploymentBlock: 22488041,
19
- isLegacy: false,
19
+ isLegacy: true,
20
20
  };
21
21
  export const SUMMER_VAULT_USDT_MAINNET_LR = {
22
22
  type: SummerVaultType.SummerVaultUSDTMainnetLR,
@@ -25,7 +25,7 @@ export const SUMMER_VAULT_USDT_MAINNET_LR = {
25
25
  asset: 'USDT',
26
26
  network: NetworkNumber.Eth,
27
27
  deploymentBlock: 21795276,
28
- isLegacy: false,
28
+ isLegacy: true,
29
29
  };
30
30
  export const SUMMER_VAULT_ETH_MAINNET_LR = {
31
31
  type: SummerVaultType.SummerVaultETHMainnetLR,
@@ -34,7 +34,7 @@ export const SUMMER_VAULT_ETH_MAINNET_LR = {
34
34
  asset: 'WETH',
35
35
  network: NetworkNumber.Eth,
36
36
  deploymentBlock: 21795390,
37
- isLegacy: false,
37
+ isLegacy: true,
38
38
  };
39
39
  export const SUMMER_VAULT_ETH_MAINNET_HR = {
40
40
  type: SummerVaultType.SummerVaultETHMainnetHR,
@@ -43,7 +43,7 @@ export const SUMMER_VAULT_ETH_MAINNET_HR = {
43
43
  asset: 'WETH',
44
44
  network: NetworkNumber.Eth,
45
45
  deploymentBlock: 22291252,
46
- isLegacy: false,
46
+ isLegacy: true,
47
47
  };
48
48
  export const SUMMER_VAULT_USDC_ARBITRUM_LR = {
49
49
  type: SummerVaultType.SummerVaultUSDCArbitrumLR,
@@ -52,7 +52,7 @@ export const SUMMER_VAULT_USDC_ARBITRUM_LR = {
52
52
  asset: 'USDC',
53
53
  network: NetworkNumber.Arb,
54
54
  deploymentBlock: 404339289,
55
- isLegacy: false,
55
+ isLegacy: true,
56
56
  };
57
57
  export const SUMMER_VAULT_USDT_ARBITRUM_LR = {
58
58
  type: SummerVaultType.SummerVaultUSDTArbitrumLR,
@@ -61,7 +61,7 @@ export const SUMMER_VAULT_USDT_ARBITRUM_LR = {
61
61
  asset: 'USDT',
62
62
  network: NetworkNumber.Arb,
63
63
  deploymentBlock: 303594065,
64
- isLegacy: false,
64
+ isLegacy: true,
65
65
  };
66
66
  export const SUMMER_VAULT_USDC_BASE_LR = {
67
67
  type: SummerVaultType.SummerVaultUSDCBaseLR,
@@ -70,7 +70,7 @@ export const SUMMER_VAULT_USDC_BASE_LR = {
70
70
  asset: 'USDC',
71
71
  network: NetworkNumber.Base,
72
72
  deploymentBlock: 303594065,
73
- isLegacy: false,
73
+ isLegacy: true,
74
74
  };
75
75
  export const SUMMER_VAULT_EURC_BASE_LR = {
76
76
  type: SummerVaultType.SummerVaultEURCBaseLR,
@@ -79,7 +79,7 @@ export const SUMMER_VAULT_EURC_BASE_LR = {
79
79
  asset: 'EURC',
80
80
  network: NetworkNumber.Base,
81
81
  deploymentBlock: 27544064,
82
- isLegacy: false,
82
+ isLegacy: true,
83
83
  };
84
84
  export const SUMMER_VAULT_ETH_BASE_LR = {
85
85
  type: SummerVaultType.SummerVaultETHBaseLR,
@@ -88,7 +88,7 @@ export const SUMMER_VAULT_ETH_BASE_LR = {
88
88
  asset: 'WETH',
89
89
  network: NetworkNumber.Base,
90
90
  deploymentBlock: 29066887,
91
- isLegacy: false,
91
+ isLegacy: true,
92
92
  };
93
93
  export const SUMMER_VAULTS = {
94
94
  [SummerVaultType.SummerVaultUSDCMainnetLR]: SUMMER_VAULT_USDC_MAINNET_LR,
@@ -68,6 +68,7 @@ export const STAKING_ASSETS = [
68
68
  'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT', 'syrupUSDC', 'wstUSR',
69
69
  'PT sUSDe Feb', 'PT USDe Feb', 'PT sUSDe Apr', 'PT USDe Apr', 'PT sUSDe May', 'PT USDe May', 'PT srUSDe Apr', 'GHO',
70
70
  'PT sUSDe June', 'PT USDe June', 'PT srUSDe Jun', 'PT srUSDe Oct', 'PT USDG May', 'PT sUSDS Nov', 'PT sUSDe Oct', 'PT USDG Sep',
71
+ 'syrupUSDG',
71
72
  ];
72
73
  export const getStakingApy = memoize((asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...args_1], void 0, function* (asset, network = NetworkNumber.Eth) {
73
74
  try {
@@ -125,6 +126,8 @@ export const getStakingApy = memoize((asset_1, ...args_1) => __awaiter(void 0, [
125
126
  return yield getApyFromDfsApi('syrupUSDT');
126
127
  if (asset === 'syrupUSDC')
127
128
  return yield getApyFromDfsApi('syrupUSDC');
129
+ if (asset === 'syrupUSDG')
130
+ return yield getApyFromDfsApi('syrupUSDG');
128
131
  if (asset === 'wstUSR')
129
132
  return yield getApyFromDfsApi('wstUSR');
130
133
  if (asset === 'PT sUSDe Feb')
@@ -16,7 +16,8 @@ export declare enum AaveV4SpokesType {
16
16
  AaveV4LidoSpoke = "aave_v4_lido_spoke",
17
17
  AaveV4LombardBtcSpoke = "aave_v4_lombard_btc_spoke",
18
18
  AaveV4MainSpoke = "aave_v4_main_spoke",
19
- AaveV4USDGPendleSpoke = "aave_v4_usdg_pendle_spoke"
19
+ AaveV4USDGPendleSpoke = "aave_v4_usdg_pendle_spoke",
20
+ AaveV4USDGMapleSpoke = "aave_v4_usdg_maple_spoke"
20
21
  }
21
22
  export interface AaveV4HubInfo {
22
23
  chainIds: NetworkNumber[];
@@ -18,4 +18,5 @@ export var AaveV4SpokesType;
18
18
  AaveV4SpokesType["AaveV4LombardBtcSpoke"] = "aave_v4_lombard_btc_spoke";
19
19
  AaveV4SpokesType["AaveV4MainSpoke"] = "aave_v4_main_spoke";
20
20
  AaveV4SpokesType["AaveV4USDGPendleSpoke"] = "aave_v4_usdg_pendle_spoke";
21
+ AaveV4SpokesType["AaveV4USDGMapleSpoke"] = "aave_v4_usdg_maple_spoke";
21
22
  })(AaveV4SpokesType || (AaveV4SpokesType = {}));
@@ -14,6 +14,8 @@ export declare enum ClaimType {
14
14
  SPARK_AIRDROP = "SPARK_AIRDROP",
15
15
  /** Spark Airdrop */
16
16
  SPARK_WST_ETH_REWARDS = "SPARK_WST_ETH_REWARDS",// TODO: This will be removed once we fully refactor spark rewards
17
+ /** UNI Airdrop */
18
+ UNI_REWARDS = "UNI_REWARDS",
17
19
  ETHENA_AIRDROP = "ETHENA_AIRDROP"
18
20
  }
19
21
  type _ClaimableTokenPartial = {
@@ -81,5 +83,13 @@ export type SparkWstEthRewardsClaimableToken = _ClaimableTokenPartial & {
81
83
  export type EthenaAirdropClaimableToken = _ClaimableTokenPartial & {
82
84
  claimType: ClaimType.ETHENA_AIRDROP;
83
85
  };
86
+ export type UniswapAirdropClaimableToken = _ClaimableTokenPartial & {
87
+ claimType: ClaimType.UNI_REWARDS;
88
+ additionalClaimFields: {
89
+ index: number;
90
+ isClaimed: boolean;
91
+ proof: string[];
92
+ };
93
+ };
84
94
  export type ClaimableToken = AaveRewardsClaimableToken | AaveMeritRewardsClaimableToken | CompoundV3CompClaimableToken | SparkRewardsClaimableToken | KingRewardsClaimableToken | SparkAirdropClaimableToken | SparkWstEthRewardsClaimableToken | EthenaAirdropClaimableToken;
85
95
  export {};
@@ -14,6 +14,8 @@ export var ClaimType;
14
14
  ClaimType["SPARK_AIRDROP"] = "SPARK_AIRDROP";
15
15
  /** Spark Airdrop */
16
16
  ClaimType["SPARK_WST_ETH_REWARDS"] = "SPARK_WST_ETH_REWARDS";
17
+ /** UNI Airdrop */
18
+ ClaimType["UNI_REWARDS"] = "UNI_REWARDS";
17
19
  ClaimType["ETHENA_AIRDROP"] = "ETHENA_AIRDROP";
18
20
  })(ClaimType || (ClaimType = {}));
19
21
  export var SparkAirdropType;
@@ -212,6 +212,15 @@ export interface MorphoBluePositionData {
212
212
  borrowShares: string;
213
213
  exposure: string;
214
214
  }
215
+ export interface MorphoBlueEarnData {
216
+ apy: string;
217
+ amount: string;
218
+ amountUsd: string;
219
+ }
220
+ export interface MorphoBlueMarketRewards {
221
+ supplyApy: string;
222
+ borrowApy: string;
223
+ }
215
224
  export interface MorphoBlueVault {
216
225
  address: string;
217
226
  }
@@ -60,6 +60,15 @@ export interface MorphoMidnightMarketInfo {
60
60
  utillization: string;
61
61
  assetsData: MorphoMidnightAssetsData;
62
62
  }
63
+ export interface MorphoMidnightBookOffer {
64
+ rate: string;
65
+ liquidity: string;
66
+ }
67
+ export interface MorphoMidnightParsedBook {
68
+ bestRate: string;
69
+ totalLiquidity: string;
70
+ offers: MorphoMidnightBookOffer[];
71
+ }
63
72
  export interface MorphoMidnightAggregatedPositionData {
64
73
  suppliedUsd: string;
65
74
  suppliedCollateralUsd: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.127-midnight-1-dev",
3
+ "version": "2.1.127-midnight-3-dev",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -21,7 +21,7 @@
21
21
  "author": "",
22
22
  "license": "ISC",
23
23
  "dependencies": {
24
- "@defisaver/tokens": "^1.7.41",
24
+ "@defisaver/tokens": "^1.7.44",
25
25
  "@types/lodash": "^4.17.15",
26
26
  "@types/memoizee": "^0.4.12",
27
27
  "decimal.js": "^10.6.0",
@@ -84,9 +84,21 @@ export async function _getAaveV3MarketData(provider: Client, network: NetworkNum
84
84
  const aaveIncentivesContract = AaveIncentiveDataProviderV3ContractViem(provider, network);
85
85
  const marketAddress = market.providerAddress;
86
86
  const networksWithIncentives = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Opt, NetworkNumber.Linea, NetworkNumber.Plasma];
87
+ // Limit each view call to 10 tokens, run chunks concurrently, then combine their results into loanInfo.
88
+ const addressesPerRequest = 10;
89
+ const addressChunks: EthAddress[][] = _addresses.length > addressesPerRequest
90
+ ? Array.from(
91
+ { length: Math.ceil(_addresses.length / addressesPerRequest) },
92
+ (_, index) => _addresses.slice(index * addressesPerRequest, (index + 1) * addressesPerRequest) as EthAddress[],
93
+ )
94
+ : [_addresses as EthAddress[]];
95
+ const loanInfoPromise = Promise.all(addressChunks.map((addresses) => loanInfoContract.read.getFullTokensInfo(
96
+ [marketAddress, addresses],
97
+ setViemBlockNumber(blockNumber),
98
+ ))).then((loanInfoChunks) => loanInfoChunks.flat());
87
99
  // eslint-disable-next-line prefer-const
88
100
  let [loanInfo, eModesInfo, rewardInfo, merkleRewardsMap, meritRewardsMap] = await Promise.all([
89
- loanInfoContract.read.getFullTokensInfo([marketAddress, _addresses as EthAddress[]], setViemBlockNumber(blockNumber)),
101
+ loanInfoPromise,
90
102
  loanInfoContract.read.getAllEmodes([marketAddress], setViemBlockNumber(blockNumber)),
91
103
  networksWithIncentives.includes(network) ? aaveIncentivesContract.read.getReservesIncentivesData([marketAddress], setViemBlockNumber(blockNumber)) : null,
92
104
  getMerkleCampaigns(network),
@@ -49,9 +49,9 @@ export const AAVE_V4_TOKENIZED_SPOKES: Record<string, EthAddress> = {
49
49
  USDC_PRIME: '0x486415fb1F8b062c89ED548f871cf64304AACb31',
50
50
  USDT_PRIME: '0x46c588DD8453aC259c1f6a54b4C9A93C2aC3762D',
51
51
 
52
- USDC_PAXOS: '0x4131E0B2E7AFeCEAf3d3b4225aA61a3B2B7535b8',
53
- USDT_PAXOS: '0x8Dabe53E8cB991c57f0307F6f419E6D469b0deAA',
54
- PT_USDG_Sep_PAXOS: '0x27eF1140364948A0E30E248297FfDFE5a4091ec4',
52
+ USDC_PAXOS: '0xaed7c529bD2878170B61C758DfAa215AC7a4FD07',
53
+ USDT_PAXOS: '0xa0e97e45C2f89003730E467Bd484fA3eEcE5B4Cf',
54
+ PT_USDG_Sep_PAXOS: '0x7Df10B4A01350D2A1d95cFbE7c9207d7210A2663',
55
55
  };
56
56
 
57
57
  export const AAVE_V4_TOKENIZED_SPOKE_ADDRESSES: Partial<Record<NetworkNumber, EthAddress[]>> = {
@@ -2,10 +2,12 @@ import * as aaveV3Claim from './aaveV3';
2
2
  import * as compV3Claim from './compV3';
3
3
  import * as kingV3Claim from './king';
4
4
  import * as sparkClaim from './spark';
5
+ import * as uniswapClaim from './uniswap';
5
6
 
6
7
  export {
7
8
  aaveV3Claim,
8
9
  compV3Claim,
9
10
  kingV3Claim,
10
11
  sparkClaim,
12
+ uniswapClaim,
11
13
  };
@@ -0,0 +1,70 @@
1
+ import Dec from 'decimal.js';
2
+ import { Client } from 'viem';
3
+ import { assetAmountInEth } from '@defisaver/tokens';
4
+ import { EthAddress, NetworkNumber } from '../types';
5
+ import { UniswapTokenDistributorViem } from '../contracts';
6
+ import { ClaimType, UniswapAirdropClaimableToken } from '../types/claiming';
7
+
8
+ const EMPTY_DATA = (walletAddress: EthAddress) => ({
9
+ address: walletAddress, index: 0, amount: '0x0', proof: [],
10
+ });
11
+
12
+ export const fetchUniswapRewardsData = async (walletAddress: EthAddress) => {
13
+ try {
14
+ const res = await fetch(`https://fe.defisaver.com/api/rewards/uniswap?user=${walletAddress}`,
15
+ { signal: AbortSignal.timeout(5000) });
16
+
17
+ if (!res.ok) throw new Error(await res.text());
18
+
19
+ const data = await res.json();
20
+ if (data.data.error) return EMPTY_DATA(walletAddress);
21
+
22
+ return data.data;
23
+ } catch (err) {
24
+ return EMPTY_DATA(walletAddress);
25
+ }
26
+ };
27
+
28
+ export const getUniswapRewards = async (provider: Client, network: NetworkNumber, walletAddresses: EthAddress[]): Promise<Record<string, UniswapAirdropClaimableToken[]>> => {
29
+ // Fetch all API data in parallel (these are external API calls, can't be batched with multicall)
30
+ const apiDataPromises = walletAddresses.map(address => fetchUniswapRewardsData(address));
31
+ const apiDataArray = await Promise.all(apiDataPromises);
32
+
33
+ // Batch all contract calls using multicall
34
+ const contract = UniswapTokenDistributorViem(provider, network);
35
+ const cumulativePromises = apiDataArray.map(data => (data.index ? contract.read.isClaimed([data.index]) : Promise.resolve(false)),
36
+ );
37
+ const cumulativeResults = await Promise.all(cumulativePromises);
38
+
39
+ // Process results
40
+ const results: Record<string, UniswapAirdropClaimableToken[]> = {};
41
+
42
+ for (let i = 0; i < walletAddresses.length; i++) {
43
+ const walletAddress = walletAddresses[i];
44
+ const data = apiDataArray[i];
45
+ const cumulative = cumulativeResults[i];
46
+
47
+ const amountToClaim = new Dec(data.amount);
48
+
49
+ if (amountToClaim.lessThanOrEqualTo('0') || cumulative) {
50
+ results[walletAddress.toLowerCase() as EthAddress] = [];
51
+ } else {
52
+ results[walletAddress.toLowerCase() as EthAddress] = [{
53
+ symbol: 'UNI',
54
+ underlyingSymbol: 'UNI',
55
+ label: 'Uniswap Airdrop',
56
+ tokenAddress: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
57
+ amount: assetAmountInEth(amountToClaim.toString(), 'UNI'),
58
+ walletAddress,
59
+ claimType: ClaimType.UNI_REWARDS,
60
+ additionalClaimFields: {
61
+ index: data.index,
62
+ isClaimed: cumulative,
63
+ proof: data.proof,
64
+ },
65
+ }];
66
+ }
67
+ }
68
+
69
+ return results;
70
+ };
@@ -1360,3 +1360,12 @@ export const AaveV4View = {
1360
1360
  }
1361
1361
  }
1362
1362
  } as const;
1363
+
1364
+ export const UniswapTokenDistributor = {
1365
+ "abi": [{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"isClaimed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}],
1366
+ "networks": {
1367
+ "1": {
1368
+ "address": "0x090D4613473dEE047c3f2706764f49E0821D256e",
1369
+ }
1370
+ }
1371
+ } as const;
package/src/contracts.ts CHANGED
@@ -182,3 +182,5 @@ export const MakerDsrContractViem = createViemContractFromConfigFunc('MakerDsr')
182
182
  export const SkySavingsContractView = createViemContractFromConfigFunc('SkySavings');
183
183
 
184
184
  export const AaveV4ViewContractViem = createViemContractFromConfigFunc('AaveV4View');
185
+
186
+ export const UniswapTokenDistributorViem = createViemContractFromConfigFunc('UniswapTokenDistributor');
@@ -24,7 +24,7 @@ const getAndFormatBands = async (provider: Client, network: NetworkNumber, selec
24
24
  // getBandsData uses a lot of gas to get all of the bands at once, so we use pagination and fetch 200 bands at a time
25
25
  let i = minBand;
26
26
  while (i < maxBand) {
27
- i += 200;
27
+ i += 20;
28
28
  if (i > maxBand) {
29
29
  pivots.push(maxBand);
30
30
  } else {
@@ -389,6 +389,7 @@ export const getRewardsForMarket = async (marketId: string, network: NetworkNumb
389
389
  query: REWARDS_QUERY,
390
390
  variables: { marketId, chainId: network },
391
391
  }),
392
+ signal: AbortSignal.timeout(LONGER_TIMEOUT),
392
393
  });
393
394
 
394
395
  const data = await response.json();
@@ -1,13 +1,20 @@
1
1
  import Dec from 'decimal.js';
2
- import { assetAmountInEth } from '@defisaver/tokens';
2
+ import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
3
3
  import {
4
4
  calcLeverageLiqPrice, getAssetsTotal, getExposure, isLeveragedPos,
5
5
  } from '../../moneymarket';
6
6
  import { calculateNetApy } from '../../staking';
7
7
  import {
8
- LeverageType, MMAssetsData, MMUsedAsset, MMUsedAssets,
8
+ LeverageType, MMAssetsData, MMUsedAsset, MMUsedAssets, NetworkNumber,
9
9
  } from '../../types/common';
10
- import { MorphoMidnightAggregatedPositionData, MorphoMidnightAssetsData, MorphoMidnightMarketInfo } from '../../types';
10
+ import {
11
+ MorphoMidnightAggregatedPositionData,
12
+ MorphoMidnightAssetsData,
13
+ MorphoMidnightBookOffer,
14
+ MorphoMidnightMarketData,
15
+ MorphoMidnightMarketInfo,
16
+ MorphoMidnightParsedBook,
17
+ } from '../../types';
11
18
  import { SECONDS_PER_DAY, WAD } from '../../constants';
12
19
  import { LONGER_TIMEOUT } from '../../services/utils';
13
20
 
@@ -98,6 +105,15 @@ export const getMorphoMidnightAggregatedPositionData = ({
98
105
  const MIDNIGHT_API_BASE = 'https://api.morpho.org/v0/midnight';
99
106
  const nowInSeconds = () => Math.floor(Date.now() / 1000);
100
107
 
108
+ // The book endpoint is markedly slower than the rest of the API — `LONGER_TIMEOUT` (5s) aborts it often
109
+ // enough that markets drop out of the list for no reason.
110
+ const MIDNIGHT_BOOK_TIMEOUT = 30000;
111
+
112
+ // The quote endpoint's `slippage` query param is validated as a string: 0.1–100, at most one decimal
113
+ // place (`0.50` is rejected even though `0.5` passes). See `midnightSlippageParam`.
114
+ const MIDNIGHT_SLIPPAGE_MIN = 0.1;
115
+ const MIDNIGHT_SLIPPAGE_MAX = 100;
116
+
101
117
  interface MidnightTransaction {
102
118
  event_type: string,
103
119
  market_id: string,
@@ -105,6 +121,25 @@ interface MidnightTransaction {
105
121
  data: { seller_assets?: string, units?: string },
106
122
  }
107
123
 
124
+ interface MidnightApiError {
125
+ code?: string,
126
+ message?: string,
127
+ details?: ({ field?: string, issue?: string })[] | null,
128
+ }
129
+
130
+ interface MidnightRawBid {
131
+ price: string, // WAD-scaled loan-per-unit
132
+ assets: string, // loan-token base units available at this offer
133
+ }
134
+
135
+ interface MidnightQuoteResponse {
136
+ average_best_price?: string,
137
+ average_worst_price?: string,
138
+ available_assets?: string,
139
+ available_units?: string,
140
+ takeable_offers?: unknown[],
141
+ }
142
+
108
143
  export interface MorphoMidnightBorrowInfo {
109
144
  borrowRate: string, // weighted-average borrow APY as a percent
110
145
  debtBase: string, // base borrowed (Σ seller_assets), loan-token units
@@ -116,9 +151,9 @@ export interface MorphoMidnightBorrowQuote {
116
151
  bestPrice: string, // average_best_price, loan-per-unit
117
152
  worstPrice: string, // average_worst_price, slippage-adjusted
118
153
  estBorrowRate: string, // estimated borrow APY as a percent
119
- maxRate: string, // estBorrowRate + slippage (display only, not sent on-chain)
154
+ maxRate: string, // borrow APY the on-chain cap permits, i.e. `maxUnits` annualized (display only)
120
155
  newUnits: string, // debt added at best price, raw loan-token base units
121
- maxUnits: string, // slippage-capped debt (on-chain cap), raw loan-token base units
156
+ maxUnits: string, // capped debt (on-chain cap), raw loan-token base units
122
157
  availableAssets: string,
123
158
  availableUnits: string,
124
159
  takeableOffers: any[], // opaque orderbook offers, forwarded verbatim to on-chain execution
@@ -138,6 +173,33 @@ export const midnightApyFromPrice = (price: Dec.Value, ttmDays: Dec.Value): stri
138
173
  .toString();
139
174
  };
140
175
 
176
+ /**
177
+ * Inverse of `midnightApyFromPrice`: the loan-per-unit price a borrow APY implies,
178
+ * price = (1 + rate)^(−ttmDays / 365).
179
+ *
180
+ * This is what turns an absolute rate ceiling into an on-chain `maxUnits` cap (units = assets / price),
181
+ * and equally the principal a unit of borrow power is worth — Midnight debt is recorded at its maturity
182
+ * face value, so borrowing the full limit as principal would overshoot it by the interest.
183
+ */
184
+ export const midnightPriceFromApy = (ratePercent: Dec.Value, ttmDays: Dec.Value): string => {
185
+ const rate = new Dec(ratePercent);
186
+ const ttm = new Dec(ttmDays);
187
+ if (rate.lte(0) || ttm.lte(0)) return '1';
188
+ return new Dec(1).div(new Dec(1).add(rate.div(100)).pow(ttm.div(365))).toString();
189
+ };
190
+
191
+ /**
192
+ * Coerce a slippage into what the quote endpoint accepts: 0.1–100 with at most one decimal place. The
193
+ * validation is lexical, so a computed value (`4.15066671050631467`) is rejected outright — without this
194
+ * the request 400s and the quote looks unavailable.
195
+ *
196
+ * Rounded **down**, since a wider slippage is a looser cap than the caller asked for.
197
+ */
198
+ export const midnightSlippageParam = (slippagePercent: Dec.Value): string => Dec.min(
199
+ Dec.max(new Dec(slippagePercent), MIDNIGHT_SLIPPAGE_MIN),
200
+ MIDNIGHT_SLIPPAGE_MAX,
201
+ ).toDP(1, Dec.ROUND_DOWN).toString();
202
+
141
203
  /**
142
204
  * Current borrower rate + debt breakdown from the Midnight transactions API. On-chain we can only read the
143
205
  * total debt at maturity (`units`); the base-vs-interest split and the effective borrow rate require the
@@ -180,30 +242,93 @@ export const getMorphoMidnightUserBorrowInfo = async (
180
242
  };
181
243
 
182
244
  /**
183
- * Estimate the borrow rate + slippage cap for a prospective borrow by quoting the Midnight order book.
184
- * `assetsRaw` (and the returned `newUnits`/`maxUnits`) are raw loan-token base units — callers convert to/from
185
- * human amounts. `maxUnits` (from the slippage-adjusted worst price) is the cap sent on-chain to protect the
186
- * user if better offers get filled first. Throws if the book can't fill the amount (caller handles).
245
+ * A market's resting bids, as rates rather than the API's WAD-scaled loan-per-unit prices. Annualizing
246
+ * each price against time-to-maturity gives the rate a borrower filling that offer pays — verified
247
+ * against Morpho's fixed-market UI, where per-offer rates match to the cent.
248
+ *
249
+ * Returns `null` for an empty book: there is nothing to borrow against, so a market listing should skip
250
+ * the market rather than advertise it at a 0% rate. Throws when the request fails — an error response is
251
+ * rarely JSON, so without the `res.ok` check it parses as an empty book and the market silently vanishes.
252
+ */
253
+ export const getMorphoMidnightMarketBook = async (
254
+ market: MorphoMidnightMarketData,
255
+ network: NetworkNumber,
256
+ ): Promise<MorphoMidnightParsedBook | null> => {
257
+ const loanSymbol = getAssetInfoByAddress(market.loanToken, network).symbol;
258
+ const res = await fetch(`${MIDNIGHT_API_BASE}/books/${market.marketId}`, { signal: AbortSignal.timeout(MIDNIGHT_BOOK_TIMEOUT) });
259
+ if (!res.ok) throw new Error(`Midnight book request failed for ${market.value} (${res.status})`);
260
+
261
+ const json: { data?: { bids?: MidnightRawBid[] } } = await res.json();
262
+ const ttmDays = midnightTimeToMaturityDays(market.maturity);
263
+
264
+ const offers: MorphoMidnightBookOffer[] = (json?.data?.bids || [])
265
+ .map((bid) => ({
266
+ rate: midnightApyFromPrice(new Dec(bid.price).div(WAD), ttmDays),
267
+ liquidity: assetAmountInEth(bid.assets, loanSymbol),
268
+ }))
269
+ .sort((a, b) => new Dec(a.rate).minus(b.rate).toNumber());
270
+
271
+ if (offers.length === 0) return null;
272
+
273
+ return {
274
+ bestRate: offers[0].rate,
275
+ totalLiquidity: offers.reduce((sum, offer) => sum.add(offer.liquidity), new Dec(0)).toString(),
276
+ offers,
277
+ };
278
+ };
279
+
280
+ // The API says why a quote failed — NOT_FOUND (market matured or not open yet), INSUFFICIENT_LIQUIDITY
281
+ // (book can't fill the size), VALIDATION_ERROR (bad param, with the offending field in `details`).
282
+ // Callers surface this to the user, so keep the reason rather than collapsing everything into one string.
283
+ const midnightQuoteError = (error?: MidnightApiError): string => {
284
+ const detail = (error?.details || []).map(({ issue }) => issue).filter(Boolean).join('; ');
285
+ const reason = detail || error?.message || error?.code;
286
+ return reason ? `Morpho Midnight quote unavailable: ${reason}` : 'Morpho Midnight quote unavailable';
287
+ };
288
+
289
+ /**
290
+ * Quote a prospective borrow against the Midnight order book: the estimated rate, the debt units it adds,
291
+ * and the `maxUnits` cap sent on-chain to protect the user if better offers get filled first. `assetsRaw`
292
+ * (and the returned `newUnits`/`maxUnits`) are raw loan-token base units — callers convert to/from human
293
+ * amounts. Throws if the book can't fill the amount (caller handles).
294
+ *
295
+ * Two ways to set the cap:
296
+ * - `maxBorrowRate` — an absolute APY ceiling, honoured **exactly**: the cap price is derived locally via
297
+ * `midnightPriceFromApy`. Prefer this when a user pins a max rate.
298
+ * - otherwise `slippagePercent`, the API's own knob. Note it is a **price**-level slippage, not APY points:
299
+ * near maturity the annualisation factor (365 / ttmDays) multiplies it heavily, so on a 22-day market a
300
+ * slippage of 0.5 permitted an APY ~9pp above the estimate, not 0.5pp. It also saturates at the book's
301
+ * cheapest bid. `maxRate` therefore reports what the cap actually permits, derived from the cap price.
302
+ *
303
+ * A `maxBorrowRate` below `estBorrowRate` yields `maxUnits < newUnits` — the borrow would revert on-chain.
304
+ * Compare the two before submitting and tell the user their ceiling is under the market rate.
187
305
  */
188
306
  export const getMorphoMidnightBorrowQuote = async (
189
307
  marketId: string,
190
308
  assetsRaw: string,
191
309
  slippagePercent: Dec.Value,
192
310
  maturity: number,
311
+ maxBorrowRate?: Dec.Value,
193
312
  ): Promise<MorphoMidnightBorrowQuote> => {
194
- const url = `${MIDNIGHT_API_BASE}/books/${marketId}/bids/quote?assets=${assetsRaw}&slippage=${slippagePercent}`;
313
+ const url = `${MIDNIGHT_API_BASE}/books/${marketId}/bids/quote?assets=${assetsRaw}&slippage=${midnightSlippageParam(slippagePercent)}`;
195
314
  const res = await fetch(url, { signal: AbortSignal.timeout(LONGER_TIMEOUT) });
196
- const json: { data?: any } = await res.json();
315
+ const json: { data?: MidnightQuoteResponse, error?: MidnightApiError } = await res.json();
197
316
  const d = json?.data;
198
- if (!d?.average_best_price) throw new Error('Morpho Midnight quote unavailable');
317
+ if (!d?.average_best_price) throw new Error(midnightQuoteError(json?.error));
199
318
 
200
319
  const bestPrice = new Dec(d.average_best_price).div(WAD).toString();
201
- const worstPrice = new Dec(d.average_worst_price).div(WAD).toString();
320
+ const worstPrice = new Dec(d.average_worst_price || 0).div(WAD).toString();
202
321
  const ttmDays = midnightTimeToMaturityDays(maturity);
203
322
  const estBorrowRate = midnightApyFromPrice(bestPrice, ttmDays);
204
- const maxRate = new Dec(estBorrowRate).add(slippagePercent).toString();
323
+
324
+ // Price the cap sits at, and the rate that price represents — one derivation, so `maxRate` and
325
+ // `maxUnits` can never disagree about what the user is protected at.
326
+ const capPrice = maxBorrowRate !== undefined && new Dec(maxBorrowRate).gt(0)
327
+ ? midnightPriceFromApy(maxBorrowRate, ttmDays)
328
+ : worstPrice;
329
+ const maxRate = midnightApyFromPrice(capPrice, ttmDays);
205
330
  const newUnits = new Dec(bestPrice).lte(0) ? '0' : new Dec(assetsRaw).div(bestPrice).toFixed(0);
206
- const maxUnits = new Dec(worstPrice).lte(0) ? '0' : new Dec(assetsRaw).div(worstPrice).toFixed(0);
331
+ const maxUnits = new Dec(capPrice).lte(0) ? '0' : new Dec(assetsRaw).div(capPrice).toFixed(0);
207
332
 
208
333
  return {
209
334
  bestPrice,
@@ -212,8 +337,8 @@ export const getMorphoMidnightBorrowQuote = async (
212
337
  maxRate,
213
338
  newUnits,
214
339
  maxUnits,
215
- availableAssets: d.available_assets,
216
- availableUnits: d.available_units,
340
+ availableAssets: d.available_assets || '0',
341
+ availableUnits: d.available_units || '0',
217
342
  takeableOffers: d.takeable_offers || [],
218
343
  };
219
344
  };
@@ -24,7 +24,7 @@ const getAndFormatBands = async (provider: Client, network: NetworkNumber, selec
24
24
  // getBandsData uses a lot of gas to get all of the bands at once, so we use pagination and fetch 200 bands at a time
25
25
  let i = minBand;
26
26
  while (i < maxBand) {
27
- i += 200;
27
+ i += 20;
28
28
  if (i > maxBand) {
29
29
  pivots.push(maxBand);
30
30
  } else {
@@ -193,6 +193,18 @@ export const AAVE_V4_USDG_PENDLE_SPOKE = (networkId: NetworkNumber): AaveV4Spoke
193
193
  ],
194
194
  });
195
195
 
196
+ export const AAVE_V4_USDG_MAPLE_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
197
+ chainIds: [NetworkNumber.Eth],
198
+ label: 'USDG Maple',
199
+ value: AaveV4SpokesType.AaveV4USDGMapleSpoke,
200
+ url: 'usdg-maple',
201
+ address: '0x774b9655413c34809c1f1b16b654465A89EBE989',
202
+ hubs: [
203
+ AAVE_V4_PAXOS_HUB(NetworkNumber.Eth).address,
204
+ AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
205
+ ],
206
+ });
207
+
196
208
  export const AaveV4Spokes = (networkId: NetworkNumber) => ({
197
209
  [AaveV4SpokesType.AaveV4BluechipSpoke]: AAVE_V4_BLUECHIP_SPOKE(networkId),
198
210
  [AaveV4SpokesType.AaveV4EthenaCorrelatedSpoke]: AAVE_V4_ETHENA_CORRELATED_SPOKE(networkId),
@@ -205,6 +217,7 @@ export const AaveV4Spokes = (networkId: NetworkNumber) => ({
205
217
  [AaveV4SpokesType.AaveV4LombardBtcSpoke]: AAVE_V4_LOMBARD_BTC_SPOKE(networkId),
206
218
  [AaveV4SpokesType.AaveV4MainSpoke]: AAVE_V4_MAIN_SPOKE(networkId),
207
219
  [AaveV4SpokesType.AaveV4USDGPendleSpoke]: AAVE_V4_USDG_PENDLE_SPOKE(networkId),
220
+ [AaveV4SpokesType.AaveV4USDGMapleSpoke]: AAVE_V4_USDG_MAPLE_SPOKE(networkId),
208
221
  }) as const;
209
222
 
210
223
  export const getAaveV4SpokeTypeInfo = (type: AaveV4SpokesType, network?: NetworkNumber) => ({ ...AaveV4Spokes(network ?? NetworkNumber.Eth) }[type]);