ChainlinkConversionPath

Description:

Smart contract deployed on Ethereum with Factory, Oracle features.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "@openzeppelin/contracts/utils/Context.sol": {
      "content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (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;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
"
    },
    "contracts/ChainlinkConversionPath.sol": {
      "content": "// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.0;\r
\r
import "./legacy_openzeppelin/contracts/access/roles/WhitelistAdminRole.sol";\r
\r
interface ERC20fraction {\r
    function decimals() external view returns (uint8);\r
}\r
\r
interface AggregatorFraction {\r
    function decimals() external view returns (uint8);\r
\r
    function latestAnswer() external view returns (int256);\r
\r
    function latestTimestamp() external view returns (uint256);\r
}\r
\r
/**\r
 * @title ChainlinkConversionPath\r
 *\r
 * @notice ChainlinkConversionPath is a contract computing currency conversion rates based on Chainlink aggretators\r
 */\r
contract ChainlinkConversionPath is WhitelistAdminRole {\r
    uint256 constant PRECISION = 1e18;\r
    uint256 constant NATIVE_TOKEN_DECIMALS = 18;\r
    uint256 constant FIAT_DECIMALS = 8;\r
    address public nativeTokenHash;\r
\r
    /**\r
     * @param _nativeTokenHash hash of the native token\r
     */\r
    constructor(address _nativeTokenHash) {\r
        nativeTokenHash = _nativeTokenHash;\r
    }\r
\r
    // Mapping of Chainlink aggregators (input currency => output currency => contract address)\r
    // input & output currencies are the addresses of the ERC20 contracts OR the sha3("currency code")\r
    mapping(address => mapping(address => address)) public allAggregators;\r
\r
    // declare a new aggregator\r
    event AggregatorUpdated(\r
        address _input,\r
        address _output,\r
        address _aggregator\r
    );\r
\r
    /**\r
     * @notice Gets native token hash\r
     */\r
    function getNativeTokenHashAddress() external view returns (address data) {\r
        return nativeTokenHash;\r
    }\r
\r
    /**\r
     * @notice Update an aggregator\r
     * @param _input address representing the input currency\r
     * @param _output address representing the output currency\r
     * @param _aggregator address of the aggregator contract\r
     */\r
    function updateAggregator(\r
        address _input,\r
        address _output,\r
        address _aggregator\r
    ) external onlyWhitelistAdmin {\r
        allAggregators[_input][_output] = _aggregator;\r
        emit AggregatorUpdated(_input, _output, _aggregator);\r
    }\r
\r
    /**\r
     * @notice Update a list of aggregators\r
     * @param _inputs list of addresses representing the input currencies\r
     * @param _outputs list of addresses representing the output currencies\r
     * @param _aggregators list of addresses of the aggregator contracts\r
     */\r
    function updateAggregatorsList(\r
        address[] calldata _inputs,\r
        address[] calldata _outputs,\r
        address[] calldata _aggregators\r
    ) external onlyWhitelistAdmin {\r
        require(\r
            _inputs.length == _outputs.length,\r
            "arrays must have the same length"\r
        );\r
        require(\r
            _inputs.length == _aggregators.length,\r
            "arrays must have the same length"\r
        );\r
\r
        // For every conversions of the path\r
        for (uint256 i; i < _inputs.length; i++) {\r
            allAggregators[_inputs[i]][_outputs[i]] = _aggregators[i];\r
            emit AggregatorUpdated(_inputs[i], _outputs[i], _aggregators[i]);\r
        }\r
    }\r
\r
    /**\r
     * @notice Computes the conversion of an amount through a list of intermediate conversions\r
     * @param _amountIn Amount to convert\r
     * @param _path List of addresses representing the currencies for the intermediate conversions\r
     * @return result The result after all the conversions\r
     * @return oldestRateTimestamp The oldest timestamp of the path\r
     */\r
    function getConversion(\r
        uint256 _amountIn,\r
        address[] calldata _path\r
    ) external view returns (uint256 result, uint256 oldestRateTimestamp) {\r
        (uint256 rate, uint256 timestamp, uint256 decimals) = getRate(_path);\r
\r
        // initialize the result\r
        result = (_amountIn * rate) / decimals;\r
\r
        oldestRateTimestamp = timestamp;\r
    }\r
\r
    /**\r
     * @notice Computes the conversion rate from a list of currencies\r
     * @param _path List of addresses representing the currencies for the conversions\r
     * @return rate The rate\r
     * @return oldestRateTimestamp The oldest timestamp of the path\r
     * @return decimals of the conversion rate\r
     */\r
    function getRate(\r
        address[] memory _path\r
    )\r
        public\r
        view\r
        returns (uint256 rate, uint256 oldestRateTimestamp, uint256 decimals)\r
    {\r
        // initialize the result with 18 decimals (for more precision)\r
        rate = PRECISION;\r
        decimals = PRECISION;\r
        oldestRateTimestamp = block.timestamp;\r
\r
        // For every conversion of the path\r
        for (uint256 i; i < _path.length - 1; i++) {\r
            (\r
                AggregatorFraction aggregator,\r
                bool reverseAggregator,\r
                uint256 decimalsInput,\r
                uint256 decimalsOutput\r
            ) = getAggregatorAndDecimals(_path[i], _path[i + 1]);\r
\r
            // store the latest timestamp of the path\r
            uint256 currentTimestamp = aggregator.latestTimestamp();\r
            if (currentTimestamp < oldestRateTimestamp) {\r
                oldestRateTimestamp = currentTimestamp;\r
            }\r
\r
            // get the rate of the current step\r
            uint256 currentRate = uint256(aggregator.latestAnswer());\r
            // get the number of decimals of the current rate\r
            uint256 decimalsAggregator = uint256(aggregator.decimals());\r
\r
            // mul with the difference of decimals before the current rate computation (for more precision)\r
            if (decimalsAggregator > decimalsInput) {\r
                rate = rate * (10 ** (decimalsAggregator - decimalsInput));\r
            }\r
            if (decimalsAggregator < decimalsOutput) {\r
                rate = rate * (10 ** (decimalsOutput - decimalsAggregator));\r
            }\r
\r
            // Apply the current rate (if path uses an aggregator in the reverse way, div instead of mul)\r
            if (reverseAggregator) {\r
                rate = (rate * (10 ** decimalsAggregator)) / currentRate;\r
            } else {\r
                rate = (rate * currentRate) / (10 ** decimalsAggregator);\r
            }\r
\r
            // div with the difference of decimals AFTER the current rate computation (for more precision)\r
            if (decimalsAggregator < decimalsInput) {\r
                rate = rate / (10 ** (decimalsInput - decimalsAggregator));\r
            }\r
            if (decimalsAggregator > decimalsOutput) {\r
                rate = rate / (10 ** (decimalsAggregator - decimalsOutput));\r
            }\r
        }\r
    }\r
\r
    /**\r
     * @notice Gets aggregators and decimals of two currencies\r
     * @param _input input Address\r
     * @param _output output Address\r
     * @return aggregator to get the rate between the two currencies\r
     * @return reverseAggregator true if the aggregator returned give the rate from _output to _input\r
     * @return decimalsInput decimals of _input\r
     * @return decimalsOutput decimals of _output\r
     */\r
    function getAggregatorAndDecimals(\r
        address _input,\r
        address _output\r
    )\r
        private\r
        view\r
        returns (\r
            AggregatorFraction aggregator,\r
            bool reverseAggregator,\r
            uint256 decimalsInput,\r
            uint256 decimalsOutput\r
        )\r
    {\r
        // Try to get the right aggregator for the conversion\r
        aggregator = AggregatorFraction(allAggregators[_input][_output]);\r
        reverseAggregator = false;\r
\r
        // if no aggregator found we try to find an aggregator in the reverse way\r
        if (address(aggregator) == address(0x00)) {\r
            aggregator = AggregatorFraction(allAggregators[_output][_input]);\r
            reverseAggregator = true;\r
        }\r
\r
        require(address(aggregator) != address(0x00), "No aggregator found");\r
\r
        // get the decimals for the two currencies\r
        decimalsInput = getDecimals(_input);\r
        decimalsOutput = getDecimals(_output);\r
    }\r
\r
    /**\r
     * @notice Gets decimals from an address currency\r
     * @param _addr address to check\r
     * @return decimals number of decimals\r
     */\r
    function getDecimals(\r
        address _addr\r
    ) private view returns (uint256 decimals) {\r
        // by default we assume it is fiat\r
        decimals = FIAT_DECIMALS;\r
        // if address is the hash of the ETH currency\r
        if (_addr == nativeTokenHash) {\r
            decimals = NATIVE_TOKEN_DECIMALS;\r
        } else if (isContract(_addr)) {\r
            // otherwise, we get the decimals from the erc20 directly\r
            decimals = ERC20fraction(_addr).decimals();\r
        }\r
    }\r
\r
    /**\r
     * @notice Checks if an address is a contract\r
     * @param _addr Address to check\r
     * @return true if the address hosts a contract, false otherwise\r
     */\r
    function isContract(address _addr) private view returns (bool) {\r
        uint32 size;\r
        // solium-disable security/no-inline-assembly\r
        assembly {\r
            size := extcodesize(_addr)\r
        }\r
        return (size > 0);\r
    }\r
}\r
"
    },
    "contracts/legacy_openzeppelin/contracts/access/Roles.sol": {
      "content": "// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.0;\r
\r
/**\r
 * @title Roles\r
 * @dev Library for managing addresses assigned to a Role.\r
 */\r
library Roles {\r
  struct Role {\r
    mapping(address => bool) bearer;\r
  }\r
\r
  /**\r
   * @dev Give an account access to this role.\r
   */\r
  function add(Role storage role, address account) internal {\r
    require(!has(role, account), 'Roles: account already has role');\r
    role.bearer[account] = true;\r
  }\r
\r
  /**\r
   * @dev Remove an account's access to this role.\r
   */\r
  function remove(Role storage role, address account) internal {\r
    require(has(role, account), 'Roles: account does not have role');\r
    role.bearer[account] = false;\r
  }\r
\r
  /**\r
   * @dev Check if an account has this role.\r
   * @return bool\r
   */\r
  function has(Role storage role, address account) internal view returns (bool) {\r
    require(account != address(0), 'Roles: account is the zero address');\r
    return role.bearer[account];\r
  }\r
}\r
"
    },
    "contracts/legacy_openzeppelin/contracts/access/roles/WhitelistAdminRole.sol": {
      "content": "// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.0;\r
\r
import '@openzeppelin/contracts/utils/Context.sol';\r
import '../Roles.sol';\r
\r
/**\r
 * @title WhitelistAdminRole\r
 * @dev WhitelistAdmins are responsible for assigning and removing Whitelisted accounts.\r
 */\r
abstract contract WhitelistAdminRole is Context {\r
  using Roles for Roles.Role;\r
\r
  event WhitelistAdminAdded(address indexed account);\r
  event WhitelistAdminRemoved(address indexed account);\r
\r
  Roles.Role private _whitelistAdmins;\r
\r
  constructor() {\r
    _addWhitelistAdmin(_msgSender());\r
  }\r
\r
  modifier onlyWhitelistAdmin() {\r
    require(\r
      isWhitelistAdmin(_msgSender()),\r
      'WhitelistAdminRole: caller does not have the WhitelistAdmin role'\r
    );\r
    _;\r
  }\r
\r
  function isWhitelistAdmin(address account) public view returns (bool) {\r
    return _whitelistAdmins.has(account);\r
  }\r
\r
  function addWhitelistAdmin(address account) public onlyWhitelistAdmin {\r
    _addWhitelistAdmin(account);\r
  }\r
\r
  function renounceWhitelistAdmin() public {\r
    _removeWhitelistAdmin(_msgSender());\r
  }\r
\r
  function _addWhitelistAdmin(address account) internal {\r
    _whitelistAdmins.add(account);\r
    emit WhitelistAdminAdded(account);\r
  }\r
\r
  function _removeWhitelistAdmin(address account) internal {\r
    _whitelistAdmins.remove(account);\r
    emit WhitelistAdminRemoved(account);\r
  }\r
}\r
"
    }
  },
  "settings": {
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "evmVersion": "paris",
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "metadata": {
      "useLiteralContent": true
    }
  }
}}

Tags:
Factory, Oracle|addr:0xddc01188a5140649a2d8109ebdcaa86ceb3a36f4|verified:true|block:23718683|tx:0x49fb77f97bf37d925dcf91241f72d830f296a70e7b7fa4a830b828db2b35a6f5|first_check:1762175089

Submitted on: 2025-11-03 14:04:49

Comments

Log in to comment.

No comments yet.