Skip to content

Commit

Permalink
feat: stop deposits flag
Browse files Browse the repository at this point in the history
  • Loading branch information
0xvv committed Apr 14, 2023
1 parent 3aa6576 commit aad23d5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/contracts/StakingContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ contract StakingContract {
error InvalidCall();
error Unauthorized();
error DepositFailure();
error DepositsStopped();
error InvalidArgument();
error UnsortedIndexes();
error InvalidPublicKeys();
Expand Down Expand Up @@ -58,6 +59,7 @@ contract StakingContract {
event ChangedGlobalFee(uint256 newGlobalFee);
event ChangedOperatorFee(uint256 newOperatorFee);
event ChangedAdmin(address newAdmin);
event ChangedDepositsStopped(bool isStopped);
event NewOperator(address operatorAddress, address feeRecipientAddress, uint256 index);
event ChangedOperatorAddresses(uint256 operatorIndex, address operatorAddress, address feeRecipientAddress);
event DeactivatedOperator(uint256 _operatorIndex);
Expand Down Expand Up @@ -268,6 +270,11 @@ contract StakingContract {
StakingContractStorageLib.getWithdrawnMap().value[_publicKeyRoot] = true;
}

/// @notice Returns false if the users can deposit, true if deposits are stopped
function getDepositsStopped() external view returns (bool) {
return StakingContractStorageLib.getDepositStopped();
}

/// @notice Retrieve operator details
/// @param _operatorIndex Operator index
function getOperator(uint256 _operatorIndex)
Expand Down Expand Up @@ -677,6 +684,12 @@ contract StakingContract {
}
}

/// @notice Utility to stop or allow deposits
function setDepositsStopped(bool val) external onlyAdmin {
emit ChangedDepositsStopped(val);
StakingContractStorageLib.setDepositStopped(val);
}

/// ██ ███ ██ ████████ ███████ ██████ ███ ██ █████ ██
/// ██ ████ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
/// ██ ██ ██ ██ ██ █████ ██████ ██ ██ ██ ███████ ██
Expand Down Expand Up @@ -1010,6 +1023,9 @@ contract StakingContract {
}

function _deposit(address _withdrawer) internal {
if (StakingContractStorageLib.getDepositStopped()) {
revert DepositsStopped();
}
if (msg.value == 0 || msg.value % DEPOSIT_SIZE != 0) {
revert InvalidDepositValue();
}
Expand Down
14 changes: 14 additions & 0 deletions src/contracts/libs/StakingContractStorageLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,18 @@ library StakingContractStorageLib {
function setOperatorCommissionLimit(uint256 value) internal {
setUint256(OPERATOR_COMMISSION_LIMIT_SLOT, value);
}

/* ========================================
===========================================
=========================================*/

bytes32 internal constant DEPOSIT_STOPPED_SLOT = keccak256("StakingContract.depositStopped");

function getDepositStopped() internal view returns (bool) {
return getBool(DEPOSIT_STOPPED_SLOT);
}

function setDepositStopped(bool val) internal {
setBool(DEPOSIT_STOPPED_SLOT, val);
}
}
28 changes: 28 additions & 0 deletions src/test/StakingContract.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,34 @@ contract StakingContractOneValidatorTest is Test {
assert(stakingContract.getGlobalFee() == 1000);
}

event ChangedDepositsStopped(bool isStopped);

function testSetDepositStopped() public {
address staker = makeAddr("staker");
vm.deal(staker, 64 ether);
assert(stakingContract.getDepositsStopped() == false);
vm.prank(staker);
stakingContract.deposit{value: 32 ether}();

vm.expectEmit(true, true, true, true);
emit ChangedDepositsStopped(true);
vm.startPrank(admin);
stakingContract.setDepositsStopped(true);
vm.stopPrank();

assert(stakingContract.getDepositsStopped() == true);
vm.expectRevert(abi.encodeWithSignature("DepositsStopped()"));
vm.prank(staker);
stakingContract.deposit{value: 32 ether}();
}

function testSetDepositStopped_asRandom() public {
assert(stakingContract.getDepositsStopped() == false);
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
vm.startPrank(address(0x31337));
stakingContract.setDepositsStopped(true);
}

function testFeeRecipients() public {
bytes
memory publicKey = hex"21d2e725aef3a8f9e09d8f4034948bb7f79505fc7c40e7a7ca15734bad4220a594bf0c6257cef7db88d9fc3fd4360759";
Expand Down

0 comments on commit aad23d5

Please sign in to comment.