@defisaver/positions-sdk 2.1.22-dev-savings-2 → 2.1.22-dev-xpl-2

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 (143) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/config/contracts.d.ts +0 -149
  5. package/cjs/config/contracts.js +1 -15
  6. package/cjs/contracts.d.ts +0 -1344
  7. package/cjs/contracts.js +1 -20
  8. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  9. package/cjs/index.d.ts +1 -2
  10. package/cjs/index.js +1 -3
  11. package/cjs/markets/aave/marketAssets.js +1 -1
  12. package/cjs/types/index.d.ts +0 -1
  13. package/cjs/types/index.js +0 -1
  14. package/esm/config/contracts.d.ts +0 -149
  15. package/esm/config/contracts.js +0 -14
  16. package/esm/contracts.d.ts +0 -1344
  17. package/esm/contracts.js +0 -17
  18. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  19. package/esm/index.d.ts +1 -2
  20. package/esm/index.js +1 -2
  21. package/esm/markets/aave/marketAssets.js +1 -1
  22. package/esm/types/index.d.ts +0 -1
  23. package/esm/types/index.js +0 -1
  24. package/package.json +47 -48
  25. package/src/aaveV2/index.ts +240 -240
  26. package/src/aaveV3/index.ts +614 -614
  27. package/src/aaveV3/merit.ts +97 -97
  28. package/src/aaveV3/merkl.ts +74 -74
  29. package/src/claiming/aaveV3.ts +154 -154
  30. package/src/claiming/compV3.ts +22 -22
  31. package/src/claiming/index.ts +12 -12
  32. package/src/claiming/king.ts +66 -66
  33. package/src/claiming/morphoBlue.ts +118 -118
  34. package/src/claiming/spark.ts +225 -225
  35. package/src/compoundV2/index.ts +244 -244
  36. package/src/compoundV3/index.ts +274 -274
  37. package/src/config/contracts.ts +1259 -1273
  38. package/src/constants/index.ts +10 -10
  39. package/src/contracts.ts +121 -141
  40. package/src/curveUsd/index.ts +254 -254
  41. package/src/eulerV2/index.ts +324 -324
  42. package/src/exchange/index.ts +25 -25
  43. package/src/fluid/index.ts +1774 -1774
  44. package/src/helpers/aaveHelpers/index.ts +187 -187
  45. package/src/helpers/compoundHelpers/index.ts +283 -283
  46. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  47. package/src/helpers/eulerHelpers/index.ts +222 -222
  48. package/src/helpers/fluidHelpers/index.ts +326 -326
  49. package/src/helpers/index.ts +10 -10
  50. package/src/helpers/liquityV2Helpers/index.ts +82 -82
  51. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  52. package/src/helpers/makerHelpers/index.ts +52 -52
  53. package/src/helpers/morphoBlueHelpers/index.ts +396 -396
  54. package/src/helpers/sparkHelpers/index.ts +158 -158
  55. package/src/index.ts +47 -49
  56. package/src/liquity/index.ts +159 -159
  57. package/src/liquityV2/index.ts +703 -703
  58. package/src/llamaLend/index.ts +305 -305
  59. package/src/maker/index.ts +223 -223
  60. package/src/markets/aave/index.ts +116 -116
  61. package/src/markets/aave/marketAssets.ts +54 -54
  62. package/src/markets/compound/index.ts +238 -238
  63. package/src/markets/compound/marketsAssets.ts +97 -97
  64. package/src/markets/curveUsd/index.ts +69 -69
  65. package/src/markets/euler/index.ts +26 -26
  66. package/src/markets/fluid/index.ts +2900 -2900
  67. package/src/markets/index.ts +25 -25
  68. package/src/markets/liquityV2/index.ts +102 -102
  69. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  70. package/src/markets/llamaLend/index.ts +235 -235
  71. package/src/markets/morphoBlue/index.ts +971 -971
  72. package/src/markets/spark/index.ts +29 -29
  73. package/src/markets/spark/marketAssets.ts +12 -12
  74. package/src/moneymarket/moneymarketCommonService.ts +84 -84
  75. package/src/morphoBlue/index.ts +274 -274
  76. package/src/portfolio/index.ts +572 -572
  77. package/src/services/priceService.ts +278 -278
  78. package/src/services/utils.ts +115 -115
  79. package/src/services/viem.ts +34 -34
  80. package/src/setup.ts +8 -8
  81. package/src/spark/index.ts +456 -456
  82. package/src/staking/eligibility.ts +53 -53
  83. package/src/staking/index.ts +1 -1
  84. package/src/staking/staking.ts +183 -183
  85. package/src/types/aave.ts +189 -189
  86. package/src/types/claiming.ts +109 -109
  87. package/src/types/common.ts +107 -107
  88. package/src/types/compound.ts +136 -136
  89. package/src/types/curveUsd.ts +123 -123
  90. package/src/types/euler.ts +175 -175
  91. package/src/types/fluid.ts +483 -483
  92. package/src/types/index.ts +14 -15
  93. package/src/types/liquity.ts +30 -30
  94. package/src/types/liquityV2.ts +126 -126
  95. package/src/types/llamaLend.ts +159 -159
  96. package/src/types/maker.ts +63 -63
  97. package/src/types/merit.ts +1 -1
  98. package/src/types/merkl.ts +70 -70
  99. package/src/types/morphoBlue.ts +200 -200
  100. package/src/types/portfolio.ts +60 -60
  101. package/src/types/spark.ts +133 -133
  102. package/src/umbrella/index.ts +69 -69
  103. package/src/umbrella/umbrellaUtils.ts +29 -29
  104. package/cjs/savings/index.d.ts +0 -6
  105. package/cjs/savings/index.js +0 -71
  106. package/cjs/savings/morphoVaults/index.d.ts +0 -7
  107. package/cjs/savings/morphoVaults/index.js +0 -116
  108. package/cjs/savings/morphoVaults/options.d.ts +0 -16
  109. package/cjs/savings/morphoVaults/options.js +0 -99
  110. package/cjs/savings/yearnVaults/index.d.ts +0 -7
  111. package/cjs/savings/yearnVaults/index.js +0 -95
  112. package/cjs/savings/yearnVaults/options.d.ts +0 -6
  113. package/cjs/savings/yearnVaults/options.js +0 -26
  114. package/cjs/types/savings/index.d.ts +0 -18
  115. package/cjs/types/savings/index.js +0 -18
  116. package/cjs/types/savings/morphoVaults.d.ts +0 -22
  117. package/cjs/types/savings/morphoVaults.js +0 -19
  118. package/cjs/types/savings/yearnVaults.d.ts +0 -11
  119. package/cjs/types/savings/yearnVaults.js +0 -9
  120. package/esm/savings/index.d.ts +0 -6
  121. package/esm/savings/index.js +0 -33
  122. package/esm/savings/morphoVaults/index.d.ts +0 -7
  123. package/esm/savings/morphoVaults/index.js +0 -75
  124. package/esm/savings/morphoVaults/options.d.ts +0 -16
  125. package/esm/savings/morphoVaults/options.js +0 -95
  126. package/esm/savings/yearnVaults/index.d.ts +0 -7
  127. package/esm/savings/yearnVaults/index.js +0 -54
  128. package/esm/savings/yearnVaults/options.d.ts +0 -6
  129. package/esm/savings/yearnVaults/options.js +0 -22
  130. package/esm/types/savings/index.d.ts +0 -18
  131. package/esm/types/savings/index.js +0 -2
  132. package/esm/types/savings/morphoVaults.d.ts +0 -22
  133. package/esm/types/savings/morphoVaults.js +0 -16
  134. package/esm/types/savings/yearnVaults.d.ts +0 -11
  135. package/esm/types/savings/yearnVaults.js +0 -6
  136. package/src/savings/index.ts +0 -38
  137. package/src/savings/morphoVaults/index.ts +0 -79
  138. package/src/savings/morphoVaults/options.ts +0 -110
  139. package/src/savings/yearnVaults/index.ts +0 -56
  140. package/src/savings/yearnVaults/options.ts +0 -27
  141. package/src/types/savings/index.ts +0 -21
  142. package/src/types/savings/morphoVaults.ts +0 -24
  143. package/src/types/savings/yearnVaults.ts +0 -13
@@ -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,184 +1,184 @@
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
- ];
68
-
69
- export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
70
- try {
71
- if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
72
- if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
73
- if (asset === 'rETH') return await getApyFromDfsApi('rETH');
74
- if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
75
- if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
76
- if (asset === 'weETH') return await getApyFromDfsApi('weETH');
77
- if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
78
- if (asset === 'osETH') return await getApyFromDfsApi('osETH');
79
- if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
80
- if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
81
- if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
82
- if (asset === 'wsuperOETHb') return await getSuperOETHApy();
83
- if (asset === 'sUSDS') return await getSsrApy();
84
- if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
85
- if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
86
- if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
87
- if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
88
- if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
89
- if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
90
- if (asset === 'tETH') return await getApyFromDfsApi('tETH');
91
- if (asset === 'USDe') return await getApyFromDfsApi('USDe');
92
- if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
93
- if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
94
- if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
95
- if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
96
- if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
97
- if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
98
- if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
99
- } catch (e) {
100
- console.error(`Failed to fetch APY for ${asset}`);
101
- }
102
- return '0';
103
- }, { promise: true, maxAge: 2 * 60 * 1000 });
104
-
105
- export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
106
- let interval = 1;
107
-
108
- if (+interest === 0) return 0;
109
-
110
- if (type === 'month') interval = 1 / 12;
111
- if (type === 'week') interval = 1 / 52.1429;
112
-
113
- if (apy) {
114
- // interest rate already compounded
115
- return (+principal * (1 + (+interest / 100 * interval))) - +principal;
116
- }
117
-
118
- return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
119
- };
120
-
121
- export const calculateNetApy = ({
122
- usedAssets, assetsData, optionalData,
123
- }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
124
- const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
125
- const acc = { ..._acc };
126
- const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
127
-
128
- if (usedAsset.isSupplied) {
129
- const amount = usedAsset.suppliedUsd;
130
- acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
131
- const rate = assetData.supplyRate;
132
- const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
133
- acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
134
-
135
- for (const supplyIncentive of assetData.supplyIncentives) {
136
- const { apy, eligibilityId } = supplyIncentive;
137
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
138
- if (eligibilityCheck) {
139
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
140
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
141
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
142
- } else {
143
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
144
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
145
- }
146
- }
147
- }
148
-
149
- if (usedAsset.isBorrowed) {
150
- const amount = usedAsset.borrowedUsd;
151
- acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
152
- const rate = assetData.borrowRate;
153
- const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
154
- acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
155
-
156
- for (const borrowIncentive of assetData.borrowIncentives) {
157
- const { apy, eligibilityId } = borrowIncentive;
158
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
159
- if (eligibilityCheck) {
160
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
161
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
162
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
163
- } else {
164
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
165
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
166
- }
167
- }
168
- }
169
-
170
- return acc;
171
- }, {
172
- borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
173
- });
174
-
175
- const {
176
- borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
177
- } = sumValues;
178
-
179
- const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
180
- const balance = new Dec(suppliedUsd).sub(borrowedUsd);
181
- const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
182
-
183
- 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
+ ];
68
+
69
+ export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
70
+ try {
71
+ if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
72
+ if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
73
+ if (asset === 'rETH') return await getApyFromDfsApi('rETH');
74
+ if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
75
+ if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
76
+ if (asset === 'weETH') return await getApyFromDfsApi('weETH');
77
+ if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
78
+ if (asset === 'osETH') return await getApyFromDfsApi('osETH');
79
+ if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
80
+ if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
81
+ if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
82
+ if (asset === 'wsuperOETHb') return await getSuperOETHApy();
83
+ if (asset === 'sUSDS') return await getSsrApy();
84
+ if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
85
+ if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
86
+ if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
87
+ if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
88
+ if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
89
+ if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
90
+ if (asset === 'tETH') return await getApyFromDfsApi('tETH');
91
+ if (asset === 'USDe') return await getApyFromDfsApi('USDe');
92
+ if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
93
+ if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
94
+ if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
95
+ if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
96
+ if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
97
+ if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
98
+ if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
99
+ } catch (e) {
100
+ console.error(`Failed to fetch APY for ${asset}`);
101
+ }
102
+ return '0';
103
+ }, { promise: true, maxAge: 2 * 60 * 1000 });
104
+
105
+ export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
106
+ let interval = 1;
107
+
108
+ if (+interest === 0) return 0;
109
+
110
+ if (type === 'month') interval = 1 / 12;
111
+ if (type === 'week') interval = 1 / 52.1429;
112
+
113
+ if (apy) {
114
+ // interest rate already compounded
115
+ return (+principal * (1 + (+interest / 100 * interval))) - +principal;
116
+ }
117
+
118
+ return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
119
+ };
120
+
121
+ export const calculateNetApy = ({
122
+ usedAssets, assetsData, optionalData,
123
+ }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
124
+ const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
125
+ const acc = { ..._acc };
126
+ const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
127
+
128
+ if (usedAsset.isSupplied) {
129
+ const amount = usedAsset.suppliedUsd;
130
+ acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
131
+ const rate = assetData.supplyRate;
132
+ const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
133
+ acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
134
+
135
+ for (const supplyIncentive of assetData.supplyIncentives) {
136
+ const { apy, eligibilityId } = supplyIncentive;
137
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
138
+ if (eligibilityCheck) {
139
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
140
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
141
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
142
+ } else {
143
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
144
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
145
+ }
146
+ }
147
+ }
148
+
149
+ if (usedAsset.isBorrowed) {
150
+ const amount = usedAsset.borrowedUsd;
151
+ acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
152
+ const rate = assetData.borrowRate;
153
+ const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
154
+ acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
155
+
156
+ for (const borrowIncentive of assetData.borrowIncentives) {
157
+ const { apy, eligibilityId } = borrowIncentive;
158
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
159
+ if (eligibilityCheck) {
160
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
161
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
162
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
163
+ } else {
164
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
165
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
166
+ }
167
+ }
168
+ }
169
+
170
+ return acc;
171
+ }, {
172
+ borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
173
+ });
174
+
175
+ const {
176
+ borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
177
+ } = sumValues;
178
+
179
+ const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
180
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
181
+ const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
182
+
183
+ return { netApy, totalInterestUsd, incentiveUsd };
184
184
  };