Skip to content

Commit

Permalink
Merge pull request #129 from liquity/gas_refactor_receive_eth
Browse files Browse the repository at this point in the history
fix: Save gas by refactoring ActivePool `receiveETH`
  • Loading branch information
bingen committed May 1, 2024
2 parents df31435 + c103c3b commit 0e9786f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
14 changes: 12 additions & 2 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,21 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
function receiveETH(uint256 _amount) external {
_requireCallerIsBorrowerOperationsOrDefaultPool();

uint256 newETHBalance = ETHBalance + _amount;
ETHBalance = newETHBalance;
_accountForReceivedETH(_amount);

// Pull ETH tokens from sender
ETH.safeTransferFrom(msg.sender, address(this), _amount);
}

function accountForReceivedETH(uint256 _amount) public {
_requireCallerIsBorrowerOperationsOrDefaultPool();

_accountForReceivedETH(_amount);
}

function _accountForReceivedETH(uint256 _amount) internal {
uint256 newETHBalance = ETHBalance + _amount;
ETHBalance = newETHBalance;

emit ActivePoolETHBalanceUpdated(newETHBalance);
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,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(
Expand Down
1 change: 1 addition & 0 deletions contracts/src/Interfaces/IActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 0e9786f

Please sign in to comment.