Skip to content
Verified contractExactBUSL-1.1

FluidGenericOracle

Address0x40ce87bbae34d95717aac2bc8ca8f936c0d32aa5
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.21

 

Same code

2 more contracts

 

Details

Compiler
solc 0.8.21+commit.d9974bed
Language
solidity
Optimization
Yes, 10,000,000 runs
EVM version
paris
License
BUSL-1.1
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,280,333
Creation transaction
0x5cbf8d79b7…4e8b5a0db8

contracts/oracle/oracles/genericOracle.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { FluidOracle } from "../fluidOracle.sol";
import { FluidGenericOracleBase } from "./genericOracleBase.sol";

/// @notice generic configurable Oracle
/// combines up to 4 hops from sources such as
///  - an existing IFluidOracle (e.g. ContractRate)
///  - Redstone
///  - Chainlink
contract FluidGenericOracle is FluidOracle, FluidGenericOracleBase {
    constructor(
        string memory infoName_,
        uint8 targetDecimals_,
        OracleHopSource[] memory sources_
    ) FluidOracle(infoName_, targetDecimals_) FluidGenericOracleBase(sources_) {}

    /// @inheritdoc FluidOracle
    function getExchangeRateOperate() public view virtual override returns (uint256 exchangeRate_) {
        exchangeRate_ = _getHopsExchangeRate(true);
    }

    /// @inheritdoc FluidOracle
    function getExchangeRateLiquidate() public view virtual override returns (uint256 exchangeRate_) {
        exchangeRate_ = _getHopsExchangeRate(false);
    }

    /// @inheritdoc FluidOracle
    function getExchangeRate() public view virtual override returns (uint256 exchangeRate_) {
        exchangeRate_ = _getHopsExchangeRate(false);
    }
}

@openzeppelin/contracts/interfaces/IERC4626.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (interfaces/IERC4626.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/IERC20.sol";
import "../token/ERC20/extensions/IERC20Metadata.sol";

/**
 * @dev Interface of the ERC4626 "Tokenized Vault Standard", as defined in
 * https://eips.ethereum.org/EIPS/eip-4626[ERC-4626].
 *
 * _Available since v4.7._
 */
interface IERC4626 is IERC20, IERC20Metadata {
    event Deposit(address indexed sender, address indexed owner, uint256 assets, uint256 shares);

    event Withdraw(
        address indexed sender,
        address indexed receiver,
        address indexed owner,
        uint256 assets,
        uint256 shares
    );

    /**
     * @dev Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing.
     *
     * - MUST be an ERC-20 token contract.
     * - MUST NOT revert.
     */
    function asset() external view returns (address assetTokenAddress);

    /**
     * @dev Returns the total amount of the underlying asset that is “managed” by Vault.
     *
     * - SHOULD include any compounding that occurs from yield.
     * - MUST be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT revert.
     */
    function totalAssets() external view returns (uint256 totalManagedAssets);

    /**
     * @dev Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToShares(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToAssets(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver,
     * through a deposit call.
     *
     * - MUST return a limited value if receiver is subject to some deposit limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited.
     * - MUST NOT revert.
     */
    function maxDeposit(address receiver) external view returns (uint256 maxAssets);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of Vault shares that would be minted in a deposit
     *   call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called
     *   in the same transaction.
     * - MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the
     *   deposit would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewDeposit SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewDeposit(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Mints shares Vault shares to receiver by depositing exactly amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   deposit execution, and are accounted for during deposit.
     * - MUST revert if all of assets cannot be deposited (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function deposit(uint256 assets, address receiver) external returns (uint256 shares);

    /**
     * @dev Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call.
     * - MUST return a limited value if receiver is subject to some mint limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted.
     * - MUST NOT revert.
     */
    function maxMint(address receiver) external view returns (uint256 maxShares);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of assets that would be deposited in a mint call
     *   in the same transaction. I.e. mint should return the same or fewer assets as previewMint if called in the
     *   same transaction.
     * - MUST NOT account for mint limits like those returned from maxMint and should always act as though the mint
     *   would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewMint SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by minting.
     */
    function previewMint(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Mints exactly shares Vault shares to receiver by depositing amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the mint
     *   execution, and are accounted for during mint.
     * - MUST revert if all of shares cannot be minted (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function mint(uint256 shares, address receiver) external returns (uint256 assets);

    /**
     * @dev Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the
     * Vault, through a withdraw call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxWithdraw(address owner) external view returns (uint256 maxAssets);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw
     *   call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if
     *   called
     *   in the same transaction.
     * - MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though
     *   the withdrawal would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewWithdraw SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewWithdraw(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Burns shares from owner and sends exactly assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   withdraw execution, and are accounted for during withdraw.
     * - MUST revert if all of assets cannot be withdrawn (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * Note that some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function withdraw(
        uint256 assets,
        address receiver,
        address owner
    ) external returns (uint256 shares);

    /**
     * @dev Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault,
     * through a redeem call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxRedeem(address owner) external view returns (uint256 maxShares);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of assets that would be withdrawn in a redeem call
     *   in the same transaction. I.e. redeem should return the same or more assets as previewRedeem if called in the
     *   same transaction.
     * - MUST NOT account for redemption limits like those returned from maxRedeem and should always act as though the
     *   redemption would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewRedeem SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by redeeming.
     */
    function previewRedeem(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Burns exactly shares from owner and sends assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   redeem execution, and are accounted for during redeem.
     * - MUST revert if all of shares cannot be redeemed (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function redeem(
        uint256 shares,
        address receiver,
        address owner
    ) external returns (uint256 assets);
}

@openzeppelin/contracts/token/ERC20/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @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.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @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 `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, 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 `from` to `to` 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 from,
        address to,
        uint256 amount
    ) external returns (bool);
}

@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

contracts/oracle/error.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

contract Error {
    error FluidOracleError(uint256 errorId_);
}

contracts/oracle/errorTypes.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

library ErrorTypes {
    /***********************************|
    |           FluidOracleL2           | 
    |__________________________________*/

    /// @notice thrown when sequencer on a L2 has an outage and grace period has not yet passed.
    uint256 internal constant FluidOracleL2__SequencerOutage = 60000;

    /***********************************|
    |     UniV3CheckCLRSOracle          | 
    |__________________________________*/

    /// @notice thrown when the delta between main price source and check rate source is exceeding the allowed delta
    uint256 internal constant UniV3CheckCLRSOracle__InvalidPrice = 60001;

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant UniV3CheckCLRSOracle__InvalidParams = 60002;

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant UniV3CheckCLRSOracle__ExchangeRateZero = 60003;

    /***********************************|
    |           FluidOracle             | 
    |__________________________________*/

    /// @notice thrown when an invalid info name is passed into a fluid oracle (e.g. not set or too long)
    uint256 internal constant FluidOracle__InvalidInfoName = 60010;

    /***********************************|
    |            sUSDe Oracle           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant SUSDeOracle__InvalidParams = 60102;

    /***********************************|
    |           Pendle Oracle           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant PendleOracle__InvalidParams = 60201;

    /// @notice thrown when the Pendle market Oracle has not been initialized yet
    uint256 internal constant PendleOracle__MarketNotInitialized = 60202;

    /// @notice thrown when the Pendle market does not have 18 decimals
    uint256 internal constant PendleOracle__MarketInvalidDecimals = 60203;

    /// @notice thrown when the Pendle market returns an unexpected price
    uint256 internal constant PendleOracle__InvalidPrice = 60204;

    /***********************************|
    |    CLRS2UniV3CheckCLRSOracleL2    | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant CLRS2UniV3CheckCLRSOracleL2__ExchangeRateZero = 60301;

    /***********************************|
    |    Ratio2xFallbackCLRSOracleL2    | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant Ratio2xFallbackCLRSOracleL2__ExchangeRateZero = 60311;

    /***********************************|
    |            WeETHsOracle           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant WeETHsOracle__InvalidParams = 60321;

    /***********************************|
    |        DexSmartColOracle          | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant DexSmartColOracle__InvalidParams = 60331;

    /// @notice thrown when smart col is not enabled
    uint256 internal constant DexSmartColOracle__SmartColNotEnabled = 60332;

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant DexSmartColOracle__ExchangeRateZero = 60333;

    /***********************************|
    |        DexSmartDebtOracle         | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant DexSmartDebtOracle__InvalidParams = 60341;

    /// @notice thrown when smart debt is not enabled
    uint256 internal constant DexSmartDebtOracle__SmartDebtNotEnabled = 60342;

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant DexSmartDebtOracle__ExchangeRateZero = 60343;

    /***********************************|
    |            ContractRate           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant ContractRate__InvalidParams = 60351;

    /// @notice thrown when caller is not authorized
    uint256 internal constant ContractRate__Unauthorized = 60352;

    /// @notice thrown when minimum diff for triggering update on the stared rate is not reached
    uint256 internal constant ContractRate__MinUpdateDiffNotReached = 60353;

    /// @notice thrown when the external rate source returns 0 for the new rate
    uint256 internal constant ContractRate__NewRateZero = 60354;

    /***********************************|
    |            sUSDs Oracle           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant SUSDsOracle__InvalidParams = 60361;

    /***********************************|
    |            Peg Oracle             | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant PegOracle__InvalidParams = 60371;

    /***********************************|
    |              DexOracle            | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant DexOracle__InvalidParams = 60381;

    /// @notice thrown when the exchange rate is zero, even after all possible fallbacks depending on config
    uint256 internal constant DexOracle__ExchangeRateZero = 60382;

    /***********************************|
    |           GenericOracle           | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant GenericOracle__InvalidParams = 60401;

    /// @notice thrown when reaching an unexepcted config state
    uint256 internal constant GenericOracle__UnexpectedConfig = 60402;

    /// @notice thrown when the exchange rate is zero
    uint256 internal constant GenericOracle__RateZero = 60403;

    /***********************************|
    |          Chainlink Oracle         | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant ChainlinkOracle__InvalidParams = 61001;

    /***********************************|
    |          UniswapV3 Oracle         | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant UniV3Oracle__InvalidParams = 62001;

    /// @notice thrown when constructor is called with invalid ordered seconds agos values
    uint256 internal constant UniV3Oracle__InvalidSecondsAgos = 62002;

    /// @notice thrown when constructor is called with invalid delta values > 100%
    uint256 internal constant UniV3Oracle__InvalidDeltas = 62003;

    /***********************************|
    |            WstETh Oracle          | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant WstETHOracle__InvalidParams = 63001;

    /***********************************|
    |           Redstone Oracle         | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant RedstoneOracle__InvalidParams = 64001;

    /***********************************|
    |          Fallback Oracle          | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant FallbackOracle__InvalidParams = 65001;

    /***********************************|
    |       FallbackCLRSOracle          | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even for the fallback oracle source (if enabled)
    uint256 internal constant FallbackCLRSOracle__ExchangeRateZero = 66001;

    /***********************************|
    |         WstETHCLRSOracle          | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even for the fallback oracle source (if enabled)
    uint256 internal constant WstETHCLRSOracle__ExchangeRateZero = 67001;

    /***********************************|
    |        CLFallbackUniV3Oracle      | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even for the uniV3 rate
    uint256 internal constant CLFallbackUniV3Oracle__ExchangeRateZero = 68001;

    /***********************************|
    |  WstETHCLRS2UniV3CheckCLRSOracle  | 
    |__________________________________*/

    /// @notice thrown when the exchange rate is zero, even for the uniV3 rate
    uint256 internal constant WstETHCLRS2UniV3CheckCLRSOracle__ExchangeRateZero = 69001;

    /***********************************|
    |             WeETh Oracle          | 
    |__________________________________*/

    /// @notice thrown when an invalid parameter is passed to a method
    uint256 internal constant WeETHOracle__InvalidParams = 70001;
}

contracts/oracle/fluidOracle.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { IFluidOracle } from "./interfaces/iFluidOracle.sol";
import { ErrorTypes } from "./errorTypes.sol";
import { Error as OracleError } from "./error.sol";

/// @title   FluidOracle
/// @notice  Base contract that any Fluid Oracle must implement
abstract contract FluidOracle is IFluidOracle, OracleError {
    /// @dev short helper string to easily identify the oracle. E.g. token symbols
    //
    // using a bytes32 because string can not be immutable.
    bytes32 private immutable _infoName;

    /// @dev target decimals of the oracle when scaling to 1e27. E.g. for ETH / USDC it would be 15
    /// because diff of ETH decimals to 1e27 is 9, and USDC has 6 decimals, so 6+9 = 15, e.g. 2029,047772120364926
    /// For USDC / ETH: 21 + 18 = 39, e.g. 0,000492842018675092636829357843847601646
    uint8 private immutable _targetDecimals;

    constructor(string memory infoName_, uint8 targetDecimals_) {
        if (bytes(infoName_).length > 32 || bytes(infoName_).length == 0) {
            revert FluidOracleError(ErrorTypes.FluidOracle__InvalidInfoName);
        }

        if (targetDecimals_ < 15 || targetDecimals_ > 39) {
            revert FluidOracleError(ErrorTypes.GenericOracle__InvalidParams);
        }
        _targetDecimals = targetDecimals_;

        // convert string to bytes32
        bytes32 infoNameBytes32_;
        assembly {
            infoNameBytes32_ := mload(add(infoName_, 32))
        }
        _infoName = infoNameBytes32_;
    }

    /// @inheritdoc IFluidOracle
    function targetDecimals() external view returns (uint8) {
        return _targetDecimals;
    }

    /// @inheritdoc IFluidOracle
    function infoName() external view returns (string memory) {
        // convert bytes32 to string
        uint256 length_;
        while (length_ < 32 && _infoName[length_] != 0) {
            length_++;
        }
        bytes memory infoNameBytes_ = new bytes(length_);
        for (uint256 i; i < length_; i++) {
            infoNameBytes_[i] = _infoName[i];
        }
        return string(infoNameBytes_);
    }

    /// @inheritdoc IFluidOracle
    function getExchangeRate() external view virtual returns (uint256 exchangeRate_);

    /// @inheritdoc IFluidOracle
    function getExchangeRateOperate() external view virtual returns (uint256 exchangeRate_);

    /// @inheritdoc IFluidOracle
    function getExchangeRateLiquidate() external view virtual returns (uint256 exchangeRate_);
}

contracts/oracle/interfaces/external/IChainlinkAggregatorV3.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

/// from https://github.com/smartcontractkit/chainlink/blob/master/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol
/// Copyright (c) 2018 SmartContract ChainLink, Ltd.

interface IChainlinkAggregatorV3 {
    /// @notice represents the number of decimals the aggregator responses represent.
    function decimals() external view returns (uint8);

    function description() external view returns (string memory);

    function version() external view returns (uint256);

    function getRoundData(
        uint80 _roundId
    )
        external
        view
        returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

    function latestRoundData()
        external
        view
        returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

contracts/oracle/interfaces/iFluidOracle.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

interface IFluidOracle {
    /// @dev Deprecated. Use `getExchangeRateOperate()` and `getExchangeRateLiquidate()` instead. Only implemented for
    ///      backwards compatibility.
    function getExchangeRate() external view returns (uint256 exchangeRate_);

    /// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for operates
    function getExchangeRateOperate() external view returns (uint256 exchangeRate_);

    /// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for liquidations
    function getExchangeRateLiquidate() external view returns (uint256 exchangeRate_);

    /// @notice helper string to easily identify the oracle. E.g. token symbols
    function infoName() external view returns (string memory);

    /// @notice target decimals of the returned oracle rate when scaling to 1e27. E.g. for ETH / USDC it would be 15
    /// because diff of ETH decimals to 1e27 is 9, and USDC has 6 decimals, so 6+9 = 15, e.g. 2029,047772120364926
    /// For USDC / ETH: 21 + 18 = 39, e.g. 0,000492842018675092636829357843847601646
    function targetDecimals() external view returns (uint8);
}

contracts/oracle/libraries/oracleUtils.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

/// @title Oracle utils library
/// @notice implements common utility methods for Fluid Oracles
library OracleUtils {
    /// @dev The scaler for max delta point math (100%)
    uint256 internal constant HUNDRED_PERCENT_DELTA_SCALER = 10_000;
    /// @dev output precision of rates
    uint256 internal constant RATE_OUTPUT_DECIMALS = 27;

    /// @dev checks if `mainSourceRate_` is within a `maxDeltaPercent_` of `checkSourceRate_`. Returns true if so.
    function isRateOutsideDelta(
        uint256 mainSourceRate_,
        uint256 checkSourceRate_,
        uint256 maxDeltaPercent_
    ) internal pure returns (bool) {
        uint256 offset_ = (checkSourceRate_ * maxDeltaPercent_) / HUNDRED_PERCENT_DELTA_SCALER;
        return (mainSourceRate_ > (checkSourceRate_ + offset_) || mainSourceRate_ < (checkSourceRate_ - offset_));
    }
}

contracts/oracle/oracles/genericOracleBase.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { ErrorTypes } from "../errorTypes.sol";
import { Error as OracleError } from "../error.sol";
import { ChainlinkSourceReader } from "../sourceReaders/chainlinkSourceReader.sol";
import { FluidSourceReader } from "../sourceReaders/fluidSourceReader.sol";
import { ERC4626SourceReader } from "../sourceReaders/erc4626SourceReader.sol";
import { OracleUtils } from "../libraries/oracleUtils.sol";

abstract contract GenericOracleStructs {
    enum SourceType {
        Fluid, // 0, e.g. ContractRate or some other IFluidOracle
        Redstone, // 1
        Chainlink, // 2
        UniV3Checked, // 3
        ERC4626 // 4
    }

    struct OracleHopSource {
        address source;
        bool invertRate;
        uint256 multiplier;
        uint256 divisor;
        SourceType sourceType; // e.g. FLUID, REDSTONE, UNIV3CHECKED, CHAINLINK
    }
}

/// @notice generic configurable Oracle Base
/// combines up to 4 hops from sources such as
///  - an existing IFluidOracle (e.g. ContractRate)
///  - Redstone
///  - Chainlink
abstract contract FluidGenericOracleBase is
    OracleError,
    GenericOracleStructs,
    ChainlinkSourceReader,
    FluidSourceReader,
    ERC4626SourceReader
{
    address internal immutable _SOURCE1;
    bool internal immutable _SOURCE1_INVERT;
    uint256 internal immutable _SOURCE1_MULTIPLIER;
    uint256 internal immutable _SOURCE1_DIVISOR;
    SourceType internal immutable _SOURCE1_TYPE;

    address internal immutable _SOURCE2;
    bool internal immutable _SOURCE2_INVERT;
    uint256 internal immutable _SOURCE2_MULTIPLIER;
    uint256 internal immutable _SOURCE2_DIVISOR;
    SourceType internal immutable _SOURCE2_TYPE;

    address internal immutable _SOURCE3;
    bool internal immutable _SOURCE3_INVERT;
    uint256 internal immutable _SOURCE3_MULTIPLIER;
    uint256 internal immutable _SOURCE3_DIVISOR;
    SourceType internal immutable _SOURCE3_TYPE;

    address internal immutable _SOURCE4;
    bool internal immutable _SOURCE4_INVERT;
    uint256 internal immutable _SOURCE4_MULTIPLIER;
    uint256 internal immutable _SOURCE4_DIVISOR;
    SourceType internal immutable _SOURCE4_TYPE;

    address internal immutable _SOURCE5;
    bool internal immutable _SOURCE5_INVERT;
    uint256 internal immutable _SOURCE5_MULTIPLIER;
    uint256 internal immutable _SOURCE5_DIVISOR;
    SourceType internal immutable _SOURCE5_TYPE;

    constructor(OracleHopSource[] memory sources_) {
        if (sources_.length == 0 || sources_.length > 5) {
            revert FluidOracleError(ErrorTypes.GenericOracle__InvalidParams);
        }

        _verifyOracleHopSource(sources_[0]);
        _SOURCE1 = sources_[0].source;
        _SOURCE1_INVERT = sources_[0].invertRate;
        _SOURCE1_MULTIPLIER = sources_[0].multiplier;
        _SOURCE1_DIVISOR = sources_[0].divisor;
        _SOURCE1_TYPE = sources_[0].sourceType;

        if (sources_.length > 1) {
            _verifyOracleHopSource(sources_[1]);
            _SOURCE2 = sources_[1].source;
            _SOURCE2_INVERT = sources_[1].invertRate;
            _SOURCE2_MULTIPLIER = sources_[1].multiplier;
            _SOURCE2_DIVISOR = sources_[1].divisor;
            _SOURCE2_TYPE = sources_[1].sourceType;
        }

        if (sources_.length > 2) {
            _verifyOracleHopSource(sources_[2]);
            _SOURCE3 = sources_[2].source;
            _SOURCE3_INVERT = sources_[2].invertRate;
            _SOURCE3_MULTIPLIER = sources_[2].multiplier;
            _SOURCE3_DIVISOR = sources_[2].divisor;
            _SOURCE3_TYPE = sources_[2].sourceType;
        }

        if (sources_.length > 3) {
            _verifyOracleHopSource(sources_[3]);
            _SOURCE4 = sources_[3].source;
            _SOURCE4_INVERT = sources_[3].invertRate;
            _SOURCE4_MULTIPLIER = sources_[3].multiplier;
            _SOURCE4_DIVISOR = sources_[3].divisor;
            _SOURCE4_TYPE = sources_[3].sourceType;
        }

        if (sources_.length > 4) {
            _verifyOracleHopSource(sources_[4]);
            _SOURCE5 = sources_[4].source;
            _SOURCE5_INVERT = sources_[4].invertRate;
            _SOURCE5_MULTIPLIER = sources_[4].multiplier;
            _SOURCE5_DIVISOR = sources_[4].divisor;
            _SOURCE5_TYPE = sources_[4].sourceType;
        }
    }

    /// @dev verifies a hop source config
    function _verifyOracleHopSource(OracleHopSource memory source_) internal view virtual {
        if (
            address(source_.source) == address(0) ||
            source_.sourceType == SourceType.UniV3Checked ||
            source_.divisor == 0 ||
            source_.multiplier == 0 ||
            source_.divisor > 1e40 ||
            source_.multiplier > 1e40
        ) {
            revert FluidOracleError(ErrorTypes.GenericOracle__InvalidParams);
        }
    }

    /// @dev reads the exchange rate for a hop source
    function _readSource(
        address source_,
        SourceType sourceType_,
        bool isOperate_
    ) internal view virtual returns (uint256 rate_) {
        if (sourceType_ == SourceType.Redstone || sourceType_ == SourceType.Chainlink) {
            rate_ = _readChainlinkSource(source_);
        } else if (sourceType_ == SourceType.Fluid) {
            rate_ = _readFluidSource(source_, isOperate_);
        } else if (sourceType_ == SourceType.ERC4626) {
            rate_ = _readERC4626Source(source_);
        } else {
            // should never happen because of config checks in constructor
            revert FluidOracleError(ErrorTypes.GenericOracle__UnexpectedConfig);
        }
    }

    /// @dev gets the exchange rate for a single configured hop
    function _getExchangeRateForHop(
        uint256 curHopsRate_,
        bool isOperate_,
        OracleHopSource memory source_
    ) internal view virtual returns (uint256 rate_) {
        rate_ = _readSource(source_.source, source_.sourceType, isOperate_);

        // scale to 1e27
        rate_ = (rate_ * source_.multiplier) / source_.divisor;

        if (source_.invertRate && rate_ > 0) {
            rate_ = (10 ** (OracleUtils.RATE_OUTPUT_DECIMALS * 2)) / uint256(rate_);
        }

        rate_ = (curHopsRate_ * rate_) / (10 ** OracleUtils.RATE_OUTPUT_DECIMALS); // combine with current hops rate
    }

    /// @dev gets the exchange rate combined for all configured hops
    function _getHopsExchangeRate(bool isOperate_) internal view returns (uint256 rate_) {
        rate_ = _getExchangeRateForHop(
            (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
            isOperate_,
            OracleHopSource(_SOURCE1, _SOURCE1_INVERT, _SOURCE1_MULTIPLIER, _SOURCE1_DIVISOR, _SOURCE1_TYPE)
        );
        if (rate_ == 0) {
            revert FluidOracleError(ErrorTypes.GenericOracle__RateZero);
        }
        if (address(_SOURCE2) == address(0) && _SOURCE2_TYPE != SourceType.UniV3Checked) {
            return rate_;
        }

        // 2 hops -> return rate of hop 1 combined with hop 2
        rate_ = _getExchangeRateForHop(
            rate_,
            isOperate_,
            OracleHopSource(_SOURCE2, _SOURCE2_INVERT, _SOURCE2_MULTIPLIER, _SOURCE2_DIVISOR, _SOURCE2_TYPE)
        );
        if (rate_ == 0) {
            revert FluidOracleError(ErrorTypes.GenericOracle__RateZero);
        }
        if (address(_SOURCE3) == address(0) && _SOURCE3_TYPE != SourceType.UniV3Checked) {
            return rate_;
        }

        // 3 hops -> return rate of hop 1 combined with hop 2 & hop 3
        rate_ = _getExchangeRateForHop(
            rate_,
            isOperate_,
            OracleHopSource(_SOURCE3, _SOURCE3_INVERT, _SOURCE3_MULTIPLIER, _SOURCE3_DIVISOR, _SOURCE3_TYPE)
        );
        if (rate_ == 0) {
            revert FluidOracleError(ErrorTypes.GenericOracle__RateZero);
        }
        if (address(_SOURCE4) == address(0) && _SOURCE4_TYPE != SourceType.UniV3Checked) {
            return rate_;
        }

        // 4 hops -> return rate of hop 1 combined with hop 2, hop 3 & hop 4
        rate_ = _getExchangeRateForHop(
            rate_,
            isOperate_,
            OracleHopSource(_SOURCE4, _SOURCE4_INVERT, _SOURCE4_MULTIPLIER, _SOURCE4_DIVISOR, _SOURCE4_TYPE)
        );
        if (rate_ == 0) {
            revert FluidOracleError(ErrorTypes.GenericOracle__RateZero);
        }
        if (address(_SOURCE5) == address(0) && _SOURCE5_TYPE != SourceType.UniV3Checked) {
            return rate_;
        }

        // 5 hops -> return rate of hop 1 combined with hop 2, hop 3, hop 4 & hop 5
        rate_ = _getExchangeRateForHop(
            rate_,
            isOperate_,
            OracleHopSource(_SOURCE5, _SOURCE5_INVERT, _SOURCE5_MULTIPLIER, _SOURCE5_DIVISOR, _SOURCE5_TYPE)
        );
        if (rate_ == 0) {
            revert FluidOracleError(ErrorTypes.GenericOracle__RateZero);
        }
    }

    /// @notice Returns the exchange rate for each hop.
    /// @return rateSource1Operate_ The exchange rate for hop 1 during operate.
    /// @return rateSource1Liquidate_ The exchange rate for hop 1 during liquidate.
    /// @return rateSource2Operate_ The exchange rate for hop 2 during operate.
    /// @return rateSource2Liquidate_ The exchange rate for hop 2 during liquidate.
    /// @return rateSource3Operate_ The exchange rate for hop 3 during operate.
    /// @return rateSource3Liquidate_ The exchange rate for hop 3 during liquidate.
    /// @return rateSource4Operate_ The exchange rate for hop 4 during operate.
    /// @return rateSource4Liquidate_ The exchange rate for hop 4 during liquidate.
    /// @return rateSource5Operate_ The exchange rate for hop 5 during operate.
    /// @return rateSource5Liquidate_ The exchange rate for hop 5 during liquidate.
    function getHopExchangeRates()
        public
        view
        returns (
            uint256 rateSource1Operate_,
            uint256 rateSource1Liquidate_,
            uint256 rateSource2Operate_,
            uint256 rateSource2Liquidate_,
            uint256 rateSource3Operate_,
            uint256 rateSource3Liquidate_,
            uint256 rateSource4Operate_,
            uint256 rateSource4Liquidate_,
            uint256 rateSource5Operate_,
            uint256 rateSource5Liquidate_
        )
    {
        rateSource1Operate_ = _getExchangeRateForHop(
            (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
            true,
            OracleHopSource(_SOURCE1, _SOURCE1_INVERT, _SOURCE1_MULTIPLIER, _SOURCE1_DIVISOR, _SOURCE1_TYPE)
        );
        rateSource1Liquidate_ = _getExchangeRateForHop(
            (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
            false,
            OracleHopSource(_SOURCE1, _SOURCE1_INVERT, _SOURCE1_MULTIPLIER, _SOURCE1_DIVISOR, _SOURCE1_TYPE)
        );

        if (address(_SOURCE2) != address(0) || _SOURCE2_TYPE == SourceType.UniV3Checked) {
            rateSource2Operate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                true,
                OracleHopSource(_SOURCE2, _SOURCE2_INVERT, _SOURCE2_MULTIPLIER, _SOURCE2_DIVISOR, _SOURCE2_TYPE)
            );
            rateSource2Liquidate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                false,
                OracleHopSource(_SOURCE2, _SOURCE2_INVERT, _SOURCE2_MULTIPLIER, _SOURCE2_DIVISOR, _SOURCE2_TYPE)
            );
        }

        if (address(_SOURCE3) != address(0) || _SOURCE3_TYPE == SourceType.UniV3Checked) {
            rateSource3Operate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                true,
                OracleHopSource(_SOURCE3, _SOURCE3_INVERT, _SOURCE3_MULTIPLIER, _SOURCE3_DIVISOR, _SOURCE3_TYPE)
            );
            rateSource3Liquidate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                false,
                OracleHopSource(_SOURCE3, _SOURCE3_INVERT, _SOURCE3_MULTIPLIER, _SOURCE3_DIVISOR, _SOURCE3_TYPE)
            );
        }

        if (address(_SOURCE4) != address(0) || _SOURCE4_TYPE == SourceType.UniV3Checked) {
            rateSource4Operate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                true,
                OracleHopSource(_SOURCE4, _SOURCE4_INVERT, _SOURCE4_MULTIPLIER, _SOURCE4_DIVISOR, _SOURCE4_TYPE)
            );
            rateSource4Liquidate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                false,
                OracleHopSource(_SOURCE4, _SOURCE4_INVERT, _SOURCE4_MULTIPLIER, _SOURCE4_DIVISOR, _SOURCE4_TYPE)
            );
        }

        if (address(_SOURCE5) != address(0) || _SOURCE5_TYPE == SourceType.UniV3Checked) {
            rateSource5Operate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                true,
                OracleHopSource(_SOURCE5, _SOURCE5_INVERT, _SOURCE5_MULTIPLIER, _SOURCE5_DIVISOR, _SOURCE5_TYPE)
            );
            rateSource5Liquidate_ = _getExchangeRateForHop(
                (10 ** OracleUtils.RATE_OUTPUT_DECIMALS),
                false,
                OracleHopSource(_SOURCE5, _SOURCE5_INVERT, _SOURCE5_MULTIPLIER, _SOURCE5_DIVISOR, _SOURCE5_TYPE)
            );
        }
    }

    /// @notice Returns the configured OracleHopSources
    function getOracleHopSources() public view returns (OracleHopSource[] memory sources_) {
        sources_ = new OracleHopSource[](5);
        sources_[0] = OracleHopSource(_SOURCE1, _SOURCE1_INVERT, _SOURCE1_MULTIPLIER, _SOURCE1_DIVISOR, _SOURCE1_TYPE);
        sources_[1] = OracleHopSource(_SOURCE2, _SOURCE2_INVERT, _SOURCE2_MULTIPLIER, _SOURCE2_DIVISOR, _SOURCE2_TYPE);
        sources_[2] = OracleHopSource(_SOURCE3, _SOURCE3_INVERT, _SOURCE3_MULTIPLIER, _SOURCE3_DIVISOR, _SOURCE3_TYPE);
        sources_[3] = OracleHopSource(_SOURCE4, _SOURCE4_INVERT, _SOURCE4_MULTIPLIER, _SOURCE4_DIVISOR, _SOURCE4_TYPE);
        sources_[4] = OracleHopSource(_SOURCE5, _SOURCE5_INVERT, _SOURCE5_MULTIPLIER, _SOURCE5_DIVISOR, _SOURCE5_TYPE);
    }
}

contracts/oracle/sourceReaders/chainlinkSourceReader.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { IChainlinkAggregatorV3 } from "../interfaces/external/IChainlinkAggregatorV3.sol";

abstract contract ChainlinkSourceReader {
    function _readChainlinkSource(address feed_) internal view returns (uint256 rate_) {
        try IChainlinkAggregatorV3(feed_).latestRoundData() returns (
            uint80,
            int256 exchangeRate_,
            uint256,
            uint256,
            uint80
        ) {
            // Return the price in `OracleUtils.RATE_OUTPUT_DECIMALS`
            return uint256(exchangeRate_);
        } catch {}
    }
}

contracts/oracle/sourceReaders/erc4626SourceReader.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import { OracleUtils } from "../libraries/oracleUtils.sol";

abstract contract ERC4626SourceReader {
    function _readERC4626Source(address feed_) internal view returns (uint256 rate_) {
        try IERC4626(feed_).convertToAssets(10 ** OracleUtils.RATE_OUTPUT_DECIMALS) returns (uint256 exchangeRate_) {
            // Return the price in `OracleUtils.RATE_OUTPUT_DECIMALS`
            return exchangeRate_;
        } catch {}
    }
}

contracts/oracle/sourceReaders/fluidSourceReader.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import { IFluidOracle } from "../interfaces/iFluidOracle.sol";

abstract contract FluidSourceReader {
    function _readFluidSource(address oracle_, bool isOperate_) internal view returns (uint256 rate_) {
        if (isOperate_) {
            try IFluidOracle(oracle_).getExchangeRateOperate() returns (uint256 exchangeRate_) {
                return exchangeRate_;
            } catch {}
        } else {
            try IFluidOracle(oracle_).getExchangeRateLiquidate() returns (uint256 exchangeRate_) {
                return exchangeRate_;
            } catch {}
        }
    }
}
Compiler settingsShow
{
  "metadata": {
    "useLiteralContent": true
  },
  "libraries": {},
  "optimizer": {
    "runs": 10000000,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.