From 30802621378313792c0a25460acd0c172ad594a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Wed, 24 Apr 2024 18:24:18 +0100 Subject: [PATCH 1/5] fix: Save gas by refactoring ActivePool `receiveETH` --- contracts/src/ActivePool.sol | 12 +++++++++--- contracts/src/BorrowerOperations.sol | 8 ++++---- contracts/src/Interfaces/IActivePool.sol | 1 + 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index c048f7f3..e9d8a76b 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -160,12 +160,18 @@ contract ActivePool is Ownable, CheckContract, IActivePool { function receiveETH(uint256 _amount) external { _requireCallerIsBorrowerOperationsOrDefaultPool(); - uint256 newETHBalance = ETHBalance + _amount; - ETHBalance = newETHBalance; - // Pull ETH tokens from sender ETH.safeTransferFrom(msg.sender, address(this), _amount); + accountForReceivedETH(_amount); + } + + function accountForReceivedETH(uint256 _amount) public { + _requireCallerIsBorrowerOperationsOrDefaultPool(); + + uint256 newETHBalance = ETHBalance + _amount; + ETHBalance = newETHBalance; + emit ActivePoolETHBalanceUpdated(newETHBalance); } diff --git a/contracts/src/BorrowerOperations.sol b/contracts/src/BorrowerOperations.sol index 3dffedb2..e1db5975 100644 --- a/contracts/src/BorrowerOperations.sol +++ b/contracts/src/BorrowerOperations.sol @@ -603,10 +603,10 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe } function _pullETHAndSendToActivePool(IActivePool _activePool, uint256 _amount) internal { - // Pull ETH tokens from sender (we may save gas by pulling directly from Active Pool, but then the approval UX for user would be weird) - ETH.safeTransferFrom(msg.sender, address(this), _amount); - // Move the ether to the Active Pool - _activePool.receiveETH(_amount); + // Send ETH tokens from sender to active pool + ETH.safeTransferFrom(msg.sender, address(_activePool), _amount); + // Make sure Active Pool accountancy is right + _activePool.accountForReceivedETH(_amount); } function _updateActivePoolTrackersNoDebtChange( diff --git a/contracts/src/Interfaces/IActivePool.sol b/contracts/src/Interfaces/IActivePool.sol index ac858b03..b328d949 100644 --- a/contracts/src/Interfaces/IActivePool.sol +++ b/contracts/src/Interfaces/IActivePool.sol @@ -42,4 +42,5 @@ interface IActivePool { function sendETH(address _account, uint256 _amount) external; function sendETHToDefaultPool(uint256 _amount) external; function receiveETH(uint256 _amount) external; + function accountForReceivedETH(uint256 _amount) external; } From 7a8959838ae4acf74d75f2f86415bb5ad4c955ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Fri, 26 Apr 2024 08:31:43 +0100 Subject: [PATCH 2/5] fix: Remove duplicated access control --- contracts/src/ActivePool.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index e9d8a76b..76e539fa 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -167,8 +167,6 @@ contract ActivePool is Ownable, CheckContract, IActivePool { } function accountForReceivedETH(uint256 _amount) public { - _requireCallerIsBorrowerOperationsOrDefaultPool(); - uint256 newETHBalance = ETHBalance + _amount; ETHBalance = newETHBalance; From b4c480d9ef9de7cafcdde33af90e72ae57015e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Fri, 26 Apr 2024 08:35:32 +0100 Subject: [PATCH 3/5] Revert "fix: Remove duplicated access control" This reverts commit 7a8959838ae4acf74d75f2f86415bb5ad4c955ec. --- contracts/src/ActivePool.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index 76e539fa..e9d8a76b 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -167,6 +167,8 @@ contract ActivePool is Ownable, CheckContract, IActivePool { } function accountForReceivedETH(uint256 _amount) public { + _requireCallerIsBorrowerOperationsOrDefaultPool(); + uint256 newETHBalance = ETHBalance + _amount; ETHBalance = newETHBalance; From 6a8ebdef8b5bbc5747672fef5a2113cee3602272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Fri, 26 Apr 2024 09:06:32 +0100 Subject: [PATCH 4/5] fix: Remove duplicated access control --- contracts/src/ActivePool.sol | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index e9d8a76b..63a05cfd 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -163,12 +163,15 @@ contract ActivePool is Ownable, CheckContract, IActivePool { // Pull ETH tokens from sender ETH.safeTransferFrom(msg.sender, address(this), _amount); - accountForReceivedETH(_amount); + _accountForReceivedETH(_amount); } function accountForReceivedETH(uint256 _amount) public { _requireCallerIsBorrowerOperationsOrDefaultPool(); + _accountForReceivedETH(_amount); + } + function _accountForReceivedETH(uint256 _amount) internal { uint256 newETHBalance = ETHBalance + _amount; ETHBalance = newETHBalance; From c103c3b17f78254e5928dd76bd97d5c476c46f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Fri, 26 Apr 2024 09:09:24 +0100 Subject: [PATCH 5/5] fix: Keep order for events consistent in ActivePool --- contracts/src/ActivePool.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index 63a05cfd..948856ae 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -160,14 +160,15 @@ contract ActivePool is Ownable, CheckContract, IActivePool { function receiveETH(uint256 _amount) external { _requireCallerIsBorrowerOperationsOrDefaultPool(); + _accountForReceivedETH(_amount); + // Pull ETH tokens from sender ETH.safeTransferFrom(msg.sender, address(this), _amount); - - _accountForReceivedETH(_amount); } function accountForReceivedETH(uint256 _amount) public { _requireCallerIsBorrowerOperationsOrDefaultPool(); + _accountForReceivedETH(_amount); }