@defisaver/positions-sdk 2.1.96 → 2.1.98-aave-v4-apy-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 (136) 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/aaveV4/index.js +2 -0
  6. package/cjs/config/contracts.d.ts +61 -1
  7. package/cjs/config/contracts.js +2 -2
  8. package/cjs/contracts.d.ts +2963 -82
  9. package/cjs/helpers/aaveV4Helpers/index.d.ts +11 -1
  10. package/cjs/helpers/aaveV4Helpers/index.js +58 -1
  11. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  12. package/cjs/savings/morphoVaults/index.js +17 -17
  13. package/cjs/services/utils.d.ts +1 -1
  14. package/cjs/services/utils.js +2 -1
  15. package/cjs/types/aaveV4.d.ts +2 -0
  16. package/esm/aaveV3/index.js +3 -3
  17. package/esm/aaveV4/index.js +2 -0
  18. package/esm/config/contracts.d.ts +61 -1
  19. package/esm/config/contracts.js +2 -2
  20. package/esm/contracts.d.ts +2963 -82
  21. package/esm/helpers/aaveV4Helpers/index.d.ts +11 -1
  22. package/esm/helpers/aaveV4Helpers/index.js +56 -0
  23. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  24. package/esm/savings/morphoVaults/index.js +17 -17
  25. package/esm/services/utils.d.ts +1 -1
  26. package/esm/services/utils.js +2 -1
  27. package/esm/types/aaveV4.d.ts +2 -0
  28. package/package.json +48 -48
  29. package/src/aaveV2/index.ts +240 -240
  30. package/src/aaveV3/index.ts +634 -634
  31. package/src/aaveV3/merit.ts +97 -97
  32. package/src/aaveV3/merkl.ts +74 -74
  33. package/src/aaveV4/index.ts +238 -236
  34. package/src/aaveV4/lend.ts +185 -185
  35. package/src/claiming/aaveV3.ts +154 -154
  36. package/src/claiming/compV3.ts +22 -22
  37. package/src/claiming/ethena.ts +61 -61
  38. package/src/claiming/index.ts +12 -12
  39. package/src/claiming/king.ts +66 -66
  40. package/src/claiming/morphoBlue.ts +118 -118
  41. package/src/claiming/spark.ts +225 -225
  42. package/src/compoundV2/index.ts +245 -245
  43. package/src/compoundV3/index.ts +280 -280
  44. package/src/config/contracts.ts +1357 -1357
  45. package/src/constants/index.ts +10 -10
  46. package/src/contracts.ts +183 -183
  47. package/src/curveUsd/index.ts +254 -254
  48. package/src/eulerV2/index.ts +330 -330
  49. package/src/exchange/index.ts +25 -25
  50. package/src/fluid/index.ts +1800 -1800
  51. package/src/helpers/aaveHelpers/index.ts +203 -203
  52. package/src/helpers/aaveV4Helpers/index.ts +349 -291
  53. package/src/helpers/compoundHelpers/index.ts +280 -280
  54. package/src/helpers/curveUsdHelpers/index.ts +44 -44
  55. package/src/helpers/eulerHelpers/index.ts +230 -230
  56. package/src/helpers/fluidHelpers/index.ts +338 -338
  57. package/src/helpers/index.ts +11 -11
  58. package/src/helpers/liquityV2Helpers/index.ts +85 -85
  59. package/src/helpers/llamaLendHelpers/index.ts +56 -56
  60. package/src/helpers/makerHelpers/index.ts +52 -52
  61. package/src/helpers/morphoBlueHelpers/index.ts +406 -406
  62. package/src/helpers/sparkHelpers/index.ts +170 -170
  63. package/src/index.ts +51 -51
  64. package/src/liquity/index.ts +161 -161
  65. package/src/liquityV2/index.ts +703 -703
  66. package/src/llamaLend/index.ts +305 -305
  67. package/src/maker/index.ts +225 -225
  68. package/src/markets/aave/index.ts +118 -118
  69. package/src/markets/aave/marketAssets.ts +54 -54
  70. package/src/markets/aaveV4/index.ts +191 -191
  71. package/src/markets/compound/index.ts +243 -243
  72. package/src/markets/compound/marketsAssets.ts +97 -97
  73. package/src/markets/curveUsd/index.ts +69 -69
  74. package/src/markets/euler/index.ts +26 -26
  75. package/src/markets/fluid/index.ts +2900 -2900
  76. package/src/markets/index.ts +29 -29
  77. package/src/markets/liquityV2/index.ts +102 -102
  78. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  79. package/src/markets/llamaLend/index.ts +235 -235
  80. package/src/markets/morphoBlue/index.ts +1370 -1370
  81. package/src/markets/spark/index.ts +29 -29
  82. package/src/markets/spark/marketAssets.ts +12 -12
  83. package/src/moneymarket/moneymarketCommonService.ts +91 -91
  84. package/src/morphoBlue/index.ts +274 -274
  85. package/src/portfolio/index.ts +606 -606
  86. package/src/savings/index.ts +111 -111
  87. package/src/savings/makerDsr/index.ts +53 -53
  88. package/src/savings/makerDsr/options.ts +9 -9
  89. package/src/savings/morphoVaults/index.ts +80 -80
  90. package/src/savings/morphoVaults/options.ts +193 -193
  91. package/src/savings/skyOptions/index.ts +95 -95
  92. package/src/savings/skyOptions/options.ts +10 -10
  93. package/src/savings/sparkSavingsVaults/index.ts +60 -60
  94. package/src/savings/sparkSavingsVaults/options.ts +35 -35
  95. package/src/savings/summerVaults/index.ts +56 -56
  96. package/src/savings/summerVaults/options.ts +117 -117
  97. package/src/savings/yearnV3Vaults/index.ts +61 -61
  98. package/src/savings/yearnV3Vaults/options.ts +55 -55
  99. package/src/savings/yearnVaults/index.ts +73 -73
  100. package/src/savings/yearnVaults/options.ts +32 -32
  101. package/src/services/priceService.ts +278 -278
  102. package/src/services/utils.ts +122 -117
  103. package/src/services/viem.ts +57 -57
  104. package/src/setup.ts +8 -8
  105. package/src/spark/index.ts +460 -460
  106. package/src/staking/eligibility.ts +105 -105
  107. package/src/staking/index.ts +1 -1
  108. package/src/staking/staking.ts +201 -201
  109. package/src/types/aave.ts +200 -200
  110. package/src/types/aaveV4.ts +179 -177
  111. package/src/types/claiming.ts +114 -114
  112. package/src/types/common.ts +117 -117
  113. package/src/types/compound.ts +147 -147
  114. package/src/types/curveUsd.ts +125 -125
  115. package/src/types/euler.ts +177 -177
  116. package/src/types/fluid.ts +486 -486
  117. package/src/types/index.ts +16 -16
  118. package/src/types/liquity.ts +31 -31
  119. package/src/types/liquityV2.ts +130 -130
  120. package/src/types/llamaLend.ts +163 -163
  121. package/src/types/maker.ts +64 -64
  122. package/src/types/merit.ts +1 -1
  123. package/src/types/merkl.ts +70 -70
  124. package/src/types/morphoBlue.ts +228 -228
  125. package/src/types/portfolio.ts +64 -64
  126. package/src/types/savings/index.ts +25 -25
  127. package/src/types/savings/makerDsr.ts +13 -13
  128. package/src/types/savings/morphoVaults.ts +32 -32
  129. package/src/types/savings/sky.ts +14 -14
  130. package/src/types/savings/sparkSavingsVaults.ts +15 -15
  131. package/src/types/savings/summerVaults.ts +24 -24
  132. package/src/types/savings/yearnV3Vaults.ts +17 -17
  133. package/src/types/savings/yearnVaults.ts +14 -14
  134. package/src/types/spark.ts +136 -136
  135. package/src/umbrella/index.ts +69 -69
  136. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -1,106 +1,106 @@
1
- import Dec from 'decimal.js';
2
- import { IncentiveEligibilityId, MMUsedAssets } from '../types/common';
3
-
4
- type EthenaPairEligibilityConfig = {
5
- baseSymbol: string;
6
- pairSymbol: string;
7
- allowedBorrowAssets: string[];
8
- maxHealthRatio: string; // exclusive upper bound
9
- rewardSymbol: string;
10
- };
11
-
12
- const isEligibleForEthenaPairRewards = (
13
- usedAssets: MMUsedAssets,
14
- { healthRatio }: { healthRatio: string },
15
- {
16
- baseSymbol,
17
- pairSymbol,
18
- allowedBorrowAssets,
19
- maxHealthRatio,
20
- rewardSymbol,
21
- }: EthenaPairEligibilityConfig,
22
- ) => {
23
- const baseSuppliedUsd = usedAssets[baseSymbol]?.suppliedUsd || '0';
24
- const pairSuppliedUsd = usedAssets[pairSymbol]?.suppliedUsd || '0';
25
-
26
- const anythingElseSupplied = Object.values(usedAssets).some(
27
- (asset) => asset.isSupplied && asset.symbol !== baseSymbol && asset.symbol !== pairSymbol,
28
- );
29
- if (anythingElseSupplied) return { isEligible: false, eligibleUSDAmount: '0' };
30
-
31
- const totalAmountSupplied = new Dec(baseSuppliedUsd).add(pairSuppliedUsd);
32
- if (totalAmountSupplied.eq(0)) return { isEligible: false, eligibleUSDAmount: '0' };
33
-
34
- const percentageInBase = new Dec(baseSuppliedUsd).div(totalAmountSupplied).toNumber();
35
- if (percentageInBase < 0.45 || percentageInBase > 0.55) return { isEligible: false, eligibleUSDAmount: '0' };
36
-
37
- const percentageInPair = new Dec(pairSuppliedUsd).div(totalAmountSupplied).toNumber();
38
- if (percentageInPair < 0.45 || percentageInPair > 0.55) return { isEligible: false, eligibleUSDAmount: '0' };
39
-
40
- const anythingBorrowedNotAllowed = Object.values(usedAssets).some(
41
- (asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol),
42
- );
43
- if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
44
-
45
- if (new Dec(healthRatio).gte(maxHealthRatio)) return { isEligible: false, eligibleUSDAmount: '0' };
46
-
47
- const halfAmountSupplied = totalAmountSupplied.div(2);
48
- const rewardSuppliedUsd = usedAssets[rewardSymbol]?.suppliedUsd || '0';
49
- const eligibleUSDAmount = Dec.min(rewardSuppliedUsd, halfAmountSupplied).toString();
50
-
51
- return { isEligible: true, eligibleUSDAmount };
52
- };
53
-
54
- export const isEligibleForEthenaUSDeRewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => isEligibleForEthenaPairRewards(
55
- usedAssets,
56
- { healthRatio },
57
- {
58
- baseSymbol: 'USDe',
59
- pairSymbol: 'sUSDe',
60
- allowedBorrowAssets: ['USDC', 'USDT', 'USDS'],
61
- maxHealthRatio: '2.5',
62
- rewardSymbol: 'USDe',
63
- },
64
- );
65
-
66
- export const isEligibleForEthenaGHORewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => isEligibleForEthenaPairRewards(
67
- usedAssets,
68
- { healthRatio },
69
- {
70
- baseSymbol: 'syrupUSDT',
71
- pairSymbol: 'GHO',
72
- allowedBorrowAssets: ['USDT'],
73
- maxHealthRatio: '2',
74
- rewardSymbol: 'GHO',
75
- },
76
- );
77
-
78
- export const isEligibleForAaveV3ArbitrumEthSupply = (usedAssets: MMUsedAssets) => {
79
- const ETHAmountSupplied = usedAssets.ETH?.suppliedUsd || '0';
80
- const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
81
- const delta = new Dec(ETHAmountSupplied).sub(ETHAmountBorrowed).toString();
82
-
83
- return { isEligible: true, eligibleUSDAmount: Dec.max(delta, 0).toString() };
84
- };
85
-
86
- export const isEligibleForAaveV3ArbitrumETHLSBorrow = (usedAssets: MMUsedAssets) => {
87
- const allowedBorrowAssets = ['ETH'];
88
- const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
89
- if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
90
-
91
- const allowedSupplyAssets = ['wstETH', 'ezETH', 'weETH', 'rsETH'];
92
- const anythingSuppliedNotAllowed = Object.values(usedAssets).some((asset) => asset.isSupplied && !allowedSupplyAssets.includes(asset.symbol));
93
- if (anythingSuppliedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
94
-
95
- const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
96
-
97
- return { isEligible: true, eligibleUSDAmount: ETHAmountBorrowed };
98
- };
99
-
100
- export const EligibilityMapping: { [key in IncentiveEligibilityId]: (usedAssets: MMUsedAssets, optionalData: any) => { isEligible: boolean; eligibleUSDAmount: string } } = {
101
- [IncentiveEligibilityId.AaveV3EthenaLiquidLeverage]: isEligibleForEthenaUSDeRewards,
102
- [IncentiveEligibilityId.AaveV3ArbitrumEthSupply]: isEligibleForAaveV3ArbitrumEthSupply,
103
- [IncentiveEligibilityId.AaveV3ArbitrumETHLSBorrow]: isEligibleForAaveV3ArbitrumETHLSBorrow,
104
- [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasma]: isEligibleForEthenaUSDeRewards,
105
- [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasmaGHO]: isEligibleForEthenaGHORewards,
1
+ import Dec from 'decimal.js';
2
+ import { IncentiveEligibilityId, MMUsedAssets } from '../types/common';
3
+
4
+ type EthenaPairEligibilityConfig = {
5
+ baseSymbol: string;
6
+ pairSymbol: string;
7
+ allowedBorrowAssets: string[];
8
+ maxHealthRatio: string; // exclusive upper bound
9
+ rewardSymbol: string;
10
+ };
11
+
12
+ const isEligibleForEthenaPairRewards = (
13
+ usedAssets: MMUsedAssets,
14
+ { healthRatio }: { healthRatio: string },
15
+ {
16
+ baseSymbol,
17
+ pairSymbol,
18
+ allowedBorrowAssets,
19
+ maxHealthRatio,
20
+ rewardSymbol,
21
+ }: EthenaPairEligibilityConfig,
22
+ ) => {
23
+ const baseSuppliedUsd = usedAssets[baseSymbol]?.suppliedUsd || '0';
24
+ const pairSuppliedUsd = usedAssets[pairSymbol]?.suppliedUsd || '0';
25
+
26
+ const anythingElseSupplied = Object.values(usedAssets).some(
27
+ (asset) => asset.isSupplied && asset.symbol !== baseSymbol && asset.symbol !== pairSymbol,
28
+ );
29
+ if (anythingElseSupplied) return { isEligible: false, eligibleUSDAmount: '0' };
30
+
31
+ const totalAmountSupplied = new Dec(baseSuppliedUsd).add(pairSuppliedUsd);
32
+ if (totalAmountSupplied.eq(0)) return { isEligible: false, eligibleUSDAmount: '0' };
33
+
34
+ const percentageInBase = new Dec(baseSuppliedUsd).div(totalAmountSupplied).toNumber();
35
+ if (percentageInBase < 0.45 || percentageInBase > 0.55) return { isEligible: false, eligibleUSDAmount: '0' };
36
+
37
+ const percentageInPair = new Dec(pairSuppliedUsd).div(totalAmountSupplied).toNumber();
38
+ if (percentageInPair < 0.45 || percentageInPair > 0.55) return { isEligible: false, eligibleUSDAmount: '0' };
39
+
40
+ const anythingBorrowedNotAllowed = Object.values(usedAssets).some(
41
+ (asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol),
42
+ );
43
+ if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
44
+
45
+ if (new Dec(healthRatio).gte(maxHealthRatio)) return { isEligible: false, eligibleUSDAmount: '0' };
46
+
47
+ const halfAmountSupplied = totalAmountSupplied.div(2);
48
+ const rewardSuppliedUsd = usedAssets[rewardSymbol]?.suppliedUsd || '0';
49
+ const eligibleUSDAmount = Dec.min(rewardSuppliedUsd, halfAmountSupplied).toString();
50
+
51
+ return { isEligible: true, eligibleUSDAmount };
52
+ };
53
+
54
+ export const isEligibleForEthenaUSDeRewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => isEligibleForEthenaPairRewards(
55
+ usedAssets,
56
+ { healthRatio },
57
+ {
58
+ baseSymbol: 'USDe',
59
+ pairSymbol: 'sUSDe',
60
+ allowedBorrowAssets: ['USDC', 'USDT', 'USDS'],
61
+ maxHealthRatio: '2.5',
62
+ rewardSymbol: 'USDe',
63
+ },
64
+ );
65
+
66
+ export const isEligibleForEthenaGHORewards = (usedAssets: MMUsedAssets, { healthRatio }: { healthRatio: string }) => isEligibleForEthenaPairRewards(
67
+ usedAssets,
68
+ { healthRatio },
69
+ {
70
+ baseSymbol: 'syrupUSDT',
71
+ pairSymbol: 'GHO',
72
+ allowedBorrowAssets: ['USDT'],
73
+ maxHealthRatio: '2',
74
+ rewardSymbol: 'GHO',
75
+ },
76
+ );
77
+
78
+ export const isEligibleForAaveV3ArbitrumEthSupply = (usedAssets: MMUsedAssets) => {
79
+ const ETHAmountSupplied = usedAssets.ETH?.suppliedUsd || '0';
80
+ const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
81
+ const delta = new Dec(ETHAmountSupplied).sub(ETHAmountBorrowed).toString();
82
+
83
+ return { isEligible: true, eligibleUSDAmount: Dec.max(delta, 0).toString() };
84
+ };
85
+
86
+ export const isEligibleForAaveV3ArbitrumETHLSBorrow = (usedAssets: MMUsedAssets) => {
87
+ const allowedBorrowAssets = ['ETH'];
88
+ const anythingBorrowedNotAllowed = Object.values(usedAssets).some((asset) => asset.isBorrowed && !allowedBorrowAssets.includes(asset.symbol));
89
+ if (anythingBorrowedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
90
+
91
+ const allowedSupplyAssets = ['wstETH', 'ezETH', 'weETH', 'rsETH'];
92
+ const anythingSuppliedNotAllowed = Object.values(usedAssets).some((asset) => asset.isSupplied && !allowedSupplyAssets.includes(asset.symbol));
93
+ if (anythingSuppliedNotAllowed) return { isEligible: false, eligibleUSDAmount: '0' };
94
+
95
+ const ETHAmountBorrowed = usedAssets.ETH?.borrowedUsd || '0';
96
+
97
+ return { isEligible: true, eligibleUSDAmount: ETHAmountBorrowed };
98
+ };
99
+
100
+ export const EligibilityMapping: { [key in IncentiveEligibilityId]: (usedAssets: MMUsedAssets, optionalData: any) => { isEligible: boolean; eligibleUSDAmount: string } } = {
101
+ [IncentiveEligibilityId.AaveV3EthenaLiquidLeverage]: isEligibleForEthenaUSDeRewards,
102
+ [IncentiveEligibilityId.AaveV3ArbitrumEthSupply]: isEligibleForAaveV3ArbitrumEthSupply,
103
+ [IncentiveEligibilityId.AaveV3ArbitrumETHLSBorrow]: isEligibleForAaveV3ArbitrumETHLSBorrow,
104
+ [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasma]: isEligibleForEthenaUSDeRewards,
105
+ [IncentiveEligibilityId.AaveV3EthenaLiquidLeveragePlasmaGHO]: isEligibleForEthenaGHORewards,
106
106
  };
@@ -1,2 +1,2 @@
1
- export * from './staking';
1
+ export * from './staking';
2
2
  export * from './eligibility';
@@ -1,202 +1,202 @@
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 sUSDe Apr', 'PT USDe Apr', 'PT sUSDe May', 'PT USDe May', 'PT srUSDe Apr', 'GHO',
68
- 'PT sUSDe June', 'PT USDe June', 'PT srUSDe Jun', 'PT USDG May',
69
- ];
70
-
71
- export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
72
- try {
73
- if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
74
- if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
75
- if (asset === 'rETH') return await getApyFromDfsApi('rETH');
76
- if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
77
- if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
78
- if (asset === 'weETH') return await getApyFromDfsApi('weETH');
79
- if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
80
- if (asset === 'osETH') return await getApyFromDfsApi('osETH');
81
- if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
82
- if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
83
- if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
84
- if (asset === 'wsuperOETHb') return await getSuperOETHApy();
85
- if (asset === 'sUSDS') return await getSsrApy();
86
- if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
87
- if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
88
- if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
89
- if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
90
- if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
91
- if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
92
- if (asset === 'tETH') return await getApyFromDfsApi('tETH');
93
- if (asset === 'USDe') return await getApyFromDfsApi('USDe', network);
94
- if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
95
- if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
96
- if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
97
- if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
98
- if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
99
- if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
100
- if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
101
- if (asset === 'PT sUSDe Feb') return await getApyFromDfsApi('PT sUSDe Feb', network);
102
- if (asset === 'PT USDe Feb') return await getApyFromDfsApi('PT USDe Feb', network);
103
- if (asset === 'PT sUSDe Apr') return await getApyFromDfsApi('PT sUSDe Apr', network);
104
- if (asset === 'PT USDe Apr') return await getApyFromDfsApi('PT USDe Apr', network);
105
- if (asset === 'PT sUSDe May') return await getApyFromDfsApi('PT sUSDe May', network);
106
- if (asset === 'PT USDe May') return await getApyFromDfsApi('PT USDe May', network);
107
- if (asset === 'PT srUSDe Apr') return await getApyFromDfsApi('PT srUSDe Apr', network);
108
- if (asset === 'PT srUSDe Jun') return await getApyFromDfsApi('PT srUSDe Jun', network);
109
- if (asset === 'PT sUSDe June') return await getApyFromDfsApi('PT sUSDe June', network);
110
- if (asset === 'PT USDe June') return await getApyFromDfsApi('PT USDe June', network);
111
- if (asset === 'PT USDG May') return await getApyFromDfsApi('PT USDG May', network);
112
- if (asset === 'GHO') return await getApyFromDfsApi('GHO', network);
113
- } catch (e) {
114
- console.error(`Failed to fetch APY for ${asset}`);
115
- }
116
- return '0';
117
- }, { promise: true, maxAge: 2 * 60 * 1000 });
118
-
119
- export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
120
- let interval = 1;
121
-
122
- if (+interest === 0) return 0;
123
-
124
- if (type === 'month') interval = 1 / 12;
125
- if (type === 'week') interval = 1 / 52.1429;
126
-
127
- if (apy) {
128
- // interest rate already compounded
129
- return (+principal * (1 + (+interest / 100 * interval))) - +principal;
130
- }
131
-
132
- return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
133
- };
134
-
135
- export const calculateNetApy = ({
136
- usedAssets, assetsData, optionalData,
137
- }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
138
- const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
139
- const acc = { ..._acc };
140
- const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
141
-
142
- if (usedAsset.isSupplied) {
143
- const amount = usedAsset.suppliedUsd;
144
- acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
145
- const rate = assetData.supplyRate;
146
- const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
147
- acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
148
-
149
- if (assetData.supplyIncentives) {
150
- for (const supplyIncentive of assetData.supplyIncentives) {
151
- const { apy, eligibilityId } = supplyIncentive;
152
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
153
- if (eligibilityCheck) {
154
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
155
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
156
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
157
- } else {
158
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
159
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
160
- }
161
- }
162
- }
163
- }
164
-
165
- if (usedAsset.isBorrowed) {
166
- const amount = usedAsset.borrowedUsd;
167
- acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
168
- const rate = assetData.borrowRate;
169
- const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
170
- acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
171
-
172
- if (assetData.borrowIncentives) {
173
- for (const borrowIncentive of assetData.borrowIncentives) {
174
- const { apy, eligibilityId } = borrowIncentive;
175
- const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
176
- if (eligibilityCheck) {
177
- const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
178
- const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
179
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
180
- } else {
181
- const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
182
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
183
- }
184
- }
185
- }
186
- }
187
-
188
- return acc;
189
- }, {
190
- borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
191
- });
192
-
193
- const {
194
- borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
195
- } = sumValues;
196
-
197
- const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
198
- const balance = new Dec(suppliedUsd).sub(borrowedUsd);
199
- const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
200
-
201
- 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 sUSDe Apr', 'PT USDe Apr', 'PT sUSDe May', 'PT USDe May', 'PT srUSDe Apr', 'GHO',
68
+ 'PT sUSDe June', 'PT USDe June', 'PT srUSDe Jun', 'PT USDG May',
69
+ ];
70
+
71
+ export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
72
+ try {
73
+ if (asset === 'stETH' || asset === 'wstETH') return await getApyFromDfsApi('wstETH');
74
+ if (asset === 'cbETH') return await getApyFromDfsApi('cbETH');
75
+ if (asset === 'rETH') return await getApyFromDfsApi('rETH');
76
+ if (asset === 'sDAI') return await getApyFromDfsApi('sDAI');
77
+ if (asset === 'sUSDe') return await getApyFromDfsApi('sUSDe');
78
+ if (asset === 'weETH') return await getApyFromDfsApi('weETH');
79
+ if (asset === 'ezETH') return await getApyFromDfsApi('ezETH');
80
+ if (asset === 'osETH') return await getApyFromDfsApi('osETH');
81
+ if (asset === 'ETHx') return await getApyFromDfsApi('ETHx');
82
+ if (asset === 'rsETH' || asset === 'wrsETH') return await getApyFromDfsApi('rsETH');
83
+ if (asset === 'pufETH') return await getApyFromDfsApi('pufETH');
84
+ if (asset === 'wsuperOETHb') return await getSuperOETHApy();
85
+ if (asset === 'sUSDS') return await getSsrApy();
86
+ if (asset === 'PT eUSDe May') return await getApyFromDfsApi('PT eUSDe May', network);
87
+ if (asset === 'PT sUSDe July') return await getApyFromDfsApi('PT sUSDe July', network);
88
+ if (asset === 'PT USDe July') return await getApyFromDfsApi('PT USDe July', network);
89
+ if (asset === 'PT eUSDe Aug') return await getApyFromDfsApi('PT eUSDe Aug', network);
90
+ if (asset === 'PT sUSDe Sep') return await getApyFromDfsApi('PT sUSDe Sep', network);
91
+ if (asset === 'PT USDe Sep') return await getApyFromDfsApi('PT USDe Sep', network);
92
+ if (asset === 'tETH') return await getApyFromDfsApi('tETH');
93
+ if (asset === 'USDe') return await getApyFromDfsApi('USDe', network);
94
+ if (asset === 'PT sUSDe Nov') return await getApyFromDfsApi('PT sUSDe Nov', network);
95
+ if (asset === 'PT USDe Nov') return await getApyFromDfsApi('PT USDe Nov', network);
96
+ if (asset === 'PT USDe Jan') return await getApyFromDfsApi('PT USDe Jan', network);
97
+ if (asset === 'PT sUSDe Jan') return await getApyFromDfsApi('PT sUSDe Jan', network);
98
+ if (asset === 'syrupUSDT') return await getApyFromDfsApi('syrupUSDT');
99
+ if (asset === 'syrupUSDC') return await getApyFromDfsApi('syrupUSDC');
100
+ if (asset === 'wstUSR') return await getApyFromDfsApi('wstUSR');
101
+ if (asset === 'PT sUSDe Feb') return await getApyFromDfsApi('PT sUSDe Feb', network);
102
+ if (asset === 'PT USDe Feb') return await getApyFromDfsApi('PT USDe Feb', network);
103
+ if (asset === 'PT sUSDe Apr') return await getApyFromDfsApi('PT sUSDe Apr', network);
104
+ if (asset === 'PT USDe Apr') return await getApyFromDfsApi('PT USDe Apr', network);
105
+ if (asset === 'PT sUSDe May') return await getApyFromDfsApi('PT sUSDe May', network);
106
+ if (asset === 'PT USDe May') return await getApyFromDfsApi('PT USDe May', network);
107
+ if (asset === 'PT srUSDe Apr') return await getApyFromDfsApi('PT srUSDe Apr', network);
108
+ if (asset === 'PT srUSDe Jun') return await getApyFromDfsApi('PT srUSDe Jun', network);
109
+ if (asset === 'PT sUSDe June') return await getApyFromDfsApi('PT sUSDe June', network);
110
+ if (asset === 'PT USDe June') return await getApyFromDfsApi('PT USDe June', network);
111
+ if (asset === 'PT USDG May') return await getApyFromDfsApi('PT USDG May', network);
112
+ if (asset === 'GHO') return await getApyFromDfsApi('GHO', network);
113
+ } catch (e) {
114
+ console.error(`Failed to fetch APY for ${asset}`);
115
+ }
116
+ return '0';
117
+ }, { promise: true, maxAge: 2 * 60 * 1000 });
118
+
119
+ export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
120
+ let interval = 1;
121
+
122
+ if (+interest === 0) return 0;
123
+
124
+ if (type === 'month') interval = 1 / 12;
125
+ if (type === 'week') interval = 1 / 52.1429;
126
+
127
+ if (apy) {
128
+ // interest rate already compounded
129
+ return (+principal * (1 + (+interest / 100 * interval))) - +principal;
130
+ }
131
+
132
+ return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
133
+ };
134
+
135
+ export const calculateNetApy = ({
136
+ usedAssets, assetsData, optionalData,
137
+ }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
138
+ const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
139
+ const acc = { ..._acc };
140
+ const assetData = assetsData[usedAsset.symbol] || assetsData[(usedAsset as EulerV2UsedAsset).vaultAddress?.toLowerCase() || ''];
141
+
142
+ if (usedAsset.isSupplied) {
143
+ const amount = usedAsset.suppliedUsd;
144
+ acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
145
+ const rate = assetData.supplyRate;
146
+ const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
147
+ acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
148
+
149
+ if (assetData.supplyIncentives) {
150
+ for (const supplyIncentive of assetData.supplyIncentives) {
151
+ const { apy, eligibilityId } = supplyIncentive;
152
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
153
+ if (eligibilityCheck) {
154
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
155
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
156
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
157
+ } else {
158
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
159
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
160
+ }
161
+ }
162
+ }
163
+ }
164
+
165
+ if (usedAsset.isBorrowed) {
166
+ const amount = usedAsset.borrowedUsd;
167
+ acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
168
+ const rate = assetData.borrowRate;
169
+ const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
170
+ acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
171
+
172
+ if (assetData.borrowIncentives) {
173
+ for (const borrowIncentive of assetData.borrowIncentives) {
174
+ const { apy, eligibilityId } = borrowIncentive;
175
+ const eligibilityCheck = eligibilityId ? EligibilityMapping[eligibilityId] : null;
176
+ if (eligibilityCheck) {
177
+ const { isEligible, eligibleUSDAmount } = eligibilityCheck(usedAssets, optionalData);
178
+ const incentiveInterest = isEligible ? calculateInterestEarned(eligibleUSDAmount, apy, 'year', true) : '0';
179
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
180
+ } else {
181
+ const incentiveInterest = calculateInterestEarned(amount, apy, 'year', true);
182
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
183
+ }
184
+ }
185
+ }
186
+ }
187
+
188
+ return acc;
189
+ }, {
190
+ borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
191
+ });
192
+
193
+ const {
194
+ borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
195
+ } = sumValues;
196
+
197
+ const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
198
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
199
+ const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
200
+
201
+ return { netApy, totalInterestUsd, incentiveUsd };
202
202
  };