// SPDX-License-Identifier: LZBL-1.2 pragma solidity ^0.8.20; import { Pausable } from "@openzeppelin/contracts/security/Pausable.sol"; import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol"; import { ISendLib } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ISendLib.sol"; import { Transfer } from "@layerzerolabs/lz-evm-protocol-v2/contracts/libs/Transfer.sol"; import { IWorker } from "./interfaces/IWorker.sol"; abstract contract Worker is AccessControl, Pausable, IWorker { bytes32 internal constant MESSAGE_LIB_ROLE = keccak256("MESSAGE_LIB_ROLE"); bytes32 internal constant ALLOWLIST = keccak256("ALLOWLIST"); bytes32 internal constant DENYLIST = keccak256("DENYLIST"); bytes32 internal constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); address public workerFeeLib; uint64 public allowlistSize; uint16 public defaultMultiplierBps; address public priceFeed; mapping(uint32 eid => uint8[] optionTypes) internal supportedOptionTypes; // ========================= Constructor ========================= /// @param _messageLibs array of message lib addresses that are granted the MESSAGE_LIB_ROLE /// @param _priceFeed price feed address /// @param _defaultMultiplierBps default multiplier for worker fee /// @param _roleAdmin address that is granted the DEFAULT_ADMIN_ROLE (can grant and revoke all roles) /// @param _admins array of admin addresses that are granted the ADMIN_ROLE constructor( address[] memory _messageLibs, address _priceFeed, uint16 _defaultMultiplierBps, address _roleAdmin, address[] memory _admins ) { defaultMultiplierBps = _defaultMultiplierBps; priceFeed = _priceFeed; if (_roleAdmin != address(0x0)) { _grantRole(DEFAULT_ADMIN_ROLE, _roleAdmin); // _roleAdmin can grant and revoke all roles } for (uint256 i = 0; i < _messageLibs.length; ++i) { _grantRole(MESSAGE_LIB_ROLE, _messageLibs[i]); } for (uint256 i = 0; i < _admins.length; ++i) { _grantRole(ADMIN_ROLE, _admins[i]); } } // ========================= Modifier ========================= modifier onlyAcl(address _sender) { if (!hasAcl(_sender)) { revert Worker_NotAllowed(); } _; } /// @dev Access control list using allowlist and denylist /// @dev 1) if one address is in the denylist -> deny /// @dev 2) else if address in the allowlist OR allowlist is empty (allows everyone)-> allow /// @dev 3) else deny /// @param _sender address to check function hasAcl(address _sender) public view returns (bool) { if (hasRole(DENYLIST, _sender)) { return false; } else if (allowlistSize == 0 || hasRole(ALLOWLIST, _sender)) { return true; } else { return false; } } // ========================= OnyDefaultAdmin ========================= /// @dev flag to pause execution of workers (if used with whenNotPaused modifier) /// @param _paused true to pause, false to unpause function setPaused(bool _paused) external onlyRole(DEFAULT_ADMIN_ROLE) { if (_paused) { _pause(); } else { _unpause(); } } // ========================= OnlyAdmin ========================= /// @param _priceFeed price feed address function setPriceFeed(address _priceFeed) external onlyRole(ADMIN_ROLE) { priceFeed = _priceFeed; emit SetPriceFeed(_priceFeed); } /// @param _workerFeeLib worker fee lib address function setWorkerFeeLib(address _workerFeeLib) external onlyRole(ADMIN_ROLE) { workerFeeLib = _workerFeeLib; emit SetWorkerLib(_workerFeeLib); } /// @param _multiplierBps default multiplier for worker fee function setDefaultMultiplierBps(uint16 _multiplierBps) external onlyRole(ADMIN_ROLE) { defaultMultiplierBps = _multiplierBps; emit SetDefaultMultiplierBps(_multiplierBps); } /// @dev supports withdrawing fee from ULN301, ULN302 and more /// @param _lib message lib address /// @param _to address to withdraw fee to /// @param _amount amount to withdraw function withdrawFee(address _lib, address _to, uint256 _amount) external onlyRole(ADMIN_ROLE) { if (!hasRole(MESSAGE_LIB_ROLE, _lib)) revert Worker_OnlyMessageLib(); ISendLib(_lib).withdrawFee(_to, _amount); emit Withdraw(_lib, _to, _amount); } /// @dev supports withdrawing token from the contract /// @param _token token address /// @param _to address to withdraw token to /// @param _amount amount to withdraw function withdrawToken(address _token, address _to, uint256 _amount) external onlyRole(ADMIN_ROLE) { // transfers native if _token is address(0x0) Transfer.nativeOrToken(_token, _to, _amount); } function setSupportedOptionTypes(uint32 _eid, uint8[] calldata _optionTypes) external onlyRole(ADMIN_ROLE) { supportedOptionTypes[_eid] = _optionTypes; } // ========================= View Functions ========================= function getSupportedOptionTypes(uint32 _eid) external view returns (uint8[] memory) { return supportedOptionTypes[_eid]; } // ========================= Internal Functions ========================= /// @dev overrides AccessControl to allow for counting of allowlistSize /// @param _role role to grant /// @param _account address to grant role to function _grantRole(bytes32 _role, address _account) internal override { if (_role == ALLOWLIST && !hasRole(_role, _account)) { ++allowlistSize; } super._grantRole(_role, _account); } /// @dev overrides AccessControl to allow for counting of allowlistSize /// @param _role role to revoke /// @param _account address to revoke role from function _revokeRole(bytes32 _role, address _account) internal override { if (_role == ALLOWLIST && hasRole(_role, _account)) { --allowlistSize; } super._revokeRole(_role, _account); } /// @dev overrides AccessControl to disable renouncing of roles function renounceRole(bytes32 /*role*/, address /*account*/) public pure override { revert Worker_RoleRenouncingDisabled(); } }