Overview
ETH Balance
0 ETH
Eth Value
$0.00More Info
Private Name Tags
ContractCreator
Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
---|---|---|---|---|---|---|---|---|---|
Loading...
Loading
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x09486378...1A7969C52 The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
UniswapV3LikeOracle
Compiler Version
v0.8.19+commit.7dd6d404
Optimization Enabled:
Yes with 1000000 runs
Other Settings:
default evmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: MIT pragma solidity 0.8.19; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/utils/Address.sol"; import "@openzeppelin/contracts/utils/math/Math.sol"; import "../interfaces/IOracle.sol"; import "../interfaces/IUniswapV3Pool.sol"; import "../libraries/OraclePrices.sol"; contract UniswapV3LikeOracle is IOracle { using Address for address; using OraclePrices for OraclePrices.Data; using Math for uint256; IERC20 private constant _NONE = IERC20(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF); int24 private constant _TICK_STEPS = 2; uint256 public immutable SUPPORTED_FEES_COUNT; address public immutable FACTORY; bytes32 public immutable INITCODE_HASH; uint24[10] public fees; constructor(address _factory, bytes32 _initcodeHash, uint24[] memory _fees) { FACTORY = _factory; INITCODE_HASH = _initcodeHash; SUPPORTED_FEES_COUNT = _fees.length; unchecked { for (uint256 i = 0; i < SUPPORTED_FEES_COUNT; i++) { fees[i] = _fees[i]; } } } function getRate(IERC20 srcToken, IERC20 dstToken, IERC20 connector, uint256 thresholdFilter) external override view returns (uint256 rate, uint256 weight) { OraclePrices.Data memory ratesAndWeights; unchecked { if (connector == _NONE) { ratesAndWeights = OraclePrices.init(SUPPORTED_FEES_COUNT); for (uint256 i = 0; i < SUPPORTED_FEES_COUNT; i++) { (uint256 rate0, uint256 w) = _getRate(srcToken, dstToken, fees[i]); ratesAndWeights.append(OraclePrices.OraclePrice(rate0, w)); } } else { ratesAndWeights = OraclePrices.init(SUPPORTED_FEES_COUNT**2); for (uint256 i = 0; i < SUPPORTED_FEES_COUNT; i++) { for (uint256 j = 0; j < SUPPORTED_FEES_COUNT; j++) { (uint256 rate0, uint256 w0) = _getRate(srcToken, connector, fees[i]); if (w0 == 0) { continue; } (uint256 rate1, uint256 w1) = _getRate(connector, dstToken, fees[j]); if (w1 == 0) { continue; } ratesAndWeights.append(OraclePrices.OraclePrice(Math.mulDiv(rate0, rate1, 1e18), Math.min(w0, w1))); } } } } return ratesAndWeights.getRateAndWeight(thresholdFilter); } function _getRate(IERC20 srcToken, IERC20 dstToken, uint24 fee) internal view returns (uint256 rate, uint256 liquidity) { (IERC20 token0, IERC20 token1) = srcToken < dstToken ? (srcToken, dstToken) : (dstToken, srcToken); address pool = _getPool(address(token0), address(token1), fee); if (!pool.isContract() ) { return (0, 0); } liquidity = IUniswapV3Pool(pool).liquidity(); if (liquidity == 0) { return (0, 0); } (uint256 sqrtPriceX96, int24 tick) = IUniswapV3Pool(pool).slot0(); int24 tickSpacing = IUniswapV3Pool(pool).tickSpacing(); tick = tick / tickSpacing * tickSpacing; int256 liquidityShiftsLeft = int256(liquidity); int256 liquidityShiftsRight = int256(liquidity); unchecked { for (int24 i = 0; i <= _TICK_STEPS; i++) { (, int256 liquidityNet,,,,,,) = IUniswapV3Pool(pool).ticks(tick + i * tickSpacing); liquidityShiftsRight += liquidityNet; liquidity = Math.min(liquidity, uint256(liquidityShiftsRight)); if (liquidityShiftsRight == 0) { return (0, 0); } (, liquidityNet,,,,,,) = IUniswapV3Pool(pool).ticks(tick - i * tickSpacing); liquidityShiftsLeft -= liquidityNet; liquidity = Math.min(liquidity, uint256(liquidityShiftsLeft)); if (liquidityShiftsLeft == 0) { return (0, 0); } } } if (srcToken == token0) { rate = (((1e18 * sqrtPriceX96) >> 96) * sqrtPriceX96) >> 96; } else { rate = (1e18 << 192) / sqrtPriceX96 / sqrtPriceX96; } } function _getPool(address token0, address token1, uint24 fee) private view returns (address) { return address(uint160(uint256( keccak256( abi.encodePacked( hex'ff', FACTORY, keccak256(abi.encode(token0, token1, fee)), INITCODE_HASH ) ) ))); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.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); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol) pragma solidity ^0.8.1; /** * @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 * * Furthermore, `isContract` will also return true if the target contract within * the same transaction is already scheduled for destruction by `SELFDESTRUCT`, * which only has an effect at the end of a transaction. * ==== * * [IMPORTANT] * ==== * You shouldn't rely on `isContract` to protect against flash loan attacks! * * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract * constructor. * ==== */ function isContract(address account) internal view returns (bool) { // This method relies on extcodesize/address.code.length, which returns 0 // for contracts in construction, since the code is only stored at the end // of the constructor execution. return account.code.length > 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://consensys.net/diligence/blog/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.8.0/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"); (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 functionCallWithValue(target, data, 0, "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"); (bool success, bytes memory returndata) = target.call{value: value}(data); return verifyCallResultFromTarget(target, success, returndata, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a static call. * * _Available since v3.3._ */ function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) { return functionStaticCall(target, data, "Address: low-level static call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], * but performing a static call. * * _Available since v3.3._ */ function functionStaticCall( address target, bytes memory data, string memory errorMessage ) internal view returns (bytes memory) { (bool success, bytes memory returndata) = target.staticcall(data); return verifyCallResultFromTarget(target, success, returndata, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a delegate call. * * _Available since v3.4._ */ function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) { return functionDelegateCall(target, data, "Address: low-level delegate call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], * but performing a delegate call. * * _Available since v3.4._ */ function functionDelegateCall( address target, bytes memory data, string memory errorMessage ) internal returns (bytes memory) { (bool success, bytes memory returndata) = target.delegatecall(data); return verifyCallResultFromTarget(target, success, returndata, errorMessage); } /** * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract. * * _Available since v4.8._ */ function verifyCallResultFromTarget( address target, bool success, bytes memory returndata, string memory errorMessage ) internal view returns (bytes memory) { if (success) { if (returndata.length == 0) { // only check isContract if the call was successful and the return data is empty // otherwise we already know that it was a contract require(isContract(target), "Address: call to non-contract"); } return returndata; } else { _revert(returndata, errorMessage); } } /** * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the * revert reason or using the provided one. * * _Available since v4.3._ */ function verifyCallResult( bool success, bytes memory returndata, string memory errorMessage ) internal pure returns (bytes memory) { if (success) { return returndata; } else { _revert(returndata, errorMessage); } } function _revert(bytes memory returndata, string memory errorMessage) private pure { // 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 /// @solidity memory-safe-assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert(errorMessage); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol) pragma solidity ^0.8.0; /** * @dev Standard math utilities missing in the Solidity language. */ library Math { enum Rounding { Down, // Toward negative infinity Up, // Toward infinity Zero // Toward zero } /** * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return a > b ? a : b; } /** * @dev Returns the smallest of two numbers. */ function min(uint256 a, uint256 b) internal pure returns (uint256) { return a < b ? a : b; } /** * @dev Returns the average of two numbers. The result is rounded towards * zero. */ function average(uint256 a, uint256 b) internal pure returns (uint256) { // (a + b) / 2 can overflow. return (a & b) + (a ^ b) / 2; } /** * @dev Returns the ceiling of the division of two numbers. * * This differs from standard division with `/` in that it rounds up instead * of rounding down. */ function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) { // (a + b - 1) / b can overflow on addition, so we distribute. return a == 0 ? 0 : (a - 1) / b + 1; } /** * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0 * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) * with further edits by Uniswap Labs also under MIT license. */ function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) { unchecked { // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2^256 + prod0. uint256 prod0; // Least significant 256 bits of the product uint256 prod1; // Most significant 256 bits of the product assembly { let mm := mulmod(x, y, not(0)) prod0 := mul(x, y) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } // Handle non-overflow cases, 256 by 256 division. if (prod1 == 0) { // Solidity will revert if denominator == 0, unlike the div opcode on its own. // The surrounding unchecked block does not change this fact. // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic. return prod0 / denominator; } // Make sure the result is less than 2^256. Also prevents denominator == 0. require(denominator > prod1, "Math: mulDiv overflow"); /////////////////////////////////////////////// // 512 by 256 division. /////////////////////////////////////////////// // Make division exact by subtracting the remainder from [prod1 prod0]. uint256 remainder; assembly { // Compute remainder using mulmod. remainder := mulmod(x, y, denominator) // Subtract 256 bit number from 512 bit number. prod1 := sub(prod1, gt(remainder, prod0)) prod0 := sub(prod0, remainder) } // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1. // See https://cs.stackexchange.com/q/138556/92363. // Does not overflow because the denominator cannot be zero at this stage in the function. uint256 twos = denominator & (~denominator + 1); assembly { // Divide denominator by twos. denominator := div(denominator, twos) // Divide [prod1 prod0] by twos. prod0 := div(prod0, twos) // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one. twos := add(div(sub(0, twos), twos), 1) } // Shift in bits from prod1 into prod0. prod0 |= prod1 * twos; // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for // four bits. That is, denominator * inv = 1 mod 2^4. uint256 inverse = (3 * denominator) ^ 2; // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works // in modular arithmetic, doubling the correct bits in each step. inverse *= 2 - denominator * inverse; // inverse mod 2^8 inverse *= 2 - denominator * inverse; // inverse mod 2^16 inverse *= 2 - denominator * inverse; // inverse mod 2^32 inverse *= 2 - denominator * inverse; // inverse mod 2^64 inverse *= 2 - denominator * inverse; // inverse mod 2^128 inverse *= 2 - denominator * inverse; // inverse mod 2^256 // Because the division is now exact we can divide by multiplying with the modular inverse of denominator. // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1 // is no longer required. result = prod0 * inverse; return result; } } /** * @notice Calculates x * y / denominator with full precision, following the selected rounding direction. */ function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) { uint256 result = mulDiv(x, y, denominator); if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) { result += 1; } return result; } /** * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down. * * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11). */ function sqrt(uint256 a) internal pure returns (uint256) { if (a == 0) { return 0; } // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target. // // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`. // // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)` // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))` // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)` // // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit. uint256 result = 1 << (log2(a) >> 1); // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128, // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision // into the expected uint128 result. unchecked { result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; return min(result, a / result); } } /** * @notice Calculates sqrt(a), following the selected rounding direction. */ function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = sqrt(a); return result + (rounding == Rounding.Up && result * result < a ? 1 : 0); } } /** * @dev Return the log in base 2, rounded down, of a positive value. * Returns 0 if given 0. */ function log2(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >> 128 > 0) { value >>= 128; result += 128; } if (value >> 64 > 0) { value >>= 64; result += 64; } if (value >> 32 > 0) { value >>= 32; result += 32; } if (value >> 16 > 0) { value >>= 16; result += 16; } if (value >> 8 > 0) { value >>= 8; result += 8; } if (value >> 4 > 0) { value >>= 4; result += 4; } if (value >> 2 > 0) { value >>= 2; result += 2; } if (value >> 1 > 0) { result += 1; } } return result; } /** * @dev Return the log in base 2, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log2(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log2(value); return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0); } } /** * @dev Return the log in base 10, rounded down, of a positive value. * Returns 0 if given 0. */ function log10(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >= 10 ** 64) { value /= 10 ** 64; result += 64; } if (value >= 10 ** 32) { value /= 10 ** 32; result += 32; } if (value >= 10 ** 16) { value /= 10 ** 16; result += 16; } if (value >= 10 ** 8) { value /= 10 ** 8; result += 8; } if (value >= 10 ** 4) { value /= 10 ** 4; result += 4; } if (value >= 10 ** 2) { value /= 10 ** 2; result += 2; } if (value >= 10 ** 1) { result += 1; } } return result; } /** * @dev Return the log in base 10, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log10(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log10(value); return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0); } } /** * @dev Return the log in base 256, rounded down, of a positive value. * Returns 0 if given 0. * * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string. */ function log256(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { if (value >> 128 > 0) { value >>= 128; result += 16; } if (value >> 64 > 0) { value >>= 64; result += 8; } if (value >> 32 > 0) { value >>= 32; result += 4; } if (value >> 16 > 0) { value >>= 16; result += 2; } if (value >> 8 > 0) { result += 1; } } return result; } /** * @dev Return the log in base 256, following the selected rounding direction, of a positive value. * Returns 0 if given 0. */ function log256(uint256 value, Rounding rounding) internal pure returns (uint256) { unchecked { uint256 result = log256(value); return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (utils/math/SafeMath.sol) pragma solidity ^0.8.0; // CAUTION // This version of SafeMath should only be used with Solidity 0.8 or later, // because it relies on the compiler's built in overflow checks. /** * @dev Wrappers over Solidity's arithmetic operations. * * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler * now has built in overflow checking. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, with an overflow flag. * * _Available since v3.4._ */ function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { uint256 c = a + b; if (c < a) return (false, 0); return (true, c); } } /** * @dev Returns the subtraction of two unsigned integers, with an overflow flag. * * _Available since v3.4._ */ function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b > a) return (false, 0); return (true, a - b); } } /** * @dev Returns the multiplication of two unsigned integers, with an overflow flag. * * _Available since v3.4._ */ function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { // 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 (true, 0); uint256 c = a * b; if (c / a != b) return (false, 0); return (true, c); } } /** * @dev Returns the division of two unsigned integers, with a division by zero flag. * * _Available since v3.4._ */ function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a / b); } } /** * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. * * _Available since v3.4._ */ function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a % b); } } /** * @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) { return a + b; } /** * @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 a - b; } /** * @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) { return a * b; } /** * @dev Returns the integer division of two unsigned integers, reverting on * division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. * * Requirements: * * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return a / b; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * reverting 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 a % b; } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on * overflow (when the result is negative). * * CAUTION: This function is deprecated because it requires allocating memory for the error * message unnecessarily. For custom revert reasons use {trySub}. * * Counterpart to Solidity's `-` operator. * * Requirements: * * - Subtraction cannot overflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { unchecked { require(b <= a, errorMessage); return a - b; } } /** * @dev Returns the integer division of two unsigned integers, reverting 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) { unchecked { require(b > 0, errorMessage); return a / b; } } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * reverting with custom message when dividing by zero. * * CAUTION: This function is deprecated because it requires allocating memory for the error * message unnecessarily. For custom revert reasons use {tryMod}. * * 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) { unchecked { require(b > 0, errorMessage); return a % b; } } }
// SPDX-License-Identifier: MIT pragma solidity 0.8.19; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; interface IOracle { error ConnectorShouldBeNone(); error PoolNotFound(); error PoolWithConnectorNotFound(); function getRate(IERC20 srcToken, IERC20 dstToken, IERC20 connector, uint256 thresholdFilter) external view returns (uint256 rate, uint256 weight); }
// SPDX-License-Identifier: MIT pragma solidity 0.8.19; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; interface IUniswapV3Pool { function slot0() external view returns (uint160 sqrtPriceX96, int24); // returns reduced because forks use different types of returned values that we do not use function ticks(int24 tick) external view returns (uint128, int128, uint256, uint256, int56, uint160, uint32, bool); function tickSpacing() external view returns (int24); function token0() external view returns (IERC20 token); function liquidity() external view returns (uint128); }
// SPDX-License-Identifier: MIT pragma solidity 0.8.19; import "@openzeppelin/contracts/utils/math/SafeMath.sol"; /** * @title OraclePrices * @notice A library that provides functionalities for processing and analyzing token rate and weight data provided by an oracle. * The library is used when an oracle uses multiple pools to determine a token's price. * It allows to filter out pools with low weight and significantly incorrect price, which could distort the weighted price. * The level of low-weight pool filtering can be managed using the thresholdFilter parameter. */ library OraclePrices { using SafeMath for uint256; /** * @title Oracle Price Data Structure * @notice This structure encapsulates the rate and weight information for tokens as provided by an oracle * @dev An array of OraclePrice structures can be used to represent oracle data for multiple pools * @param rate The oracle-provided rate for a token * @param weight The oracle-provided derived weight for a token */ struct OraclePrice { uint256 rate; uint256 weight; } /** * @title Oracle Prices Data Structure * @notice This structure encapsulates information about a list of oracles prices and weights * @dev The structure is initialized with a maximum possible length by the `init` function * @param oraclePrices An array of OraclePrice structures, each containing a rate and weight * @param maxOracleWeight The maximum weight among the OraclePrice elements in the oraclePrices array * @param size The number of meaningful OraclePrice elements added to the oraclePrices array */ struct Data { uint256 maxOracleWeight; uint256 size; OraclePrice[] oraclePrices; } /** * @notice Initializes an array of OraclePrices with a given maximum length and returns it wrapped inside a Data struct * @dev Uses inline assembly for memory allocation to avoid array zeroing and extra array copy to struct * @param maxArrLength The maximum length of the oraclePrices array * @return data Returns an instance of Data struct containing an OraclePrice array with a specified maximum length */ function init(uint256 maxArrLength) internal pure returns (Data memory data) { assembly ("memory-safe") { // solhint-disable-line no-inline-assembly data := mload(0x40) mstore(0x40, add(data, add(0x80, mul(maxArrLength, 0x40)))) mstore(add(data, 0x00), 0) mstore(add(data, 0x20), 0) mstore(add(data, 0x40), add(data, 0x60)) mstore(add(data, 0x60), maxArrLength) } } /** * @notice Appends an OraclePrice to the oraclePrices array in the provided Data struct if the OraclePrice has a non-zero weight * @dev If the weight of the OraclePrice is greater than the current maxOracleWeight, the maxOracleWeight is updated. The size (number of meaningful elements) of the array is incremented after appending the OraclePrice. * @param data The Data struct that contains the oraclePrices array, maxOracleWeight, and the current size * @param oraclePrice The OraclePrice to be appended to the oraclePrices array * @return isAppended A flag indicating whether the oraclePrice was appended or not */ function append(Data memory data, OraclePrice memory oraclePrice) internal pure returns (bool isAppended) { if (oraclePrice.weight > 0) { data.oraclePrices[data.size] = oraclePrice; data.size++; if (oraclePrice.weight > data.maxOracleWeight) { data.maxOracleWeight = oraclePrice.weight; } return true; } return false; } /** * @notice Calculates the weighted rate from the oracle prices data using a threshold filter * @dev Shrinks the `oraclePrices` array to remove any unused space, though it's unclear how this optimizes the code, but it is. Then calculates the weighted rate * considering only the oracle prices whose weight is above the threshold which is percent from max weight * @param data The data structure containing oracle prices, the maximum oracle weight and the size of the used oracle prices array * @param thresholdFilter The threshold to filter oracle prices based on their weight * @return weightedRate The calculated weighted rate * @return totalWeight The total weight of the oracle prices that passed the threshold */ function getRateAndWeight(Data memory data, uint256 thresholdFilter) internal pure returns (uint256 weightedRate, uint256 totalWeight) { // shrink oraclePrices array uint256 size = data.size; assembly ("memory-safe") { // solhint-disable-line no-inline-assembly let ptr := mload(add(data, 64)) mstore(ptr, size) } // calculate weighted rate for (uint256 i = 0; i < size; i++) { OraclePrice memory p = data.oraclePrices[i]; if (p.weight * 100 < data.maxOracleWeight * thresholdFilter) { continue; } weightedRate += p.rate * p.weight; totalWeight += p.weight; } if (totalWeight > 0) { unchecked { weightedRate /= totalWeight; } } } /** * @notice See `getRateAndWeight`. It uses SafeMath to prevent overflows. */ function getRateAndWeightWithSafeMath(Data memory data, uint256 thresholdFilter) internal pure returns (uint256 weightedRate, uint256 totalWeight) { // shrink oraclePrices array uint256 size = data.size; assembly ("memory-safe") { // solhint-disable-line no-inline-assembly let ptr := mload(add(data, 64)) mstore(ptr, size) } // calculate weighted rate for (uint256 i = 0; i < size; i++) { OraclePrice memory p = data.oraclePrices[i]; if (p.weight * 100 < data.maxOracleWeight * thresholdFilter) { continue; } (bool ok, uint256 weightedRateI) = p.rate.tryMul(p.weight); if (ok) { (ok, weightedRate) = _tryAdd(weightedRate, weightedRateI); if (ok) totalWeight += p.weight; } } if (totalWeight > 0) { unchecked { weightedRate /= totalWeight; } } } function _tryAdd(uint256 value, uint256 addition) private pure returns (bool, uint256) { unchecked { uint256 result = value + addition; if (result < value) return (false, value); return (true, result); } } }
{ "optimizer": { "enabled": true, "runs": 1000000 }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"bytes32","name":"_initcodeHash","type":"bytes32"},{"internalType":"uint24[]","name":"_fees","type":"uint24[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ConnectorShouldBeNone","type":"error"},{"inputs":[],"name":"PoolNotFound","type":"error"},{"inputs":[],"name":"PoolWithConnectorNotFound","type":"error"},{"inputs":[],"name":"FACTORY","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"INITCODE_HASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SUPPORTED_FEES_COUNT","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"fees","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"srcToken","type":"address"},{"internalType":"contract IERC20","name":"dstToken","type":"address"},{"internalType":"contract IERC20","name":"connector","type":"address"},{"internalType":"uint256","name":"thresholdFilter","type":"uint256"}],"name":"getRate","outputs":[{"internalType":"uint256","name":"rate","type":"uint256"},{"internalType":"uint256","name":"weight","type":"uint256"}],"stateMutability":"view","type":"function"}]
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106100675760003560e01c80633d549b32116100505780633d549b32146100f25780634acc79ed1461011a578063f84618841461014157600080fd5b8063152b8c691461006c5780632dd31000146100a6575b600080fd5b6100937f000000000000000000000000000000000000000000000000000000000000000481565b6040519081526020015b60405180910390f35b6100cd7f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f98481565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200161009d565b610105610100366004610cea565b610168565b6040805192835260208301919091520161009d565b61012d610128366004610d3b565b6103e5565b60405162ffffff909116815260200161009d565b6100937fe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b5481565b60008061018f60405180606001604052806000815260200160008152602001606081525090565b7fffffffffffffffffffffffff000000000000000000000000000000000000000173ffffffffffffffffffffffffffffffffffffffff861601610299576101f57f0000000000000000000000000000000000000000000000000000000000000004610414565b905060005b7f00000000000000000000000000000000000000000000000000000000000000048110156102935760008061025d8a8a600086600a811061023d5761023d610d54565b600a91828204019190066003029054906101000a900462ffffff16610461565b91509150610288604051806040016040528084815260200183815250856108fb90919063ffffffff16565b5050506001016101fa565b506103cd565b6102c560027f00000000000000000000000000000000000000000000000000000000000000040a610414565b905060005b7f00000000000000000000000000000000000000000000000000000000000000048110156103cb5760005b7f00000000000000000000000000000000000000000000000000000000000000048110156103c2576000806103388b8a600087600a811061023d5761023d610d54565b915091508060000361034b5750506103ba565b6000806103668b8d600088600a811061023d5761023d610d54565b915091508060000361037b57505050506103ba565b6103b4604051806040016040528061039c8786670de0b6b3a764000061096a565b81526020016103ab8685610a9e565b905288906108fb565b50505050505b6001016102f5565b506001016102ca565b505b6103d78185610ab4565b925092505094509492505050565b600081600a81106103f557600080fd5b600a9182820401919006600302915054906101000a900462ffffff1681565b61043860405180606001604052806000815260200160008152602001606081525090565b506040805182820281016080018252600080825260208201526060810191810182905291905290565b6000806000808573ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff16106104a15785876104a4565b86865b9150915060006104b5838388610b83565b905073ffffffffffffffffffffffffffffffffffffffff81163b6104e257600080945094505050506108f3565b8073ffffffffffffffffffffffffffffffffffffffff16631a6865026040518163ffffffff1660e01b8152600401602060405180830381865afa15801561052d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105519190610da8565b6fffffffffffffffffffffffffffffffff1693508360000361057c57600080945094505050506108f3565b6000808273ffffffffffffffffffffffffffffffffffffffff16633850c7bd6040518163ffffffff1660e01b81526004016040805180830381865afa1580156105c9573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105ed9190610dd5565b915073ffffffffffffffffffffffffffffffffffffffff16915060008373ffffffffffffffffffffffffffffffffffffffff1663d0c93a7c6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610654573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106789190610e0a565b9050806106858184610e83565b61068f9190610ef7565b9150868060005b600281810b13610854576040517ff30dba93000000000000000000000000000000000000000000000000000000008152848202860160020b600482015260009073ffffffffffffffffffffffffffffffffffffffff89169063f30dba939060240161010060405180830381865afa158015610715573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107399190610f2e565b505050505050600f0b91505080830192506107548b84610a9e565b9a5082600003610774576000809b509b50505050505050505050506108f3565b6040517ff30dba93000000000000000000000000000000000000000000000000000000008152858302870360020b600482015273ffffffffffffffffffffffffffffffffffffffff89169063f30dba939060240161010060405180830381865afa1580156107e6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061080a9190610f2e565b50505050600f9290920b9687900396935061082b92508d9150869050610a9e565b9a508360000361084b576000809b509b50505050505050505050506108f3565b50600101610696565b508773ffffffffffffffffffffffffffffffffffffffff168d73ffffffffffffffffffffffffffffffffffffffff16036108b2576060858161089e82670de0b6b3a7640000610fd8565b6108a992911c610fd8565b901c99506108ea565b846108dd817f0de0b6b3a7640000000000000000000000000000000000000000000000000000610fef565b6108e79190610fef565b99505b50505050505050505b935093915050565b6020810151600090156109605781836040015184602001518151811061092357610923610d54565b602002602001018190525082602001805180919061094090611003565b90525082516020830151111561095857602082015183525b506001610964565b5060005b92915050565b600080807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff858709858702925082811083820303915050806000036109c2578382816109b8576109b8610e25565b0492505050610a97565b808411610a2f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601560248201527f4d6174683a206d756c446976206f766572666c6f770000000000000000000000604482015260640160405180910390fd5b600084868809851960019081018716968790049682860381900495909211909303600082900391909104909201919091029190911760038402600290811880860282030280860282030280860282030280860282030280860282030280860290910302029150505b9392505050565b6000818310610aad5781610a97565b5090919050565b602082015160408301518190526000908190815b81811015610b6157600086604001518281518110610ae857610ae8610d54565b60200260200101519050858760000151610b029190610fd8565b6020820151610b12906064610fd8565b1015610b1e5750610b4f565b60208101518151610b2f9190610fd8565b610b39908661103b565b9450806020015184610b4b919061103b565b9350505b80610b5981611003565b915050610ac8565b508115610b7b57818381610b7757610b77610e25565b0492505b509250929050565b6040805173ffffffffffffffffffffffffffffffffffffffff808616602083015284169181019190915262ffffff821660608201526000907f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f98490608001604051602081830303815290604052805190602001207fe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54604051602001610c87939291907fff00000000000000000000000000000000000000000000000000000000000000815260609390931b7fffffffffffffffffffffffffffffffffffffffff0000000000000000000000001660018401526015830191909152603582015260550190565b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190528051602090910120949350505050565b73ffffffffffffffffffffffffffffffffffffffff81168114610ce757600080fd5b50565b60008060008060808587031215610d0057600080fd5b8435610d0b81610cc5565b93506020850135610d1b81610cc5565b92506040850135610d2b81610cc5565b9396929550929360600135925050565b600060208284031215610d4d57600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b80516fffffffffffffffffffffffffffffffff81168114610da357600080fd5b919050565b600060208284031215610dba57600080fd5b610a9782610d83565b8051600281900b8114610da357600080fd5b60008060408385031215610de857600080fd5b8251610df381610cc5565b9150610e0160208401610dc3565b90509250929050565b600060208284031215610e1c57600080fd5b610a9782610dc3565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160020b8360020b80610e9a57610e9a610e25565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81147fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000083141615610eee57610eee610e54565b90059392505050565b60008260020b8260020b028060020b9150808214610f1757610f17610e54565b5092915050565b80518015158114610da357600080fd5b600080600080600080600080610100898b031215610f4b57600080fd5b610f5489610d83565b9750602089015180600f0b8114610f6a57600080fd5b80975050604089015195506060890151945060808901518060060b8114610f9057600080fd5b60a08a0151909450610fa181610cc5565b60c08a015190935063ffffffff81168114610fbb57600080fd5b9150610fc960e08a01610f1e565b90509295985092959890939650565b808202811582820484141761096457610964610e54565b600082610ffe57610ffe610e25565b500490565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff820361103457611034610e54565b5060010190565b8082018082111561096457610964610e5456fea264697066735822122031dfbadb7b3ef436c400eef0dcbb9ef6363d188c45926d4ee147083843978fcf64736f6c63430008130033
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 34 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
Loading...
Loading
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.