Skip to content
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

add forward fee allocator #71

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions contracts/ZkLink.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ contract ZkLink is
/// @dev Used to indicate that eth withdrawal was already processed
mapping(uint256 l2BatchNumber => mapping(uint256 l2ToL1MessageNumber => bool isFinalized))
public isEthWithdrawalFinalized;
/// @dev The forward fee allocator
address public forwardFeeAllocator;

/// @notice Gateway init
event InitGateway(IL2Gateway gateway);
Expand All @@ -93,11 +95,13 @@ contract ZkLink is
/// @notice Emitted when receive l2 tx hash from primary chain.
event SyncL2TxHash(bytes32 l2TxHash, bytes32 primaryChainL2TxHash);
/// @notice Emitted when validator withdraw forward fee
event WithdrawForwardFee(uint256 amount);
event WithdrawForwardFee(address receiver, uint256 amount);
/// @notice Emitted when the withdrawal is finalized on L1 and funds are released.
/// @param to The address to which the funds were sent
/// @param amount The amount of funds that were sent
event EthWithdrawalFinalized(address indexed to, uint256 amount);
/// @notice Forward fee allocator changed
event ForwardFeeAllocatorUpdate(address oldAllocator, address newAllocator);

/// @notice Check if msg sender is gateway
modifier onlyGateway() {
Expand All @@ -111,6 +115,12 @@ contract ZkLink is
_;
}

/// @notice Checks if msg sender is forward fee allocator
modifier onlyForwardFeeAllocator() {
require(msg.sender == forwardFeeAllocator, "Not forward fee allocator");
_;
}

constructor(bool _isEthGasToken) {
IS_ETH_GAS_TOKEN = _isEthGasToken;
_disableInitializers();
Expand Down Expand Up @@ -199,6 +209,14 @@ contract ZkLink is
emit NewFeeParams(oldFeeParams, _newFeeParams);
}

/// @dev Update the forward fee allocator
function setForwardFeeAllocator(address _newForwardFeeAllocator) external onlyOwner {
require(_newForwardFeeAllocator != address(0), "Invalid allocator");
address oldAllocator = forwardFeeAllocator;
forwardFeeAllocator = _newForwardFeeAllocator;
emit ForwardFeeAllocatorUpdate(oldAllocator, _newForwardFeeAllocator);
}

function l2TransactionBaseCost(
uint256 _gasPrice,
uint256 _l2GasLimit,
Expand Down Expand Up @@ -418,17 +436,17 @@ contract ZkLink is
emit SyncL2TxHash(_l2TxHash, _primaryChainL2TxHash);
}

function withdrawForwardFee(uint256 _amount) external nonReentrant onlyValidator {
function withdrawForwardFee(address _receiver, uint256 _amount) external nonReentrant onlyForwardFeeAllocator {
require(_amount > 0, "Invalid amount");
uint256 newWithdrawnFee = totalValidatorForwardFeeWithdrawn + _amount;
require(totalValidatorForwardFee >= newWithdrawnFee, "Withdraw exceed");

// Update withdrawn fee
totalValidatorForwardFeeWithdrawn = newWithdrawnFee;
// solhint-disable-next-line avoid-low-level-calls
(bool success, ) = msg.sender.call{value: _amount}("");
(bool success, ) = _receiver.call{value: _amount}("");
require(success, "Withdraw failed");
emit WithdrawForwardFee(_amount);
emit WithdrawForwardFee(_receiver, _amount);
}

/// @notice Derives the price for L2 gas in ETH to be paid.
Expand Down
Loading