Address io1mscd5k42tayvwy2kx5ert2jdwvpx8hf3v2w5nm

Contract Overview

Balance:
0 IOTX

IOTX Value:
$ 0

Token:
Txn Hash
Block
From
To
Value [Txn Fee]
Parent Txn Hash Block From To Value
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
ChainlinkWrapper

Compiler Version
v0.6.4+commit.1dca32f3

Optimization Enabled:
Yes with 200 runs

Other Settings:
constantinople evmVersion, None license

Contract Source Code (Solidity)

pragma solidity 0.6.4;
pragma experimental ABIEncoderV2;


// SPDX-License-Identifier: MIT
library EthAddressLib {
    /**
     * @dev returns the address used within the protocol to identify ETH
     * @return the address assigned to ETH
     */
    function ethAddress() internal pure returns (address) {
        return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
    }
}

// SPDX-License-Identifier: MIT
interface IPriceOracles {
    function get(address token) external view returns (uint256, bool);
}

// SPDX-License-Identifier: MIT
/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount)
        external
        returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender)
        external
        view
        returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    function decimals() external view returns (uint8);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

// SPDX-License-Identifier: MIT
/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts with custom message when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }

    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a <= b ? a : b;
    }

    function abs(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a < b) {
            return b - a;
        }
        return a - b;
    }
}

// SPDX-License-Identifier: MIT
/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies in extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly {
            size := extcodesize(account)
        }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(
            address(this).balance >= amount,
            "Address: insufficient balance"
        );

        // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
        (bool success, ) = recipient.call{value: amount}("");
        require(
            success,
            "Address: unable to send value, recipient may have reverted"
        );
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain`call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data)
        internal
        returns (bytes memory)
    {
        return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return _functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return
            functionCallWithValue(
                target,
                data,
                value,
                "Address: low-level call with value failed"
            );
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(
            address(this).balance >= value,
            "Address: insufficient balance for call"
        );
        return _functionCallWithValue(target, data, value, errorMessage);
    }

    function _functionCallWithValue(
        address target,
        bytes memory data,
        uint256 weiValue,
        string memory errorMessage
    ) private returns (bytes memory) {
        require(isContract(target), "Address: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.call{value: weiValue}(
            data
        );
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                // solhint-disable-next-line no-inline-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// SPDX-License-Identifier: MIT
/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using SafeMath for uint256;
    using Address for address;

    function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.transfer.selector, to, value)
        );
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.transferFrom.selector, from, to, value)
        );
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        // solhint-disable-next-line max-line-length
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.approve.selector, spender, value)
        );
    }

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender).add(
            value
        );
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(
                token.approve.selector,
                spender,
                newAllowance
            )
        );
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender).sub(
            value,
            "SafeERC20: decreased allowance below zero"
        );
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(
                token.approve.selector,
                spender,
                newAllowance
            )
        );
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(
            data,
            "SafeERC20: low-level call failed"
        );
        if (returndata.length > 0) {
            // Return data is optional
            // solhint-disable-next-line max-line-length
            require(
                abi.decode(returndata, (bool)),
                "SafeERC20: ERC20 operation did not succeed"
            );
        }
    }
}

/**
 * @title Initializable
 *
 * @dev Helper contract to support initializer functions. To use it, replace
 * the constructor with a function that has the `initializer` modifier.
 * WARNING: Unlike constructors, initializer functions must be manually
 * invoked. This applies both to deploying an Initializable contract, as well
 * as extending an Initializable contract via inheritance.
 * WARNING: When used with inheritance, manual care must be taken to not invoke
 * a parent initializer twice, or ensure that all initializers are idempotent,
 * because this is not dealt with automatically as with constructors.
 */
contract Initializable {

  /**
   * @dev Indicates that the contract has been initialized.
   */
  bool private initialized;

  /**
   * @dev Indicates that the contract is in the process of being initialized.
   */
  bool private initializing;

  /**
   * @dev Modifier to use in the initializer function of a contract.
   */
  modifier initializer() {
    require(initializing || isConstructor() || !initialized, "Contract instance has already been initialized");

    bool isTopLevelCall = !initializing;
    if (isTopLevelCall) {
      initializing = true;
      initialized = true;
    }

    _;

    if (isTopLevelCall) {
      initializing = false;
    }
  }

  /// @dev Returns true if and only if the function is running in the constructor
  function isConstructor() private view returns (bool) {
    // extcodesize checks the size of the code stored in an address, and
    // address returns the current address. Since the code is still not
    // deployed when running a constructor, any checks on its code size will
    // yield zero, making it an effective way to detect if a contract is
    // under construction or not.
    address self = address(this);
    uint256 cs;
    assembly { cs := extcodesize(self) }
    return cs == 0;
  }

  // Reserved storage space to allow for layout changes in the future.
  uint256[50] private ______gap;
}

// SPDX-License-Identifier: MIT
// chainlink 价格合约接口
interface AggregatorInterface {
    function latestAnswer() external view returns (int256);
    function latestRoundData()
        external
        view
        returns (
            uint80 roundId,
            int256 answer, //
            uint256 startedAt,
            uint256 updatedAt, //
            uint80 answeredInRound
        );
}

contract ChainlinkWrapper is Initializable, IPriceOracles {
    using SafeERC20 for IERC20;
    using SafeMath for uint256;

    address public multiSig;
    address public admin;
    
    // ETH网络:存储 ETH/USD 交易对合约地址, 其他xxx_ETH合约通过ETH/USD进行中转计算
    address public ethToUsdPriceOracle;
    // 维护需要从chainlink取价格的token 地址 => chainlink 价格合约地址的映射
    mapping(address => address) public tokenChainlinkMap;

    struct Price {
        uint price;
        uint expiration;
    }

    mapping (address => Price) public prices;

    mapping (address => bool) public directTokenMap;//直接调用 token=> true, other false
    
    //BNB网络:存储BNB/USD 交易对合约地址, 其他xxx_BNB合约通过BNB/USD进行中转计算
    address public bnbToUsdPriceOracle;
    //MATIC网络:存储MATIC/USD 交易对合约地址, 其他xxx_MATIC合约通过MATIC/USD进行中转计算
    //nativeToUsdPriceOrace作为evm兼容链的原生token语言机,例如matic,
    address public nativeToUsdPriceOracle;

    event SetTokenChainlinkMap(address token, address chainlink);
    event SetTokenChainlinkMaps(address[] tokens, address[] chainlinks);

    event SetDirectTokenMap(address token, bool direct);
    event SetDirectTokenMaps(address[] tokens, bool direct);


    function initialize(address _multiSig)
        public
        initializer
    {
        multiSig = _multiSig;
        admin = msg.sender;
    }

    // constructor(address _multiSig) public
    // {
    //     multiSig = _multiSig;
    //     admin = msg.sender;
    // }

    receive() external payable {}

    modifier onlyAdmin {
        require(msg.sender == admin, "require admin");
        _;
    }

    modifier onlyMultiSig {
        require(msg.sender == multiSig, "require multiSig");
        _;
    }

    function setMultiSig(address _multiSig) external onlyMultiSig {
        multiSig = _multiSig;
    }

    function setAdmin(address _admin) external onlyMultiSig {
        admin = _admin;
    }

    function isEthOrBnB(address token) public view returns (bool) {
        if (token == address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
             || token == address(0)
             || token == address(0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB)) {
            return true;
        }
        return false;
    }

    function get(address token) external override view returns (uint256, bool) {
        if (tokenChainlinkMap[token] != address(0)) {
            return getChainLinkPrice(token);
        }
        
        return (0, false);
    }

    function setEthToUsdPriceOracle(address _ethToUsdPriceOracle) external onlyAdmin {
        ethToUsdPriceOracle = _ethToUsdPriceOracle;
    }

    function setBnbToUsdPriceOracle(address _bnbToUsdPriceOracle) external onlyAdmin {
        bnbToUsdPriceOracle = _bnbToUsdPriceOracle;
    }

    function setNativeToUsdPriceOracle(address _nativeToUsdPriceOracle) external onlyAdmin {
        nativeToUsdPriceOracle = _nativeToUsdPriceOracle;
    }

    function setTokenChainlinkMap(address token, address chainlink)
        public
        onlyAdmin
    {
        tokenChainlinkMap[token] = chainlink;
        emit SetTokenChainlinkMap(token, chainlink);
    }

    function setTokenChainlinkMaps(address[] calldata tokens, address[] calldata chainlinks)
        external
        onlyAdmin
    {
        require(tokens.length == chainlinks.length, "inconsistent length");
        for (uint i = 0; i < tokens.length; i++) {
            setTokenChainlinkMap(tokens[i], chainlinks[i]);
        }
        emit SetTokenChainlinkMaps(tokens, chainlinks);
    }

    function setDirectTokenMap(address token, bool direct) external onlyAdmin {
        directTokenMap[token] = direct;

        emit SetDirectTokenMap(token, direct);
    }

    function setDirectTokenMaps(address[] calldata tokens, bool direct) external onlyAdmin {
        for (uint i = 0; i < tokens.length; i++) {
            directTokenMap[tokens[i]] = direct;
        }

        emit SetDirectTokenMaps(tokens, direct);
    }

    function getChainLinkPrice(address token)
        public
        view
        returns (uint256, bool)
    {
        // // 未设置中转,返回无效价格
        // if (ethToUsdPriceOracle == address(0) && bnbToUsdPriceOracle == address(0)) {
        //     return (0, false);
        // }

        // // 同时设置,返回失败
        // if (ethToUsdPriceOracle != address(0) && bnbToUsdPriceOracle != address(0)) {
        //     return (0, false);
        // }

        // address referenceOracle = address(0);
        // if (ethToUsdPriceOracle != address(0) && bnbToUsdPriceOracle == address(0)) {
        //     referenceOracle = ethToUsdPriceOracle;
        // }

        // if (ethToUsdPriceOracle == address(0) && bnbToUsdPriceOracle != address(0)) {
        //     referenceOracle = bnbToUsdPriceOracle;
        // }

        if (nativeToUsdPriceOracle == address(0)) {
            return (0, false);
        }
        address referenceOracle = nativeToUsdPriceOracle;

        // 构造 chainlink 合约实例
        AggregatorInterface chainlinkContract = AggregatorInterface(
            referenceOracle
        );
        // 获取 ETH/USD 交易对的价格,单位是 1e8
        int256 basePrice = chainlinkContract.latestAnswer();
        // 若要获取 ETH 的价格,则返回 1e8 * 1e10 = 1e18
        // ETH上的ETH,BSC的BNB,直接调用chainlink,或者设置的直接调用的token,直接调用chainlink
        if (isEthOrBnB(token)) {
            return (uint256(basePrice).mul(1e10), true);
        }

        if (directTokenMap[token]) {
            // 构造 chainlink 合约实例
            chainlinkContract = AggregatorInterface(tokenChainlinkMap[token]);
            // 获取 XXX/USD 交易对的价格,单位是 1e8
            basePrice = chainlinkContract.latestAnswer();
            return (uint256(basePrice).mul(1e10), true);
        }

        // // 获取 token/ETH 交易对的价格(目前是 USDT 和 USDC ),单位是 1e18
        // FIXME: matic网络无xxx_matic交易对
        chainlinkContract = AggregatorInterface(tokenChainlinkMap[token]);
        int256 tokenPrice = chainlinkContract.latestAnswer();
        return (uint256(basePrice).mul(uint256(tokenPrice)).div(1e8), true);
    }

    function getLatestRoundData(address token)
        public
        view
        returns (uint256, uint256)
    {
        // // 未设置中转,返回无效价格
        // if (ethToUsdPriceOracle == address(0) && bnbToUsdPriceOracle == address(0)) {
        //     return (0, 0);
        // }

        // // 同时设置,返回失败
        // if (ethToUsdPriceOracle != address(0) && bnbToUsdPriceOracle != address(0)) {
        //     return (0, 0);
        // }

        // address referenceOracle = address(0);
        
        // if (ethToUsdPriceOracle != address(0) && bnbToUsdPriceOracle == address(0)) {
        //     referenceOracle = ethToUsdPriceOracle;
        // }

        // if (ethToUsdPriceOracle == address(0) && bnbToUsdPriceOracle != address(0)) {
        //     referenceOracle = bnbToUsdPriceOracle;
        // }

        if (nativeToUsdPriceOracle == address(0)) {
            return (0, 0);
        }
        address referenceOracle = nativeToUsdPriceOracle;

        // 构造 chainlink 合约实例
        AggregatorInterface chainlinkContract = AggregatorInterface(
            referenceOracle
        );
        // 获取 ETH/USD 交易对的价格,单位是 1e8
        int256 basePrice = chainlinkContract.latestAnswer();

        (,,, uint256 updatedAt,) = chainlinkContract.latestRoundData();


        // 若要获取 ETH 的价格,则返回 1e8 * 1e10 = 1e18
        // ETH上的ETH,BSC的BNB,直接调用chainlink,或者设置的直接调用的token,直接调用chainlink
        if (isEthOrBnB(token)) {
            return (uint256(basePrice).mul(1e10), updatedAt);
        }

        if (directTokenMap[token]) {
            // 构造 chainlink 合约实例
            chainlinkContract = AggregatorInterface(tokenChainlinkMap[token]);
    
            (,,,updatedAt,) = chainlinkContract.latestRoundData();

            // 获取 XXX/USD 交易对的价格,单位是 1e8
            basePrice = chainlinkContract.latestAnswer();
            return (uint256(basePrice).mul(1e10), updatedAt);
        }

        // // 获取 token/ETH 交易对的价格(目前是 USDT 和 USDC ),单位是 1e18
        // FIXME: matic网络无xxx_matic交易对
        chainlinkContract = AggregatorInterface(tokenChainlinkMap[token]);
        int256 tokenPrice = chainlinkContract.latestAnswer();

        (,,, uint256 updatedAt1,) = chainlinkContract.latestRoundData();
        
        uint256 latest = updatedAt1 > updatedAt ? updatedAt1 : updatedAt;

        return (uint256(basePrice).mul(uint256(tokenPrice)).div(1e8), latest);
    }

    struct ChainlinkPrice {
        uint256 px;
        uint256 updatedAt;
    }

    function gets(address[] calldata tokens) external view returns (ChainlinkPrice[] memory) {
        uint n = tokens.length;
        ChainlinkPrice[] memory res = new ChainlinkPrice[](n);
        for (uint i = 0; i < n; i++) {
            (res[i].px, res[i].updatedAt) = getLatestRoundData(tokens[i]);
        }

        return res;
    }
}

Contract ABI

[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"bool","name":"direct","type":"bool"}],"name":"SetDirectTokenMap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address[]","name":"tokens","type":"address[]"},{"indexed":false,"internalType":"bool","name":"direct","type":"bool"}],"name":"SetDirectTokenMaps","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"address","name":"chainlink","type":"address"}],"name":"SetTokenChainlinkMap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address[]","name":"tokens","type":"address[]"},{"indexed":false,"internalType":"address[]","name":"chainlinks","type":"address[]"}],"name":"SetTokenChainlinkMaps","type":"event"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bnbToUsdPriceOracle","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"directTokenMap","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ethToUsdPriceOracle","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"getChainLinkPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"getLatestRoundData","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"}],"name":"gets","outputs":[{"components":[{"internalType":"uint256","name":"px","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"}],"internalType":"struct ChainlinkWrapper.ChainlinkPrice[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_multiSig","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"isEthOrBnB","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"multiSig","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nativeToUsdPriceOracle","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"prices","outputs":[{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"expiration","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_admin","type":"address"}],"name":"setAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_bnbToUsdPriceOracle","type":"address"}],"name":"setBnbToUsdPriceOracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"bool","name":"direct","type":"bool"}],"name":"setDirectTokenMap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"bool","name":"direct","type":"bool"}],"name":"setDirectTokenMaps","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_ethToUsdPriceOracle","type":"address"}],"name":"setEthToUsdPriceOracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_multiSig","type":"address"}],"name":"setMultiSig","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_nativeToUsdPriceOracle","type":"address"}],"name":"setNativeToUsdPriceOracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"chainlink","type":"address"}],"name":"setTokenChainlinkMap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"address[]","name":"chainlinks","type":"address[]"}],"name":"setTokenChainlinkMaps","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenChainlinkMap","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]

Contract Creation Code

6080604052600436106101445760003560e01c8063a2d29cd0116100b6578063cfed246b1161006f578063cfed246b1461039d578063e0c09c7d146103bd578063f851a440146103dd578063f890477a146103f2578063f96a652514610407578063ff28ca60146104275761014b565b8063a2d29cd0146102da578063a42b6890146102ef578063a725fe3d1461030f578063c2bc2efc1461032f578063c4d66de81461035d578063cca4bc711461037d5761014b565b806336e0004a1161010857806336e0004a146102155780634662d55a146102375780636e37eef414610257578063704b6c0214610277578063725e6f0a146102975780637715795c146102c55761014b565b806309cce06f146101505780632096560714610172578063284d30ef146101a857806336592773146101c857806336ba0dfc146101e85761014b565b3661014b57005b600080fd5b34801561015c57600080fd5b5061017061016b3660046112c5565b610447565b005b34801561017e57600080fd5b5061019261018d36600461134f565b61049c565b60405161019f91906115a3565b60405180910390f35b3480156101b457600080fd5b506101706101c33660046112c5565b610556565b3480156101d457600080fd5b506101706101e33660046113f8565b6105a2565b3480156101f457600080fd5b506102086102033660046112c5565b61066e565b60405161019f91906115f2565b34801561022157600080fd5b5061022a610683565b60405161019f9190611502565b34801561024357600080fd5b506101706102523660046112e0565b610692565b34801561026357600080fd5b506101706102723660046112c5565b610729565b34801561028357600080fd5b506101706102923660046112c5565b610775565b3480156102a357600080fd5b506102b76102b23660046112c5565b6107c1565b60405161019f92919061176d565b3480156102d157600080fd5b5061022a610ba2565b3480156102e657600080fd5b5061022a610bb1565b3480156102fb57600080fd5b5061022a61030a3660046112c5565b610bc0565b34801561031b57600080fd5b5061017061032a36600461138f565b610bdb565b34801561033b57600080fd5b5061034f61034a3660046112c5565b610cc0565b60405161019f92919061175d565b34801561036957600080fd5b506101706103783660046112c5565b610d01565b34801561038957600080fd5b506101706103983660046112c5565b610dae565b3480156103a957600080fd5b506102b76103b83660046112c5565b610dfa565b3480156103c957600080fd5b506101706103d8366004611314565b610e13565b3480156103e957600080fd5b5061022a610e93565b3480156103fe57600080fd5b5061022a610ea2565b34801561041357600080fd5b506102086104223660046112c5565b610eb1565b34801561043357600080fd5b5061034f6104423660046112c5565b610f22565b6034546001600160a01b0316331461047a5760405162461bcd60e51b815260040161047190611709565b60405180910390fd5b603580546001600160a01b0319166001600160a01b0392909216919091179055565b604080518281526020808402820101909152606090829082908280156104dc57816020015b6104c9611231565b8152602001906001900390816104c15790505b50905060005b8281101561054b5761050e8686838181106104f957fe5b90506020020160208101906102b291906112c5565b83838151811061051a57fe5b602002602001015160000184848151811061053157fe5b6020908102919091018101510191909152526001016104e2565b509150505b92915050565b6033546001600160a01b031633146105805760405162461bcd60e51b815260040161047190611691565b603380546001600160a01b0319166001600160a01b0392909216919091179055565b6034546001600160a01b031633146105cc5760405162461bcd60e51b815260040161047190611709565b60005b8281101561062d5781603860008686858181106105e857fe5b90506020020160208101906105fd91906112c5565b6001600160a01b031681526020810191909152604001600020805460ff19169115159190911790556001016105cf565b507f3394b1cd2be9e98def887fe040f8f4ba95e45eb008fcea1d978b5dac9fe635868383836040516106619392919061157d565b60405180910390a1505050565b60386020526000908152604090205460ff1681565b6033546001600160a01b031681565b6034546001600160a01b031633146106bc5760405162461bcd60e51b815260040161047190611709565b6001600160a01b038281166000908152603660205260409081902080546001600160a01b03191692841692909217909155517f2a872aeaad6a9f83db27f14f84454c3e5865b7c85ff6ff12b6bbbe73b10c1d1d9061071d9084908490611516565b60405180910390a15050565b6034546001600160a01b031633146107535760405162461bcd60e51b815260040161047190611709565b603980546001600160a01b0319166001600160a01b0392909216919091179055565b6033546001600160a01b0316331461079f5760405162461bcd60e51b815260040161047190611691565b603480546001600160a01b0319166001600160a01b0392909216919091179055565b603a5460009081906001600160a01b03166107e157506000905080610b9d565b603a54604080516350d25bcd60e01b815290516001600160a01b0390921691829160009183916350d25bcd916004808301926020929190829003018186803b15801561082c57600080fd5b505afa158015610840573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108649190611451565b90506000826001600160a01b031663feaf968c6040518163ffffffff1660e01b815260040160a06040518083038186803b1580156108a157600080fd5b505afa1580156108b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108d99190611469565b5093505050506108e887610eb1565b1561090f57610902826402540be40063ffffffff61117116565b95509350610b9d92505050565b6001600160a01b03871660009081526038602052604090205460ff1615610a53576001600160a01b0380881660009081526036602052604090819020548151633fabe5a360e21b8152915192169450849163feaf968c9160048082019260a092909190829003018186803b15801561098657600080fd5b505afa15801561099a573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109be9190611469565b50604080516350d25bcd60e01b815290519195506001600160a01b03881694506350d25bcd93506004808201935060209291829003018186803b158015610a0457600080fd5b505afa158015610a18573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a3c9190611451565b9150610902826402540be40063ffffffff61117116565b6001600160a01b0380881660009081526036602090815260408083205481516350d25bcd60e01b8152915194169650919286926350d25bcd926004808301939192829003018186803b158015610aa857600080fd5b505afa158015610abc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ae09190611451565b90506000846001600160a01b031663feaf968c6040518163ffffffff1660e01b815260040160a06040518083038186803b158015610b1d57600080fd5b505afa158015610b31573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b559190611469565b5093505050506000838211610b6a5783610b6c565b815b9050610b926305f5e100610b86878663ffffffff61117116565b9063ffffffff6111b216565b985096505050505050505b915091565b603a546001600160a01b031681565b6035546001600160a01b031681565b6036602052600090815260409020546001600160a01b031681565b6034546001600160a01b03163314610c055760405162461bcd60e51b815260040161047190611709565b828114610c245760405162461bcd60e51b815260040161047190611730565b60005b83811015610c7c57610c74858583818110610c3e57fe5b9050602002016020810190610c5391906112c5565b848484818110610c5f57fe5b905060200201602081019061025291906112c5565b600101610c27565b507f1b5e8cd48b0b5a553c1d15cbe99a2da7a5932ba4dba913e59da01ec387b11f0284848484604051610cb2949392919061154b565b60405180910390a150505050565b6001600160a01b03818116600090815260366020526040812054909182911615610cf657610ced83610f22565b91509150610b9d565b506000905080915091565b600054610100900460ff1680610d1a5750610d1a6111f4565b80610d28575060005460ff16155b610d445760405162461bcd60e51b8152600401610471906116bb565b600054610100900460ff16158015610d6f576000805460ff1961ff0019909116610100171660011790555b603380546001600160a01b0384166001600160a01b03199182161790915560348054909116331790558015610daa576000805461ff00191690555b5050565b6034546001600160a01b03163314610dd85760405162461bcd60e51b815260040161047190611709565b603a80546001600160a01b0319166001600160a01b0392909216919091179055565b6037602052600090815260409020805460019091015482565b6034546001600160a01b03163314610e3d5760405162461bcd60e51b815260040161047190611709565b6001600160a01b03821660009081526038602052604090819020805460ff1916831515179055517fc7381df9565beecc370039b2f031e56935754a478a83a43d288a6651337cc2e09061071d9084908490611530565b6034546001600160a01b031681565b6039546001600160a01b031681565b60006001600160a01b03821673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee1480610ee557506001600160a01b038216155b80610f0c57506001600160a01b03821673bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb145b15610f1957506001610f1d565b5060005b919050565b603a5460009081906001600160a01b0316610f4257506000905080610b9d565b603a54604080516350d25bcd60e01b815290516001600160a01b0390921691829160009183916350d25bcd916004808301926020929190829003018186803b158015610f8d57600080fd5b505afa158015610fa1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610fc59190611451565b9050610fd086610eb1565b15610ff857610fea816402540be40063ffffffff61117116565b600194509450505050610b9d565b6001600160a01b03861660009081526038602052604090205460ff16156110bc576001600160a01b038087166000908152603660209081526040918290205482516350d25bcd60e01b815292519316945084926350d25bcd926004808201939291829003018186803b15801561106d57600080fd5b505afa158015611081573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906110a59190611451565b9050610fea816402540be40063ffffffff61117116565b6001600160a01b0380871660009081526036602090815260408083205481516350d25bcd60e01b8152915194169550919285926350d25bcd926004808301939192829003018186803b15801561111157600080fd5b505afa158015611125573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111499190611451565b90506111636305f5e100610b86848463ffffffff61117116565b976001975095505050505050565b60008261118057506000610550565b8282028284828161118d57fe5b04146111ab5760405162461bcd60e51b815260040161047190611650565b9392505050565b60006111ab83836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f0000000000008152506111fa565b303b1590565b6000818361121b5760405162461bcd60e51b815260040161047191906115fd565b50600083858161122757fe5b0495945050505050565b604051806040016040528060008152602001600081525090565b80356001600160a01b038116811461055057600080fd5b60008083601f840112611273578182fd5b50813567ffffffffffffffff81111561128a578182fd5b60208301915083602080830285010111156112a457600080fd5b9250929050565b805169ffffffffffffffffffff8116811461055057600080fd5b6000602082840312156112d6578081fd5b6111ab838361124b565b600080604083850312156112f2578081fd5b6112fc848461124b565b915061130b846020850161124b565b90509250929050565b60008060408385031215611326578182fd5b611330848461124b565b915060208301358015158114611344578182fd5b809150509250929050565b60008060208385031215611361578182fd5b823567ffffffffffffffff811115611377578283fd5b61138385828601611262565b90969095509350505050565b600080600080604085870312156113a4578182fd5b843567ffffffffffffffff808211156113bb578384fd5b6113c788838901611262565b909650945060208701359150808211156113df578384fd5b506113ec87828801611262565b95989497509550505050565b60008060006040848603121561140c578283fd5b833567ffffffffffffffff811115611422578384fd5b61142e86828701611262565b90945092505060208401358015158114611446578182fd5b809150509250925092565b600060208284031215611462578081fd5b5051919050565b600080600080600060a08688031215611480578081fd5b61148a87876112ab565b94506020860151935060408601519250606086015191506114ae87608088016112ab565b90509295509295909350565b60008284526020808501945082825b858110156114f7578183016001600160a01b036114e6828561124b565b1688529683019691506001016114c9565b509495945050505050565b6001600160a01b0391909116815260200190565b6001600160a01b0392831681529116602082015260400190565b6001600160a01b039290921682521515602082015260400190565b60006040825261155f6040830186886114ba565b82810360208401526115728185876114ba565b979650505050505050565b6000604082526115916040830185876114ba565b90508215156020830152949350505050565b602080825282518282018190526000919060409081850190868401855b828110156115e5578151805185528601518685015292840192908501906001016115c0565b5091979650505050505050565b901515815260200190565b6000602080835283518082850152825b818110156116295785810183015185820160400152820161160d565b8181111561163a5783604083870101525b50601f01601f1916929092016040019392505050565b60208082526021908201527f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f6040820152607760f81b606082015260800190565b60208082526010908201526f72657175697265206d756c746953696760801b604082015260600190565b6020808252602e908201527f436f6e747261637420696e7374616e63652068617320616c726561647920626560408201526d195b881a5b9a5d1a585b1a5e995960921b606082015260800190565b6020808252600d908201526c3932b8bab4b9329030b236b4b760991b604082015260600190565b6020808252601390820152720d2dcc6dedce6d2e6e8cadce840d8cadccee8d606b1b604082015260600190565b9182521515602082015260400190565b91825260208201526040019056fea2646970667358221220b29d89c2824152d45476091b484375961efd88743f9fa498013cfb9f4cd7132564736f6c63430006040033

Block Transaction Gas Used Reward
Age Block Fee Address BC Fee Address Voting Power Jailed Incoming
Block Uncle Number Difficulty Gas Used Reward
Loading
Loading
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.