From aa0dedc52cb894d530efb35c344eb7fb3a82f8ca Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 16 Dec 2023 19:00:43 +0530 Subject: [PATCH] feat: optimise --- .../mainnet/connectors/morpho-blue/events.sol | 26 ++--- .../connectors/morpho-blue/helpers.sol | 56 +++------ .../mainnet/connectors/morpho-blue/main.sol | 106 +++++++++++------- 3 files changed, 96 insertions(+), 92 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-blue/events.sol b/contracts/mainnet/connectors/morpho-blue/events.sol index 6dd2355..b0c1025 100644 --- a/contracts/mainnet/connectors/morpho-blue/events.sol +++ b/contracts/mainnet/connectors/morpho-blue/events.sol @@ -1,10 +1,10 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.8.2; -import {MarketParams} from "./interfaces/IMorpho.sol"; +import {Id, MarketParams} from "./interfaces/IMorpho.sol"; contract Events { event LogSupplyAssets( - MarketParams marketParams, + Id indexed id, uint256 assets, uint256 shares, uint256 getId, @@ -12,7 +12,7 @@ contract Events { ); event LogSupplyOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 assets, uint256 shares, address onBehalf, @@ -21,14 +21,14 @@ contract Events { ); event LogSupplyCollateral( - MarketParams marketParams, + Id indexed id, uint256 assets, uint256 getId, uint256 setId ); event LogSupplyCollateralOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 assets, address onBehalf, uint256 getId, @@ -36,7 +36,7 @@ contract Events { ); event LogBorrow( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, uint256 getId, @@ -44,7 +44,7 @@ contract Events { ); event LogBorrowOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, address onBehalf, @@ -54,7 +54,7 @@ contract Events { ); event LogWithdraw( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, uint256 getId, @@ -62,7 +62,7 @@ contract Events { ); event LogWithdrawOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, address onBehalf, @@ -71,14 +71,14 @@ contract Events { ); event LogWithdrawCollateral( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 getId, uint256 setId ); event LogWithdrawCollateralOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 amounts, address onBehalf, address receiver, @@ -87,7 +87,7 @@ contract Events { ); event LogRepay( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, uint256 getId, @@ -95,7 +95,7 @@ contract Events { ); event LogRepayOnBehalf( - MarketParams marketParams, + Id indexed id, uint256 amounts, uint256 shares, address onBehalf, diff --git a/contracts/mainnet/connectors/morpho-blue/helpers.sol b/contracts/mainnet/connectors/morpho-blue/helpers.sol index ab0dfd8..285cdcf 100644 --- a/contracts/mainnet/connectors/morpho-blue/helpers.sol +++ b/contracts/mainnet/connectors/morpho-blue/helpers.sol @@ -8,11 +8,13 @@ import "../../common/interfaces.sol"; import {MorphoBalancesLib} from "./libraries/periphery/MorphoBalancesLib.sol"; import {UtilsLib} from "./libraries/UtilsLib.sol"; import {MarketParamsLib} from "./libraries/MarketParamsLib.sol"; +import {SharesMathLib} from "./libraries/SharesMathLib.sol"; abstract contract Helpers is Stores, Basic { using MorphoBalancesLib for IMorpho; using MarketParamsLib for MarketParams; using UtilsLib for uint256; + using SharesMathLib for uint256; IMorpho public constant MORPHO_BLUE = IMorpho(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0); // TODO: Update @@ -40,7 +42,7 @@ abstract contract Helpers is Stores, Basic { address _onBehalf, uint256 _getId, Mode _mode - ) internal returns (MarketParams memory, uint256 _amt) { + ) internal returns (Id _id, MarketParams memory, uint256 _amt) { _amt = getUint(_getId, _assets); bool _isEth = _mode == Mode.Collateral @@ -49,6 +51,8 @@ abstract contract Helpers is Stores, Basic { _marketParams = updateTokenAddresses(_marketParams); + _id = _marketParams.id(); + // Check for max value if (_assets == type(uint256).max) { uint256 _maxAvailable = _isEth @@ -57,8 +61,8 @@ abstract contract Helpers is Stores, Basic { address(this) ); if (_mode == Mode.Repay) { - uint256 _amtDebt = getPaybackBalance(_marketParams, _onBehalf); - _amt = _maxAvailable.min(_amtDebt); // TODO: Ask + uint256 _amtDebt = getPaybackBalance(_id, _marketParams, _onBehalf); + _amt = UtilsLib.min(_maxAvailable, _amtDebt); } else { _amt = _maxAvailable; } @@ -67,7 +71,7 @@ abstract contract Helpers is Stores, Basic { // Perform conversion if necessary convertEthToWeth(true, TokenInterface(_marketParams.loanToken), _amt); - return (_marketParams, _amt); + return (_id, _marketParams, _amt); } /// @notice Handles Eth to Weth conversion if shares are provided. @@ -77,12 +81,14 @@ abstract contract Helpers is Stores, Basic { address _onBehalf, uint256 _getId, bool _isRepay - ) internal returns (MarketParams memory, uint256 _assets) { + ) internal returns (Id _id, MarketParams memory, uint256 _assets) { uint256 _shareAmt = getUint(_getId, _shares); bool _isEth = _marketParams.loanToken == ethAddr; _marketParams = updateTokenAddresses(_marketParams); + _id = _marketParams.id(); + // Handle the max share case if (_shares == type(uint256).max) { uint256 _maxAvailable = _isEth @@ -93,9 +99,7 @@ abstract contract Helpers is Stores, Basic { // If it's repay calculate the min of balance available and debt to repay if (_isRepay) { - _assets = _maxAvailable.min( - getPaybackBalance(_marketParams, _onBehalf) // TODO: Ask - ); + _assets = UtilsLib.min(_maxAvailable, getPaybackBalance(_id, _marketParams, _onBehalf)); } else { _assets = _maxAvailable; } @@ -107,11 +111,7 @@ abstract contract Helpers is Stores, Basic { ) = MORPHO_BLUE.expectedMarketBalances(_marketParams); - _assets = _toAssetsUp( - _shareAmt, - totalSupplyAssets, - totalSupplyShares - ); + _assets = _shareAmt.toAssetsUp(totalSupplyAssets, totalSupplyShares); } // Perform ETH to WETH conversion if necessary @@ -121,45 +121,21 @@ abstract contract Helpers is Stores, Basic { _assets ); - return (_marketParams, _assets); + return (_id, _marketParams, _assets); } /// @notice Returns the payback balance in assets. function getPaybackBalance( + Id _id, MarketParams memory _marketParams, address _onBehalf ) internal view returns (uint256 _assets) { - Id _id = _marketParams.id(); - uint256 _shareAmt = MORPHO_BLUE.position(_id, _onBehalf).supplyShares; (uint256 totalSupplyAssets, uint256 totalSupplyShares, , ) = MORPHO_BLUE .expectedMarketBalances(_marketParams); - _assets = _toAssetsUp(_shareAmt, totalSupplyAssets, totalSupplyShares); - } - - /// @notice Calculates the value of `shares` quoted in assets, rounding up. - function _toAssetsUp( - uint256 _shares, - uint256 _totalAssets, - uint256 _totalShares - ) internal pure returns (uint256) { - return - _mulDivUp( - _shares, - _totalAssets + VIRTUAL_ASSETS, - _totalShares + VIRTUAL_SHARES - ); - } - - /// @notice Returns (`x` * `y`) / `d` rounded up. - function _mulDivUp( - uint256 x, - uint256 y, - uint256 d - ) internal pure returns (uint256) { - return (x * y + (d - 1)) / d; + _assets = _shareAmt.toAssetsUp(totalSupplyAssets, totalSupplyShares); } function updateTokenAddresses( diff --git a/contracts/mainnet/connectors/morpho-blue/main.sol b/contracts/mainnet/connectors/morpho-blue/main.sol index 48f0c3f..3730df8 100644 --- a/contracts/mainnet/connectors/morpho-blue/main.sol +++ b/contracts/mainnet/connectors/morpho-blue/main.sol @@ -27,7 +27,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt ) = _performEthToWethConversion( @@ -55,9 +57,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogSupplyAssets((address,address,address,address,unit256),unit256,unit256,unit256,unit256)"; + _eventName = "LogSupplyAssets(bytes32,unit256,unit256,unit256,unit256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _getId, @@ -86,7 +88,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt ) = _performEthToWethConversion( @@ -114,9 +118,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogSupplyOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogSupplyOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _onBehalf, @@ -146,7 +150,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt // Shares amount converted to assets ) = _performEthToWethSharesConversion( @@ -163,19 +169,20 @@ abstract contract MorphoBlue is Helpers, Events { _amt ); - (uint256 _assets, ) = MORPHO_BLUE.supply( + uint256 _assets; + (_assets, _shares) = MORPHO_BLUE.supply( _marketParams, _amt, - _shares, + 0, _onBehalf, new bytes(0) ); setUint(_setId, _assets); - _eventName = "LogSupplyOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogSupplyOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _onBehalf, @@ -202,7 +209,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt ) = _performEthToWethConversion( @@ -229,8 +238,8 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogSupplyCollateral((address,address,address,address,unit256),uint256,uint256,uint256)"; - _eventParam = abi.encode(_marketParams, _assets, _getId, _setId); + _eventName = "LogSupplyCollateral(bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_id, _assets, _getId, _setId); } /** @@ -253,8 +262,10 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; // Final assets amount and token contract ( + _id, _marketParams, // Updated token contracts in case of Eth _amt ) = _performEthToWethConversion( @@ -281,9 +292,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogSupplyCollateralOnBehalf((address,address,address,address,unit256),uint256,address,uint256,uint256)"; + _eventName = "LogSupplyCollateralOnBehalf(bytes32,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _onBehalf, _getId, @@ -313,9 +324,10 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + // If amount is max, fetch collateral value from Morpho's contract if (_amt == type(uint256).max) { - Id _id = _marketParams.id(); Position memory _pos = MORPHO_BLUE.position(_id, address(this)); _amt = _pos.collateral; } @@ -335,8 +347,8 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogWithdrawCollateral((address,address,address,address,unit256),uint256,uint256,uint256)"; - _eventParam = abi.encode(_marketParams, _amt, _getId, _setId); + _eventName = "LogWithdrawCollateral(bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_id, _amt, _getId, _setId); } /** @@ -364,9 +376,10 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + // If amount is max, fetch collateral value from Morpho's contract if (_amt == type(uint256).max) { - Id _id = _marketParams.id(); Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf); _amt = _pos.collateral; } @@ -387,9 +400,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogWithdrawCollateralOnBehalf((address,address,address,address,unit256),uint256,address,address,uint256,uint256)"; + _eventName = "LogWithdrawCollateralOnBehalf(bytes32,uint256,address,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _amt, _onBehalf, _receiver, @@ -419,12 +432,13 @@ abstract contract MorphoBlue is Helpers, Events { uint256 _amt = getUint(_getId, _assets); _marketParams = updateTokenAddresses(_marketParams); + + Id _id = _marketParams.id(); uint256 _shares = 0; // Using shares for max amounts to make sure no dust is left on the contract if (_amt == type(uint256).max) { - Id _id = _marketParams.id(); Position memory _pos = MORPHO_BLUE.position(_id, address(this)); _shares = _pos.supplyShares; _amt = 0; @@ -447,9 +461,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogWithdraw((address,address,address,address,unit256),uint256,uint256,uint256,uint256)"; + _eventName = "LogWithdraw(bytes32,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _getId, @@ -482,10 +496,11 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + uint256 _shares = 0; // Using shares for max amounts to make sure no dust is left on the contract if (_amt == type(uint256).max) { - Id _id = _marketParams.id(); Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf); _shares = _pos.supplyShares; _amt = 0; @@ -508,9 +523,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogWithdrawOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogWithdrawOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _onBehalf, @@ -544,8 +559,9 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + if (_shareAmt == type(uint256).max) { - Id _id = _marketParams.id(); Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf); _shareAmt = _pos.supplyShares; } @@ -567,9 +583,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogWithdrawOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogWithdrawOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shareAmt, _onBehalf, @@ -600,6 +616,8 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + (, uint256 _shares) = MORPHO_BLUE.borrow( _marketParams, _amt, @@ -616,8 +634,8 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogBorrow((address,address,address,address,unit256),uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(_marketParams, _amt, _shares, _getId, _setId); + _eventName = "LogBorrow(bytes32,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(_id, _amt, _shares, _getId, _setId); } /** @@ -646,6 +664,8 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + (, uint256 _shares) = MORPHO_BLUE.borrow( _marketParams, _amt, @@ -663,9 +683,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogBorrowOnBehalf((address,address,address,address,unit256),uint256,uint256,address,address,uint256,uint256)"; + _eventName = "LogBorrowOnBehalf(bytes32,uint256,uint256,address,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _amt, _shares, _onBehalf, @@ -701,6 +721,8 @@ abstract contract MorphoBlue is Helpers, Events { _marketParams = updateTokenAddresses(_marketParams); + Id _id = _marketParams.id(); + (uint256 _assets, ) = MORPHO_BLUE.borrow( _marketParams, 0, @@ -718,9 +740,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogBorrowOnBehalf((address,address,address,address,unit256),uint256,uint256,address,address,uint256,uint256)"; + _eventName = "LogBorrowOnBehalf(bytes32,uint256,uint256,address,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shareAmt, _onBehalf, @@ -749,7 +771,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt // Assets final amount to repay ) = _performEthToWethConversion( @@ -778,9 +802,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogRepay((address,address,address,address,unit256),uint256,uint256,uint256,uint256)"; + _eventName = "LogRepay(bytes32,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _getId, @@ -809,7 +833,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _amt // Assets final amount to repay ) = _performEthToWethConversion( @@ -838,9 +864,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogRepayOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogRepayOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _onBehalf, @@ -870,7 +896,9 @@ abstract contract MorphoBlue is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _assetsAmt; + Id _id; ( + _id, _marketParams, // Updated token contracts in case of Eth _assetsAmt // Assets final amount to repay ) = _performEthToWethSharesConversion( @@ -897,9 +925,9 @@ abstract contract MorphoBlue is Helpers, Events { setUint(_setId, _assets); - _eventName = "LogRepayOnBehalf((address,address,address,address,unit256),uint256,uint256,address,uint256,uint256)"; + _eventName = "LogRepayOnBehalf(bytes32,uint256,uint256,address,uint256,uint256)"; _eventParam = abi.encode( - _marketParams, + _id, _assets, _shares, _onBehalf,