@defisaver/positions-sdk 2.1.54 → 2.1.56-dev-exposure

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 (184) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV3/index.d.ts +1 -0
  5. package/cjs/aaveV3/index.js +5 -2
  6. package/cjs/compoundV2/index.d.ts +1 -0
  7. package/cjs/compoundV2/index.js +1 -0
  8. package/cjs/compoundV3/index.d.ts +1 -0
  9. package/cjs/compoundV3/index.js +1 -0
  10. package/cjs/eulerV2/index.d.ts +1 -0
  11. package/cjs/eulerV2/index.js +1 -0
  12. package/cjs/fluid/index.d.ts +3 -0
  13. package/cjs/helpers/aaveHelpers/index.js +1 -0
  14. package/cjs/helpers/compoundHelpers/index.js +2 -0
  15. package/cjs/helpers/curveUsdHelpers/index.js +1 -0
  16. package/cjs/helpers/eulerHelpers/index.js +1 -0
  17. package/cjs/helpers/fluidHelpers/index.js +1 -0
  18. package/cjs/helpers/liquityV2Helpers/index.js +1 -0
  19. package/cjs/helpers/llamaLendHelpers/index.js +1 -0
  20. package/cjs/helpers/morphoBlueHelpers/index.js +67 -66
  21. package/cjs/helpers/sparkHelpers/index.js +1 -0
  22. package/cjs/liquity/index.js +2 -0
  23. package/cjs/maker/index.js +2 -0
  24. package/cjs/markets/morphoBlue/index.d.ts +4 -0
  25. package/cjs/markets/morphoBlue/index.js +36 -2
  26. package/cjs/moneymarket/moneymarketCommonService.d.ts +1 -0
  27. package/cjs/moneymarket/moneymarketCommonService.js +8 -1
  28. package/cjs/savings/morphoVaults/index.js +17 -17
  29. package/cjs/spark/index.d.ts +1 -0
  30. package/cjs/spark/index.js +1 -0
  31. package/cjs/types/aave.d.ts +1 -0
  32. package/cjs/types/common.d.ts +1 -0
  33. package/cjs/types/compound.d.ts +1 -0
  34. package/cjs/types/curveUsd.d.ts +2 -0
  35. package/cjs/types/euler.d.ts +1 -0
  36. package/cjs/types/fluid.d.ts +1 -0
  37. package/cjs/types/liquity.d.ts +1 -0
  38. package/cjs/types/liquityV2.d.ts +2 -0
  39. package/cjs/types/llamaLend.d.ts +2 -0
  40. package/cjs/types/maker.d.ts +1 -0
  41. package/cjs/types/morphoBlue.d.ts +4 -0
  42. package/cjs/types/morphoBlue.js +2 -0
  43. package/cjs/types/spark.d.ts +1 -0
  44. package/esm/aaveV3/index.d.ts +1 -0
  45. package/esm/aaveV3/index.js +5 -2
  46. package/esm/compoundV2/index.d.ts +1 -0
  47. package/esm/compoundV2/index.js +1 -0
  48. package/esm/compoundV3/index.d.ts +1 -0
  49. package/esm/compoundV3/index.js +1 -0
  50. package/esm/eulerV2/index.d.ts +1 -0
  51. package/esm/eulerV2/index.js +1 -0
  52. package/esm/fluid/index.d.ts +3 -0
  53. package/esm/helpers/aaveHelpers/index.js +2 -1
  54. package/esm/helpers/compoundHelpers/index.js +3 -1
  55. package/esm/helpers/curveUsdHelpers/index.js +2 -1
  56. package/esm/helpers/eulerHelpers/index.js +2 -1
  57. package/esm/helpers/fluidHelpers/index.js +2 -1
  58. package/esm/helpers/liquityV2Helpers/index.js +2 -1
  59. package/esm/helpers/llamaLendHelpers/index.js +2 -1
  60. package/esm/helpers/morphoBlueHelpers/index.js +68 -67
  61. package/esm/helpers/sparkHelpers/index.js +2 -1
  62. package/esm/liquity/index.js +2 -0
  63. package/esm/maker/index.js +2 -0
  64. package/esm/markets/morphoBlue/index.d.ts +4 -0
  65. package/esm/markets/morphoBlue/index.js +32 -0
  66. package/esm/moneymarket/moneymarketCommonService.d.ts +1 -0
  67. package/esm/moneymarket/moneymarketCommonService.js +6 -0
  68. package/esm/savings/morphoVaults/index.js +17 -17
  69. package/esm/spark/index.d.ts +1 -0
  70. package/esm/spark/index.js +1 -0
  71. package/esm/types/aave.d.ts +1 -0
  72. package/esm/types/common.d.ts +1 -0
  73. package/esm/types/compound.d.ts +1 -0
  74. package/esm/types/curveUsd.d.ts +2 -0
  75. package/esm/types/euler.d.ts +1 -0
  76. package/esm/types/fluid.d.ts +1 -0
  77. package/esm/types/liquity.d.ts +1 -0
  78. package/esm/types/liquityV2.d.ts +2 -0
  79. package/esm/types/llamaLend.d.ts +2 -0
  80. package/esm/types/maker.d.ts +1 -0
  81. package/esm/types/morphoBlue.d.ts +4 -0
  82. package/esm/types/morphoBlue.js +2 -0
  83. package/esm/types/spark.d.ts +1 -0
  84. package/package.json +48 -48
  85. package/src/aaveV2/index.ts +240 -240
  86. package/src/aaveV3/index.ts +638 -635
  87. package/src/aaveV3/merit.ts +97 -97
  88. package/src/aaveV3/merkl.ts +74 -74
  89. package/src/claiming/aaveV3.ts +154 -154
  90. package/src/claiming/compV3.ts +22 -22
  91. package/src/claiming/ethena.ts +61 -61
  92. package/src/claiming/index.ts +12 -12
  93. package/src/claiming/king.ts +66 -66
  94. package/src/claiming/morphoBlue.ts +118 -118
  95. package/src/claiming/spark.ts +225 -225
  96. package/src/compoundV2/index.ts +245 -244
  97. package/src/compoundV3/index.ts +275 -274
  98. package/src/config/contracts.ts +1320 -1320
  99. package/src/constants/index.ts +10 -10
  100. package/src/contracts.ts +171 -171
  101. package/src/curveUsd/index.ts +254 -254
  102. package/src/eulerV2/index.ts +325 -324
  103. package/src/exchange/index.ts +25 -25
  104. package/src/fluid/index.ts +1800 -1800
  105. package/src/helpers/aaveHelpers/index.ts +203 -202
  106. package/src/helpers/compoundHelpers/index.ts +278 -276
  107. package/src/helpers/curveUsdHelpers/index.ts +44 -40
  108. package/src/helpers/eulerHelpers/index.ts +230 -229
  109. package/src/helpers/fluidHelpers/index.ts +338 -335
  110. package/src/helpers/index.ts +10 -10
  111. package/src/helpers/liquityV2Helpers/index.ts +85 -82
  112. package/src/helpers/llamaLendHelpers/index.ts +56 -53
  113. package/src/helpers/makerHelpers/index.ts +52 -52
  114. package/src/helpers/morphoBlueHelpers/index.ts +406 -405
  115. package/src/helpers/sparkHelpers/index.ts +170 -169
  116. package/src/index.ts +49 -49
  117. package/src/liquity/index.ts +161 -159
  118. package/src/liquityV2/index.ts +703 -703
  119. package/src/llamaLend/index.ts +305 -305
  120. package/src/maker/index.ts +225 -223
  121. package/src/markets/aave/index.ts +118 -118
  122. package/src/markets/aave/marketAssets.ts +54 -54
  123. package/src/markets/compound/index.ts +243 -243
  124. package/src/markets/compound/marketsAssets.ts +97 -97
  125. package/src/markets/curveUsd/index.ts +69 -69
  126. package/src/markets/euler/index.ts +26 -26
  127. package/src/markets/fluid/index.ts +2900 -2900
  128. package/src/markets/index.ts +25 -25
  129. package/src/markets/liquityV2/index.ts +102 -102
  130. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  131. package/src/markets/llamaLend/index.ts +235 -235
  132. package/src/markets/morphoBlue/index.ts +1020 -988
  133. package/src/markets/spark/index.ts +29 -29
  134. package/src/markets/spark/marketAssets.ts +12 -12
  135. package/src/moneymarket/moneymarketCommonService.ts +90 -84
  136. package/src/morphoBlue/index.ts +274 -274
  137. package/src/portfolio/index.ts +586 -586
  138. package/src/savings/index.ts +95 -95
  139. package/src/savings/makerDsr/index.ts +53 -53
  140. package/src/savings/makerDsr/options.ts +9 -9
  141. package/src/savings/morphoVaults/index.ts +80 -80
  142. package/src/savings/morphoVaults/options.ts +193 -193
  143. package/src/savings/skyOptions/index.ts +95 -95
  144. package/src/savings/skyOptions/options.ts +10 -10
  145. package/src/savings/sparkSavingsVaults/index.ts +60 -60
  146. package/src/savings/sparkSavingsVaults/options.ts +35 -35
  147. package/src/savings/yearnV3Vaults/index.ts +61 -61
  148. package/src/savings/yearnV3Vaults/options.ts +55 -55
  149. package/src/savings/yearnVaults/index.ts +73 -73
  150. package/src/savings/yearnVaults/options.ts +32 -32
  151. package/src/services/priceService.ts +278 -278
  152. package/src/services/utils.ts +115 -115
  153. package/src/services/viem.ts +57 -57
  154. package/src/setup.ts +8 -8
  155. package/src/spark/index.ts +460 -459
  156. package/src/staking/eligibility.ts +53 -53
  157. package/src/staking/index.ts +1 -1
  158. package/src/staking/staking.ts +192 -192
  159. package/src/types/aave.ts +200 -199
  160. package/src/types/claiming.ts +114 -114
  161. package/src/types/common.ts +116 -115
  162. package/src/types/compound.ts +146 -145
  163. package/src/types/curveUsd.ts +125 -123
  164. package/src/types/euler.ts +177 -176
  165. package/src/types/fluid.ts +486 -485
  166. package/src/types/index.ts +15 -15
  167. package/src/types/liquity.ts +31 -30
  168. package/src/types/liquityV2.ts +130 -128
  169. package/src/types/llamaLend.ts +163 -161
  170. package/src/types/maker.ts +64 -63
  171. package/src/types/merit.ts +1 -1
  172. package/src/types/merkl.ts +70 -70
  173. package/src/types/morphoBlue.ts +206 -202
  174. package/src/types/portfolio.ts +60 -60
  175. package/src/types/savings/index.ts +23 -23
  176. package/src/types/savings/makerDsr.ts +13 -13
  177. package/src/types/savings/morphoVaults.ts +32 -32
  178. package/src/types/savings/sky.ts +14 -14
  179. package/src/types/savings/sparkSavingsVaults.ts +15 -15
  180. package/src/types/savings/yearnV3Vaults.ts +17 -17
  181. package/src/types/savings/yearnVaults.ts +14 -14
  182. package/src/types/spark.ts +136 -135
  183. package/src/umbrella/index.ts +69 -69
  184. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -1,275 +1,275 @@
1
- import Dec from 'decimal.js';
2
- import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
3
- import { Client } from 'viem';
4
- import {
5
- Blockish, EthAddress, EthereumProvider, IncentiveKind, MMUsedAssets, NetworkNumber, PositionBalances, MMAssetsData,
6
- } from '../types/common';
7
- import {
8
- DFSFeedRegistryContractViem, FeedRegistryContractViem, MorphoBlueViewContractViem,
9
- } from '../contracts';
10
- import {
11
- MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
12
- } from '../types';
13
- import { USD_QUOTE, WAD } from '../constants';
14
- import { calculateNetApy, getStakingApy, STAKING_ASSETS } from '../staking';
15
- import { isMainnetNetwork, wethToEth } from '../services/utils';
16
- import {
17
- getBorrowRate, getMorphoBlueAggregatedPositionData, getRewardsForMarket, getSupplyRate,
18
- } from '../helpers/morphoBlueHelpers';
19
- import { getChainlinkAssetAddress } from '../services/priceService';
20
- import { getViemProvider, setViemBlockNumber } from '../services/viem';
21
-
22
- export async function _getMorphoBlueMarketData(provider: Client, network: NetworkNumber, selectedMarket: MorphoBlueMarketData): Promise<MorphoBlueMarketInfo> {
23
- const {
24
- loanToken, collateralToken, oracle, irm, lltv, oracleType,
25
- } = selectedMarket;
26
-
27
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
28
- const loanTokenInfo = getAssetInfoByAddress(loanToken, network);
29
- const collateralTokenInfo = getAssetInfoByAddress(collateralToken, network);
30
-
31
- const loanTokenFeedAddress = getChainlinkAssetAddress(loanTokenInfo.symbol, network);
32
-
33
- const morphoBlueViewContract = MorphoBlueViewContractViem(provider, network);
34
-
35
- let marketInfo;
36
- let loanTokenPrice;
37
- const isTokenUSDA = loanTokenInfo.symbol === 'USDA';
38
- const isMainnet = isMainnetNetwork(network);
39
- if (isMainnet) {
40
- const feedRegistryContract = FeedRegistryContractViem(provider, NetworkNumber.Eth);
41
- const [_loanTokenPrice, _marketInfo] = await Promise.all([
42
- isTokenUSDA ? Promise.resolve('100000000') : feedRegistryContract.read.latestAnswer([loanTokenFeedAddress, USD_QUOTE]),
43
- morphoBlueViewContract.read.getMarketInfoNotTuple([loanToken, collateralToken, oracle, irm, BigInt(lltvInWei)]),
44
- ]);
45
- marketInfo = _marketInfo;
46
- loanTokenPrice = _loanTokenPrice;
47
- } else {
48
- // Currently only base network is supported
49
- const feedRegistryContract = DFSFeedRegistryContractViem(provider, network);
50
-
51
- const [loanTokenPriceRound, _marketInfo] = await Promise.all([
52
- isTokenUSDA ? Promise.resolve([0, '100000000']) // Normalize to match the expected object structure
53
- : feedRegistryContract.read.latestRoundData([loanTokenFeedAddress, USD_QUOTE]),
54
- morphoBlueViewContract.read.getMarketInfoNotTuple([loanToken, collateralToken, oracle, irm, BigInt(lltvInWei)]),
55
- ]);
56
- marketInfo = _marketInfo;
57
- loanTokenPrice = loanTokenPriceRound[1].toString();
58
- }
59
-
60
- let morphoSupplyApy = '0';
61
- let morphoBorrowApy = '0';
62
- try {
63
- const { supplyApy: _morphoSupplyApy, borrowApy: _morphoBorrowApy } = await getRewardsForMarket(selectedMarket.marketId, network);
64
- morphoSupplyApy = _morphoSupplyApy;
65
- morphoBorrowApy = _morphoBorrowApy;
66
- } catch (e) {
67
- console.error(e);
68
- }
69
-
70
- const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets.toString(), marketInfo.totalBorrowAssets.toString(), marketInfo.borrowRate.toString(), marketInfo.fee.toString());
71
- const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate.toString(), marketInfo.totalBorrowShares.toString());
72
- const utillization = new Dec(marketInfo.totalBorrowAssets.toString()).div(marketInfo.totalSupplyAssets.toString()).mul(100).toString();
73
-
74
- const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
75
- const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
76
-
77
- const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
78
-
79
- const oracleRate = new Dec(marketInfo.oracle.toString()).div(oracleScale).toString();
80
- const assetsData: MorphoBlueAssetsData = {};
81
- assetsData[wethToEth(loanTokenInfo.symbol)] = {
82
- symbol: wethToEth(loanTokenInfo.symbol),
83
- address: loanToken,
84
- price: new Dec(loanTokenPrice).div(1e8).toString(),
85
- supplyRate,
86
- borrowRate: compoundedBorrowRate,
87
- totalSupply: new Dec(marketInfo.totalSupplyAssets.toString()).div(scale).toString(),
88
- totalBorrow: new Dec(marketInfo.totalBorrowAssets.toString()).div(scale).toString(),
89
- canBeSupplied: true,
90
- canBeBorrowed: true,
91
- supplyIncentives: [{
92
- token: 'MORPHO',
93
- apy: morphoSupplyApy,
94
- incentiveKind: IncentiveKind.Reward,
95
- description: 'Eligible for protocol-level MORPHO incentives.',
96
- }],
97
- borrowIncentives: [{
98
- token: 'MORPHO',
99
- apy: morphoBorrowApy,
100
- incentiveKind: IncentiveKind.Reward,
101
- description: 'Eligible for protocol-level MORPHO incentives.',
102
- }],
103
- };
104
-
105
- assetsData[wethToEth(collateralTokenInfo.symbol)] = {
106
- symbol: wethToEth(collateralTokenInfo.symbol),
107
- address: collateralToken,
108
- price: new Dec(assetsData[wethToEth(loanTokenInfo.symbol)].price).mul(oracleRate).toString(),
109
- supplyRate: '0',
110
- borrowRate: '0',
111
- canBeSupplied: true,
112
- canBeBorrowed: false,
113
- supplyIncentives: [],
114
- borrowIncentives: [],
115
- };
116
- if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
117
- assetsData[collateralTokenInfo.symbol].supplyIncentives = [{
118
- apy: await getStakingApy(collateralTokenInfo.symbol),
119
- token: collateralTokenInfo.symbol,
120
- incentiveKind: IncentiveKind.Staking,
121
- description: `Native ${collateralTokenInfo.symbol} yield.`,
122
- }];
123
- }
124
-
125
- return {
126
- id: marketInfo.id,
127
- fee: new Dec(marketInfo.fee.toString()).div(WAD).toString(),
128
- loanToken: wethToEth(loanTokenInfo.symbol),
129
- collateralToken: wethToEth(collateralTokenInfo.symbol),
130
- utillization,
131
- oracle: oracleRate,
132
- oracleType,
133
- lltv: new Dec(lltv).toString(),
134
- minRatio: new Dec(1).div(lltv).mul(100).toString(),
135
- assetsData,
136
- };
137
- }
138
-
139
- export async function getMorphoBlueMarketData(provider: EthereumProvider, network: NetworkNumber, selectedMarket: MorphoBlueMarketData): Promise<MorphoBlueMarketInfo> {
140
- return _getMorphoBlueMarketData(getViemProvider(provider, network), network, selectedMarket);
141
- }
142
-
143
- export const _getMorphoBlueAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoBlueMarketData): Promise<PositionBalances> => {
144
- let balances: PositionBalances = {
145
- collateral: {},
146
- debt: {},
147
- };
148
-
149
- if (!address) {
150
- return balances;
151
- }
152
-
153
- const viewContract = MorphoBlueViewContractViem(provider, network, block);
154
- const {
155
- loanToken, collateralToken, oracle, irm, lltv,
156
- } = selectedMarket;
157
- const lltvInWei = BigInt(new Dec(lltv).mul(WAD).toString());
158
- const marketObject = {
159
- loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
160
- };
161
-
162
- const loanInfo = await viewContract.read.getUserInfo([marketObject, address], setViemBlockNumber(block));
163
- const loanTokenInfo = getAssetInfoByAddress(selectedMarket.loanToken, network);
164
- const collateralTokenInfo = getAssetInfoByAddress(selectedMarket.collateralToken, network);
165
-
166
- balances = {
167
- collateral: {
168
- [addressMapping ? collateralTokenInfo.address.toLowerCase() : wethToEth(collateralTokenInfo.symbol)]: assetAmountInEth(loanInfo.collateral.toString(), collateralTokenInfo.symbol),
169
- },
170
- debt: {
171
- [addressMapping ? loanTokenInfo.address.toLowerCase() : wethToEth(loanTokenInfo.symbol)]: assetAmountInEth(loanInfo.borrowedInAssets.toString(), loanTokenInfo.symbol),
172
- },
173
- };
174
-
175
- return balances;
176
- };
177
-
178
- export const getMorphoBlueAccountBalances = async (
179
- provider: EthereumProvider,
180
- network: NetworkNumber,
181
- block: Blockish,
182
- addressMapping: boolean,
183
- address: EthAddress,
184
- selectedMarket: MorphoBlueMarketData,
185
- ): Promise<PositionBalances> => _getMorphoBlueAccountBalances(getViemProvider(provider, network), network, block, addressMapping, address, selectedMarket);
186
-
187
- export async function _getMorphoBlueAccountData(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
188
- const {
189
- loanToken, collateralToken, oracle, irm, lltv,
190
- } = selectedMarket;
191
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
192
- const viewContract = MorphoBlueViewContractViem(provider, network);
193
- const loanInfo = (await viewContract.read.getUserInfo([
194
- {
195
- loanToken, collateralToken, oracle, irm, lltv: BigInt(lltvInWei),
196
- },
197
- account]));
198
- const usedAssets: MMUsedAssets = {};
199
-
200
- const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
201
- const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets.toString(), marketInfo.loanToken);
202
- const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets.toString(), marketInfo.loanToken);
203
- usedAssets[marketInfo.loanToken] = {
204
- symbol: loanTokenInfo.symbol,
205
- supplied: loanTokenSupplied,
206
- borrowed: loanTokenBorrowed,
207
- isSupplied: new Dec(loanInfo.suppliedInAssets.toString()).gt(0),
208
- isBorrowed: new Dec(loanInfo.borrowedInAssets.toString()).gt(0),
209
- collateral: false,
210
- suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
211
- borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
212
- };
213
-
214
- const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
215
- const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral.toString(), marketInfo.collateralToken);
216
- usedAssets[marketInfo.collateralToken] = {
217
- symbol: collateralTokenInfo.symbol,
218
- supplied: collateralTokenSupplied,
219
- borrowed: '0',
220
- isSupplied: new Dec(loanInfo.collateral.toString()).gt(0),
221
- isBorrowed: false,
222
- collateral: true,
223
- suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
224
- borrowedUsd: '0',
225
- };
226
-
227
- return {
228
- supplyShares: loanInfo.supplyShares.toString(),
229
- borrowShares: loanInfo.borrowShares.toString(),
230
- usedAssets,
231
- ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
232
- };
233
- }
234
-
235
- export async function getMorphoBlueAccountData(provider: EthereumProvider, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
236
- return _getMorphoBlueAccountData(getViemProvider(provider, network), network, account, selectedMarket, marketInfo);
237
- }
238
-
239
- export async function getMorphoEarn(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<{ apy: string, amount: string, amountUsd: string }> {
240
- const {
241
- loanToken, collateralToken, oracle, irm, lltv,
242
- } = selectedMarket;
243
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
244
-
245
- const viewContract = MorphoBlueViewContractViem(provider, network);
246
- const loanInfo = (await viewContract.read.getUserInfo([
247
- {
248
- loanToken, collateralToken, oracle, irm, lltv: BigInt(lltvInWei),
249
- },
250
- account]));
251
-
252
- const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
253
- const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets.toString(), marketInfo.loanToken);
254
- const loanTokenSuppliedUsd = new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString();
255
- const usedAssets: MMUsedAssets = {
256
- [marketInfo.loanToken]: {
257
- symbol: loanTokenInfo.symbol,
258
- supplied: loanTokenSupplied,
259
- borrowed: '0',
260
- isSupplied: new Dec(loanInfo.suppliedInAssets.toString()).gt(0),
261
- isBorrowed: false,
262
- collateral: false,
263
- suppliedUsd: loanTokenSuppliedUsd,
264
- borrowedUsd: '0',
265
- },
266
- };
267
-
268
- const { netApy } = calculateNetApy({ usedAssets, assetsData: marketInfo.assetsData as unknown as MMAssetsData });
269
-
270
- return {
271
- apy: netApy,
272
- amount: loanTokenSupplied,
273
- amountUsd: loanTokenSuppliedUsd,
274
- };
1
+ import Dec from 'decimal.js';
2
+ import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
3
+ import { Client } from 'viem';
4
+ import {
5
+ Blockish, EthAddress, EthereumProvider, IncentiveKind, MMUsedAssets, NetworkNumber, PositionBalances, MMAssetsData,
6
+ } from '../types/common';
7
+ import {
8
+ DFSFeedRegistryContractViem, FeedRegistryContractViem, MorphoBlueViewContractViem,
9
+ } from '../contracts';
10
+ import {
11
+ MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
12
+ } from '../types';
13
+ import { USD_QUOTE, WAD } from '../constants';
14
+ import { calculateNetApy, getStakingApy, STAKING_ASSETS } from '../staking';
15
+ import { isMainnetNetwork, wethToEth } from '../services/utils';
16
+ import {
17
+ getBorrowRate, getMorphoBlueAggregatedPositionData, getRewardsForMarket, getSupplyRate,
18
+ } from '../helpers/morphoBlueHelpers';
19
+ import { getChainlinkAssetAddress } from '../services/priceService';
20
+ import { getViemProvider, setViemBlockNumber } from '../services/viem';
21
+
22
+ export async function _getMorphoBlueMarketData(provider: Client, network: NetworkNumber, selectedMarket: MorphoBlueMarketData): Promise<MorphoBlueMarketInfo> {
23
+ const {
24
+ loanToken, collateralToken, oracle, irm, lltv, oracleType,
25
+ } = selectedMarket;
26
+
27
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
28
+ const loanTokenInfo = getAssetInfoByAddress(loanToken, network);
29
+ const collateralTokenInfo = getAssetInfoByAddress(collateralToken, network);
30
+
31
+ const loanTokenFeedAddress = getChainlinkAssetAddress(loanTokenInfo.symbol, network);
32
+
33
+ const morphoBlueViewContract = MorphoBlueViewContractViem(provider, network);
34
+
35
+ let marketInfo;
36
+ let loanTokenPrice;
37
+ const isTokenUSDA = loanTokenInfo.symbol === 'USDA';
38
+ const isMainnet = isMainnetNetwork(network);
39
+ if (isMainnet) {
40
+ const feedRegistryContract = FeedRegistryContractViem(provider, NetworkNumber.Eth);
41
+ const [_loanTokenPrice, _marketInfo] = await Promise.all([
42
+ isTokenUSDA ? Promise.resolve('100000000') : feedRegistryContract.read.latestAnswer([loanTokenFeedAddress, USD_QUOTE]),
43
+ morphoBlueViewContract.read.getMarketInfoNotTuple([loanToken, collateralToken, oracle, irm, BigInt(lltvInWei)]),
44
+ ]);
45
+ marketInfo = _marketInfo;
46
+ loanTokenPrice = _loanTokenPrice;
47
+ } else {
48
+ // Currently only base network is supported
49
+ const feedRegistryContract = DFSFeedRegistryContractViem(provider, network);
50
+
51
+ const [loanTokenPriceRound, _marketInfo] = await Promise.all([
52
+ isTokenUSDA ? Promise.resolve([0, '100000000']) // Normalize to match the expected object structure
53
+ : feedRegistryContract.read.latestRoundData([loanTokenFeedAddress, USD_QUOTE]),
54
+ morphoBlueViewContract.read.getMarketInfoNotTuple([loanToken, collateralToken, oracle, irm, BigInt(lltvInWei)]),
55
+ ]);
56
+ marketInfo = _marketInfo;
57
+ loanTokenPrice = loanTokenPriceRound[1].toString();
58
+ }
59
+
60
+ let morphoSupplyApy = '0';
61
+ let morphoBorrowApy = '0';
62
+ try {
63
+ const { supplyApy: _morphoSupplyApy, borrowApy: _morphoBorrowApy } = await getRewardsForMarket(selectedMarket.marketId, network);
64
+ morphoSupplyApy = _morphoSupplyApy;
65
+ morphoBorrowApy = _morphoBorrowApy;
66
+ } catch (e) {
67
+ console.error(e);
68
+ }
69
+
70
+ const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets.toString(), marketInfo.totalBorrowAssets.toString(), marketInfo.borrowRate.toString(), marketInfo.fee.toString());
71
+ const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate.toString(), marketInfo.totalBorrowShares.toString());
72
+ const utillization = new Dec(marketInfo.totalBorrowAssets.toString()).div(marketInfo.totalSupplyAssets.toString()).mul(100).toString();
73
+
74
+ const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
75
+ const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
76
+
77
+ const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
78
+
79
+ const oracleRate = new Dec(marketInfo.oracle.toString()).div(oracleScale).toString();
80
+ const assetsData: MorphoBlueAssetsData = {};
81
+ assetsData[wethToEth(loanTokenInfo.symbol)] = {
82
+ symbol: wethToEth(loanTokenInfo.symbol),
83
+ address: loanToken,
84
+ price: new Dec(loanTokenPrice).div(1e8).toString(),
85
+ supplyRate,
86
+ borrowRate: compoundedBorrowRate,
87
+ totalSupply: new Dec(marketInfo.totalSupplyAssets.toString()).div(scale).toString(),
88
+ totalBorrow: new Dec(marketInfo.totalBorrowAssets.toString()).div(scale).toString(),
89
+ canBeSupplied: true,
90
+ canBeBorrowed: true,
91
+ supplyIncentives: [{
92
+ token: 'MORPHO',
93
+ apy: morphoSupplyApy,
94
+ incentiveKind: IncentiveKind.Reward,
95
+ description: 'Eligible for protocol-level MORPHO incentives.',
96
+ }],
97
+ borrowIncentives: [{
98
+ token: 'MORPHO',
99
+ apy: morphoBorrowApy,
100
+ incentiveKind: IncentiveKind.Reward,
101
+ description: 'Eligible for protocol-level MORPHO incentives.',
102
+ }],
103
+ };
104
+
105
+ assetsData[wethToEth(collateralTokenInfo.symbol)] = {
106
+ symbol: wethToEth(collateralTokenInfo.symbol),
107
+ address: collateralToken,
108
+ price: new Dec(assetsData[wethToEth(loanTokenInfo.symbol)].price).mul(oracleRate).toString(),
109
+ supplyRate: '0',
110
+ borrowRate: '0',
111
+ canBeSupplied: true,
112
+ canBeBorrowed: false,
113
+ supplyIncentives: [],
114
+ borrowIncentives: [],
115
+ };
116
+ if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
117
+ assetsData[collateralTokenInfo.symbol].supplyIncentives = [{
118
+ apy: await getStakingApy(collateralTokenInfo.symbol),
119
+ token: collateralTokenInfo.symbol,
120
+ incentiveKind: IncentiveKind.Staking,
121
+ description: `Native ${collateralTokenInfo.symbol} yield.`,
122
+ }];
123
+ }
124
+
125
+ return {
126
+ id: marketInfo.id,
127
+ fee: new Dec(marketInfo.fee.toString()).div(WAD).toString(),
128
+ loanToken: wethToEth(loanTokenInfo.symbol),
129
+ collateralToken: wethToEth(collateralTokenInfo.symbol),
130
+ utillization,
131
+ oracle: oracleRate,
132
+ oracleType,
133
+ lltv: new Dec(lltv).toString(),
134
+ minRatio: new Dec(1).div(lltv).mul(100).toString(),
135
+ assetsData,
136
+ };
137
+ }
138
+
139
+ export async function getMorphoBlueMarketData(provider: EthereumProvider, network: NetworkNumber, selectedMarket: MorphoBlueMarketData): Promise<MorphoBlueMarketInfo> {
140
+ return _getMorphoBlueMarketData(getViemProvider(provider, network), network, selectedMarket);
141
+ }
142
+
143
+ export const _getMorphoBlueAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoBlueMarketData): Promise<PositionBalances> => {
144
+ let balances: PositionBalances = {
145
+ collateral: {},
146
+ debt: {},
147
+ };
148
+
149
+ if (!address) {
150
+ return balances;
151
+ }
152
+
153
+ const viewContract = MorphoBlueViewContractViem(provider, network, block);
154
+ const {
155
+ loanToken, collateralToken, oracle, irm, lltv,
156
+ } = selectedMarket;
157
+ const lltvInWei = BigInt(new Dec(lltv).mul(WAD).toString());
158
+ const marketObject = {
159
+ loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
160
+ };
161
+
162
+ const loanInfo = await viewContract.read.getUserInfo([marketObject, address], setViemBlockNumber(block));
163
+ const loanTokenInfo = getAssetInfoByAddress(selectedMarket.loanToken, network);
164
+ const collateralTokenInfo = getAssetInfoByAddress(selectedMarket.collateralToken, network);
165
+
166
+ balances = {
167
+ collateral: {
168
+ [addressMapping ? collateralTokenInfo.address.toLowerCase() : wethToEth(collateralTokenInfo.symbol)]: assetAmountInEth(loanInfo.collateral.toString(), collateralTokenInfo.symbol),
169
+ },
170
+ debt: {
171
+ [addressMapping ? loanTokenInfo.address.toLowerCase() : wethToEth(loanTokenInfo.symbol)]: assetAmountInEth(loanInfo.borrowedInAssets.toString(), loanTokenInfo.symbol),
172
+ },
173
+ };
174
+
175
+ return balances;
176
+ };
177
+
178
+ export const getMorphoBlueAccountBalances = async (
179
+ provider: EthereumProvider,
180
+ network: NetworkNumber,
181
+ block: Blockish,
182
+ addressMapping: boolean,
183
+ address: EthAddress,
184
+ selectedMarket: MorphoBlueMarketData,
185
+ ): Promise<PositionBalances> => _getMorphoBlueAccountBalances(getViemProvider(provider, network), network, block, addressMapping, address, selectedMarket);
186
+
187
+ export async function _getMorphoBlueAccountData(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
188
+ const {
189
+ loanToken, collateralToken, oracle, irm, lltv,
190
+ } = selectedMarket;
191
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
192
+ const viewContract = MorphoBlueViewContractViem(provider, network);
193
+ const loanInfo = (await viewContract.read.getUserInfo([
194
+ {
195
+ loanToken, collateralToken, oracle, irm, lltv: BigInt(lltvInWei),
196
+ },
197
+ account]));
198
+ const usedAssets: MMUsedAssets = {};
199
+
200
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
201
+ const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets.toString(), marketInfo.loanToken);
202
+ const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets.toString(), marketInfo.loanToken);
203
+ usedAssets[marketInfo.loanToken] = {
204
+ symbol: loanTokenInfo.symbol,
205
+ supplied: loanTokenSupplied,
206
+ borrowed: loanTokenBorrowed,
207
+ isSupplied: new Dec(loanInfo.suppliedInAssets.toString()).gt(0),
208
+ isBorrowed: new Dec(loanInfo.borrowedInAssets.toString()).gt(0),
209
+ collateral: false,
210
+ suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
211
+ borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
212
+ };
213
+
214
+ const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
215
+ const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral.toString(), marketInfo.collateralToken);
216
+ usedAssets[marketInfo.collateralToken] = {
217
+ symbol: collateralTokenInfo.symbol,
218
+ supplied: collateralTokenSupplied,
219
+ borrowed: '0',
220
+ isSupplied: new Dec(loanInfo.collateral.toString()).gt(0),
221
+ isBorrowed: false,
222
+ collateral: true,
223
+ suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
224
+ borrowedUsd: '0',
225
+ };
226
+
227
+ return {
228
+ supplyShares: loanInfo.supplyShares.toString(),
229
+ borrowShares: loanInfo.borrowShares.toString(),
230
+ usedAssets,
231
+ ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
232
+ };
233
+ }
234
+
235
+ export async function getMorphoBlueAccountData(provider: EthereumProvider, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
236
+ return _getMorphoBlueAccountData(getViemProvider(provider, network), network, account, selectedMarket, marketInfo);
237
+ }
238
+
239
+ export async function getMorphoEarn(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<{ apy: string, amount: string, amountUsd: string }> {
240
+ const {
241
+ loanToken, collateralToken, oracle, irm, lltv,
242
+ } = selectedMarket;
243
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
244
+
245
+ const viewContract = MorphoBlueViewContractViem(provider, network);
246
+ const loanInfo = (await viewContract.read.getUserInfo([
247
+ {
248
+ loanToken, collateralToken, oracle, irm, lltv: BigInt(lltvInWei),
249
+ },
250
+ account]));
251
+
252
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
253
+ const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets.toString(), marketInfo.loanToken);
254
+ const loanTokenSuppliedUsd = new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString();
255
+ const usedAssets: MMUsedAssets = {
256
+ [marketInfo.loanToken]: {
257
+ symbol: loanTokenInfo.symbol,
258
+ supplied: loanTokenSupplied,
259
+ borrowed: '0',
260
+ isSupplied: new Dec(loanInfo.suppliedInAssets.toString()).gt(0),
261
+ isBorrowed: false,
262
+ collateral: false,
263
+ suppliedUsd: loanTokenSuppliedUsd,
264
+ borrowedUsd: '0',
265
+ },
266
+ };
267
+
268
+ const { netApy } = calculateNetApy({ usedAssets, assetsData: marketInfo.assetsData as unknown as MMAssetsData });
269
+
270
+ return {
271
+ apy: netApy,
272
+ amount: loanTokenSupplied,
273
+ amountUsd: loanTokenSuppliedUsd,
274
+ };
275
275
  }