From fe1b6ab2e73f89f736580594f357eb669a4abe1f Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 19 Jan 2023 17:49:46 +0100 Subject: [PATCH 1/2] feat: add claim to treasury function --- src/MorphoInternal.sol | 19 +++++++++++++++++++ src/MorphoSetters.sol | 4 ++++ src/libraries/Events.sol | 2 ++ 3 files changed, 25 insertions(+) diff --git a/src/MorphoInternal.sol b/src/MorphoInternal.sol index f752d9641..c29a02330 100644 --- a/src/MorphoInternal.sol +++ b/src/MorphoInternal.sol @@ -88,6 +88,25 @@ abstract contract MorphoInternal is MorphoStorage { emit Events.MarketCreated(underlying, reserveFactor, p2pIndexCursor); } + function _claimToTreasury(address[] calldata underlyings, uint256[] calldata amounts) internal { + if (_treasuryVault == address(0)) revert Errors.AddressIsZero(); + + for (uint256 i; i < underlyings.length; ++i) { + address underlying = underlyings[i]; + + if (!_market[underlying].isCreated()) continue; + + uint256 underlyingBalance = ERC20(underlying).balanceOf(address(this)); + + if (underlyingBalance == 0) continue; + + uint256 claimed = Math.min(amounts[i], underlyingBalance); + + ERC20(underlying).safeTransfer(_treasuryVault, claimed); + emit Events.ReserveFeeClaimed(underlying, claimed); + } + } + function _increaseP2PDeltas(address underlying, uint256 amount) internal { Types.Indexes256 memory indexes = _updateIndexes(underlying); diff --git a/src/MorphoSetters.sol b/src/MorphoSetters.sol index 22ba613d9..d1fc14f85 100644 --- a/src/MorphoSetters.sol +++ b/src/MorphoSetters.sol @@ -44,6 +44,10 @@ abstract contract MorphoSetters is IMorphoSetters, MorphoInternal { _createMarket(underlying, reserveFactor, p2pIndexCursor); } + function claimToTreasury(address[] calldata underlyings, uint256[] calldata amounts) external onlyOwner { + _claimToTreasury(underlyings, amounts); + } + function increaseP2PDeltas(address underlying, uint256 amount) external onlyOwner isMarketCreated(underlying) { _increaseP2PDeltas(underlying, amount); } diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index 1265e0a79..a391a03ce 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -113,4 +113,6 @@ library Events { uint256 poolSupplyIndex, uint256 poolBorrowIndex ); + + event ReserveFeeClaimed(address indexed underlying, uint256 claimed); } From 0598aedad85ab0b7ae5d6172babee979489e7475 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Fri, 20 Jan 2023 08:02:10 +0100 Subject: [PATCH 2/2] perf: var in stack --- src/MorphoInternal.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MorphoInternal.sol b/src/MorphoInternal.sol index c29a02330..a64f23153 100644 --- a/src/MorphoInternal.sol +++ b/src/MorphoInternal.sol @@ -89,7 +89,8 @@ abstract contract MorphoInternal is MorphoStorage { } function _claimToTreasury(address[] calldata underlyings, uint256[] calldata amounts) internal { - if (_treasuryVault == address(0)) revert Errors.AddressIsZero(); + address treasuryVault = _treasuryVault; + if (treasuryVault == address(0)) revert Errors.AddressIsZero(); for (uint256 i; i < underlyings.length; ++i) { address underlying = underlyings[i]; @@ -102,7 +103,7 @@ abstract contract MorphoInternal is MorphoStorage { uint256 claimed = Math.min(amounts[i], underlyingBalance); - ERC20(underlying).safeTransfer(_treasuryVault, claimed); + ERC20(underlying).safeTransfer(treasuryVault, claimed); emit Events.ReserveFeeClaimed(underlying, claimed); } }