-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snippets): mm tests improvement
- Loading branch information
Showing
8 changed files
with
366 additions
and
90 deletions.
There are no files selected for viewing
Submodule metamorpho
updated
28 files
+3 −3 | .github/workflows/foundry.yml | |
+53 −22 | README.md | |
+1 −1 | lib/morpho-blue | |
+215 −189 | src/MetaMorpho.sol | |
+41 −36 | src/interfaces/IMetaMorpho.sol | |
+24 −6 | src/libraries/ErrorsLib.sol | |
+14 −13 | src/libraries/EventsLib.sol | |
+55 −0 | src/libraries/PendingLib.sol | |
+2 −0 | src/mocks/ERC20Mock.sol | |
+4 −1 | test/forge/ERC4626ComplianceTest.sol | |
+12 −15 | test/forge/ERC4626Test.sol | |
+33 −6 | test/forge/FeeTest.sol | |
+69 −17 | test/forge/GuardianTest.sol | |
+63 −90 | test/forge/MarketTest.sol | |
+1 −1 | test/forge/MetaMorphoFactoryTest.sol | |
+1 −4 | test/forge/MetaMorphoInternalTest.sol | |
+2 −2 | test/forge/MulticallTest.sol | |
+38 −21 | test/forge/PermitTest.sol | |
+15 −16 | test/forge/ReallocateIdleTest.sol | |
+74 −109 | test/forge/ReallocateWithdrawTest.sol | |
+140 −0 | test/forge/RevokeTest.sol | |
+6 −8 | test/forge/RoleTest.sol | |
+203 −115 | test/forge/TimelockTest.sol | |
+27 −29 | test/forge/UrdTest.sol | |
+18 −2 | test/forge/helpers/BaseTest.sol | |
+61 −26 | test/forge/helpers/IntegrationTest.sol | |
+49 −28 | test/hardhat/MetaMorpho.spec.ts | |
+78 −75 | test/metamorpho_tests.tree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
pragma solidity ^0.8.0; | ||
|
||
import {SwapMock} from "@snippets/blue/mocks/SwapMock.sol"; | ||
import { | ||
IMorphoSupplyCollateralCallback, | ||
IMorphoLiquidateCallback, | ||
IMorphoRepayCallback | ||
} from "@morpho-blue/interfaces/IMorphoCallbacks.sol"; | ||
|
||
import {Id, IMorpho, MarketParams, Market} from "@morpho-blue/interfaces/IMorpho.sol"; | ||
import {SafeTransferLib, ERC20} from "@solmate/utils/SafeTransferLib.sol"; | ||
import {MathLib} from "@morpho-blue/libraries/MathLib.sol"; | ||
import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol"; | ||
import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol"; | ||
import {MarketParamsLib} from "@morpho-blue/libraries/MarketParamsLib.sol"; | ||
|
||
/* | ||
The following implementation regarding the swap mocked has been done for educationnal purpose | ||
the swap mock is giving back, thanks to the orace of the market, the exact value in terms of amount between a | ||
collateral and a loan token | ||
One should be aware that has to be taken into account on potential swap: | ||
1. slippage | ||
2. fees | ||
add a definition of what snippets are | ||
*/ | ||
contract CallbacksSnippets is IMorphoSupplyCollateralCallback, IMorphoRepayCallback, IMorphoLiquidateCallback { | ||
using MathLib for uint256; | ||
using MorphoLib for IMorpho; | ||
using MarketParamsLib for MarketParams; | ||
|
||
IMorpho public immutable morpho; | ||
SwapMock swapMock; | ||
|
||
constructor(address morphoAddress) { | ||
morpho = IMorpho(morphoAddress); | ||
} | ||
|
||
/* | ||
Callbacks | ||
remember that at a given market, one can leverage itself up to 1/1-LLTV, | ||
leverageFactor so for an LLTV of 80% -> 5 is the max leverage factor | ||
loanLeverageFactor max loanLeverageFactor would have to be on LLTV * leverageFactor to be safe | ||
*/ | ||
|
||
function onMorphoSupplyCollateral(uint256 amount, bytes calldata data) external { | ||
require(msg.sender == address(morpho)); | ||
(bytes4 selector, bytes memory _data) = abi.decode(data, (bytes4, bytes)); | ||
if (selector == this.leverageMe.selector) { | ||
(uint256 toBorrow, MarketParams memory marketParams) = abi.decode(_data, (uint256, MarketParams)); | ||
(uint256 amountBis,) = morpho.borrow(marketParams, toBorrow, 0, address(this), address(this)); | ||
ERC20(marketParams.collateralToken).approve(address(swapMock), amount); | ||
|
||
// Logic to Implement. Following example is a swap, could be a 'unwrap + stake + wrap staked' for | ||
// wETH(wstETH) Market | ||
swapMock.swapLoanToCollat(amountBis); | ||
} | ||
} | ||
|
||
function onMorphoLiquidate(uint256 repaidAssets, bytes calldata data) external onlyMorpho { | ||
require(msg.sender == address(morpho)); | ||
(bytes4 selector, bytes memory _data) = abi.decode(data, (bytes4, bytes)); | ||
if (selector == this.liquidateWithoutCollat.selector) { | ||
(uint256 toSwap, MarketParams memory marketParams) = abi.decode(_data, (uint256, MarketParams)); | ||
uint256 returnedAmount = swapMock.swapCollatToLoan(toSwap); | ||
require(returnedAmount > repaidAssets); // Add logic for gas cost threshold for instance | ||
ERC20(marketParams.loanToken).approve(address(swapMock), returnedAmount); | ||
} | ||
} | ||
|
||
function onMorphoRepay(uint256 amount, bytes calldata data) external { | ||
require(msg.sender == address(morpho)); | ||
(bytes4 selector, bytes memory _data) = abi.decode(data, (bytes4, bytes)); | ||
if (selector == this.deLeverageMe.selector) { | ||
(uint256 toWithdraw, MarketParams memory marketParams) = abi.decode(_data, (uint256, MarketParams)); | ||
morpho.withdrawCollateral(marketParams, toWithdraw, address(this), address(this)); | ||
|
||
ERC20(marketParams.loanToken).approve(address(morpho), amount); | ||
swapMock.swapCollatToLoan(toWithdraw); | ||
} | ||
} | ||
|
||
// function onMorphoFlashLoan(uint256 amount, bytes memory data) external { | ||
// require(msg.sender == address(morpho)); | ||
// (bytes4 selector, bytes memory _data) = abi.decode(data, (bytes4, bytes)); | ||
// if (selector == this.flashLoan.selector) { | ||
// assertEq(loanToken.balanceOf(address(this)), amount); | ||
// loanToken.approve(address(morpho), amount); | ||
// } | ||
// } | ||
|
||
// leverage function | ||
function leverageMe( | ||
uint256 leverageFactor, | ||
uint256 loanLeverageFactor, | ||
uint256 initialCollateral, | ||
SwapMock _swapMock, | ||
MarketParams calldata marketParams | ||
) public { | ||
_setSwapMock(_swapMock); | ||
|
||
uint256 collateralAssets = initialCollateral * leverageFactor; | ||
uint256 loanAmount = initialCollateral * loanLeverageFactor; | ||
|
||
_approveMaxTo(address(marketParams.collateralToken), address(this)); | ||
|
||
morpho.supplyCollateral( | ||
marketParams, | ||
collateralAssets, | ||
address(this), | ||
abi.encode(this.leverageMe.selector, abi.encode(loanAmount, marketParams)) | ||
); | ||
} | ||
|
||
function liquidateWithoutCollat( | ||
address borrower, | ||
uint256 loanAmountToRepay, | ||
uint256 assetsToSeize, | ||
SwapMock _swapMock, | ||
MarketParams calldata marketParams | ||
) public returns (uint256 seizedAssets, uint256 repaidAssets) { | ||
_setSwapMock(_swapMock); | ||
|
||
_approveMaxTo(address(marketParams.collateralToken), address(this)); | ||
|
||
uint256 repaidShares = 0; | ||
|
||
(seizedAssets, repaidAssets) = morpho.liquidate( | ||
marketParams, | ||
borrower, | ||
assetsToSeize, | ||
repaidShares, | ||
abi.encode(this.liquidateWithoutCollat.selector, abi.encode(loanAmountToRepay)) | ||
); | ||
} | ||
|
||
// deleverage function | ||
function deLeverageMe( | ||
uint256 leverageFactor, | ||
uint256 loanLeverageFactor, | ||
uint256 initialCollateral, | ||
SwapMock _swapMock, | ||
MarketParams calldata marketParams | ||
) public returns (uint256 amountRepayed) { | ||
// might be redundant | ||
_setSwapMock(_swapMock); | ||
|
||
uint256 collateralAssets = initialCollateral * leverageFactor; | ||
uint256 loanAmount = initialCollateral * loanLeverageFactor; | ||
|
||
_approveMaxTo(address(marketParams.collateralToken), address(this)); | ||
|
||
(amountRepayed,) = morpho.repay( | ||
marketParams, | ||
loanAmount, | ||
0, | ||
address(this), | ||
abi.encode(this.deLeverageMe.selector, abi.encode(collateralAssets, marketParams)) | ||
); | ||
} | ||
|
||
modifier onlyMorpho() { | ||
require(msg.sender == address(morpho), "msg.sender should be Morpho Blue"); | ||
_; | ||
} | ||
|
||
function _approveMaxTo(address asset, address spender) internal { | ||
if (ERC20(asset).allowance(address(this), spender) == 0) { | ||
ERC20(asset).approve(spender, type(uint256).max); | ||
} | ||
} | ||
|
||
function _setSwapMock(SwapMock _swapMock) public { | ||
swapMock = _swapMock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import {ORACLE_PRICE_SCALE} from "@morpho-blue/libraries/ConstantsLib.sol"; | ||
|
||
import "@morpho-blue/mocks/ERC20Mock.sol"; | ||
import {IOracle} from "@morpho-blue/interfaces/IOracle.sol"; | ||
|
||
import "@morpho-blue/libraries/MathLib.sol"; | ||
|
||
contract SwapMock { | ||
using MathLib for uint256; | ||
|
||
ERC20Mock public immutable collateralToken; | ||
ERC20Mock public immutable loanToken; | ||
|
||
address public immutable oracle; | ||
|
||
constructor(address collateralAddress, address loanAddress, address oracleAddress) { | ||
collateralToken = ERC20Mock(collateralAddress); | ||
loanToken = ERC20Mock(loanAddress); | ||
|
||
oracle = oracleAddress; | ||
} | ||
|
||
function swapCollatToLoan(uint256 amount) external returns (uint256 returnedAmount) { | ||
returnedAmount = amount.mulDivDown(IOracle(oracle).price(), ORACLE_PRICE_SCALE); | ||
|
||
collateralToken.transferFrom(msg.sender, address(this), amount); | ||
|
||
loanToken.setBalance(address(this), returnedAmount); | ||
loanToken.transfer(msg.sender, returnedAmount); | ||
} | ||
|
||
function swapLoanToCollat(uint256 amount) external returns (uint256 returnedAmount) { | ||
returnedAmount = amount.mulDivDown(ORACLE_PRICE_SCALE, IOracle(oracle).price()); | ||
|
||
loanToken.transferFrom(msg.sender, address(this), amount); | ||
|
||
collateralToken.setBalance(address(this), returnedAmount); | ||
collateralToken.transfer(msg.sender, returnedAmount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.