Skip to content

Commit

Permalink
fix: Save gas by refactoring ActivePool receiveETH
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Apr 24, 2024
1 parent 54105e9 commit 3080262
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
12 changes: 9 additions & 3 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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 3080262

Please sign in to comment.