-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: withdrawal credentials #904
Open
mkurayan
wants to merge
24
commits into
develop
Choose a base branch
from
feat/withdrawal-credentials
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9b454a8
feat: add withdrawal credentials lib
mkurayan 3bfe5ac
feat: split full and partial withdrawals
mkurayan 4420a7c
feat: decouple fee allocation strategy from withdrawal request library
mkurayan 1a394bf
feat: rename triggerable withdrawals lib
mkurayan 5183e89
feat: add unit tests for triggerable withdrawals lib
mkurayan 2fc90ec
feat: add unit tests for triggerable withdrawals in the withdrawal va…
mkurayan 5888fac
feat: use lido locator instead of direct VEB address
mkurayan c251b90
feat: add access control to WithdrawalVault contract
mkurayan 9cf5ea4
Merge commit '49c97519e932f5084d4faa698f14b261befa055d' into feat/wit…
mkurayan 1b2dd97
refactor: remove unnecessary memory allocation
mkurayan d26dddc
feat: specify fee per request instead of total fee in TW library
mkurayan 66ccbcf
feat: tightly pack pubkeys
mkurayan 0f37e51
refactor: format code
mkurayan 6f303e5
refactor: move TriggerableWithdrawals lib from 0.8.9 to common
mkurayan ade67a7
refactor: improve naming for address validation utility
mkurayan 89d583a
test: add unit tests for Withdrawal Vault excess fee refund behavior
mkurayan cfadfb4
refactor: improve TriggerableWithdrawals lib methods description
mkurayan 9f268cf
refactor: triggerable withdrawals lib rename errors for clarity
mkurayan 811fdf8
refactor: describe full withdrawal method in withdrawal vault
mkurayan 6cdc711
Merge commit '2d11786ebc1f06165c501fa8e9fecef053e12ab9' into feat/wit…
mkurayan 6da1d6f
feat: grant withdrawal request role to ValidatorsExitBusOracle contra…
mkurayan 1af1d3a
feat: validate withdrawal fee response
mkurayan c27de34
feat: update eip-7002 contract address
mkurayan f99a141
Merge commit '1ef6b88a7241ab6d54fb5f7f224ab88568a0d4b9' into feat/wit…
mkurayan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2023 Lido <[email protected]> | ||
// SPDX-FileCopyrightText: 2025 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
/* See contracts/COMPILERS.md */ | ||
|
@@ -9,6 +9,9 @@ | |
import "@openzeppelin/contracts-v4.4/token/ERC20/utils/SafeERC20.sol"; | ||
|
||
import {Versioned} from "./utils/Versioned.sol"; | ||
import {AccessControlEnumerable} from "./utils/access/AccessControlEnumerable.sol"; | ||
import {TriggerableWithdrawals} from "../common/lib/TriggerableWithdrawals.sol"; | ||
import {ILidoLocator} from "../common/interfaces/ILidoLocator.sol"; | ||
|
||
interface ILido { | ||
/** | ||
|
@@ -22,12 +25,14 @@ | |
/** | ||
* @title A vault for temporary storage of withdrawals | ||
*/ | ||
contract WithdrawalVault is Versioned { | ||
contract WithdrawalVault is AccessControlEnumerable, Versioned { | ||
using SafeERC20 for IERC20; | ||
|
||
ILido public immutable LIDO; | ||
address public immutable TREASURY; | ||
|
||
bytes32 public constant ADD_FULL_WITHDRAWAL_REQUEST_ROLE = keccak256("ADD_FULL_WITHDRAWAL_REQUEST_ROLE"); | ||
|
||
// Events | ||
/** | ||
* Emitted when the ERC20 `token` recovered (i.e. transferred) | ||
|
@@ -42,34 +47,48 @@ | |
event ERC721Recovered(address indexed requestedBy, address indexed token, uint256 tokenId); | ||
|
||
// Errors | ||
error LidoZeroAddress(); | ||
error TreasuryZeroAddress(); | ||
error ZeroAddress(); | ||
error NotLido(); | ||
error NotEnoughEther(uint256 requested, uint256 balance); | ||
error ZeroAmount(); | ||
error InsufficientTriggerableWithdrawalFee( | ||
uint256 providedTotalFee, | ||
uint256 requiredTotalFee, | ||
uint256 requestCount | ||
); | ||
error TriggerableWithdrawalRefundFailed(); | ||
|
||
/** | ||
* @param _lido the Lido token (stETH) address | ||
* @param _treasury the Lido treasury address (see ERC20/ERC721-recovery interfaces) | ||
*/ | ||
constructor(ILido _lido, address _treasury) { | ||
if (address(_lido) == address(0)) { | ||
revert LidoZeroAddress(); | ||
} | ||
if (_treasury == address(0)) { | ||
revert TreasuryZeroAddress(); | ||
} | ||
constructor(address _lido, address _treasury) { | ||
_onlyNonZeroAddress(_lido); | ||
_onlyNonZeroAddress(_treasury); | ||
|
||
LIDO = _lido; | ||
LIDO = ILido(_lido); | ||
TREASURY = _treasury; | ||
} | ||
|
||
/** | ||
* @notice Initialize the contract explicitly. | ||
* Sets the contract version to '1'. | ||
*/ | ||
function initialize() external { | ||
_initializeContractVersionTo(1); | ||
/// @notice Initializes the contract. Can be called only once. | ||
/// @param _admin Lido DAO Aragon agent contract address. | ||
/// @dev Proxy initialization method. | ||
function initialize(address _admin) external { | ||
// Initializations for v0 --> v2 | ||
_checkContractVersion(0); | ||
|
||
_initialize_v2(_admin); | ||
_initializeContractVersionTo(2); | ||
} | ||
|
||
/// @notice Finalizes upgrade to v2 (from v1). Can be called only once. | ||
/// @param _admin Lido DAO Aragon agent contract address. | ||
function finalizeUpgrade_v2(address _admin) external { | ||
// Finalization for v1 --> v2 | ||
_checkContractVersion(1); | ||
|
||
_initialize_v2(_admin); | ||
_updateContractVersion(2); | ||
} | ||
|
||
/** | ||
|
@@ -122,4 +141,66 @@ | |
|
||
_token.transferFrom(address(this), TREASURY, _tokenId); | ||
} | ||
|
||
/** | ||
* @dev Submits EIP-7002 full withdrawal requests for the specified public keys. | ||
* Each request instructs a validator to fully withdraw its stake and exit its duties as a validator. | ||
* Refunds any excess fee to the caller after deducting the total fees, | ||
* which are calculated based on the number of public keys and the current minimum fee per withdrawal request. | ||
* | ||
* @param pubkeys A tightly packed array of 48-byte public keys corresponding to validators requesting full withdrawals. | ||
* | ----- public key (48 bytes) ----- || ----- public key (48 bytes) ----- | ... | ||
* | ||
* @notice Reverts if: | ||
* - The caller does not have the `ADD_FULL_WITHDRAWAL_REQUEST_ROLE`. | ||
* - Validation of any of the provided public keys fails. | ||
* - The provided total withdrawal fee is insufficient to cover all requests. | ||
* - Refund of the excess fee fails. | ||
*/ | ||
function addFullWithdrawalRequests( | ||
bytes calldata pubkeys | ||
) external payable onlyRole(ADD_FULL_WITHDRAWAL_REQUEST_ROLE) { | ||
uint256 prevBalance = address(this).balance - msg.value; | ||
|
||
uint256 minFeePerRequest = TriggerableWithdrawals.getWithdrawalRequestFee(); | ||
uint256 totalFee = (pubkeys.length / TriggerableWithdrawals.PUBLIC_KEY_LENGTH) * minFeePerRequest; | ||
|
||
if (totalFee > msg.value) { | ||
revert InsufficientTriggerableWithdrawalFee( | ||
msg.value, | ||
totalFee, | ||
pubkeys.length / TriggerableWithdrawals.PUBLIC_KEY_LENGTH | ||
); | ||
} | ||
|
||
TriggerableWithdrawals.addFullWithdrawalRequests(pubkeys, minFeePerRequest); | ||
|
||
uint256 refund = msg.value - totalFee; | ||
if (refund > 0) { | ||
(bool success, ) = msg.sender.call{value: refund}(""); | ||
|
||
if (!success) { | ||
revert TriggerableWithdrawalRefundFailed(); | ||
} | ||
} | ||
|
||
assert(address(this).balance == prevBalance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return the refund amount for the ease of integration? |
||
} | ||
|
||
/** | ||
* @dev Retrieves the current EIP-7002 withdrawal fee. | ||
* @return The minimum fee required per withdrawal request. | ||
*/ | ||
function getWithdrawalRequestFee() external view returns (uint256) { | ||
return TriggerableWithdrawals.getWithdrawalRequestFee(); | ||
} | ||
|
||
function _onlyNonZeroAddress(address _address) internal pure { | ||
if (_address == address(0)) revert ZeroAddress(); | ||
} | ||
|
||
function _initialize_v2(address _admin) internal { | ||
_onlyNonZeroAddress(_admin); | ||
_setupRole(DEFAULT_ADMIN_ROLE, _admin); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// SPDX-FileCopyrightText: 2025 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
/* See contracts/COMPILERS.md */ | ||
// solhint-disable-next-line lido/fixed-compiler-version | ||
pragma solidity >=0.8.9 <0.9.0; | ||
|
||
/** | ||
* @title A lib for EIP-7002: Execution layer triggerable withdrawals. | ||
* Allow validators to trigger withdrawals and exits from their execution layer (0x01) withdrawal credentials. | ||
*/ | ||
library TriggerableWithdrawals { | ||
address constant WITHDRAWAL_REQUEST = 0x00000961Ef480Eb55e80D19ad83579A64c007002; | ||
|
||
uint256 internal constant PUBLIC_KEY_LENGTH = 48; | ||
uint256 internal constant WITHDRAWAL_AMOUNT_LENGTH = 8; | ||
uint256 internal constant WITHDRAWAL_REQUEST_CALLDATA_LENGTH = 56; | ||
|
||
error WithdrawalFeeReadFailed(); | ||
error WithdrawalFeeInvalidData(); | ||
error WithdrawalRequestAdditionFailed(bytes callData); | ||
|
||
error InsufficientWithdrawalFee(uint256 feePerRequest, uint256 minFeePerRequest); | ||
error TotalWithdrawalFeeExceededBalance(uint256 balance, uint256 totalWithdrawalFee); | ||
|
||
error NoWithdrawalRequests(); | ||
error MalformedPubkeysArray(); | ||
error PartialWithdrawalRequired(uint256 index); | ||
error MismatchedArrayLengths(uint256 keysCount, uint256 amountsCount); | ||
|
||
/** | ||
* @dev Send EIP-7002 full withdrawal requests for the specified public keys. | ||
* Each request instructs a validator to fully withdraw its stake and exit its duties as a validator. | ||
* | ||
* @param pubkeys A tightly packed array of 48-byte public keys corresponding to validators requesting full withdrawals. | ||
* | ----- public key (48 bytes) ----- || ----- public key (48 bytes) ----- | ... | ||
* | ||
* @param feePerRequest The withdrawal fee for each withdrawal request. | ||
* - Must be greater than or equal to the current minimal withdrawal fee. | ||
* - If set to zero, the current minimal withdrawal fee will be used automatically. | ||
* | ||
* @notice Reverts if: | ||
* - Validation of the public keys fails. | ||
* - The provided fee per request is insufficient. | ||
* - The contract has an insufficient balance to cover the total fees. | ||
*/ | ||
function addFullWithdrawalRequests(bytes calldata pubkeys, uint256 feePerRequest) internal { | ||
uint256 keysCount = _validateAndCountPubkeys(pubkeys); | ||
feePerRequest = _validateAndAdjustFee(feePerRequest, keysCount); | ||
|
||
bytes memory callData = new bytes(56); | ||
|
||
for (uint256 i = 0; i < keysCount; i++) { | ||
_copyPubkeyToMemory(pubkeys, callData, i); | ||
|
||
(bool success, ) = WITHDRAWAL_REQUEST.call{value: feePerRequest}(callData); | ||
|
||
if (!success) { | ||
revert WithdrawalRequestAdditionFailed(callData); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @dev Send EIP-7002 partial withdrawal requests for the specified public keys with corresponding amounts. | ||
* Each request instructs a validator to partially withdraw its stake. | ||
* A partial withdrawal is any withdrawal where the amount is greater than zero, | ||
* allows withdrawal of any balance exceeding 32 ETH (e.g., if a validator has 35 ETH, up to 3 ETH can be withdrawn), | ||
* the protocol enforces a minimum balance of 32 ETH per validator, even if a higher amount is requested. | ||
* | ||
* @param pubkeys A tightly packed array of 48-byte public keys corresponding to validators requesting full withdrawals. | ||
* | ----- public key (48 bytes) ----- || ----- public key (48 bytes) ----- | ... | ||
* | ||
* @param amounts An array of corresponding partial withdrawal amounts for each public key. | ||
* | ||
* @param feePerRequest The withdrawal fee for each withdrawal request. | ||
* - Must be greater than or equal to the current minimal withdrawal fee. | ||
* - If set to zero, the current minimal withdrawal fee will be used automatically. | ||
* | ||
* @notice Reverts if: | ||
* - Validation of the public keys fails. | ||
* - The pubkeys and amounts length mismatch. | ||
* - Full withdrawal requested for any pubkeys (withdrawal amount = 0). | ||
* - The provided fee per request is insufficient. | ||
* - The contract has an insufficient balance to cover the total fees. | ||
*/ | ||
function addPartialWithdrawalRequests( | ||
bytes calldata pubkeys, | ||
uint64[] calldata amounts, | ||
uint256 feePerRequest | ||
) internal { | ||
for (uint256 i = 0; i < amounts.length; i++) { | ||
if (amounts[i] == 0) { | ||
revert PartialWithdrawalRequired(i); | ||
} | ||
} | ||
|
||
addWithdrawalRequests(pubkeys, amounts, feePerRequest); | ||
} | ||
|
||
/** | ||
* @dev Send EIP-7002 partial or full withdrawal requests for the specified public keys with corresponding amounts. | ||
* Each request instructs a validator to partially or fully withdraw its stake. | ||
|
||
* 1. A partial withdrawal is any withdrawal where the amount is greater than zero, | ||
* allows withdrawal of any balance exceeding 32 ETH (e.g., if a validator has 35 ETH, up to 3 ETH can be withdrawn), | ||
* the protocol enforces a minimum balance of 32 ETH per validator, even if a higher amount is requested. | ||
* | ||
* 2. A full withdrawal is a withdrawal where the amount is equal to zero, | ||
* allows to fully withdraw validator stake and exit its duties as a validator. | ||
* | ||
* @param pubkeys A tightly packed array of 48-byte public keys corresponding to validators requesting full withdrawals. | ||
* | ----- public key (48 bytes) ----- || ----- public key (48 bytes) ----- | ... | ||
* | ||
* @param amounts An array of corresponding partial withdrawal amounts for each public key. | ||
* | ||
* @param feePerRequest The withdrawal fee for each withdrawal request. | ||
* - Must be greater than or equal to the current minimal withdrawal fee. | ||
* - If set to zero, the current minimal withdrawal fee will be used automatically. | ||
* | ||
* @notice Reverts if: | ||
* - Validation of the public keys fails. | ||
* - The pubkeys and amounts length mismatch. | ||
* - The provided fee per request is insufficient. | ||
* - The contract has an insufficient balance to cover the total fees. | ||
*/ | ||
function addWithdrawalRequests(bytes calldata pubkeys, uint64[] calldata amounts, uint256 feePerRequest) internal { | ||
uint256 keysCount = _validateAndCountPubkeys(pubkeys); | ||
|
||
if (keysCount != amounts.length) { | ||
revert MismatchedArrayLengths(keysCount, amounts.length); | ||
} | ||
|
||
feePerRequest = _validateAndAdjustFee(feePerRequest, keysCount); | ||
|
||
bytes memory callData = new bytes(56); | ||
for (uint256 i = 0; i < keysCount; i++) { | ||
_copyPubkeyToMemory(pubkeys, callData, i); | ||
_copyAmountToMemory(callData, amounts[i]); | ||
|
||
(bool success, ) = WITHDRAWAL_REQUEST.call{value: feePerRequest}(callData); | ||
|
||
if (!success) { | ||
revert WithdrawalRequestAdditionFailed(callData); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @dev Retrieves the current EIP-7002 withdrawal fee. | ||
* @return The minimum fee required per withdrawal request. | ||
*/ | ||
function getWithdrawalRequestFee() internal view returns (uint256) { | ||
(bool success, bytes memory feeData) = WITHDRAWAL_REQUEST.staticcall(""); | ||
|
||
if (!success) { | ||
revert WithdrawalFeeReadFailed(); | ||
} | ||
|
||
if (feeData.length != 32) { | ||
revert WithdrawalFeeInvalidData(); | ||
} | ||
|
||
return abi.decode(feeData, (uint256)); | ||
} | ||
|
||
function _copyPubkeyToMemory(bytes calldata pubkeys, bytes memory target, uint256 keyIndex) private pure { | ||
assembly { | ||
calldatacopy(add(target, 32), add(pubkeys.offset, mul(keyIndex, PUBLIC_KEY_LENGTH)), PUBLIC_KEY_LENGTH) | ||
} | ||
} | ||
|
||
function _copyAmountToMemory(bytes memory target, uint64 amount) private pure { | ||
assembly { | ||
mstore(add(target, 80), shl(192, amount)) | ||
} | ||
} | ||
|
||
function _validateAndCountPubkeys(bytes calldata pubkeys) private pure returns (uint256) { | ||
if (pubkeys.length % PUBLIC_KEY_LENGTH != 0) { | ||
revert MalformedPubkeysArray(); | ||
} | ||
|
||
uint256 keysCount = pubkeys.length / PUBLIC_KEY_LENGTH; | ||
if (keysCount == 0) { | ||
revert NoWithdrawalRequests(); | ||
} | ||
|
||
return keysCount; | ||
} | ||
|
||
function _validateAndAdjustFee(uint256 feePerRequest, uint256 keysCount) private view returns (uint256) { | ||
uint256 minFeePerRequest = getWithdrawalRequestFee(); | ||
|
||
if (feePerRequest == 0) { | ||
feePerRequest = minFeePerRequest; | ||
} | ||
|
||
if (feePerRequest < minFeePerRequest) { | ||
revert InsufficientWithdrawalFee(feePerRequest, minFeePerRequest); | ||
} | ||
|
||
if (address(this).balance < feePerRequest * keysCount) { | ||
revert TotalWithdrawalFeeExceededBalance(address(this).balance, feePerRequest * keysCount); | ||
} | ||
|
||
return feePerRequest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make it a separate view func so that integrating contracts can re-use it?