账户
0x0b...96e0
0x0B...96e0

0x0B...96e0

$500
此合同的源代码已经过验证!
合同元数据
编译器
0.8.18+commit.87f61d96
语言
Solidity
合同源代码
文件 1 的 4:Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^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.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}
合同源代码
文件 2 的 4:GasOracle.sol
// SPDX-License-Identifier: MIT
pragma solidity ^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.
 */
contract GasOracle is Ownable, IGasOracle {
    struct ChainData {
        // price of the chain's native token in USD
        uint128 price;
        // price of a gas unit in the chain's native token with precision according to the const ORACLE_PRECISION
        uint128 gasPrice;
    }
    uint private constant ORACLE_PRECISION = 18;
    uint private constant ORACLE_SCALING_FACTOR = 10 ** ORACLE_PRECISION;
    // number to divide by to change precision from gas oracle price precision to chain precision
    uint private immutable fromOracleToChainScalingFactor;

    mapping(uint chainId => ChainData) public override chainData;
    // current chain ID
    uint public immutable override 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`).
     */
    function setChainData(uint chainId_, uint128 price_, uint128 gasPrice) external override onlyOwner {
        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.
     */
    function setPrice(uint chainId_, uint128 price_) external override onlyOwner {
        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`).
     */
    function setGasPrice(uint chainId_, uint128 gasPrice) external override onlyOwner {
        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
     */
    function getTransactionGasCostInNativeToken(
        uint otherChainId,
        uint gasAmount
    ) external view override returns (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`
     */
    function getTransactionGasCostInUSD(uint otherChainId, uint gasAmount) external view override returns (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.
     */
    function crossRate(uint otherChainId) external view override returns (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
     */
    function price(uint chainId_) external view override returns (uint) {
        return chainData[chainId_].price;
    }

    fallback() external payable {
        revert("Unsupported");
    }

    receive() external payable {
        revert("Unsupported");
    }
}
合同源代码
文件 3 的 4:IGasOracle.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

interface IGasOracle {
    function chainData(uint chainId) external view returns (uint128 price, uint128 gasPrice);

    function chainId() external view returns (uint);

    function crossRate(uint otherChainId) external view returns (uint);

    function getTransactionGasCostInNativeToken(uint otherChainId, uint256 gasAmount) external view returns (uint);

    function getTransactionGasCostInUSD(uint otherChainId, uint256 gasAmount) external view returns (uint);

    function price(uint chainId) external view returns (uint);

    function setChainData(uint chainId, uint128 price, uint128 gasPrice) external;

    function setGasPrice(uint chainId, uint128 gasPrice) external;

    function setPrice(uint chainId, uint128 price) external;
}
合同源代码
文件 4 的 4:Ownable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^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.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed 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.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        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.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        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) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
设置
{
  "compilationTarget": {
    "contracts/GasOracle.sol": "GasOracle"
  },
  "evmVersion": "paris",
  "libraries": {},
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "enabled": true,
    "runs": 1000
  },
  "remappings": []
}
ABI
[{"inputs":[{"internalType":"uint256","name":"chainId_","type":"uint256"},{"internalType":"uint256","name":"chainPrecision","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"chainData","outputs":[{"internalType":"uint128","name":"price","type":"uint128"},{"internalType":"uint128","name":"gasPrice","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"chainId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"otherChainId","type":"uint256"}],"name":"crossRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"otherChainId","type":"uint256"},{"internalType":"uint256","name":"gasAmount","type":"uint256"}],"name":"getTransactionGasCostInNativeToken","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"otherChainId","type":"uint256"},{"internalType":"uint256","name":"gasAmount","type":"uint256"}],"name":"getTransactionGasCostInUSD","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId_","type":"uint256"}],"name":"price","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId_","type":"uint256"},{"internalType":"uint128","name":"price_","type":"uint128"},{"internalType":"uint128","name":"gasPrice","type":"uint128"}],"name":"setChainData","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId_","type":"uint256"},{"internalType":"uint128","name":"gasPrice","type":"uint128"}],"name":"setGasPrice","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId_","type":"uint256"},{"internalType":"uint128","name":"price_","type":"uint128"}],"name":"setPrice","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]