@defisaver/positions-sdk 2.1.52-dev-3 → 2.1.53-aave-v4

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 (158) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV4/index.d.ts +7 -0
  5. package/cjs/aaveV4/index.js +174 -0
  6. package/cjs/config/contracts.d.ts +1277 -0
  7. package/cjs/config/contracts.js +9 -0
  8. package/cjs/contracts.d.ts +23120 -0
  9. package/cjs/contracts.js +2 -1
  10. package/cjs/fluid/index.d.ts +3 -3
  11. package/cjs/helpers/aaveV4Helpers/index.d.ts +13 -0
  12. package/cjs/helpers/aaveV4Helpers/index.js +117 -0
  13. package/cjs/helpers/index.d.ts +1 -0
  14. package/cjs/helpers/index.js +2 -1
  15. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  16. package/cjs/index.d.ts +2 -1
  17. package/cjs/index.js +3 -1
  18. package/cjs/markets/aaveV4/index.d.ts +13 -0
  19. package/cjs/markets/aaveV4/index.js +39 -0
  20. package/cjs/markets/index.d.ts +1 -0
  21. package/cjs/markets/index.js +3 -1
  22. package/cjs/portfolio/index.js +20 -0
  23. package/cjs/savings/morphoVaults/index.js +17 -17
  24. package/cjs/types/aaveV4.d.ts +137 -0
  25. package/cjs/types/aaveV4.js +11 -0
  26. package/cjs/types/index.d.ts +2 -0
  27. package/cjs/types/index.js +2 -0
  28. package/cjs/types/portfolio.d.ts +4 -0
  29. package/esm/aaveV4/index.d.ts +7 -0
  30. package/esm/aaveV4/index.js +165 -0
  31. package/esm/config/contracts.d.ts +1277 -0
  32. package/esm/config/contracts.js +8 -0
  33. package/esm/contracts.d.ts +23120 -0
  34. package/esm/contracts.js +1 -0
  35. package/esm/fluid/index.d.ts +3 -3
  36. package/esm/helpers/aaveV4Helpers/index.d.ts +13 -0
  37. package/esm/helpers/aaveV4Helpers/index.js +108 -0
  38. package/esm/helpers/index.d.ts +1 -0
  39. package/esm/helpers/index.js +1 -0
  40. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  41. package/esm/index.d.ts +2 -1
  42. package/esm/index.js +2 -1
  43. package/esm/markets/aaveV4/index.d.ts +13 -0
  44. package/esm/markets/aaveV4/index.js +29 -0
  45. package/esm/markets/index.d.ts +1 -0
  46. package/esm/markets/index.js +1 -0
  47. package/esm/portfolio/index.js +21 -1
  48. package/esm/savings/morphoVaults/index.js +17 -17
  49. package/esm/types/aaveV4.d.ts +137 -0
  50. package/esm/types/aaveV4.js +8 -0
  51. package/esm/types/index.d.ts +2 -0
  52. package/esm/types/index.js +2 -0
  53. package/esm/types/portfolio.d.ts +4 -0
  54. package/package.json +48 -48
  55. package/src/aaveV2/index.ts +240 -240
  56. package/src/aaveV3/index.ts +635 -635
  57. package/src/aaveV3/merit.ts +97 -97
  58. package/src/aaveV3/merkl.ts +74 -74
  59. package/src/aaveV4/index.ts +176 -0
  60. package/src/claiming/aaveV3.ts +154 -154
  61. package/src/claiming/compV3.ts +22 -22
  62. package/src/claiming/ethena.ts +61 -61
  63. package/src/claiming/index.ts +12 -12
  64. package/src/claiming/king.ts +66 -66
  65. package/src/claiming/morphoBlue.ts +118 -118
  66. package/src/claiming/spark.ts +225 -225
  67. package/src/compoundV2/index.ts +244 -244
  68. package/src/compoundV3/index.ts +274 -274
  69. package/src/config/contracts.ts +1328 -1320
  70. package/src/constants/index.ts +10 -10
  71. package/src/contracts.ts +174 -172
  72. package/src/curveUsd/index.ts +254 -254
  73. package/src/eulerV2/index.ts +324 -324
  74. package/src/exchange/index.ts +25 -25
  75. package/src/fluid/index.ts +1800 -1800
  76. package/src/helpers/aaveHelpers/index.ts +202 -202
  77. package/src/helpers/aaveV4Helpers/index.ts +128 -0
  78. package/src/helpers/compoundHelpers/index.ts +276 -276
  79. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  80. package/src/helpers/eulerHelpers/index.ts +229 -229
  81. package/src/helpers/fluidHelpers/index.ts +335 -335
  82. package/src/helpers/index.ts +11 -10
  83. package/src/helpers/liquityV2Helpers/index.ts +82 -82
  84. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  85. package/src/helpers/makerHelpers/index.ts +52 -52
  86. package/src/helpers/morphoBlueHelpers/index.ts +405 -405
  87. package/src/helpers/sparkHelpers/index.ts +169 -169
  88. package/src/index.ts +51 -49
  89. package/src/liquity/index.ts +159 -159
  90. package/src/liquityV2/index.ts +703 -703
  91. package/src/llamaLend/index.ts +305 -305
  92. package/src/maker/index.ts +223 -223
  93. package/src/markets/aave/index.ts +118 -118
  94. package/src/markets/aave/marketAssets.ts +54 -54
  95. package/src/markets/aaveV4/index.ts +42 -0
  96. package/src/markets/compound/index.ts +243 -243
  97. package/src/markets/compound/marketsAssets.ts +97 -97
  98. package/src/markets/curveUsd/index.ts +69 -69
  99. package/src/markets/euler/index.ts +26 -26
  100. package/src/markets/fluid/index.ts +2900 -2900
  101. package/src/markets/index.ts +26 -25
  102. package/src/markets/liquityV2/index.ts +102 -102
  103. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  104. package/src/markets/llamaLend/index.ts +235 -235
  105. package/src/markets/morphoBlue/index.ts +988 -988
  106. package/src/markets/spark/index.ts +29 -29
  107. package/src/markets/spark/marketAssets.ts +12 -12
  108. package/src/moneymarket/moneymarketCommonService.ts +84 -84
  109. package/src/morphoBlue/index.ts +274 -274
  110. package/src/portfolio/index.ts +606 -586
  111. package/src/savings/index.ts +95 -95
  112. package/src/savings/makerDsr/index.ts +53 -53
  113. package/src/savings/makerDsr/options.ts +9 -9
  114. package/src/savings/morphoVaults/index.ts +80 -80
  115. package/src/savings/morphoVaults/options.ts +193 -193
  116. package/src/savings/skyOptions/index.ts +95 -95
  117. package/src/savings/skyOptions/options.ts +10 -10
  118. package/src/savings/sparkSavingsVaults/index.ts +60 -60
  119. package/src/savings/sparkSavingsVaults/options.ts +35 -35
  120. package/src/savings/yearnV3Vaults/index.ts +61 -61
  121. package/src/savings/yearnV3Vaults/options.ts +55 -55
  122. package/src/savings/yearnVaults/index.ts +73 -73
  123. package/src/savings/yearnVaults/options.ts +32 -32
  124. package/src/services/priceService.ts +278 -278
  125. package/src/services/utils.ts +115 -115
  126. package/src/services/viem.ts +57 -57
  127. package/src/setup.ts +8 -8
  128. package/src/spark/index.ts +459 -459
  129. package/src/staking/eligibility.ts +53 -53
  130. package/src/staking/index.ts +1 -1
  131. package/src/staking/staking.ts +192 -192
  132. package/src/types/aave.ts +199 -199
  133. package/src/types/aaveV4.ts +153 -0
  134. package/src/types/claiming.ts +114 -114
  135. package/src/types/common.ts +115 -115
  136. package/src/types/compound.ts +145 -145
  137. package/src/types/curveUsd.ts +123 -123
  138. package/src/types/euler.ts +176 -176
  139. package/src/types/fluid.ts +485 -485
  140. package/src/types/index.ts +17 -15
  141. package/src/types/liquity.ts +30 -30
  142. package/src/types/liquityV2.ts +128 -128
  143. package/src/types/llamaLend.ts +161 -161
  144. package/src/types/maker.ts +63 -63
  145. package/src/types/merit.ts +1 -1
  146. package/src/types/merkl.ts +70 -70
  147. package/src/types/morphoBlue.ts +202 -202
  148. package/src/types/portfolio.ts +64 -60
  149. package/src/types/savings/index.ts +23 -23
  150. package/src/types/savings/makerDsr.ts +13 -13
  151. package/src/types/savings/morphoVaults.ts +32 -32
  152. package/src/types/savings/sky.ts +14 -14
  153. package/src/types/savings/sparkSavingsVaults.ts +15 -15
  154. package/src/types/savings/yearnV3Vaults.ts +17 -17
  155. package/src/types/savings/yearnVaults.ts +14 -14
  156. package/src/types/spark.ts +135 -135
  157. package/src/umbrella/index.ts +69 -69
  158. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -1,54 +1,54 @@
1
- import Dec from 'decimal.js';
2
- import { IncentiveEligibilityId, MMUsedAssets } from '../types/common';
3
-
4
- export const isEligibleForEthenaUSDeRewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => {
5
- const USDeUSDAmountSupplied = usedAssets.USDe?.suppliedUsd || '0';
6
- const sUSDeUSDAmountSupplied = usedAssets.sUSDe?.suppliedUsd || '0';
7
- const anythingElseSupplied = Object.values(usedAssets).some((asset) => asset.symbol !== 'USDe' && asset.symbol !== 'sUSDe' && asset.isSupplied);
8
- if (anythingElseSupplied) return { isEligible: false, eligibleUSDAmount: '0' };
9
- const totalAmountSupplied = new Dec(USDeUSDAmountSupplied).add(sUSDeUSDAmountSupplied).toString();
10
- const percentageInUSDe = new Dec(USDeUSDAmountSupplied).div(totalAmountSupplied).toNumber();
11
- if (percentageInUSDe < 0.45 || percentageInUSDe > 0.55) return { isEligible: false, eligibleUSDAmount: '0' }; // 45% - 55% of total amount supplied must be in USDe
12
- const percentageInSUSDe = new Dec(sUSDeUSDAmountSupplied).div(totalAmountSupplied).toNumber();
13
- if (percentageInSUSDe < 0.45 || percentageInSUSDe > 0.55) return { isEligible: false, eligibleUSDAmount: '0' }; // 45% - 55% of total amount supplied must be in sUSDe
14
-
15
- const allowedBorrowAssets = ['USDC', 'USDT', 'USDS'];
16
- const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
17
- if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
18
-
19
- if (new Dec(healthRatio).gte(2.5)) return { isEligible: false, eligibleUSDAmount: '0' }; // health ratio must be below 2.5
20
-
21
- const halfAmountSupplied = new Dec(totalAmountSupplied).div(2).toString();
22
- const USDeAmountEligibleForRewards = Dec.min(USDeUSDAmountSupplied, halfAmountSupplied).toString(); // rewards are given to amount of USDe supplied up to half of total amount supplied
23
-
24
- return { isEligible: true, eligibleUSDAmount: USDeAmountEligibleForRewards };
25
- };
26
-
27
- export const isEligibleForAaveV3ArbitrumEthSupply = (usedAssets: MMUsedAssets) => {
28
- const ETHAmountSupplied = usedAssets.ETH?.suppliedUsd || '0';
29
- const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
30
- const delta = new Dec(ETHAmountSupplied).sub(ETHAmountBorrowed).toString();
31
-
32
- return { isEligible: true, eligibleUSDAmount: Dec.max(delta, 0).toString() };
33
- };
34
-
35
- export const isEligibleForAaveV3ArbitrumETHLSBorrow = (usedAssets: MMUsedAssets) => {
36
- const allowedBorrowAssets = ['ETH'];
37
- const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
38
- if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
39
-
40
- const allowedSupplyAssets = ['wstETH', 'ezETH', 'weETH', 'rsETH'];
41
- const anythingSuppliedNotAllowed = Object.values(usedAssets).some((asset) => asset.isSupplied && !allowedSupplyAssets.includes(asset.symbol));
42
- if (anythingSuppliedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
43
-
44
- const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
45
-
46
- return { isEligible: true, eligibleUSDAmount: ETHAmountBorrowed };
47
- };
48
-
49
- export const EligibilityMapping: { [key in IncentiveEligibilityId]: (usedAssets: MMUsedAssets, optionalData: any) => { isEligible: boolean; eligibleUSDAmount: string } } = {
50
- [IncentiveEligibilityId.AaveV3EthenaLiquidLeverage]: isEligibleForEthenaUSDeRewards,
51
- [IncentiveEligibilityId.AaveV3ArbitrumEthSupply]: isEligibleForAaveV3ArbitrumEthSupply,
52
- [IncentiveEligibilityId.AaveV3ArbitrumETHLSBorrow]: isEligibleForAaveV3ArbitrumETHLSBorrow,
53
- [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasma]: isEligibleForEthenaUSDeRewards,
1
+ import Dec from 'decimal.js';
2
+ import { IncentiveEligibilityId, MMUsedAssets } from '../types/common';
3
+
4
+ export const isEligibleForEthenaUSDeRewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => {
5
+ const USDeUSDAmountSupplied = usedAssets.USDe?.suppliedUsd || '0';
6
+ const sUSDeUSDAmountSupplied = usedAssets.sUSDe?.suppliedUsd || '0';
7
+ const anythingElseSupplied = Object.values(usedAssets).some((asset) => asset.symbol !== 'USDe' && asset.symbol !== 'sUSDe' && asset.isSupplied);
8
+ if (anythingElseSupplied) return { isEligible: false, eligibleUSDAmount: '0' };
9
+ const totalAmountSupplied = new Dec(USDeUSDAmountSupplied).add(sUSDeUSDAmountSupplied).toString();
10
+ const percentageInUSDe = new Dec(USDeUSDAmountSupplied).div(totalAmountSupplied).toNumber();
11
+ if (percentageInUSDe < 0.45 || percentageInUSDe > 0.55) return { isEligible: false, eligibleUSDAmount: '0' }; // 45% - 55% of total amount supplied must be in USDe
12
+ const percentageInSUSDe = new Dec(sUSDeUSDAmountSupplied).div(totalAmountSupplied).toNumber();
13
+ if (percentageInSUSDe < 0.45 || percentageInSUSDe > 0.55) return { isEligible: false, eligibleUSDAmount: '0' }; // 45% - 55% of total amount supplied must be in sUSDe
14
+
15
+ const allowedBorrowAssets = ['USDC', 'USDT', 'USDS'];
16
+ const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
17
+ if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
18
+
19
+ if (new Dec(healthRatio).gte(2.5)) return { isEligible: false, eligibleUSDAmount: '0' }; // health ratio must be below 2.5
20
+
21
+ const halfAmountSupplied = new Dec(totalAmountSupplied).div(2).toString();
22
+ const USDeAmountEligibleForRewards = Dec.min(USDeUSDAmountSupplied, halfAmountSupplied).toString(); // rewards are given to amount of USDe supplied up to half of total amount supplied
23
+
24
+ return { isEligible: true, eligibleUSDAmount: USDeAmountEligibleForRewards };
25
+ };
26
+
27
+ export const isEligibleForAaveV3ArbitrumEthSupply = (usedAssets: MMUsedAssets) => {
28
+ const ETHAmountSupplied = usedAssets.ETH?.suppliedUsd || '0';
29
+ const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
30
+ const delta = new Dec(ETHAmountSupplied).sub(ETHAmountBorrowed).toString();
31
+
32
+ return { isEligible: true, eligibleUSDAmount: Dec.max(delta, 0).toString() };
33
+ };
34
+
35
+ export const isEligibleForAaveV3ArbitrumETHLSBorrow = (usedAssets: MMUsedAssets) => {
36
+ const allowedBorrowAssets = ['ETH'];
37
+ const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
38
+ if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
39
+
40
+ const allowedSupplyAssets = ['wstETH', 'ezETH', 'weETH', 'rsETH'];
41
+ const anythingSuppliedNotAllowed = Object.values(usedAssets).some((asset) => asset.isSupplied && !allowedSupplyAssets.includes(asset.symbol));
42
+ if (anythingSuppliedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
43
+
44
+ const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
45
+
46
+ return { isEligible: true, eligibleUSDAmount: ETHAmountBorrowed };
47
+ };
48
+
49
+ export const EligibilityMapping: { [key in IncentiveEligibilityId]: (usedAssets: MMUsedAssets, optionalData: any) => { isEligible: boolean; eligibleUSDAmount: string } } = {
50
+ [IncentiveEligibilityId.AaveV3EthenaLiquidLeverage]: isEligibleForEthenaUSDeRewards,
51
+ [IncentiveEligibilityId.AaveV3ArbitrumEthSupply]: isEligibleForAaveV3ArbitrumEthSupply,
52
+ [IncentiveEligibilityId.AaveV3ArbitrumETHLSBorrow]: isEligibleForAaveV3ArbitrumETHLSBorrow,
53
+ [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasma]: isEligibleForEthenaUSDeRewards,
54
54
  };
@@ -1,2 +1,2 @@
1
- export * from './staking';
1
+ export * from './staking';
2
2
  export * from './eligibility';
@@ -1,193 +1,193 @@
1
- import Dec from 'decimal.js';
2
- import memoize from 'memoizee';
3
- import {
4
- MMAssetsData,
5
- MMUsedAssets,
6
- NetworkNumber,
7
- } from '../types/common';
8
- import { BLOCKS_IN_A_YEAR } from '../constants';
9
- import { DEFAULT_TIMEOUT } from '../services/utils';
10
- import { EligibilityMapping } from './eligibility';
11
- import { EulerV2UsedAsset } from '../types';
12
-
13
- const getSsrApy = async () => {
14
- try {
15
- const res = await fetch('https://fe.defisaver.com/api/sky/data',
16
- { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
17
- const data = await res.json();
18
- return new Dec(data.data.skyData[0].sky_savings_rate_apy).mul(100).toString();
19
- } catch (e) {
20
- console.error('External API Failure: Failed to fetch SSR APY from external API', e);
21
- return '0';
22
- }
23
- };
24
-
25
- const getSuperOETHApy = async () => {
26
- try {
27
- const res = await fetch('https://origin.squids.live/origin-squid/graphql', {
28
- method: 'POST',
29
- headers: {
30
- 'Content-Type': 'application/json',
31
- },
32
- body: JSON.stringify({
33
- query: '\n query OTokenApy($chainId: Int!, $token: String!) {\n oTokenApies(\n limit: 1\n orderBy: timestamp_DESC\n where: {chainId_eq: $chainId, otoken_containsInsensitive: $token}\n ) {\n apy7DayAvg\n apy14DayAvg\n apy30DayAvg\n apr\n apy\n }\n}\n ',
34
- variables: {
35
- token: '0xdbfefd2e8460a6ee4955a68582f85708baea60a3',
36
- chainId: 8453,
37
- },
38
- }),
39
- signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
40
- });
41
-
42
- const data = await res.json();
43
- return new Dec(data.data.oTokenApies[0].apy).mul(100).toString();
44
- } catch (e) {
45
- console.error('External API Failure: Failed to fetch Super OETH APY from external API', e);
46
- return '0';
47
- }
48
- };
49
-
50
- const getApyFromDfsApi = async (asset: string, network: number = NetworkNumber.Eth) => {
51
- try {
52
- const res = await fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}&network=${network}`,
53
- { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
54
- if (!res.ok) throw new Error(`Failed to fetch APY for ${asset}`);
55
- const data = await res.json();
56
- return String(data.apy ?? '0');
57
- } catch (e) {
58
- console.error(`External API Failure: Failed to fetch APY for ${asset} from DFS API`, e);
59
- return '0';
60
- }
61
- };
62
-
63
- export const STAKING_ASSETS = [
64
- 'cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH',
65
- 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep',
66
- 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT', 'syrupUSDC', 'wstUSR',
67
- 'PT sUSDe Feb', 'PT USDe Feb', 'PT USDe Apr', 'PT sUSDe Apr',
68
- ];
69
-
70
- export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
71
- try {
72
- if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
73
- if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
74
- if (asset === 'rETH') return await getApyFromDfsApi('rETH');
75
- if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
76
- if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
77
- if (asset === 'weETH') return await getApyFromDfsApi('weETH');
78
- if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
79
- if (asset === 'osETH') return await getApyFromDfsApi('osETH');
80
- if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
81
- if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
82
- if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
83
- if (asset === 'wsuperOETHb') return await getSuperOETHApy();
84
- if (asset === 'sUSDS') return await getSsrApy();
85
- if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
86
- if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
87
- if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
88
- if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
89
- if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
90
- if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
91
- if (asset === 'tETH') return await getApyFromDfsApi('tETH');
92
- if (asset === 'USDe') return await getApyFromDfsApi('USDe');
93
- if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
94
- if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
95
- if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
96
- if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
97
- if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
98
- if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
99
- if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
100
- if (asset === 'PT sUSDe Feb') return await getApyFromDfsApi('PT sUSDe Feb', network);
101
- if (asset === 'PT USDe Feb') return await getApyFromDfsApi('PT USDe Feb', network);
102
- if (asset === 'PT sUSDe Apr') return await getApyFromDfsApi('PT sUSDe Apr', network);
103
- if (asset === 'PT USDe Apr') return await getApyFromDfsApi('PT USDe Apr', network);
104
- } catch (e) {
105
- console.error(`Failed to fetch APY for ${asset}`);
106
- }
107
- return '0';
108
- }, { promise: true, maxAge: 2 * 60 * 1000 });
109
-
110
- export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
111
- let interval = 1;
112
-
113
- if (+interest === 0) return 0;
114
-
115
- if (type === 'month') interval = 1 / 12;
116
- if (type === 'week') interval = 1 / 52.1429;
117
-
118
- if (apy) {
119
- // interest rate already compounded
120
- return (+principal * (1 + (+interest / 100 * interval))) - +principal;
121
- }
122
-
123
- return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
124
- };
125
-
126
- export const calculateNetApy = ({
127
- usedAssets, assetsData, optionalData,
128
- }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
129
- const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
130
- const acc = { ..._acc };
131
- const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
132
-
133
- if (usedAsset.isSupplied) {
134
- const amount = usedAsset.suppliedUsd;
135
- acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
136
- const rate = assetData.supplyRate;
137
- const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
138
- acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
139
-
140
- if (assetData.supplyIncentives) {
141
- for (const supplyIncentive of assetData.supplyIncentives) {
142
- const { apy, eligibilityId } = supplyIncentive;
143
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
144
- if (eligibilityCheck) {
145
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
146
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
147
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
148
- } else {
149
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
150
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
151
- }
152
- }
153
- }
154
- }
155
-
156
- if (usedAsset.isBorrowed) {
157
- const amount = usedAsset.borrowedUsd;
158
- acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
159
- const rate = assetData.borrowRate;
160
- const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
161
- acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
162
-
163
- if (assetData.borrowIncentives) {
164
- for (const borrowIncentive of assetData.borrowIncentives) {
165
- const { apy, eligibilityId } = borrowIncentive;
166
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
167
- if (eligibilityCheck) {
168
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
169
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
170
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
171
- } else {
172
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
173
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
174
- }
175
- }
176
- }
177
- }
178
-
179
- return acc;
180
- }, {
181
- borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
182
- });
183
-
184
- const {
185
- borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
186
- } = sumValues;
187
-
188
- const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
189
- const balance = new Dec(suppliedUsd).sub(borrowedUsd);
190
- const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
191
-
192
- return { netApy, totalInterestUsd, incentiveUsd };
1
+ import Dec from 'decimal.js';
2
+ import memoize from 'memoizee';
3
+ import {
4
+ MMAssetsData,
5
+ MMUsedAssets,
6
+ NetworkNumber,
7
+ } from '../types/common';
8
+ import { BLOCKS_IN_A_YEAR } from '../constants';
9
+ import { DEFAULT_TIMEOUT } from '../services/utils';
10
+ import { EligibilityMapping } from './eligibility';
11
+ import { EulerV2UsedAsset } from '../types';
12
+
13
+ const getSsrApy = async () => {
14
+ try {
15
+ const res = await fetch('https://fe.defisaver.com/api/sky/data',
16
+ { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
17
+ const data = await res.json();
18
+ return new Dec(data.data.skyData[0].sky_savings_rate_apy).mul(100).toString();
19
+ } catch (e) {
20
+ console.error('External API Failure: Failed to fetch SSR APY from external API', e);
21
+ return '0';
22
+ }
23
+ };
24
+
25
+ const getSuperOETHApy = async () => {
26
+ try {
27
+ const res = await fetch('https://origin.squids.live/origin-squid/graphql', {
28
+ method: 'POST',
29
+ headers: {
30
+ 'Content-Type': 'application/json',
31
+ },
32
+ body: JSON.stringify({
33
+ query: '\n query OTokenApy($chainId: Int!, $token: String!) {\n oTokenApies(\n limit: 1\n orderBy: timestamp_DESC\n where: {chainId_eq: $chainId, otoken_containsInsensitive: $token}\n ) {\n apy7DayAvg\n apy14DayAvg\n apy30DayAvg\n apr\n apy\n }\n}\n ',
34
+ variables: {
35
+ token: '0xdbfefd2e8460a6ee4955a68582f85708baea60a3',
36
+ chainId: 8453,
37
+ },
38
+ }),
39
+ signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
40
+ });
41
+
42
+ const data = await res.json();
43
+ return new Dec(data.data.oTokenApies[0].apy).mul(100).toString();
44
+ } catch (e) {
45
+ console.error('External API Failure: Failed to fetch Super OETH APY from external API', e);
46
+ return '0';
47
+ }
48
+ };
49
+
50
+ const getApyFromDfsApi = async (asset: string, network: number = NetworkNumber.Eth) => {
51
+ try {
52
+ const res = await fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}&network=${network}`,
53
+ { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
54
+ if (!res.ok) throw new Error(`Failed to fetch APY for ${asset}`);
55
+ const data = await res.json();
56
+ return String(data.apy ?? '0');
57
+ } catch (e) {
58
+ console.error(`External API Failure: Failed to fetch APY for ${asset} from DFS API`, e);
59
+ return '0';
60
+ }
61
+ };
62
+
63
+ export const STAKING_ASSETS = [
64
+ 'cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH',
65
+ 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep',
66
+ 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT', 'syrupUSDC', 'wstUSR',
67
+ 'PT sUSDe Feb', 'PT USDe Feb', 'PT USDe Apr', 'PT sUSDe Apr',
68
+ ];
69
+
70
+ export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
71
+ try {
72
+ if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
73
+ if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
74
+ if (asset === 'rETH') return await getApyFromDfsApi('rETH');
75
+ if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
76
+ if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
77
+ if (asset === 'weETH') return await getApyFromDfsApi('weETH');
78
+ if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
79
+ if (asset === 'osETH') return await getApyFromDfsApi('osETH');
80
+ if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
81
+ if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
82
+ if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
83
+ if (asset === 'wsuperOETHb') return await getSuperOETHApy();
84
+ if (asset === 'sUSDS') return await getSsrApy();
85
+ if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
86
+ if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
87
+ if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
88
+ if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
89
+ if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
90
+ if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
91
+ if (asset === 'tETH') return await getApyFromDfsApi('tETH');
92
+ if (asset === 'USDe') return await getApyFromDfsApi('USDe');
93
+ if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
94
+ if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
95
+ if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
96
+ if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
97
+ if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
98
+ if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
99
+ if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
100
+ if (asset === 'PT sUSDe Feb') return await getApyFromDfsApi('PT sUSDe Feb', network);
101
+ if (asset === 'PT USDe Feb') return await getApyFromDfsApi('PT USDe Feb', network);
102
+ if (asset === 'PT sUSDe Apr') return await getApyFromDfsApi('PT sUSDe Apr', network);
103
+ if (asset === 'PT USDe Apr') return await getApyFromDfsApi('PT USDe Apr', network);
104
+ } catch (e) {
105
+ console.error(`Failed to fetch APY for ${asset}`);
106
+ }
107
+ return '0';
108
+ }, { promise: true, maxAge: 2 * 60 * 1000 });
109
+
110
+ export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
111
+ let interval = 1;
112
+
113
+ if (+interest === 0) return 0;
114
+
115
+ if (type === 'month') interval = 1 / 12;
116
+ if (type === 'week') interval = 1 / 52.1429;
117
+
118
+ if (apy) {
119
+ // interest rate already compounded
120
+ return (+principal * (1 + (+interest / 100 * interval))) - +principal;
121
+ }
122
+
123
+ return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
124
+ };
125
+
126
+ export const calculateNetApy = ({
127
+ usedAssets, assetsData, optionalData,
128
+ }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
129
+ const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
130
+ const acc = { ..._acc };
131
+ const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
132
+
133
+ if (usedAsset.isSupplied) {
134
+ const amount = usedAsset.suppliedUsd;
135
+ acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
136
+ const rate = assetData.supplyRate;
137
+ const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
138
+ acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
139
+
140
+ if (assetData.supplyIncentives) {
141
+ for (const supplyIncentive of assetData.supplyIncentives) {
142
+ const { apy, eligibilityId } = supplyIncentive;
143
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
144
+ if (eligibilityCheck) {
145
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
146
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
147
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
148
+ } else {
149
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
150
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ if (usedAsset.isBorrowed) {
157
+ const amount = usedAsset.borrowedUsd;
158
+ acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
159
+ const rate = assetData.borrowRate;
160
+ const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
161
+ acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
162
+
163
+ if (assetData.borrowIncentives) {
164
+ for (const borrowIncentive of assetData.borrowIncentives) {
165
+ const { apy, eligibilityId } = borrowIncentive;
166
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
167
+ if (eligibilityCheck) {
168
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
169
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
170
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
171
+ } else {
172
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
173
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
174
+ }
175
+ }
176
+ }
177
+ }
178
+
179
+ return acc;
180
+ }, {
181
+ borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
182
+ });
183
+
184
+ const {
185
+ borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
186
+ } = sumValues;
187
+
188
+ const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
189
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
190
+ const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
191
+
192
+ return { netApy, totalInterestUsd, incentiveUsd };
193
193
  };