Skip to content

Commit

Permalink
feat(contracts): add withdrawTo to ValidatorPool contract
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHansLee committed Mar 21, 2024
1 parent b5621b2 commit 42455c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/contracts/contracts/L1/ValidatorPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ contract ValidatorPool is ReentrancyGuardUpgradeable, ISemver {
require(success, "ValidatorPool: ETH transfer failed");
}

/**
* @notice Withdraw a given amount.
*
* @param _to Address to withdraw asset to.
* @param _amount Amount to withdraw.
*/
function withdrawTo(address _to, uint256 _amount) external nonReentrant {
require(_to != address(0), "ValidatorPool: cannot withdraw to the zero address");

_decreaseBalance(msg.sender, _amount);

bool success = SafeCall.call(_to, gasleft(), _amount, "");
require(success, "ValidatorPool: ETH transfer failed");
}

/**
* @notice Bond asset corresponding to the given output index.
* This function is called when submitting output.
Expand Down
27 changes: 27 additions & 0 deletions packages/contracts/contracts/test/ValidatorPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ contract ValidatorPoolTest is L2OutputOracle_Initializer {
assertEq(pool.validatorCount(), count - 1);
}

function test_withdraw_to_succeeds() external {
test_deposit_succeeds();

address nextValidator = pool.nextValidator();
uint256 deposits = pool.balanceOf(nextValidator);

uint256 prevNextValidatorBalance = nextValidator.balance;
uint256 prevChallengerBalance = challenger.balance;

vm.prank(nextValidator);
pool.withdrawTo(challenger, deposits);
assertEq(pool.balanceOf(nextValidator), 0);
assertEq(nextValidator.balance, prevNextValidatorBalance);
assertEq(challenger.balance, prevChallengerBalance + deposits);
}

function test_withdraw_to_zero_address_reverts() external {
test_deposit_succeeds();

address nextValidator = pool.nextValidator();
uint256 deposits = pool.balanceOf(nextValidator);

vm.prank(nextValidator);
vm.expectRevert("ValidatorPool: cannot withdraw to the zero address");
pool.withdrawTo(address(0), deposits);
}

function test_withdraw_maintainValidatorEligibility_succeeds() external {
uint256 trustedBalance = trusted.balance;
uint256 depositAmount = requiredBondAmount * 2;
Expand Down

0 comments on commit 42455c1

Please sign in to comment.