From c50e7e2ec16d448b289deb78d1a8859a8eaf971a Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 24 Jun 2023 04:43:28 +0800 Subject: [PATCH] Morpho aave v3 updates --- .../connectors/morpho-aave-v3/events.sol | 8 +- .../connectors/morpho-aave-v3/main.sol | 67 ++++++------- test/mainnet/morpho/morpho-aave-v3.test.ts | 99 ++++++++++++++++--- 3 files changed, 124 insertions(+), 50 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave-v3/events.sol b/contracts/mainnet/connectors/morpho-aave-v3/events.sol index 456c7f8d..404eb789 100644 --- a/contracts/mainnet/connectors/morpho-aave-v3/events.sol +++ b/contracts/mainnet/connectors/morpho-aave-v3/events.sol @@ -101,9 +101,10 @@ contract Events { uint256 setId ); - event LogWithdrawWithMaxIterations( + event LogWithdrawOnBehalfWithMaxIterations( address tokenAddress, uint256 amount, + address onBehalf, address receiver, uint256 maxIteration, uint256 getId, @@ -143,4 +144,9 @@ contract Events { ); event LogApproveManger(address manger, bool isAllowed); + + event LogUpdateMaxIterations( + uint256 oldIterations, + uint256 newIterations + ); } diff --git a/contracts/mainnet/connectors/morpho-aave-v3/main.sol b/contracts/mainnet/connectors/morpho-aave-v3/main.sol index e110dcb9..d614193d 100644 --- a/contracts/mainnet/connectors/morpho-aave-v3/main.sol +++ b/contracts/mainnet/connectors/morpho-aave-v3/main.sol @@ -291,7 +291,8 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param _amount The amount of the token (in underlying) to borrow. * @param _onBehalf The address of user on behalf to borrow. - * @param _receiver The address of receiver to receive the borrowed tokens. + * @param _receiver The address of receiver to receive the borrowed tokens. + Note that if receiver is not the same as the borrower, receiver will receive WETH instead of ETH. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens borrowed. */ @@ -313,7 +314,7 @@ abstract contract MorphoAaveV3 is Helpers, Events { MORPHO_AAVE_V3.borrow(_token, _amt, _onBehalf, _receiver, max_iteration); - if(_receiver == address(this)) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + if(_receiver == address(this)) convertWethToEth(_isETH, TokenInterface(_token), _amt); setUint(_setId, _amt); @@ -334,6 +335,7 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param _amount The amount of the token (in underlying) to borrow. * @param _receiver The address of receiver to receive the borrowed tokens. + Note that if receiver is not the same as the borrower, receiver will receive WETH instead of ETH. * @param _maxIteration The maximum number of iterations to be used for borrow. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens borrowed. @@ -376,6 +378,10 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @notice Borrow a token from Morpho Aave. * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param _amount The amount of the token (in underlying) to borrow. + * @param _onBehalf The address of user on behalf to borrow. + * @param _receiver The address of receiver to receive the borrowed tokens. + Note that if receiver is not the same as the borrower, receiver will receive WETH instead of ETH. + * @param _maxIteration The maximum number of iterations to be used for borrow. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens borrowed. */ @@ -459,6 +465,7 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) * @param _onBehalf Address for which tokens are being withdrawn. * @param _receiver Address to which tokens are being transferred. + Note that if receiver is not the same as the supplier, receiver will receive WETH instead of ETH. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens withdrawed. */ @@ -503,11 +510,12 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) * @param _onBehalf Address for which tokens are being withdrawn. * @param _receiver Address to which tokens are being transferred. + Note that if receiver is not the same as the supplier, receiver will receive WETH instead of ETH. * @param _maxIteration Max number of iterations to run. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens withdrawed. */ - function withdrawWithMaxIterations( + function withdrawOnBehalfWithMaxIterations( address _tokenAddress, uint256 _amount, address _onBehalf, @@ -531,10 +539,11 @@ abstract contract MorphoAaveV3 is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogWithdrawWithMaxIterations(address,uint256,address,uint256,uint256,uint256)"; + _eventName = "LogWithdrawOnBehalfWithMaxIterations(address,uint256,address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( _tokenAddress, _amt, + _onBehalf, _receiver, _maxIteration, _getId, @@ -548,6 +557,7 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) * @param _receiver Address to which tokens are being transferred. + Note that if receiver is not the same as the supplier, receiver will receive WETH instead of ETH. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens withdrawed. */ @@ -589,6 +599,7 @@ abstract contract MorphoAaveV3 is Helpers, Events { * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) * @param _onBehalf Address for which tokens are being withdrawn. * @param _receiver Address to which tokens are being transferred. + Note that if receiver is not the same as the supplier, receiver will receive WETH instead of ETH. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens withdrawed. */ @@ -643,20 +654,10 @@ abstract contract MorphoAaveV3 is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - bool _isETH = _tokenAddress == ethAddr; - uint256 _amt = getUint(_getId, _amount); - - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); - } - - convertEthToWeth(_isETH, _tokenContract, _amt); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); approve(_tokenContract, address(MORPHO_AAVE_V3), _amt); @@ -693,20 +694,10 @@ abstract contract MorphoAaveV3 is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - bool _isETH = _tokenAddress == ethAddr; - uint256 _amt = getUint(_getId, _amount); - - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); - } - - convertEthToWeth(_isETH, _tokenContract, _amt); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); approve(_tokenContract, address(MORPHO_AAVE_V3), _amt); @@ -742,8 +733,18 @@ abstract contract MorphoAaveV3 is Helpers, Events { /// @notice Updates the max iterations for a `repay` or a `withdraw`. /// @param _iterations New iteration count. - function updateMaxIterations(uint256 _iterations) external { + function updateMaxIterations(uint256 _iterations) + external + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _oldIterations = max_iteration; max_iteration = _iterations; + + _eventName = "LogUpdateMaxIterations(uint256,uint256)"; + _eventParam = abi.encode( + _oldIterations, + max_iteration + ); } } diff --git a/test/mainnet/morpho/morpho-aave-v3.test.ts b/test/mainnet/morpho/morpho-aave-v3.test.ts index 16d38f01..715df2e4 100644 --- a/test/mainnet/morpho/morpho-aave-v3.test.ts +++ b/test/mainnet/morpho/morpho-aave-v3.test.ts @@ -59,7 +59,7 @@ describe("Morpho-Aave-v3", function () { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - // blockNumber: 15714501, + blockNumber: 17544460, }, }, ], @@ -224,7 +224,7 @@ describe("Morpho-Aave-v3", function () { ); }) - it("Should deposit 2000 USDC as collateral", async function () { + it("Should deposit collateral 2000 USDC", async function () { const spells = [ { connector: connectorName, @@ -243,12 +243,12 @@ describe("Morpho-Aave-v3", function () { ); }) - it("Should deposit 2000 USDC as collateral on behalf", async function () { + it("Should deposit collateral 2000 USDC on behalf with maxValue", async function () { const spells = [ { connector: connectorName, method: "depositCollateralOnBehalf", - args: [tokens.usdc.address, "2000000000", user, "0", "0"], // 50 USDC + args: [tokens.usdc.address, dsaMaxValue, user, "0", "0"], // ~3000 USDC }, ]; @@ -258,7 +258,7 @@ describe("Morpho-Aave-v3", function () { await tx.wait(); expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte( - parseUnits('1000', 6) + parseUnits('1', 6) ); }) @@ -281,7 +281,7 @@ describe("Morpho-Aave-v3", function () { ); }) - it("Should withdraw on behalf of user", async function () { + it("Should withdraw on behalf of user with maxValue", async function () { let ethBala = await ethers.provider.getBalance(user) let wethBala = await token_weth.balanceOf(user) @@ -309,7 +309,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "borrow", - args: [tokens.weth.address, "500000000000000000", "0", "0"], // 0.7 WETH + args: [tokens.weth.address, "500000000000000000", "0", "0"], // 0.5 WETH }, ]; @@ -347,7 +347,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "borrowWithMaxIterations", - args: [tokens.weth.address, "20000000000000000", dsaWallet0.address, 10, "0", "0"], // 0.7 WETH + args: [tokens.weth.address, "20000000000000000", dsaWallet0.address, 10, "0", "0"], // 0.02 WETH }, ]; @@ -361,7 +361,26 @@ describe("Morpho-Aave-v3", function () { }) it("Test withdrawCollateral ", async function () { - const balance = await token_weth.balanceOf(dsaWallet0.address); + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_USDC], + }) + + const signer_usdc = await ethers.getSigner(ACC_USDC) + await token_usdc.connect(signer_usdc).transfer(dsaWallet0.address, parseUnits('500', 6)) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_USDC], + }) + + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('500', 6) + ); + + const balance = await token_usdc.balanceOf(dsaWallet0.address); + console.log('balance: ', balance.toString()); + const spells = [ { connector: connectorName, @@ -371,7 +390,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "withdrawCollateral", - args: [tokens.usdc.address, "20000000", dsaWallet0.address, "0", "0"], // 20 USDC + args: [tokens.usdc.address, "19000000", dsaWallet0.address, "0", "0"], // 19 USDC }, ]; @@ -383,8 +402,26 @@ describe("Morpho-Aave-v3", function () { }) - it("Test withdrawCollateralOnBehalf ", async function () { - const balance = await token_weth.balanceOf(dsaWallet0.address); + it("Test withdrawCollateralOnBehalf with maxValue", async function () { + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_USDC], + }) + + const signer_usdc = await ethers.getSigner(ACC_USDC) + await token_usdc.connect(signer_usdc).transfer(dsaWallet0.address, parseUnits('500', 6)) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_USDC], + }) + + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('500', 6) + ); + + const balance = await token_usdc.balanceOf(dsaWallet0.address); + const spells = [ { connector: connectorName, @@ -394,7 +431,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "withdrawCollateralOnBehalf", - args: [tokens.usdc.address, "20000000", dsaWallet0.address, user, "0", "0"], // 20 USDC + args: [tokens.usdc.address, dsaMaxValue, dsaWallet0.address, user, "0", "0"], // 20 USDC }, ]; @@ -404,9 +441,36 @@ describe("Morpho-Aave-v3", function () { await tx.wait(); + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('499', 6) + ); }) - it("Test payback ", async function () { + it("Test payback with maxValue", async function () { + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_USDC], + }) + + await hre.network.provider.request({ + method: 'hardhat_setBalance', + params: [dsaWallet0.address, "1000000000000000000"], + }) + + const signer_usdc = await ethers.getSigner(ACC_USDC) + await token_usdc.connect(signer_usdc).transfer(dsaWallet0.address, parseUnits('500', 6)) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_USDC], + }) + + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('500', 6) + ); + + const balance = await token_usdc.balanceOf(dsaWallet0.address); + const spells = [ { connector: connectorName, @@ -421,7 +485,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "payback", - args: [tokens.eth.address, "1000000000000000", "0", "0"], // 20 USDC + args: [tokens.eth.address, dsaMaxValue, "0", "0"], // 20 USDC }, ]; @@ -431,6 +495,9 @@ describe("Morpho-Aave-v3", function () { await tx.wait(); + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('499', 6) + ); }) it("approve manger", async () => { @@ -438,7 +505,7 @@ describe("Morpho-Aave-v3", function () { { connector: connectorName, method: "approveManager", - args: [user, true], // 2 ETH + args: [user, true], }, ] const tx = await dsaWallet0