账户
0x48...15c9
UniV2 TWAP - USC

UniV2 TWAP - USC

$500
此合同的源代码已经过验证!
合同元数据
编译器
0.8.20+commit.a1b79de6
语言
Solidity
合同源代码
文件 1 的 15:AggregatorV3Interface.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface AggregatorV3Interface {
  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
    );
}
合同源代码
文件 2 的 15:Babylonian.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

// library copied from uniswap/lib/contracts/libraries/Babylonian.sol
// the only change is in pragma solidity version

// computes square roots using the babylonian method
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
library Babylonian {
  // credit for this implementation goes to
  // https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.sol#L687
  function sqrt(uint256 x) internal pure returns (uint256) {
    if (x == 0) return 0;
    // this block is equivalent to r = uint256(1) << (BitMath.mostSignificantBit(x) / 2);
    // however that code costs significantly more gas
    uint256 xx = x;
    uint256 r = 1;
    if (xx >= 0x100000000000000000000000000000000) {
      xx >>= 128;
      r <<= 64;
    }
    if (xx >= 0x10000000000000000) {
      xx >>= 64;
      r <<= 32;
    }
    if (xx >= 0x100000000) {
      xx >>= 32;
      r <<= 16;
    }
    if (xx >= 0x10000) {
      xx >>= 16;
      r <<= 8;
    }
    if (xx >= 0x100) {
      xx >>= 8;
      r <<= 4;
    }
    if (xx >= 0x10) {
      xx >>= 4;
      r <<= 2;
    }
    if (xx >= 0x8) {
      r <<= 1;
    }
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1;
    r = (r + x / r) >> 1; // Seven iterations should be enough
    uint256 r1 = x / r;
    return (r < r1 ? r : r1);
  }
}
合同源代码
文件 3 的 15:BitMath.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

// library copied from uniswap/lib/contracts/libraries/BitMath.sol
// the only change is in pragma solidity version

library BitMath {
  // returns the 0 indexed position of the most significant bit of the input x
  // s.t. x >= 2**msb and x < 2**(msb+1)
  function mostSignificantBit(uint256 x) internal pure returns (uint8 r) {
    require(x > 0, "BitMath::mostSignificantBit: zero");

    if (x >= 0x100000000000000000000000000000000) {
      x >>= 128;
      r += 128;
    }
    if (x >= 0x10000000000000000) {
      x >>= 64;
      r += 64;
    }
    if (x >= 0x100000000) {
      x >>= 32;
      r += 32;
    }
    if (x >= 0x10000) {
      x >>= 16;
      r += 16;
    }
    if (x >= 0x100) {
      x >>= 8;
      r += 8;
    }
    if (x >= 0x10) {
      x >>= 4;
      r += 4;
    }
    if (x >= 0x4) {
      x >>= 2;
      r += 2;
    }
    if (x >= 0x2) r += 1;
  }

  // returns the 0 indexed position of the least significant bit of the input x
  // s.t. (x & 2**lsb) != 0 and (x & (2**(lsb) - 1)) == 0)
  // i.e. the bit at the index is set and the mask of all lower bits is 0
  function leastSignificantBit(uint256 x) internal pure returns (uint8 r) {
    require(x > 0, "BitMath::leastSignificantBit: zero");

    r = 255;
    if (x & type(uint128).max > 0) {
      r -= 128;
    } else {
      x >>= 128;
    }
    if (x & type(uint64).max > 0) {
      r -= 64;
    } else {
      x >>= 64;
    }
    if (x & type(uint32).max > 0) {
      r -= 32;
    } else {
      x >>= 32;
    }
    if (x & type(uint16).max > 0) {
      r -= 16;
    } else {
      x >>= 16;
    }
    if (x & type(uint8).max > 0) {
      r -= 8;
    } else {
      x >>= 8;
    }
    if (x & 0xf > 0) {
      r -= 4;
    } else {
      x >>= 4;
    }
    if (x & 0x3 > 0) {
      r -= 2;
    } else {
      x >>= 2;
    }
    if (x & 0x1 > 0) r -= 1;
  }
}
合同源代码
文件 4 的 15:FixedPoint.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

// library copied from uniswap/lib/contracts/libraries/FixedPoint.sol
// the only change is in pragma solidity version

import "./FullMath.sol";
import "./Babylonian.sol";
import "./BitMath.sol";

// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))
library FixedPoint {
  // range: [0, 2**112 - 1]
  // resolution: 1 / 2**112
  struct uq112x112 {
    uint224 _x;
  }

  // range: [0, 2**144 - 1]
  // resolution: 1 / 2**112
  struct uq144x112 {
    uint256 _x;
  }

  uint8 public constant RESOLUTION = 112;
  uint256 public constant Q112 = 0x10000000000000000000000000000; // 2**112
  uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000; // 2**224
  uint256 private constant LOWER_MASK = 0xffffffffffffffffffffffffffff; // decimal of UQ*x112 (lower 112 bits)

  // encode a uint112 as a UQ112x112
  function encode(uint112 x) internal pure returns (uq112x112 memory) {
    return uq112x112(uint224(x) << RESOLUTION);
  }

  // encodes a uint144 as a UQ144x112
  function encode144(uint144 x) internal pure returns (uq144x112 memory) {
    return uq144x112(uint256(x) << RESOLUTION);
  }

  // decode a UQ112x112 into a uint112 by truncating after the radix point
  function decode(uq112x112 memory self) internal pure returns (uint112) {
    return uint112(self._x >> RESOLUTION);
  }

  // decode a UQ144x112 into a uint144 by truncating after the radix point
  function decode144(uq144x112 memory self) internal pure returns (uint144) {
    return uint144(self._x >> RESOLUTION);
  }

  // multiply a UQ112x112 by a uint, returning a UQ144x112
  // reverts on overflow
  function mul(uq112x112 memory self, uint256 y) internal pure returns (uq144x112 memory) {
    uint256 z = 0;
    require(y == 0 || (z = self._x * y) / y == self._x, "FixedPoint::mul: overflow");
    return uq144x112(z);
  }

  // multiply a UQ112x112 by an int and decode, returning an int
  // reverts on overflow
  function muli(uq112x112 memory self, int256 y) internal pure returns (int256) {
    uint256 z = FullMath.mulDiv(self._x, uint256(y < 0 ? -y : y), Q112);
    require(z < 2 ** 255, "FixedPoint::muli: overflow");
    return y < 0 ? -int256(z) : int256(z);
  }

  // multiply a UQ112x112 by a UQ112x112, returning a UQ112x112
  // lossy
  function muluq(uq112x112 memory self, uq112x112 memory other) internal pure returns (uq112x112 memory) {
    if (self._x == 0 || other._x == 0) {
      return uq112x112(0);
    }
    uint112 upper_self = uint112(self._x >> RESOLUTION); // * 2^0
    uint112 lower_self = uint112(self._x & LOWER_MASK); // * 2^-112
    uint112 upper_other = uint112(other._x >> RESOLUTION); // * 2^0
    uint112 lower_other = uint112(other._x & LOWER_MASK); // * 2^-112

    // partial products
    uint224 upper = uint224(upper_self) * upper_other; // * 2^0
    uint224 lower = uint224(lower_self) * lower_other; // * 2^-224
    uint224 uppers_lowero = uint224(upper_self) * lower_other; // * 2^-112
    uint224 uppero_lowers = uint224(upper_other) * lower_self; // * 2^-112

    // so the bit shift does not overflow
    require(upper <= type(uint112).max, "FixedPoint::muluq: upper overflow");

    // this cannot exceed 256 bits, all values are 224 bits
    uint256 sum = uint256(upper << RESOLUTION) + uppers_lowero + uppero_lowers + (lower >> RESOLUTION);

    // so the cast does not overflow
    require(sum <= type(uint224).max, "FixedPoint::muluq: sum overflow");

    return uq112x112(uint224(sum));
  }

  // divide a UQ112x112 by a UQ112x112, returning a UQ112x112
  function divuq(uq112x112 memory self, uq112x112 memory other) internal pure returns (uq112x112 memory) {
    require(other._x > 0, "FixedPoint::divuq: division by zero");
    if (self._x == other._x) {
      return uq112x112(uint224(Q112));
    }
    if (self._x <= type(uint144).max) {
      uint256 value = (uint256(self._x) << RESOLUTION) / other._x;
      require(value <= type(uint224).max, "FixedPoint::divuq: overflow");
      return uq112x112(uint224(value));
    }

    uint256 result = FullMath.mulDiv(Q112, self._x, other._x);
    require(result <= type(uint224).max, "FixedPoint::divuq: overflow");
    return uq112x112(uint224(result));
  }

  // returns a UQ112x112 which represents the ratio of the numerator to the denominator
  // can be lossy
  function fraction(uint256 numerator, uint256 denominator) internal pure returns (uq112x112 memory) {
    require(denominator > 0, "FixedPoint::fraction: division by zero");
    if (numerator == 0) return FixedPoint.uq112x112(0);

    if (numerator <= type(uint144).max) {
      uint256 result = (numerator << RESOLUTION) / denominator;
      require(result <= type(uint224).max, "FixedPoint::fraction: overflow");
      return uq112x112(uint224(result));
    } else {
      uint256 result = FullMath.mulDiv(numerator, Q112, denominator);
      require(result <= type(uint224).max, "FixedPoint::fraction: overflow");
      return uq112x112(uint224(result));
    }
  }

  // take the reciprocal of a UQ112x112
  // reverts on overflow
  // lossy
  function reciprocal(uq112x112 memory self) internal pure returns (uq112x112 memory) {
    require(self._x != 0, "FixedPoint::reciprocal: reciprocal of zero");
    require(self._x != 1, "FixedPoint::reciprocal: overflow");
    return uq112x112(uint224(Q224 / self._x));
  }

  // square root of a UQ112x112
  // lossy between 0/1 and 40 bits
  function sqrt(uq112x112 memory self) internal pure returns (uq112x112 memory) {
    if (self._x <= type(uint144).max) {
      return uq112x112(uint224(Babylonian.sqrt(uint256(self._x) << 112)));
    }

    uint8 safeShiftBits = 255 - BitMath.mostSignificantBit(self._x);
    safeShiftBits -= safeShiftBits % 2;
    return uq112x112(uint224(Babylonian.sqrt(uint256(self._x) << safeShiftBits) << ((112 - safeShiftBits) / 2)));
  }
}
合同源代码
文件 5 的 15:FullMath.sol
// SPDX-License-Identifier: CC-BY-4.0
pragma solidity ^0.8.0;

// library copied from uniswap/lib/contracts/libraries/FullMath.sol
// the only change is in pragma solidity version

// taken from https://medium.com/coinmonks/math-in-solidity-part-3-percents-and-proportions-4db014e080b1
// license is CC-BY-4.0
library FullMath {
  function fullMul(uint256 x, uint256 y) internal pure returns (uint256 l, uint256 h) {
    uint256 mm = mulmod(x, y, type(uint256).max);
    l = x * y;
    h = mm - l;
    if (mm < l) h -= 1;
  }

  function fullDiv(uint256 l, uint256 h, uint256 d) private pure returns (uint256) {
    uint256 pow2 = d & (~d + 1);
    d /= pow2;
    l /= pow2;
    l += h * ((~pow2 + 1) / pow2 + 1);
    uint256 r = 1;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    r *= 2 - d * r;
    return l * r;
  }

  function mulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
    (uint256 l, uint256 h) = fullMul(x, y);

    uint256 mm = mulmod(x, y, d);
    if (mm > l) h -= 1;
    l -= mm;

    if (h == 0) return l / d;

    require(h < d, "FullMath: FULLDIV_OVERFLOW");
    return fullDiv(l, h, d);
  }
}
合同源代码
文件 6 的 15:IERC20.sol
// 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);
}
合同源代码
文件 7 的 15: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);
}
合同源代码
文件 8 的 15:IOracle.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IOracle {
  /// @notice Gets name of price adapter
  /// @return name Name of price adapter
  function name() external view returns (string memory name);

  /// @notice Gets decimals of price adapter
  /// @return decimals Decimals of price adapter
  function decimals() external view returns (uint8 decimals);

  /// @notice Gets price base token from Chainlink price feed
  /// @return price price in USD for the 1 baseToken
  function peek() external view returns (uint256 price);
}
合同源代码
文件 9 的 15:IUniswapV2Factory.sol
pragma solidity >=0.5.0;

interface IUniswapV2Factory {
    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}
合同源代码
文件 10 的 15:IUniswapV2Pair.sol
pragma solidity >=0.5.0;

interface IUniswapV2Pair {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external pure returns (string memory);
    function symbol() external pure returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);

    function DOMAIN_SEPARATOR() external view returns (bytes32);
    function PERMIT_TYPEHASH() external pure returns (bytes32);
    function nonces(address owner) external view returns (uint);

    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);

    function MINIMUM_LIQUIDITY() external pure returns (uint);
    function factory() external view returns (address);
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function price0CumulativeLast() external view returns (uint);
    function price1CumulativeLast() external view returns (uint);
    function kLast() external view returns (uint);

    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint amount0, uint amount1);
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function skim(address to) external;
    function sync() external;

    function initialize(address, address) external;
}
合同源代码
文件 11 的 15:IUniswapV2TwapOracle.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./IOracle.sol";

interface IUniswapV2TwapOracle is IOracle {
  struct CumulativePriceSnapshot {
    uint256 price0;
    uint256 price1;
    uint32 blockTimestamp;
  }

  event UpdateCumulativePricesSnapshot();

  error NoReserves();
  error InvalidToken();
  error PeriodNotPassed();

  /// @notice Takes cumulative price snapshot and updates previous and last snapshots
  /// @notice Cumulative price is in quote token
  /// @custom:usage This function should be called periodically by external keeper
  function updateCumulativePricesSnapshot() external;

  /// @notice Gets TWAP quote for given token
  /// @notice TWAP quote is in quote token
  /// @param token Token address
  /// @param amountIn Amount of token to get quote for
  /// @return amountOut Quote amount in quote token
  function getTwapQuote(address token, uint256 amountIn) external view returns (uint256 amountOut);

  /// @notice Gets TWAP quote for base token
  /// @dev This function is used by PriceFeedAggregator contract
  /// @return price price in USD for the 1 baseToken
  function peek() external view returns (uint256 price);
}
合同源代码
文件 12 的 15:SafeMath.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

// library copied from uniswap/v2-core/contracts/libraries/SafeMath.sol
// the only change is in pragma solidity version.
// this contract was the reason of incompatible solidity version error

// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)

library SafeMath {
  function add(uint x, uint y) internal pure returns (uint z) {
    require((z = x + y) >= x, "ds-math-add-overflow");
  }

  function sub(uint x, uint y) internal pure returns (uint z) {
    require((z = x - y) <= x, "ds-math-sub-underflow");
  }

  function mul(uint x, uint y) internal pure returns (uint z) {
    require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
  }
}
合同源代码
文件 13 的 15:UniswapV2Library.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

// library copied from uniswap/v2-core/contracts/libraries/UniswapV2Library.sol
// the only change is in pragma solidity version.

import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";

import "./SafeMath.sol";

library UniswapV2Library {
  using SafeMath for uint;

  // returns sorted token addresses, used to handle return values from pairs sorted in this order
  function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
    require(tokenA != tokenB, "UniswapV2Library: IDENTICAL_ADDRESSES");
    (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
    require(token0 != address(0), "UniswapV2Library: ZERO_ADDRESS");
  }

  // calculates the CREATE2 address for a pair without making any external calls
  function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
    (address token0, address token1) = sortTokens(tokenA, tokenB);
    pair = address(
      uint160(
        uint(
          keccak256(
            abi.encodePacked(
              hex"ff",
              factory,
              keccak256(abi.encodePacked(token0, token1)),
              hex"96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f" // init code hash
            )
          )
        )
      )
    );
  }

  // fetches and sorts the reserves for a pair
  function getReserves(
    address factory,
    address tokenA,
    address tokenB
  ) internal view returns (uint reserveA, uint reserveB) {
    (address token0, ) = sortTokens(tokenA, tokenB);
    (uint reserve0, uint reserve1, ) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
    (reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
  }

  // given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
  function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
    require(amountA > 0, "UniswapV2Library: INSUFFICIENT_AMOUNT");
    require(reserveA > 0 && reserveB > 0, "UniswapV2Library: INSUFFICIENT_LIQUIDITY");
    amountB = amountA.mul(reserveB) / reserveA;
  }

  // given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
  function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
    require(amountIn > 0, "UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT");
    require(reserveIn > 0 && reserveOut > 0, "UniswapV2Library: INSUFFICIENT_LIQUIDITY");
    uint amountInWithFee = amountIn.mul(997);
    uint numerator = amountInWithFee.mul(reserveOut);
    uint denominator = reserveIn.mul(1000).add(amountInWithFee);
    amountOut = numerator / denominator;
  }

  // given an output amount of an asset and pair reserves, returns a required input amount of the other asset
  function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
    require(amountOut > 0, "UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT");
    require(reserveIn > 0 && reserveOut > 0, "UniswapV2Library: INSUFFICIENT_LIQUIDITY");
    uint numerator = reserveIn.mul(amountOut).mul(1000);
    uint denominator = reserveOut.sub(amountOut).mul(997);
    amountIn = (numerator / denominator).add(1);
  }

  // performs chained getAmountOut calculations on any number of pairs
  function getAmountsOut(
    address factory,
    uint amountIn,
    address[] memory path
  ) internal view returns (uint[] memory amounts) {
    require(path.length >= 2, "UniswapV2Library: INVALID_PATH");
    amounts = new uint[](path.length);
    amounts[0] = amountIn;
    for (uint i; i < path.length - 1; i++) {
      (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
      amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
    }
  }

  // performs chained getAmountIn calculations on any number of pairs
  function getAmountsIn(
    address factory,
    uint amountOut,
    address[] memory path
  ) internal view returns (uint[] memory amounts) {
    require(path.length >= 2, "UniswapV2Library: INVALID_PATH");
    amounts = new uint[](path.length);
    amounts[amounts.length - 1] = amountOut;
    for (uint i = path.length - 1; i > 0; i--) {
      (uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
      amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
    }
  }
}
合同源代码
文件 14 的 15:UniswapV2OracleLibrary.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

// library copied from uniswap/v2-core/contracts/libraries/UniswapV2OracleLibrary.sol
// the only change is in pragma solidity version.

import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";

import "./FixedPoint.sol";

// library with helper methods for oracles that are concerned with computing average prices
library UniswapV2OracleLibrary {
  using FixedPoint for *;

  // helper function that returns the current block timestamp within the range of uint32, i.e. [0, 2**32 - 1]
  function currentBlockTimestamp() internal view returns (uint32) {
    return uint32(block.timestamp % 2 ** 32);
  }

  // produces the cumulative price using counterfactuals to save gas and avoid a call to sync.
  function currentCumulativePrices(
    address pair
  ) internal view returns (uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) {
    blockTimestamp = currentBlockTimestamp();
    price0Cumulative = IUniswapV2Pair(pair).price0CumulativeLast();
    price1Cumulative = IUniswapV2Pair(pair).price1CumulativeLast();

    // if time has elapsed since the last update on the pair, mock the accumulated price values
    (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast) = IUniswapV2Pair(pair).getReserves();
    if (blockTimestampLast != blockTimestamp) {
      // subtraction overflow is desired
      uint32 timeElapsed = blockTimestamp - blockTimestampLast;
      // addition overflow is desired
      // counterfactual
      price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
      // counterfactual
      price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
    }
  }
}
合同源代码
文件 15 的 15:UniswapV2TwapOracle.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "contracts/interfaces/IUniswapV2TwapOracle.sol";
import "contracts/uniswap/libraries/FixedPoint.sol";
import "contracts/uniswap/libraries/UniswapV2OracleLibrary.sol";
import "contracts/uniswap/libraries/UniswapV2Library.sol";

/// @title Uniswap V2 TWAP Oracle
/// @notice PriceFeedAggregator uses this contract to be TWAP price of USC and CHI tokens
/// @notice One instance of this contract handles one Uniswap V2 pair
/// @notice This contract takes snapshots of cumulative prices and calculates TWAP price from them
contract UniswapV2TwapOracle is IUniswapV2TwapOracle {
  using FixedPoint for *;

  AggregatorV3Interface public immutable quoteTokenChainlinkFeed;
  IUniswapV2Pair public immutable pair;

  uint8 public immutable decimals;
  uint32 public immutable updatePeriod;
  uint32 public immutable minPeriodFromLastSnapshot;

  address public immutable baseToken;
  uint128 public immutable baseAmount; // should be 10^baseToken.decimals

  address public immutable token0;
  address public immutable token1;

  CumulativePriceSnapshot public previousSnapshot;
  CumulativePriceSnapshot public lastSnapshot;

  constructor(
    address _factory,
    address _baseToken,
    address _quoteToken,
    uint32 _updatePeriod,
    uint32 _minPeriodFromLastSnapshot,
    AggregatorV3Interface _quoteTokenChainlinkFeed
  ) {
    baseToken = _baseToken;
    baseAmount = uint128(10 ** (IERC20Metadata(baseToken).decimals()));

    updatePeriod = _updatePeriod;
    minPeriodFromLastSnapshot = _minPeriodFromLastSnapshot;

    quoteTokenChainlinkFeed = _quoteTokenChainlinkFeed;
    decimals = _quoteTokenChainlinkFeed.decimals();

    IUniswapV2Pair _pair = IUniswapV2Pair(UniswapV2Library.pairFor(_factory, _baseToken, _quoteToken));
    pair = _pair;
    token0 = _pair.token0();
    token1 = _pair.token1();

    (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) = UniswapV2OracleLibrary
      .currentCumulativePrices(address(pair));

    lastSnapshot = CumulativePriceSnapshot({
      price0: price0Cumulative,
      price1: price1Cumulative,
      blockTimestamp: blockTimestamp
    });
    previousSnapshot = lastSnapshot;

    uint112 reserve0;
    uint112 reserve1;
    (reserve0, reserve1, lastSnapshot.blockTimestamp) = _pair.getReserves();

    // ensure that there's liquidity in the pair
    if (reserve0 == 0 || reserve1 == 0) {
      revert NoReserves();
    }
  }

  /// @inheritdoc IOracle
  function name() external view returns (string memory) {
    return string(abi.encodePacked("UniV2 TWAP - ", IERC20Metadata(baseToken).symbol()));
  }

  /// @inheritdoc IUniswapV2TwapOracle
  function updateCumulativePricesSnapshot() public {
    (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) = UniswapV2OracleLibrary
      .currentCumulativePrices(address(pair));

    if (blockTimestamp - lastSnapshot.blockTimestamp < updatePeriod) {
      revert PeriodNotPassed();
    }

    previousSnapshot = lastSnapshot;
    lastSnapshot = CumulativePriceSnapshot({
      price0: price0Cumulative,
      price1: price1Cumulative,
      blockTimestamp: blockTimestamp
    });

    emit UpdateCumulativePricesSnapshot();
  }

  /// @inheritdoc IUniswapV2TwapOracle
  function getTwapQuote(address token, uint256 amountIn) public view returns (uint256 amountOut) {
    (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) = UniswapV2OracleLibrary
      .currentCumulativePrices(address(pair));

    uint32 timeElapsedFromLast = blockTimestamp - lastSnapshot.blockTimestamp;

    CumulativePriceSnapshot storage snapshot;
    if (timeElapsedFromLast >= minPeriodFromLastSnapshot) {
      snapshot = lastSnapshot;
    } else {
      snapshot = previousSnapshot;
    }

    uint32 timeElapsed = blockTimestamp - snapshot.blockTimestamp;

    if (token == token0) {
      // overflow is desired, casting never truncates
      // cumulative price is in (uq112x112 price * seconds) units so we simply wrap it after division by time elapsed
      FixedPoint.uq112x112 memory price0Average = FixedPoint.uq112x112(
        uint224((price0Cumulative - snapshot.price0) / timeElapsed)
      );
      amountOut = price0Average.mul(amountIn).decode144();
    } else {
      if (token != token1) {
        revert InvalidToken();
      }
      FixedPoint.uq112x112 memory price1Average = FixedPoint.uq112x112(
        uint224((price1Cumulative - snapshot.price1) / timeElapsed)
      );
      amountOut = price1Average.mul(amountIn).decode144();
    }

    return amountOut;
  }

  /// @inheritdoc IUniswapV2TwapOracle
  function peek() external view returns (uint256 price) {
    uint256 quotedAmount = getTwapQuote(baseToken, baseAmount);

    (, int256 quoteTokenPriceInUSD, , , ) = quoteTokenChainlinkFeed.latestRoundData();
    uint256 priceInUSD = (uint256(quoteTokenPriceInUSD) * quotedAmount) / baseAmount; // quote token price will be always greater than 0, so it's ok to convert int to uint

    return priceInUSD;
  }
}
设置
{
  "compilationTarget": {
    "contracts/oracles/UniswapV2TwapOracle.sol": "UniswapV2TwapOracle"
  },
  "evmVersion": "shanghai",
  "libraries": {},
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "enabled": true,
    "runs": 1000
  },
  "remappings": [],
  "viaIR": true
}
ABI
[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_baseToken","type":"address"},{"internalType":"address","name":"_quoteToken","type":"address"},{"internalType":"uint32","name":"_updatePeriod","type":"uint32"},{"internalType":"uint32","name":"_minPeriodFromLastSnapshot","type":"uint32"},{"internalType":"contract AggregatorV3Interface","name":"_quoteTokenChainlinkFeed","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"InvalidToken","type":"error"},{"inputs":[],"name":"NoReserves","type":"error"},{"inputs":[],"name":"PeriodNotPassed","type":"error"},{"anonymous":false,"inputs":[],"name":"UpdateCumulativePricesSnapshot","type":"event"},{"inputs":[],"name":"baseAmount","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"baseToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"}],"name":"getTwapQuote","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastSnapshot","outputs":[{"internalType":"uint256","name":"price0","type":"uint256"},{"internalType":"uint256","name":"price1","type":"uint256"},{"internalType":"uint32","name":"blockTimestamp","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minPeriodFromLastSnapshot","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pair","outputs":[{"internalType":"contract IUniswapV2Pair","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"peek","outputs":[{"internalType":"uint256","name":"price","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"previousSnapshot","outputs":[{"internalType":"uint256","name":"price0","type":"uint256"},{"internalType":"uint256","name":"price1","type":"uint256"},{"internalType":"uint32","name":"blockTimestamp","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"quoteTokenChainlinkFeed","outputs":[{"internalType":"contract AggregatorV3Interface","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"updateCumulativePricesSnapshot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"updatePeriod","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"}]