@defisaver/positions-sdk 2.1.6-dev-debt-in-front → 2.1.6-dev-plasma-1

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