generated from PaulRBerg/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Livepeer: Fix Uniswap interaction, use TWAP oracle
- Loading branch information
1 parent
805ebcb
commit 59a7a1b
Showing
5 changed files
with
68 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule uniswap-v3-core
added at
f55f3a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// _____ _ _ | ||
// |_ _| | | (_) | ||
// | | ___ _ __ __| | ___ _ __ _ _______ | ||
// | |/ _ \ '_ \ / _` |/ _ \ '__| |_ / _ \ | ||
// | | __/ | | | (_| | __/ | | |/ / __/ | ||
// \_/\___|_| |_|\__,_|\___|_| |_/___\___| | ||
// | ||
// Copyright (c) Tenderize Labs Ltd | ||
|
||
import "@uniswap/v3-core/interfaces/IUniswapV3Pool.sol"; | ||
Check failure on line 12 in src/utils/TWAP.sol GitHub Actions / lint
|
||
import "@uniswap/v3-core/libraries/TickMath.sol"; | ||
import "@uniswap/v3-core/libraries/FixedPoint96.sol"; | ||
import "@uniswap/v3-core/libraries/FullMath.sol"; | ||
|
||
library TWAP { | ||
function getSqrtTwapX96(address uniswapV3Pool, uint32 twapInterval) internal view returns (uint160 sqrtPriceX96) { | ||
if (twapInterval == 0) { | ||
// return the current price if twapInterval == 0 | ||
(sqrtPriceX96,,,,,,) = IUniswapV3Pool(uniswapV3Pool).slot0(); | ||
} else { | ||
uint32[] memory secondsAgos = new uint32[](2); | ||
secondsAgos[0] = twapInterval; // from (before) | ||
secondsAgos[1] = 0; // to (now) | ||
|
||
(int56[] memory tickCumulatives,) = IUniswapV3Pool(uniswapV3Pool).observe(secondsAgos); | ||
|
||
// tick(imprecise as it's an integer) to price | ||
sqrtPriceX96 = TickMath.getSqrtRatioAtTick(int24((tickCumulatives[1] - tickCumulatives[0]) / int32(twapInterval))); | ||
} | ||
} | ||
|
||
function getPriceX96FromSqrtPriceX96(uint160 sqrtPriceX96) internal pure returns (uint256 priceX96) { | ||
return FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96); | ||
} | ||
} |