// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 2 of 9: GasOracle.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.18;import {Ownable} from"@openzeppelin/contracts/access/Ownable.sol";
import {IGasOracle} from"./interfaces/IGasOracle.sol";
/**
* @title GasOracle
* @dev A contract that provides gas price and native token USD price data on other blockchains.
*/contractGasOracleisOwnable, IGasOracle{
structChainData {
// price of the chain's native token in USDuint128 price;
// price of a gas unit in the chain's native token with precision according to the const ORACLE_PRECISIONuint128 gasPrice;
}
uintprivateconstant ORACLE_PRECISION =18;
uintprivateconstant ORACLE_SCALING_FACTOR =10** ORACLE_PRECISION;
// number to divide by to change precision from gas oracle price precision to chain precisionuintprivateimmutable fromOracleToChainScalingFactor;
mapping(uint chainId => ChainData) publicoverride chainData;
// current chain IDuintpublicimmutableoverride chainId;
constructor(uint chainId_, uint chainPrecision) {
chainId = chainId_;
fromOracleToChainScalingFactor =10** (ORACLE_PRECISION - chainPrecision);
}
/**
* @notice Sets the chain data for a given chain ID.
* @param chainId_ The ID of the given chain to set data for.
* @param price_ The price of the given chain's native token in USD.
* @param gasPrice The price of a gas unit in the given chain's native token (with precision according to the const
* `ORACLE_PRECISION`).
*/functionsetChainData(uint chainId_, uint128 price_, uint128 gasPrice) externaloverrideonlyOwner{
chainData[chainId_].price = price_;
chainData[chainId_].gasPrice = gasPrice;
}
/**
* @notice Sets only the price for a given chain ID.
* @param chainId_ The ID of the given chain to set the price for.
* @param price_ The price of the given chain's native token in USD.
*/functionsetPrice(uint chainId_, uint128 price_) externaloverrideonlyOwner{
chainData[chainId_].price = price_;
}
/**
* @notice Sets only the gas price for a given chain ID.
* @param chainId_ The ID of the given chain to set the gas price for.
* @param gasPrice The price of a gas unit in the given chain's native token (with precision according to the const
* `ORACLE_PRECISION`).
*/functionsetGasPrice(uint chainId_, uint128 gasPrice) externaloverrideonlyOwner{
chainData[chainId_].gasPrice = gasPrice;
}
/**
* @notice Calculates the gas cost of a transaction on another chain in the current chain's native token.
* @param otherChainId The ID of the chain for which to get the gas cost.
* @param gasAmount The amount of gas used in a transaction.
* @return The gas cost of a transaction in the current chain's native token
*/functiongetTransactionGasCostInNativeToken(uint otherChainId,
uint gasAmount
) externalviewoverridereturns (uint) {
return
(chainData[otherChainId].gasPrice * gasAmount * chainData[otherChainId].price) /
chainData[chainId].price /
fromOracleToChainScalingFactor;
}
/**
* @notice Calculates the gas cost of a transaction on another chain in USD.
* @param otherChainId The ID of the chain for which to get the gas cost.
* @param gasAmount The amount of gas used in a transaction.
* @return The gas cost of a transaction in USD with precision of `ORACLE_PRECISION`
*/functiongetTransactionGasCostInUSD(uint otherChainId, uint gasAmount) externalviewoverridereturns (uint) {
return (chainData[otherChainId].gasPrice * gasAmount * chainData[otherChainId].price) / ORACLE_SCALING_FACTOR;
}
/**
* @notice Get the cross-rate between the two chains' native tokens.
* @param otherChainId The ID of the other chain to get the cross-rate for.
*/functioncrossRate(uint otherChainId) externalviewoverridereturns (uint) {
return (chainData[otherChainId].price * ORACLE_SCALING_FACTOR) / chainData[chainId].price;
}
/**
* @notice Get the price of a given chain's native token in USD.
* @param chainId_ The ID of the given chain to get the price.
* @return the price of the given chain's native token in USD with precision of const ORACLE_PRECISION
*/functionprice(uint chainId_) externalviewoverridereturns (uint) {
return chainData[chainId_].price;
}
fallback() externalpayable{
revert("Unsupported");
}
receive() externalpayable{
revert("Unsupported");
}
}
Contract Source Code
File 3 of 9: GasUsage.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.18;import {Ownable} from"@openzeppelin/contracts/access/Ownable.sol";
import {IGasOracle} from"./interfaces/IGasOracle.sol";
/**
* @dev Contract module which allows children to store typical gas usage of a certain transaction on another chain.
*/abstractcontractGasUsageisOwnable{
IGasOracle internal gasOracle;
mapping(uint chainId =>uint amount) public gasUsage;
constructor(IGasOracle gasOracle_) {
gasOracle = gasOracle_;
}
/**
* @dev Sets the amount of gas used for a transaction on a given chain.
* @param chainId The ID of the chain.
* @param gasAmount The amount of gas used on the chain.
*/functionsetGasUsage(uint chainId, uint gasAmount) externalonlyOwner{
gasUsage[chainId] = gasAmount;
}
/**
* @dev Sets the Gas Oracle contract address.
* @param gasOracle_ The address of the Gas Oracle contract.
*/functionsetGasOracle(IGasOracle gasOracle_) externalonlyOwner{
gasOracle = gasOracle_;
}
/**
* @notice Get the gas cost of a transaction on another chain in the current chain's native token.
* @param chainId The ID of the chain for which to get the gas cost.
* @return The calculated gas cost of the transaction in the current chain's native token
*/functiongetTransactionCost(uint chainId) externalviewreturns (uint) {
unchecked {
return gasOracle.getTransactionGasCostInNativeToken(chainId, gasUsage[chainId]);
}
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)pragmasolidity ^0.8.0;import"../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/abstractcontractOwnableisContext{
addressprivate _owner;
eventOwnershipTransferred(addressindexed previousOwner, addressindexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/function_checkOwner() internalviewvirtual{
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/functionrenounceOwnership() publicvirtualonlyOwner{
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
require(newOwner !=address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/function_transferOwnership(address newOwner) internalvirtual{
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}