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.
104 lines
3.6 KiB
104 lines
3.6 KiB
3 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
3 years ago
|
pragma solidity >=0.8.0;
|
||
3 years ago
|
|
||
3 years ago
|
// ============ Internal Imports ============
|
||
2 years ago
|
import {IInterchainGasPaymaster} from "../../interfaces/IInterchainGasPaymaster.sol";
|
||
3 years ago
|
// ============ External Imports ============
|
||
2 years ago
|
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @title InterchainGasPaymaster
|
||
3 years ago
|
* @notice Manages payments on a source chain to cover gas costs of relaying
|
||
|
* messages to destination chains.
|
||
3 years ago
|
*/
|
||
2 years ago
|
contract InterchainGasPaymaster is IInterchainGasPaymaster, OwnableUpgradeable {
|
||
3 years ago
|
// ============ Events ============
|
||
|
|
||
|
/**
|
||
|
* @notice Emitted when a payment is made for a message's gas costs.
|
||
2 years ago
|
* @param messageId The ID of the message to pay for.
|
||
2 years ago
|
* @param gasAmount The amount of destination gas paid for.
|
||
|
* @param payment The amount of native tokens paid.
|
||
3 years ago
|
*/
|
||
2 years ago
|
event GasPayment(
|
||
|
bytes32 indexed messageId,
|
||
|
uint256 gasAmount,
|
||
|
uint256 payment
|
||
|
);
|
||
3 years ago
|
|
||
|
// ============ Constructor ============
|
||
|
|
||
2 years ago
|
constructor() {
|
||
|
initialize(); // allows contract to be used without proxying
|
||
|
}
|
||
3 years ago
|
|
||
|
// ============ External Functions ============
|
||
|
|
||
2 years ago
|
function initialize() public initializer {
|
||
|
__Ownable_init();
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
3 years ago
|
* @notice Deposits msg.value as a payment for the relaying of a message
|
||
|
* to its destination chain.
|
||
2 years ago
|
* @dev Overpayment will result in a refund of native tokens to the _refundAddress.
|
||
|
* Callers should be aware that this may present reentrancy issues.
|
||
2 years ago
|
* @param _messageId The ID of the message to pay for.
|
||
2 years ago
|
* @param _destinationDomain The domain of the message's destination chain.
|
||
2 years ago
|
* @param _gasAmount The amount of destination gas to pay for. Currently unused.
|
||
|
* @param _refundAddress The address to refund any overpayment to. Currently unused.
|
||
3 years ago
|
*/
|
||
2 years ago
|
function payForGas(
|
||
|
bytes32 _messageId,
|
||
|
uint32 _destinationDomain,
|
||
|
uint256 _gasAmount,
|
||
|
address _refundAddress
|
||
|
) external payable override {
|
||
2 years ago
|
uint256 _requiredPayment = quoteGasPayment(
|
||
|
_destinationDomain,
|
||
|
_gasAmount
|
||
|
);
|
||
|
require(
|
||
|
msg.value >= _requiredPayment,
|
||
|
"insufficient interchain gas payment"
|
||
|
);
|
||
|
uint256 _overpayment = msg.value - _requiredPayment;
|
||
|
if (_overpayment > 0) {
|
||
|
(bool _success, ) = _refundAddress.call{value: _overpayment}("");
|
||
|
require(_success, "Interchain gas payment refund failed");
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
emit GasPayment(_messageId, _gasAmount, msg.value);
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
/**
|
||
|
* @notice Quotes the amount of native tokens to pay for interchain gas.
|
||
|
* @param _destinationDomain The domain of the message's destination chain.
|
||
|
* @param _gasAmount The amount of destination gas to pay for. Currently unused.
|
||
|
* @return The amount of native tokens required to pay for interchain gas.
|
||
|
*/
|
||
|
function quoteGasPayment(uint32 _destinationDomain, uint256 _gasAmount)
|
||
|
public
|
||
|
pure
|
||
|
override
|
||
|
returns (uint256)
|
||
|
{
|
||
|
// Silence compiler warning.
|
||
|
_destinationDomain;
|
||
|
_gasAmount;
|
||
|
// Charge a flat 1 wei fee.
|
||
|
// This is an intermediate step toward fully on-chain accurate gas payment quoting.
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Transfers the entire native token balance to the owner of the contract.
|
||
|
* @dev The owner must be able to receive native tokens.
|
||
|
*/
|
||
|
function claim() external {
|
||
|
// Transfer the entire balance to owner.
|
||
|
(bool success, ) = owner().call{value: address(this).balance}("");
|
||
3 years ago
|
require(success, "!transfer");
|
||
3 years ago
|
}
|
||
|
}
|