sohkai / uniswap-v3-cross-pool-oracle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uniswap V3 "Cross Pool" Oracle

🚨 Security status: unaudited

A UniswapV3 TWAP oracle with the ability to query asset prices across an intermediate liquidity pool (e.g. WBTC -> WETH -> USDC).

Includes a "hard mode" assetToAsset() variant that allows you to specify:

Option Description Default
Route-through token The intermediary token to hop between tokenIn/tokenOut WETH9
Pool fees The pool fees to specify which tokenIn/intermediary/tokenOut pool to use 3000 (30bps)

All other exposed functionality default to the stated defaults above.

Deployments

Usage

Useful to know for all price queries:

  • Reverts if twapPeriod is 0
  • Reverts if the twapPeriod too large for the underlying pool's history. In this case, you will have to increase the history stored by the pool by calling UniswapV3Pool#increaseObservationCardinalityNext() (see whitepaper section 5.1).
  • Reverts if no applicable pool (combination of tokenIn, tokenOut, and poolFee) is found
  • A pool fee of 3000 (30bps) and a route-through token of WETH9 are used by default; use assetToAssetThruRoute() to control these

assetToEth()

Query price of asset in ETH.

Example query:

Outputs ~423000000000000000 (0.423 ETH) as the USDC/ETH price on 05-22-2021.

ethToAsset()

Query price of ETH in asset.

Example query:

Outputs ~2360000000 (2360 USDC) as the ETH/USDC price on 05-22-2021.

assetToAsset()

Query price of one asset in another asset.

Example query:

Outputs ~38000000000 (3800 USDC) as the WBTC/USDC price on 05-22-2021.

assetToAssetThroughRoute()

Query price of one asset in another asset, but with control over which pools to use.

For ease of use, you may:

  • Specify routeThruToken as address(0) to default to WETH9
  • Specify either poolFees's values as 0 to default to 3000 (30bps)

Example query that specifies a routeThruToken and both pool fees:

Outputs ~261400000 (2.614 WBTC) for a 100,000 USDC to WBTC swap on 05-22-2021, pricing the swap through USDC -> USDT -> WBTC.

About

License:GNU General Public License v2.0


Languages

Language:Solidity 79.4%Language:JavaScript 20.6%