You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.0 KiB
140 lines
4.0 KiB
1 year ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.8.0;
|
||
|
|
||
|
/*@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
|
@@@@@ HYPERLANE @@@@@@@
|
||
|
@@@@@@@@@@@@@@@@@@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@@
|
||
|
@@@@@@@@@ @@@@@@@@*/
|
||
|
|
||
|
// ============ Internal Imports ============
|
||
|
import {Message} from "../libs/Message.sol";
|
||
1 year ago
|
import {StandardHookMetadata} from "./libs/StandardHookMetadata.sol";
|
||
|
import {AbstractPostDispatchHook} from "./libs/AbstractPostDispatchHook.sol";
|
||
1 year ago
|
import {IPostDispatchHook} from "../interfaces/hooks/IPostDispatchHook.sol";
|
||
1 year ago
|
|
||
1 year ago
|
// ============ External Imports ============
|
||
|
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
|
||
|
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
||
|
|
||
|
/**
|
||
12 months ago
|
* @title ProtocolFee
|
||
1 year ago
|
* @notice Collects a static protocol fee from the sender.
|
||
|
*/
|
||
12 months ago
|
contract ProtocolFee is AbstractPostDispatchHook, Ownable {
|
||
1 year ago
|
using StandardHookMetadata for bytes;
|
||
1 year ago
|
using Address for address payable;
|
||
|
using Message for bytes;
|
||
|
|
||
|
// ============ Constants ============
|
||
|
|
||
|
/// @notice The maximum protocol fee that can be set.
|
||
|
uint256 public immutable MAX_PROTOCOL_FEE;
|
||
|
|
||
|
// ============ Public Storage ============
|
||
|
|
||
|
/// @notice The current protocol fee.
|
||
|
uint256 public protocolFee;
|
||
|
/// @notice The beneficiary of protocol fees.
|
||
|
address public beneficiary;
|
||
|
|
||
|
// ============ Constructor ============
|
||
|
|
||
|
constructor(
|
||
|
uint256 _maxProtocolFee,
|
||
|
uint256 _protocolFee,
|
||
|
address _beneficiary,
|
||
|
address _owner
|
||
|
) {
|
||
|
MAX_PROTOCOL_FEE = _maxProtocolFee;
|
||
|
_setProtocolFee(_protocolFee);
|
||
|
_setBeneficiary(_beneficiary);
|
||
|
_transferOwnership(_owner);
|
||
|
}
|
||
|
|
||
|
// ============ External Functions ============
|
||
|
|
||
1 year ago
|
/// @inheritdoc IPostDispatchHook
|
||
|
function hookType() external pure override returns (uint8) {
|
||
|
return uint8(IPostDispatchHook.Types.PROTOCOL_FEE);
|
||
|
}
|
||
|
|
||
1 year ago
|
/**
|
||
|
* @notice Sets the protocol fee.
|
||
|
* @param _protocolFee The new protocol fee.
|
||
|
*/
|
||
|
function setProtocolFee(uint256 _protocolFee) external onlyOwner {
|
||
|
_setProtocolFee(_protocolFee);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Sets the beneficiary of protocol fees.
|
||
|
* @param _beneficiary The new beneficiary.
|
||
|
*/
|
||
|
function setBeneficiary(address _beneficiary) external onlyOwner {
|
||
|
_setBeneficiary(_beneficiary);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Collects protocol fees from the contract.
|
||
|
*/
|
||
|
function collectProtocolFees() external {
|
||
|
payable(beneficiary).sendValue(address(this).balance);
|
||
|
}
|
||
|
|
||
|
// ============ Internal Functions ============
|
||
|
|
||
1 year ago
|
/// @inheritdoc AbstractPostDispatchHook
|
||
1 year ago
|
function _postDispatch(
|
||
|
bytes calldata metadata,
|
||
|
bytes calldata message
|
||
|
) internal override {
|
||
1 year ago
|
require(
|
||
|
msg.value >= protocolFee,
|
||
12 months ago
|
"ProtocolFee: insufficient protocol fee"
|
||
1 year ago
|
);
|
||
|
|
||
|
uint256 refund = msg.value - protocolFee;
|
||
1 year ago
|
if (refund > 0) {
|
||
1 year ago
|
payable(metadata.refundAddress(message.senderAddress())).sendValue(
|
||
|
refund
|
||
|
);
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
/// @inheritdoc AbstractPostDispatchHook
|
||
1 year ago
|
function _quoteDispatch(
|
||
|
bytes calldata,
|
||
|
bytes calldata
|
||
|
) internal view override returns (uint256) {
|
||
1 year ago
|
return protocolFee;
|
||
|
}
|
||
|
|
||
1 year ago
|
/**
|
||
|
* @notice Sets the protocol fee.
|
||
|
* @param _protocolFee The new protocol fee.
|
||
|
*/
|
||
|
function _setProtocolFee(uint256 _protocolFee) internal {
|
||
|
require(
|
||
|
_protocolFee <= MAX_PROTOCOL_FEE,
|
||
12 months ago
|
"ProtocolFee: exceeds max protocol fee"
|
||
1 year ago
|
);
|
||
|
protocolFee = _protocolFee;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Sets the beneficiary of protocol fees.
|
||
|
* @param _beneficiary The new beneficiary.
|
||
|
*/
|
||
|
function _setBeneficiary(address _beneficiary) internal {
|
||
12 months ago
|
require(_beneficiary != address(0), "ProtocolFee: invalid beneficiary");
|
||
1 year ago
|
beneficiary = _beneficiary;
|
||
|
}
|
||
|
}
|