Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Livepeer: correct handling of X96 Price #66

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/adapters/LivepeerAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ contract LivepeerAdapter is Adapter {
WETH.deposit{ value: ethBalance }();
ERC20(address(WETH)).safeApprove(address(UNISWAP_ROUTER), ethBalance);
// Calculate Slippage Threshold
uint256 twapPrice = TWAP.getPriceX96FromSqrtPriceX96(TWAP.getSqrtTwapX96(UNI_POOL, TWAP_INTERVAL));
uint256 twapPrice = TWAP.getInversePriceX96(TWAP.getPriceX96(TWAP.getSqrtTwapX96(UNI_POOL, TWAP_INTERVAL)));
uint256 amountOut = ethBalance * twapPrice >> 96;
// Create initial params for swap
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
tokenIn: address(WETH),
Expand All @@ -145,7 +146,7 @@ contract LivepeerAdapter is Adapter {
recipient: address(this),
deadline: block.timestamp,
amountIn: ethBalance,
amountOutMinimum: ethBalance * twapPrice * 90 / 100, // 10% slippage threshold
amountOutMinimum: amountOut * 897 / 1000, // 10% slippage threshold + 0.3% fee
sqrtPriceLimitX96: 0
});

Expand Down
6 changes: 5 additions & 1 deletion src/utils/TWAP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

pragma solidity >=0.8.19;

import "@uniswap/v3-core/interfaces/IUniswapV3Pool.sol";

Check warning on line 14 in src/utils/TWAP.sol

View workflow job for this annotation

GitHub Actions / lint

global import of path @uniswap/v3-core/interfaces/IUniswapV3Pool.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@uniswap/v3-core/libraries/TickMath.sol";

Check warning on line 15 in src/utils/TWAP.sol

View workflow job for this annotation

GitHub Actions / lint

global import of path @uniswap/v3-core/libraries/TickMath.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@uniswap/v3-core/libraries/FixedPoint96.sol";

Check warning on line 16 in src/utils/TWAP.sol

View workflow job for this annotation

GitHub Actions / lint

global import of path @uniswap/v3-core/libraries/FixedPoint96.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@uniswap/v3-core/libraries/FullMath.sol";

library TWAP {
Expand All @@ -33,7 +33,11 @@
}
}

function getPriceX96FromSqrtPriceX96(uint160 sqrtPriceX96) internal pure returns (uint256 priceX96) {
function getPriceX96(uint160 sqrtPriceX96) internal pure returns (uint256) {
return FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);
}

function getInversePriceX96(uint256 priceX96) internal pure returns (uint256) {
return FullMath.mulDiv(2 ** 96, 2 ** 96, priceX96);
}
}
Loading