@usdu-finance/usdu-core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/.env.example +6 -0
  3. package/.prettierrc.json +7 -0
  4. package/LICENSE +674 -0
  5. package/README.md +244 -0
  6. package/contracts/curve/CurveAdapterV1.sol +310 -0
  7. package/contracts/curve/CurveAdapterV1_1.sol +198 -0
  8. package/contracts/curve/helpers/ICurveStableSwapNG.json +566 -0
  9. package/contracts/curve/helpers/ICurveStableSwapNG.sol +177 -0
  10. package/contracts/deploy/VaultDeployer.sol +110 -0
  11. package/contracts/morpho/MorphoAdapterV1.sol +255 -0
  12. package/contracts/morpho/MorphoAdapterV1_1.sol +137 -0
  13. package/contracts/morpho/MorphoAdapterV1_2.sol +126 -0
  14. package/contracts/morpho/helpers/AggregatorV3Interface.sol +24 -0
  15. package/contracts/morpho/helpers/ChainlinkDataFeedLib.sol +36 -0
  16. package/contracts/morpho/helpers/ConstantsLib.sol +20 -0
  17. package/contracts/morpho/helpers/ErrorsLib.sol +17 -0
  18. package/contracts/morpho/helpers/IERC4626.sol +6 -0
  19. package/contracts/morpho/helpers/IMetaMorphoV1_1.sol +229 -0
  20. package/contracts/morpho/helpers/IMetaMorphoV1_1Factory.sol +32 -0
  21. package/contracts/morpho/helpers/IMorpho.sol +361 -0
  22. package/contracts/morpho/helpers/IMorphoCallbacks.sol +52 -0
  23. package/contracts/morpho/helpers/IMorphoChainlinkOracleV2.sol +39 -0
  24. package/contracts/morpho/helpers/IMorphoChainlinkOracleV2Factory.sol +56 -0
  25. package/contracts/morpho/helpers/IOracle.sol +15 -0
  26. package/contracts/morpho/helpers/MarketParamsLib.sol +21 -0
  27. package/contracts/morpho/helpers/MathLib.sol +45 -0
  28. package/contracts/morpho/helpers/Morpho.sol.bak +517 -0
  29. package/contracts/morpho/helpers/MorphoChainlinkOracleV2.sol +157 -0
  30. package/contracts/morpho/helpers/PendingLib.sol +47 -0
  31. package/contracts/morpho/helpers/SharesMathLib.sol +45 -0
  32. package/contracts/morpho/helpers/VaultLib.sol +18 -0
  33. package/contracts/reward/RewardDistributionV1.sol +110 -0
  34. package/contracts/reward/RewardRouterV0.sol +63 -0
  35. package/contracts/reward/Rewards.example.json +72 -0
  36. package/contracts/stablecoin/IStablecoin.sol +211 -0
  37. package/contracts/stablecoin/IStablecoinMetadata.sol +27 -0
  38. package/contracts/stablecoin/IStablecoinModifier.sol +52 -0
  39. package/contracts/stablecoin/Stablecoin.sol +376 -0
  40. package/contracts/stablecoin/libraries/ConstantsLib.sol +13 -0
  41. package/contracts/stablecoin/libraries/ErrorsLib.sol +45 -0
  42. package/contracts/stablecoin/libraries/EventsLib.sol +74 -0
  43. package/contracts/stablecoin/libraries/PendingLib.sol +38 -0
  44. package/contracts/vault/VaultAdapterRecoverV1.sol +29 -0
  45. package/contracts/vault/VaultAdapterV1.sol +126 -0
  46. package/dist/index.d.mts +16154 -0
  47. package/dist/index.d.ts +16154 -0
  48. package/dist/index.js +21134 -0
  49. package/dist/index.mjs +21061 -0
  50. package/docs/CoreVault: Integration of new Markets.md +197 -0
  51. package/docs/CoreVault: SupplyQueue.md +11 -0
  52. package/docs/Markets USDC Vault.md +35 -0
  53. package/docs/Markets USDU Vault.md +89 -0
  54. package/docs/Markets WETH Vault.md +35 -0
  55. package/docs/Overview.drawio +117 -0
  56. package/exports/abis/curve/CurveAdapterV1.ts +599 -0
  57. package/exports/abis/curve/CurveAdapterV1_1.ts +609 -0
  58. package/exports/abis/curve/CurveAdapterV1_2.ts +721 -0
  59. package/exports/abis/curve/helper/ICurveStableSwapNG.ts +1589 -0
  60. package/exports/abis/morpho/MorphoAdapterV1.ts +516 -0
  61. package/exports/abis/morpho/MorphoAdapterV1_1.ts +489 -0
  62. package/exports/abis/morpho/MorphoAdapterV1_2.ts +459 -0
  63. package/exports/abis/morpho/helper/AggregatorV3Interface.ts +113 -0
  64. package/exports/abis/morpho/helper/IMetaMorphoV1_1.ts +1483 -0
  65. package/exports/abis/morpho/helper/IMetaMorphoV1_1Base.ts +607 -0
  66. package/exports/abis/morpho/helper/IMetaMorphoV1_1StaticTyping.ts +696 -0
  67. package/exports/abis/morpho/helper/IMorpho.ts +1024 -0
  68. package/exports/abis/morpho/helper/IMorphoBase.ts +886 -0
  69. package/exports/abis/morpho/helper/IMorphoChainlinkOracleV2.ts +132 -0
  70. package/exports/abis/morpho/helper/IMorphoChainlinkOracleV2Factory.ts +109 -0
  71. package/exports/abis/morpho/helper/IMorphoFlashLoanCallback.ts +20 -0
  72. package/exports/abis/morpho/helper/IMorphoLiquidateCallback.ts +20 -0
  73. package/exports/abis/morpho/helper/IMorphoRepayCallback.ts +20 -0
  74. package/exports/abis/morpho/helper/IMorphoStaticTyping.ts +1003 -0
  75. package/exports/abis/morpho/helper/IMorphoSupplyCallback.ts +20 -0
  76. package/exports/abis/morpho/helper/IMorphoSupplyCollateralCallback.ts +20 -0
  77. package/exports/abis/morpho/helper/IMulticall.ts +21 -0
  78. package/exports/abis/morpho/helper/IOracle.ts +15 -0
  79. package/exports/abis/morpho/helper/IOwnable.ts +55 -0
  80. package/exports/abis/morpho/helper/MorphoChainlinkOracleV2.ts +188 -0
  81. package/exports/abis/openzeppelin/ERC20.ts +310 -0
  82. package/exports/abis/openzeppelin/ERC20Permit.ts +520 -0
  83. package/exports/abis/openzeppelin/IERC20.ts +185 -0
  84. package/exports/abis/openzeppelin/IERC20Metadata.ts +224 -0
  85. package/exports/abis/openzeppelin/IERC20Permit.ts +77 -0
  86. package/exports/abis/openzeppelin/IERC4626.ts +614 -0
  87. package/exports/abis/reward/RewardDistributionV1.ts +246 -0
  88. package/exports/abis/stablecoin/ErrorsLib.ts +114 -0
  89. package/exports/abis/stablecoin/EventsLib.ts +372 -0
  90. package/exports/abis/stablecoin/IStablecoin.ts +642 -0
  91. package/exports/abis/stablecoin/IStablecoinMetadata.ts +856 -0
  92. package/exports/abis/stablecoin/IStablecoinModifier.ts +15 -0
  93. package/exports/abis/stablecoin/Stablecoin.ts +1922 -0
  94. package/exports/abis/termmax/ITermMaxVault.ts +2335 -0
  95. package/exports/abis/vault/VaultAdapterRecoverV1.ts +490 -0
  96. package/exports/abis/vault/VaultAdapterV1.ts +459 -0
  97. package/exports/address.config.ts +113 -0
  98. package/exports/address.types.ts +130 -0
  99. package/exports/index.ts +61 -0
  100. package/hardhat.config.ts +231 -0
  101. package/helper/store.args.ts +17 -0
  102. package/helper/wallet.info.ts +3 -0
  103. package/helper/wallet.ts +41 -0
  104. package/install-macos.sh +46 -0
  105. package/package.json +73 -0
  106. package/tsconfig.json +15 -0
  107. package/tsup.config.ts +10 -0
@@ -0,0 +1,361 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ type Id is bytes32;
5
+
6
+ struct MarketParams {
7
+ address loanToken;
8
+ address collateralToken;
9
+ address oracle;
10
+ address irm;
11
+ uint256 lltv;
12
+ }
13
+
14
+ /// @dev Warning: For `feeRecipient`, `supplyShares` does not contain the accrued shares since the last interest
15
+ /// accrual.
16
+ struct Position {
17
+ uint256 supplyShares;
18
+ uint128 borrowShares;
19
+ uint128 collateral;
20
+ }
21
+
22
+ /// @dev Warning: `totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
23
+ /// @dev Warning: `totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
24
+ /// @dev Warning: `totalSupplyShares` does not contain the additional shares accrued by `feeRecipient` since the last
25
+ /// interest accrual.
26
+ struct Market {
27
+ uint128 totalSupplyAssets;
28
+ uint128 totalSupplyShares;
29
+ uint128 totalBorrowAssets;
30
+ uint128 totalBorrowShares;
31
+ uint128 lastUpdate;
32
+ uint128 fee;
33
+ }
34
+
35
+ struct Authorization {
36
+ address authorizer;
37
+ address authorized;
38
+ bool isAuthorized;
39
+ uint256 nonce;
40
+ uint256 deadline;
41
+ }
42
+
43
+ struct Signature {
44
+ uint8 v;
45
+ bytes32 r;
46
+ bytes32 s;
47
+ }
48
+
49
+ /// @dev This interface is used for factorizing IMorphoStaticTyping and IMorpho.
50
+ /// @dev Consider using the IMorpho interface instead of this one.
51
+ interface IMorphoBase {
52
+ /// @notice The EIP-712 domain separator.
53
+ /// @dev Warning: Every EIP-712 signed message based on this domain separator can be reused on chains sharing the
54
+ /// same chain id and on forks because the domain separator would be the same.
55
+ function DOMAIN_SEPARATOR() external view returns (bytes32);
56
+
57
+ /// @notice The owner of the contract.
58
+ /// @dev It has the power to change the owner.
59
+ /// @dev It has the power to set fees on markets and set the fee recipient.
60
+ /// @dev It has the power to enable but not disable IRMs and LLTVs.
61
+ function owner() external view returns (address);
62
+
63
+ /// @notice The fee recipient of all markets.
64
+ /// @dev The recipient receives the fees of a given market through a supply position on that market.
65
+ function feeRecipient() external view returns (address);
66
+
67
+ /// @notice Whether the `irm` is enabled.
68
+ function isIrmEnabled(address irm) external view returns (bool);
69
+
70
+ /// @notice Whether the `lltv` is enabled.
71
+ function isLltvEnabled(uint256 lltv) external view returns (bool);
72
+
73
+ /// @notice Whether `authorized` is authorized to modify `authorizer`'s position on all markets.
74
+ /// @dev Anyone is authorized to modify their own positions, regardless of this variable.
75
+ function isAuthorized(address authorizer, address authorized) external view returns (bool);
76
+
77
+ /// @notice The `authorizer`'s current nonce. Used to prevent replay attacks with EIP-712 signatures.
78
+ function nonce(address authorizer) external view returns (uint256);
79
+
80
+ /// @notice Sets `newOwner` as `owner` of the contract.
81
+ /// @dev Warning: No two-step transfer ownership.
82
+ /// @dev Warning: The owner can be set to the zero address.
83
+ function setOwner(address newOwner) external;
84
+
85
+ /// @notice Enables `irm` as a possible IRM for market creation.
86
+ /// @dev Warning: It is not possible to disable an IRM.
87
+ function enableIrm(address irm) external;
88
+
89
+ /// @notice Enables `lltv` as a possible LLTV for market creation.
90
+ /// @dev Warning: It is not possible to disable a LLTV.
91
+ function enableLltv(uint256 lltv) external;
92
+
93
+ /// @notice Sets the `newFee` for the given market `marketParams`.
94
+ /// @param newFee The new fee, scaled by WAD.
95
+ /// @dev Warning: The recipient can be the zero address.
96
+ function setFee(MarketParams memory marketParams, uint256 newFee) external;
97
+
98
+ /// @notice Sets `newFeeRecipient` as `feeRecipient` of the fee.
99
+ /// @dev Warning: If the fee recipient is set to the zero address, fees will accrue there and will be lost.
100
+ /// @dev Modifying the fee recipient will allow the new recipient to claim any pending fees not yet accrued. To
101
+ /// ensure that the current recipient receives all due fees, accrue interest manually prior to making any changes.
102
+ function setFeeRecipient(address newFeeRecipient) external;
103
+
104
+ /// @notice Creates the market `marketParams`.
105
+ /// @dev Here is the list of assumptions on the market's dependencies (tokens, IRM and oracle) that guarantees
106
+ /// Morpho behaves as expected:
107
+ /// - The token should be ERC-20 compliant, except that it can omit return values on `transfer` and `transferFrom`.
108
+ /// - The token balance of Morpho should only decrease on `transfer` and `transferFrom`. In particular, tokens with
109
+ /// burn functions are not supported.
110
+ /// - The token should not re-enter Morpho on `transfer` nor `transferFrom`.
111
+ /// - The token balance of the sender (resp. receiver) should decrease (resp. increase) by exactly the given amount
112
+ /// on `transfer` and `transferFrom`. In particular, tokens with fees on transfer are not supported.
113
+ /// - The IRM should not re-enter Morpho.
114
+ /// - The oracle should return a price with the correct scaling.
115
+ /// @dev Here is a list of assumptions on the market's dependencies which, if broken, could break Morpho's liveness
116
+ /// properties (funds could get stuck):
117
+ /// - The token should not revert on `transfer` and `transferFrom` if balances and approvals are right.
118
+ /// - The amount of assets supplied and borrowed should not go above ~1e35 (otherwise the computation of
119
+ /// `toSharesUp` and `toSharesDown` can overflow).
120
+ /// - The IRM should not revert on `borrowRate`.
121
+ /// - The IRM should not return a very high borrow rate (otherwise the computation of `interest` in
122
+ /// `_accrueInterest` can overflow).
123
+ /// - The oracle should not revert `price`.
124
+ /// - The oracle should not return a very high price (otherwise the computation of `maxBorrow` in `_isHealthy` or of
125
+ /// `assetsRepaid` in `liquidate` can overflow).
126
+ /// @dev The borrow share price of a market with less than 1e4 assets borrowed can be decreased by manipulations, to
127
+ /// the point where `totalBorrowShares` is very large and borrowing overflows.
128
+ function createMarket(MarketParams memory marketParams) external;
129
+
130
+ /// @notice Supplies `assets` or `shares` on behalf of `onBehalf`, optionally calling back the caller's
131
+ /// `onMorphoSupply` function with the given `data`.
132
+ /// @dev Either `assets` or `shares` should be zero. Most use cases should rely on `assets` as an input so the
133
+ /// caller is guaranteed to have `assets` tokens pulled from their balance, but the possibility to mint a specific
134
+ /// amount of shares is given for full compatibility and precision.
135
+ /// @dev Supplying a large amount can revert for overflow.
136
+ /// @dev Supplying an amount of shares may lead to supply more or fewer assets than expected due to slippage.
137
+ /// Consider using the `assets` parameter to avoid this.
138
+ /// @param marketParams The market to supply assets to.
139
+ /// @param assets The amount of assets to supply.
140
+ /// @param shares The amount of shares to mint.
141
+ /// @param onBehalf The address that will own the increased supply position.
142
+ /// @param data Arbitrary data to pass to the `onMorphoSupply` callback. Pass empty data if not needed.
143
+ /// @return assetsSupplied The amount of assets supplied.
144
+ /// @return sharesSupplied The amount of shares minted.
145
+ function supply(
146
+ MarketParams memory marketParams,
147
+ uint256 assets,
148
+ uint256 shares,
149
+ address onBehalf,
150
+ bytes memory data
151
+ ) external returns (uint256 assetsSupplied, uint256 sharesSupplied);
152
+
153
+ /// @notice Withdraws `assets` or `shares` on behalf of `onBehalf` and sends the assets to `receiver`.
154
+ /// @dev Either `assets` or `shares` should be zero. To withdraw max, pass the `shares`'s balance of `onBehalf`.
155
+ /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
156
+ /// @dev Withdrawing an amount corresponding to more shares than supplied will revert for underflow.
157
+ /// @dev It is advised to use the `shares` input when withdrawing the full position to avoid reverts due to
158
+ /// conversion roundings between shares and assets.
159
+ /// @param marketParams The market to withdraw assets from.
160
+ /// @param assets The amount of assets to withdraw.
161
+ /// @param shares The amount of shares to burn.
162
+ /// @param onBehalf The address of the owner of the supply position.
163
+ /// @param receiver The address that will receive the withdrawn assets.
164
+ /// @return assetsWithdrawn The amount of assets withdrawn.
165
+ /// @return sharesWithdrawn The amount of shares burned.
166
+ function withdraw(
167
+ MarketParams memory marketParams,
168
+ uint256 assets,
169
+ uint256 shares,
170
+ address onBehalf,
171
+ address receiver
172
+ ) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn);
173
+
174
+ /// @notice Borrows `assets` or `shares` on behalf of `onBehalf` and sends the assets to `receiver`.
175
+ /// @dev Either `assets` or `shares` should be zero. Most use cases should rely on `assets` as an input so the
176
+ /// caller is guaranteed to borrow `assets` of tokens, but the possibility to mint a specific amount of shares is
177
+ /// given for full compatibility and precision.
178
+ /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
179
+ /// @dev Borrowing a large amount can revert for overflow.
180
+ /// @dev Borrowing an amount of shares may lead to borrow fewer assets than expected due to slippage.
181
+ /// Consider using the `assets` parameter to avoid this.
182
+ /// @param marketParams The market to borrow assets from.
183
+ /// @param assets The amount of assets to borrow.
184
+ /// @param shares The amount of shares to mint.
185
+ /// @param onBehalf The address that will own the increased borrow position.
186
+ /// @param receiver The address that will receive the borrowed assets.
187
+ /// @return assetsBorrowed The amount of assets borrowed.
188
+ /// @return sharesBorrowed The amount of shares minted.
189
+ function borrow(
190
+ MarketParams memory marketParams,
191
+ uint256 assets,
192
+ uint256 shares,
193
+ address onBehalf,
194
+ address receiver
195
+ ) external returns (uint256 assetsBorrowed, uint256 sharesBorrowed);
196
+
197
+ /// @notice Repays `assets` or `shares` on behalf of `onBehalf`, optionally calling back the caller's
198
+ /// `onMorphoRepay` function with the given `data`.
199
+ /// @dev Either `assets` or `shares` should be zero. To repay max, pass the `shares`'s balance of `onBehalf`.
200
+ /// @dev Repaying an amount corresponding to more shares than borrowed will revert for underflow.
201
+ /// @dev It is advised to use the `shares` input when repaying the full position to avoid reverts due to conversion
202
+ /// roundings between shares and assets.
203
+ /// @dev An attacker can front-run a repay with a small repay making the transaction revert for underflow.
204
+ /// @param marketParams The market to repay assets to.
205
+ /// @param assets The amount of assets to repay.
206
+ /// @param shares The amount of shares to burn.
207
+ /// @param onBehalf The address of the owner of the debt position.
208
+ /// @param data Arbitrary data to pass to the `onMorphoRepay` callback. Pass empty data if not needed.
209
+ /// @return assetsRepaid The amount of assets repaid.
210
+ /// @return sharesRepaid The amount of shares burned.
211
+ function repay(
212
+ MarketParams memory marketParams,
213
+ uint256 assets,
214
+ uint256 shares,
215
+ address onBehalf,
216
+ bytes memory data
217
+ ) external returns (uint256 assetsRepaid, uint256 sharesRepaid);
218
+
219
+ /// @notice Supplies `assets` of collateral on behalf of `onBehalf`, optionally calling back the caller's
220
+ /// `onMorphoSupplyCollateral` function with the given `data`.
221
+ /// @dev Interest are not accrued since it's not required and it saves gas.
222
+ /// @dev Supplying a large amount can revert for overflow.
223
+ /// @param marketParams The market to supply collateral to.
224
+ /// @param assets The amount of collateral to supply.
225
+ /// @param onBehalf The address that will own the increased collateral position.
226
+ /// @param data Arbitrary data to pass to the `onMorphoSupplyCollateral` callback. Pass empty data if not needed.
227
+ function supplyCollateral(
228
+ MarketParams memory marketParams,
229
+ uint256 assets,
230
+ address onBehalf,
231
+ bytes memory data
232
+ ) external;
233
+
234
+ /// @notice Withdraws `assets` of collateral on behalf of `onBehalf` and sends the assets to `receiver`.
235
+ /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
236
+ /// @dev Withdrawing an amount corresponding to more collateral than supplied will revert for underflow.
237
+ /// @param marketParams The market to withdraw collateral from.
238
+ /// @param assets The amount of collateral to withdraw.
239
+ /// @param onBehalf The address of the owner of the collateral position.
240
+ /// @param receiver The address that will receive the collateral assets.
241
+ function withdrawCollateral(
242
+ MarketParams memory marketParams,
243
+ uint256 assets,
244
+ address onBehalf,
245
+ address receiver
246
+ ) external;
247
+
248
+ /// @notice Liquidates the given `repaidShares` of debt asset or seize the given `seizedAssets` of collateral on the
249
+ /// given market `marketParams` of the given `borrower`'s position, optionally calling back the caller's
250
+ /// `onMorphoLiquidate` function with the given `data`.
251
+ /// @dev Either `seizedAssets` or `repaidShares` should be zero.
252
+ /// @dev Seizing more than the collateral balance will underflow and revert without any error message.
253
+ /// @dev Repaying more than the borrow balance will underflow and revert without any error message.
254
+ /// @dev An attacker can front-run a liquidation with a small repay making the transaction revert for underflow.
255
+ /// @param marketParams The market of the position.
256
+ /// @param borrower The owner of the position.
257
+ /// @param seizedAssets The amount of collateral to seize.
258
+ /// @param repaidShares The amount of shares to repay.
259
+ /// @param data Arbitrary data to pass to the `onMorphoLiquidate` callback. Pass empty data if not needed.
260
+ /// @return The amount of assets seized.
261
+ /// @return The amount of assets repaid.
262
+ function liquidate(
263
+ MarketParams memory marketParams,
264
+ address borrower,
265
+ uint256 seizedAssets,
266
+ uint256 repaidShares,
267
+ bytes memory data
268
+ ) external returns (uint256, uint256);
269
+
270
+ /// @notice Executes a flash loan.
271
+ /// @dev Flash loans have access to the whole balance of the contract (the liquidity and deposited collateral of all
272
+ /// markets combined, plus donations).
273
+ /// @dev Warning: Not ERC-3156 compliant but compatibility is easily reached:
274
+ /// - `flashFee` is zero.
275
+ /// - `maxFlashLoan` is the token's balance of this contract.
276
+ /// - The receiver of `assets` is the caller.
277
+ /// @param token The token to flash loan.
278
+ /// @param assets The amount of assets to flash loan.
279
+ /// @param data Arbitrary data to pass to the `onMorphoFlashLoan` callback.
280
+ function flashLoan(address token, uint256 assets, bytes calldata data) external;
281
+
282
+ /// @notice Sets the authorization for `authorized` to manage `msg.sender`'s positions.
283
+ /// @param authorized The authorized address.
284
+ /// @param newIsAuthorized The new authorization status.
285
+ function setAuthorization(address authorized, bool newIsAuthorized) external;
286
+
287
+ /// @notice Sets the authorization for `authorization.authorized` to manage `authorization.authorizer`'s positions.
288
+ /// @dev Warning: Reverts if the signature has already been submitted.
289
+ /// @dev The signature is malleable, but it has no impact on the security here.
290
+ /// @dev The nonce is passed as argument to be able to revert with a different error message.
291
+ /// @param authorization The `Authorization` struct.
292
+ /// @param signature The signature.
293
+ function setAuthorizationWithSig(Authorization calldata authorization, Signature calldata signature) external;
294
+
295
+ /// @notice Accrues interest for the given market `marketParams`.
296
+ function accrueInterest(MarketParams memory marketParams) external;
297
+
298
+ /// @notice Returns the data stored on the different `slots`.
299
+ function extSloads(bytes32[] memory slots) external view returns (bytes32[] memory);
300
+ }
301
+
302
+ /// @dev This interface is inherited by Morpho so that function signatures are checked by the compiler.
303
+ /// @dev Consider using the IMorpho interface instead of this one.
304
+ interface IMorphoStaticTyping is IMorphoBase {
305
+ /// @notice The state of the position of `user` on the market corresponding to `id`.
306
+ /// @dev Warning: For `feeRecipient`, `supplyShares` does not contain the accrued shares since the last interest
307
+ /// accrual.
308
+ function position(
309
+ Id id,
310
+ address user
311
+ ) external view returns (uint256 supplyShares, uint128 borrowShares, uint128 collateral);
312
+
313
+ /// @notice The state of the market corresponding to `id`.
314
+ /// @dev Warning: `totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
315
+ /// @dev Warning: `totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
316
+ /// @dev Warning: `totalSupplyShares` does not contain the accrued shares by `feeRecipient` since the last interest
317
+ /// accrual.
318
+ function market(
319
+ Id id
320
+ )
321
+ external
322
+ view
323
+ returns (
324
+ uint128 totalSupplyAssets,
325
+ uint128 totalSupplyShares,
326
+ uint128 totalBorrowAssets,
327
+ uint128 totalBorrowShares,
328
+ uint128 lastUpdate,
329
+ uint128 fee
330
+ );
331
+
332
+ /// @notice The market params corresponding to `id`.
333
+ /// @dev This mapping is not used in Morpho. It is there to enable reducing the cost associated to calldata on layer
334
+ /// 2s by creating a wrapper contract with functions that take `id` as input instead of `marketParams`.
335
+ function idToMarketParams(
336
+ Id id
337
+ ) external view returns (address loanToken, address collateralToken, address oracle, address irm, uint256 lltv);
338
+ }
339
+
340
+ /// @title IMorpho
341
+ /// @author Morpho Labs
342
+ /// @custom:contact security@morpho.org
343
+ /// @dev Use this interface for Morpho to have access to all the functions with the appropriate function signatures.
344
+ interface IMorpho is IMorphoBase {
345
+ /// @notice The state of the position of `user` on the market corresponding to `id`.
346
+ /// @dev Warning: For `feeRecipient`, `p.supplyShares` does not contain the accrued shares since the last interest
347
+ /// accrual.
348
+ function position(Id id, address user) external view returns (Position memory p);
349
+
350
+ /// @notice The state of the market corresponding to `id`.
351
+ /// @dev Warning: `m.totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
352
+ /// @dev Warning: `m.totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
353
+ /// @dev Warning: `m.totalSupplyShares` does not contain the accrued shares by `feeRecipient` since the last
354
+ /// interest accrual.
355
+ function market(Id id) external view returns (Market memory m);
356
+
357
+ /// @notice The market params corresponding to `id`.
358
+ /// @dev This mapping is not used in Morpho. It is there to enable reducing the cost associated to calldata on layer
359
+ /// 2s by creating a wrapper contract with functions that take `id` as input instead of `marketParams`.
360
+ function idToMarketParams(Id id) external view returns (MarketParams memory);
361
+ }
@@ -0,0 +1,52 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ /// @title IMorphoLiquidateCallback
5
+ /// @notice Interface that liquidators willing to use `liquidate`'s callback must implement.
6
+ interface IMorphoLiquidateCallback {
7
+ /// @notice Callback called when a liquidation occurs.
8
+ /// @dev The callback is called only if data is not empty.
9
+ /// @param repaidAssets The amount of repaid assets.
10
+ /// @param data Arbitrary data passed to the `liquidate` function.
11
+ function onMorphoLiquidate(uint256 repaidAssets, bytes calldata data) external;
12
+ }
13
+
14
+ /// @title IMorphoRepayCallback
15
+ /// @notice Interface that users willing to use `repay`'s callback must implement.
16
+ interface IMorphoRepayCallback {
17
+ /// @notice Callback called when a repayment occurs.
18
+ /// @dev The callback is called only if data is not empty.
19
+ /// @param assets The amount of repaid assets.
20
+ /// @param data Arbitrary data passed to the `repay` function.
21
+ function onMorphoRepay(uint256 assets, bytes calldata data) external;
22
+ }
23
+
24
+ /// @title IMorphoSupplyCallback
25
+ /// @notice Interface that users willing to use `supply`'s callback must implement.
26
+ interface IMorphoSupplyCallback {
27
+ /// @notice Callback called when a supply occurs.
28
+ /// @dev The callback is called only if data is not empty.
29
+ /// @param assets The amount of supplied assets.
30
+ /// @param data Arbitrary data passed to the `supply` function.
31
+ function onMorphoSupply(uint256 assets, bytes calldata data) external;
32
+ }
33
+
34
+ /// @title IMorphoSupplyCollateralCallback
35
+ /// @notice Interface that users willing to use `supplyCollateral`'s callback must implement.
36
+ interface IMorphoSupplyCollateralCallback {
37
+ /// @notice Callback called when a supply of collateral occurs.
38
+ /// @dev The callback is called only if data is not empty.
39
+ /// @param assets The amount of supplied collateral.
40
+ /// @param data Arbitrary data passed to the `supplyCollateral` function.
41
+ function onMorphoSupplyCollateral(uint256 assets, bytes calldata data) external;
42
+ }
43
+
44
+ /// @title IMorphoFlashLoanCallback
45
+ /// @notice Interface that users willing to use `flashLoan`'s callback must implement.
46
+ interface IMorphoFlashLoanCallback {
47
+ /// @notice Callback called when a flash loan occurs.
48
+ /// @dev The callback is called only if data is not empty.
49
+ /// @param assets The amount of assets that was flash loaned.
50
+ /// @param data Arbitrary data passed to the `flashLoan` function.
51
+ function onMorphoFlashLoan(uint256 assets, bytes calldata data) external;
52
+ }
@@ -0,0 +1,39 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ import {IERC4626} from './IERC4626.sol';
5
+ import {IOracle} from './IOracle.sol';
6
+ import {AggregatorV3Interface} from './AggregatorV3Interface.sol';
7
+
8
+ /// @title IMorphoChainlinkOracleV2
9
+ /// @author Morpho Labs
10
+ /// @custom:contact security@morpho.org
11
+ /// @notice Interface of MorphoChainlinkOracleV2.
12
+ interface IMorphoChainlinkOracleV2 is IOracle {
13
+ /// @notice Returns the address of the base ERC4626 vault.
14
+ function BASE_VAULT() external view returns (IERC4626);
15
+
16
+ /// @notice Returns the base vault conversion sample.
17
+ function BASE_VAULT_CONVERSION_SAMPLE() external view returns (uint256);
18
+
19
+ /// @notice Returns the address of the quote ERC4626 vault.
20
+ function QUOTE_VAULT() external view returns (IERC4626);
21
+
22
+ /// @notice Returns the quote vault conversion sample.
23
+ function QUOTE_VAULT_CONVERSION_SAMPLE() external view returns (uint256);
24
+
25
+ /// @notice Returns the address of the first base feed.
26
+ function BASE_FEED_1() external view returns (AggregatorV3Interface);
27
+
28
+ /// @notice Returns the address of the second base feed.
29
+ function BASE_FEED_2() external view returns (AggregatorV3Interface);
30
+
31
+ /// @notice Returns the address of the first quote feed.
32
+ function QUOTE_FEED_1() external view returns (AggregatorV3Interface);
33
+
34
+ /// @notice Returns the address of the second quote feed.
35
+ function QUOTE_FEED_2() external view returns (AggregatorV3Interface);
36
+
37
+ /// @notice Returns the price scale factor, calculated at contract creation.
38
+ function SCALE_FACTOR() external view returns (uint256);
39
+ }
@@ -0,0 +1,56 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ import {MorphoChainlinkOracleV2} from './MorphoChainlinkOracleV2.sol';
5
+ import {IERC4626} from './VaultLib.sol';
6
+ import {AggregatorV3Interface} from './ChainlinkDataFeedLib.sol';
7
+
8
+ /// @title IMorphoChainlinkOracleV2Factory
9
+ /// @author Morpho Labs
10
+ /// @custom:contact security@morpho.org
11
+ /// @notice Interface for MorphoChainlinkOracleV2Factory
12
+ interface IMorphoChainlinkOracleV2Factory {
13
+ /// @notice Emitted when a new Chainlink oracle is created.
14
+ /// @param oracle The address of the Chainlink oracle.
15
+ /// @param caller The caller of the function.
16
+ event CreateMorphoChainlinkOracleV2(address caller, address oracle);
17
+
18
+ /// @notice Whether a Chainlink oracle vault was created with the factory.
19
+ function isMorphoChainlinkOracleV2(address target) external view returns (bool);
20
+
21
+ /// @dev Here is the list of assumptions that guarantees the oracle behaves as expected:
22
+ /// - The vaults, if set, are ERC4626-compliant.
23
+ /// - The feeds, if set, are Chainlink-interface-compliant.
24
+ /// - Decimals passed as argument are correct.
25
+ /// - The base vaults's sample shares quoted as assets and the base feed prices don't overflow when multiplied.
26
+ /// - The quote vault's sample shares quoted as assets and the quote feed prices don't overflow when multiplied.
27
+ /// @param baseVault Base vault. Pass address zero to omit this parameter.
28
+ /// @param baseVaultConversionSample The sample amount of base vault shares used to convert to underlying.
29
+ /// Pass 1 if the base asset is not a vault. Should be chosen such that converting `baseVaultConversionSample` to
30
+ /// assets has enough precision.
31
+ /// @param baseFeed1 First base feed. Pass address zero if the price = 1.
32
+ /// @param baseFeed2 Second base feed. Pass address zero if the price = 1.
33
+ /// @param baseTokenDecimals Base token decimals.
34
+ /// @param quoteVault Quote vault. Pass address zero to omit this parameter.
35
+ /// @param quoteVaultConversionSample The sample amount of quote vault shares used to convert to underlying.
36
+ /// Pass 1 if the quote asset is not a vault. Should be chosen such that converting `quoteVaultConversionSample` to
37
+ /// assets has enough precision.
38
+ /// @param quoteFeed1 First quote feed. Pass address zero if the price = 1.
39
+ /// @param quoteFeed2 Second quote feed. Pass address zero if the price = 1.
40
+ /// @param quoteTokenDecimals Quote token decimals.
41
+ /// @param salt The salt to use for the CREATE2.
42
+ /// @dev The base asset should be the collateral token and the quote asset the loan token.
43
+ function createMorphoChainlinkOracleV2(
44
+ IERC4626 baseVault,
45
+ uint256 baseVaultConversionSample,
46
+ AggregatorV3Interface baseFeed1,
47
+ AggregatorV3Interface baseFeed2,
48
+ uint256 baseTokenDecimals,
49
+ IERC4626 quoteVault,
50
+ uint256 quoteVaultConversionSample,
51
+ AggregatorV3Interface quoteFeed1,
52
+ AggregatorV3Interface quoteFeed2,
53
+ uint256 quoteTokenDecimals,
54
+ bytes32 salt
55
+ ) external returns (MorphoChainlinkOracleV2 oracle);
56
+ }
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ /// @title IOracle
5
+ /// @author Morpho Labs
6
+ /// @custom:contact security@morpho.org
7
+ /// @notice Interface that oracles used by Morpho must implement.
8
+ /// @dev It is the user's responsibility to select markets with safe oracles.
9
+ interface IOracle {
10
+ /// @notice Returns the price of 1 asset of collateral token quoted in 1 asset of loan token, scaled by 1e36.
11
+ /// @dev It corresponds to the price of 10**(collateral token decimals) assets of collateral token quoted in
12
+ /// 10**(loan token decimals) assets of loan token with `36 + loan token decimals - collateral token decimals`
13
+ /// decimals of precision.
14
+ function price() external view returns (uint256);
15
+ }
@@ -0,0 +1,21 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {Id, MarketParams} from './IMorpho.sol';
5
+
6
+ /// @title MarketParamsLib
7
+ /// @author Morpho Labs
8
+ /// @custom:contact security@morpho.org
9
+ /// @notice Library to convert a market to its id.
10
+ library MarketParamsLib {
11
+ /// @notice The length of the data used to compute the id of a market.
12
+ /// @dev The length is 5 * 32 because `MarketParams` has 5 variables of 32 bytes each.
13
+ uint256 internal constant MARKET_PARAMS_BYTES_LENGTH = 5 * 32;
14
+
15
+ /// @notice Returns the id of the market `marketParams`.
16
+ function id(MarketParams memory marketParams) internal pure returns (Id marketParamsId) {
17
+ assembly ('memory-safe') {
18
+ marketParamsId := keccak256(marketParams, MARKET_PARAMS_BYTES_LENGTH)
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,45 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity ^0.8.0;
3
+
4
+ uint256 constant WAD = 1e18;
5
+
6
+ /// @title MathLib
7
+ /// @author Morpho Labs
8
+ /// @custom:contact security@morpho.org
9
+ /// @notice Library to manage fixed-point arithmetic.
10
+ library MathLib {
11
+ /// @dev Returns (`x` * `y`) / `WAD` rounded down.
12
+ function wMulDown(uint256 x, uint256 y) internal pure returns (uint256) {
13
+ return mulDivDown(x, y, WAD);
14
+ }
15
+
16
+ /// @dev Returns (`x` * `WAD`) / `y` rounded down.
17
+ function wDivDown(uint256 x, uint256 y) internal pure returns (uint256) {
18
+ return mulDivDown(x, WAD, y);
19
+ }
20
+
21
+ /// @dev Returns (`x` * `WAD`) / `y` rounded up.
22
+ function wDivUp(uint256 x, uint256 y) internal pure returns (uint256) {
23
+ return mulDivUp(x, WAD, y);
24
+ }
25
+
26
+ /// @dev Returns (`x` * `y`) / `d` rounded down.
27
+ function mulDivDown(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
28
+ return (x * y) / d;
29
+ }
30
+
31
+ /// @dev Returns (`x` * `y`) / `d` rounded up.
32
+ function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
33
+ return (x * y + (d - 1)) / d;
34
+ }
35
+
36
+ /// @dev Returns the sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1, to approximate a
37
+ /// continuous compound interest rate.
38
+ function wTaylorCompounded(uint256 x, uint256 n) internal pure returns (uint256) {
39
+ uint256 firstTerm = x * n;
40
+ uint256 secondTerm = mulDivDown(firstTerm, firstTerm, 2 * WAD);
41
+ uint256 thirdTerm = mulDivDown(secondTerm, firstTerm, 3 * WAD);
42
+
43
+ return firstTerm + secondTerm + thirdTerm;
44
+ }
45
+ }