Skip to content

Commit

Permalink
Merge pull request #118 from morpho-labs/feat/max-fee
Browse files Browse the repository at this point in the history
feat(fee): add max fee
  • Loading branch information
MerlinEgalite authored Sep 27, 2023
2 parents a158653 + f19c07e commit 1670a0c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
}

function submitFee(uint256 newFee) external onlyOwner {
require(newFee <= WAD, ErrorsLib.MAX_FEE_EXCEEDED);
require(newFee <= MAX_FEE, ErrorsLib.MAX_FEE_EXCEEDED);
require(newFee != fee, ErrorsLib.ALREADY_SET);

if (newFee < fee || timelock == 0) {
_setFee(newFee);
} else {
// Safe "unchecked" cast because newFee <= WAD.
// Safe "unchecked" cast because newFee <= MAX_FEE.
pendingFee = PendingUint192(uint192(newFee), uint64(block.timestamp));

emit EventsLib.SubmitFee(newFee);
Expand Down Expand Up @@ -512,7 +512,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
function _setFee(uint256 newFee) internal {
require(newFee == 0 || feeRecipient != address(0), ErrorsLib.ZERO_FEE_RECIPIENT);

// Safe "unchecked" cast because newFee <= WAD.
// Safe "unchecked" cast because newFee <= MAX_FEE.
fee = uint96(newFee);

emit EventsLib.SetFee(newFee);
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/ConstantsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ uint8 constant DECIMALS_OFFSET = 6;
/// @dev The maximum supply/withdraw queue size ensuring the cost of depositing/withdrawing from the vault fits in a
/// block.
uint256 constant MAX_QUEUE_SIZE = 64;

/// @dev The maximum fee the vault can have (50%).
uint256 constant MAX_FEE = 0.5e18;
18 changes: 16 additions & 2 deletions test/forge/FeeTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ contract FeeTest is BaseTest {

function testSetFeeAccrueFee(uint256 deposited, uint256 fee, uint256 blocks) public {
deposited = bound(deposited, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
fee = bound(fee, 0, WAD);
fee = bound(fee, 0, MAX_FEE);
blocks = _boundBlocks(blocks);

vm.assume(fee != FEE);
Expand Down Expand Up @@ -232,8 +232,22 @@ contract FeeTest is BaseTest {
vault.submitFee(fee);
}

function testSubmitFeeMaxFeeExceeded(uint256 fee) public {
fee = bound(fee, MAX_FEE + 1, type(uint256).max);

vm.prank(OWNER);
vm.expectRevert(bytes(ErrorsLib.MAX_FEE_EXCEEDED));
vault.submitFee(fee);
}

function testSubmitFeeAlreadySet() public {
vm.prank(OWNER);
vm.expectRevert(bytes(ErrorsLib.ALREADY_SET));
vault.submitFee(FEE);
}

function testAcceptFeeNotOwner(uint256 fee) public {
fee = bound(fee, FEE + 1, WAD);
fee = bound(fee, FEE + 1, MAX_FEE);

_setTimelock(1);

Expand Down
16 changes: 4 additions & 12 deletions test/forge/TimelockTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ contract TimelockTest is BaseTest {
}

function testSubmitFeeIncreased(uint256 fee) public {
fee = bound(fee, FEE + 1, WAD);
fee = bound(fee, FEE + 1, MAX_FEE);

vm.prank(OWNER);
vault.submitFee(fee);
Expand All @@ -159,16 +159,8 @@ contract TimelockTest is BaseTest {
assertEq(submittedAt, block.timestamp, "submittedAt");
}

function testSubmitFeeIncreasedMaxFeeExceeded(uint256 fee) public {
fee = bound(fee, WAD + 1, type(uint256).max);

vm.prank(OWNER);
vm.expectRevert(bytes(ErrorsLib.MAX_FEE_EXCEEDED));
vault.submitFee(fee);
}

function testAcceptFee(uint256 fee) public {
fee = bound(fee, FEE + 1, WAD);
fee = bound(fee, FEE + 1, MAX_FEE);

vm.prank(OWNER);
vault.submitFee(fee);
Expand All @@ -192,7 +184,7 @@ contract TimelockTest is BaseTest {
}

function testAcceptFeeTimelockNotElapsed(uint256 fee, uint256 elapsed) public {
fee = bound(fee, FEE + 1, WAD);
fee = bound(fee, FEE + 1, MAX_FEE);
elapsed = bound(elapsed, 1, TIMELOCK - 1);

vm.prank(OWNER);
Expand All @@ -206,7 +198,7 @@ contract TimelockTest is BaseTest {
}

function testAcceptFeeTimelockExpirationExceeded(uint256 fee, uint256 elapsed) public {
fee = bound(fee, FEE + 1, WAD);
fee = bound(fee, FEE + 1, MAX_FEE);
elapsed = bound(elapsed, TIMELOCK + TIMELOCK_EXPIRATION + 1, type(uint64).max);

vm.prank(OWNER);
Expand Down
2 changes: 1 addition & 1 deletion test/forge/helpers/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol";
import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol";

import "src/libraries/ConstantsLib.sol";
import "@morpho-blue/libraries/ConstantsLib.sol";
import {ORACLE_PRICE_SCALE} from "@morpho-blue/libraries/ConstantsLib.sol";

import {IrmMock} from "src/mocks/IrmMock.sol";
import {ERC20Mock} from "src/mocks/ERC20Mock.sol";
Expand Down

0 comments on commit 1670a0c

Please sign in to comment.