Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: gas optimizations - issue 99 #297

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
/// - the timelock has expired since the pending value has been submitted.
modifier withinTimelockWindow(uint256 submittedAt) {
if (submittedAt == 0) revert ErrorsLib.NoPendingValue();
if (block.timestamp < submittedAt + timelock) revert ErrorsLib.TimelockNotElapsed();
if (block.timestamp > submittedAt + timelock + ConstantsLib.TIMELOCK_EXPIRATION) {

uint256 currentTimelock = timelock;

if (block.timestamp < submittedAt + currentTimelock) revert ErrorsLib.TimelockNotElapsed();
if (block.timestamp > submittedAt + currentTimelock + ConstantsLib.TIMELOCK_EXPIRATION) {
revert ErrorsLib.TimelockExpirationExceeded();
}

Expand Down Expand Up @@ -262,11 +265,10 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
/// @dev Warning: Submitting a cap will overwrite the current pending cap.
function submitCap(MarketParams memory marketParams, uint256 newSupplyCap) external onlyCuratorRole {
Id id = marketParams.id();
if (marketParams.loanToken != asset()) revert ErrorsLib.InconsistentAsset(id);
if (MORPHO.lastUpdate(id) == 0) revert ErrorsLib.MarketNotCreated();

uint256 supplyCap = config[id].cap;
if (marketParams.loanToken != asset()) revert ErrorsLib.InconsistentAsset(id);
if (newSupplyCap == supplyCap) revert ErrorsLib.AlreadySet();
if (MORPHO.lastUpdate(id) == 0) revert ErrorsLib.MarketNotCreated();

if (newSupplyCap < supplyCap) {
_setCap(id, newSupplyCap.toUint192());
Expand Down Expand Up @@ -667,14 +669,16 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
MarketConfig storage marketConfig = config[id];

if (supplyCap > 0 && marketConfig.withdrawRank == 0) {
supplyQueue.push(id);
withdrawQueue.push(id);

if (supplyQueue.length > ConstantsLib.MAX_QUEUE_SIZE || withdrawQueue.length > ConstantsLib.MAX_QUEUE_SIZE)
{
if (
supplyQueue.length + 1 > ConstantsLib.MAX_QUEUE_SIZE
|| withdrawQueue.length + 1 > ConstantsLib.MAX_QUEUE_SIZE
) {
revert ErrorsLib.MaxQueueSizeExceeded();
}

supplyQueue.push(id);
withdrawQueue.push(id);

// Safe "unchecked" cast because withdrawQueue.length <= MAX_QUEUE_SIZE.
marketConfig.withdrawRank = uint64(withdrawQueue.length);
}
Expand Down
5 changes: 3 additions & 2 deletions test/forge/MarketTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ contract MarketTest is IntegrationTest {
vault.submitCap(marketParams, 0);
}

function testSubmitCapMarketNotCreated(MarketParams memory marketParams) public {
function testSubmitCapMarketNotCreated(MarketParams memory marketParams, uint256 cap) public {
marketParams.loanToken = address(loanToken);

vm.assume(morpho.lastUpdate(marketParams.id()) == 0);
vm.assume(cap != 0);

vm.prank(CURATOR);
vm.expectRevert(ErrorsLib.MarketNotCreated.selector);
vault.submitCap(marketParams, 0);
vault.submitCap(marketParams, cap);
}

function testSubmitCapAlreadySet() public {
Expand Down