-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d511e09
commit 41adb7f
Showing
8 changed files
with
274 additions
and
87 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
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,88 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import "forge-std/Test.sol"; | ||
import {IHooks} from "v4-core/interfaces/IHooks.sol"; | ||
import {Hooks} from "v4-core/libraries/Hooks.sol"; | ||
import {TickMath} from "v4-core/libraries/TickMath.sol"; | ||
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol"; | ||
import {PoolKey} from "v4-core/types/PoolKey.sol"; | ||
import {BalanceDelta} from "v4-core/types/BalanceDelta.sol"; | ||
import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol"; | ||
import {Constants} from "v4-core/../test/utils/Constants.sol"; | ||
import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol"; | ||
import {HookTest} from "@v4-by-example/utils/HookTest.sol"; | ||
import {IQuoter} from "v4-periphery/interfaces/IQuoter.sol"; | ||
import {Quoter} from "v4-periphery/lens/Quoter.sol"; | ||
|
||
contract QuoterTest is HookTest { | ||
using PoolIdLibrary for PoolKey; | ||
using CurrencyLibrary for Currency; | ||
|
||
PoolKey poolKey; | ||
PoolId poolId; | ||
Quoter quoter; | ||
|
||
function setUp() public { | ||
// creates the pool manager, test tokens, and other utility routers | ||
HookTest.initHookTestEnv(); | ||
quoter = new Quoter(address(manager)); | ||
|
||
// Create the pool | ||
poolKey = | ||
PoolKey(Currency.wrap(address(token0)), Currency.wrap(address(token1)), 3000, 60, IHooks(address(0x0))); | ||
poolId = poolKey.toId(); | ||
initializeRouter.initialize(poolKey, Constants.SQRT_RATIO_1_1, ZERO_BYTES); | ||
|
||
// Provide liquidity to the pool | ||
modifyPositionRouter.modifyLiquidity( | ||
poolKey, | ||
IPoolManager.ModifyLiquidityParams(TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 1000 ether), | ||
ZERO_BYTES | ||
); | ||
} | ||
|
||
function testQuoter_output() public { | ||
uint128 amountIn = 1e18; | ||
bool zeroForOne = true; | ||
uint160 MAX_SLIPPAGE = zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT; | ||
|
||
// get the quote | ||
PoolKey memory key = poolKey; | ||
(int128[] memory deltaAmounts, uint160 sqrtPriceX96After,) = quoter.quoteExactInputSingle( | ||
IQuoter.QuoteExactSingleParams(key, zeroForOne, address(this), amountIn, MAX_SLIPPAGE, ZERO_BYTES) | ||
); | ||
|
||
// output is amount 1 | ||
int128 outputAmount = deltaAmounts[1]; | ||
console2.log("Quoted output amount: ", int256(outputAmount)); | ||
|
||
// Perform a test swap | ||
BalanceDelta swapDelta = swap(poolKey, int256(uint256(amountIn)), zeroForOne, ZERO_BYTES); | ||
|
||
// quote agrees with the actual swap | ||
assertEq(outputAmount, swapDelta.amount1()); | ||
} | ||
|
||
function testQuoter_input() public { | ||
uint128 amountOut = 1e18; | ||
bool zeroForOne = true; | ||
uint160 MAX_SLIPPAGE = zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT; | ||
|
||
// get the quote | ||
PoolKey memory key = poolKey; | ||
(int128[] memory deltaAmounts, uint160 sqrtPriceX96After,) = quoter.quoteExactOutputSingle( | ||
IQuoter.QuoteExactSingleParams(key, zeroForOne, address(this), amountOut, MAX_SLIPPAGE, ZERO_BYTES) | ||
); | ||
|
||
// input (quoted) is amount 0 | ||
int128 inputAmount = deltaAmounts[0]; | ||
console2.log("Quoted input amount: ", int256(inputAmount)); | ||
|
||
// Perform a exact-output swap | ||
BalanceDelta swapDelta = swap(poolKey, -int256(uint256(amountOut)), zeroForOne, ZERO_BYTES); | ||
assertEq(inputAmount, swapDelta.amount0()); | ||
(uint160 sqrtPriceX96,,) = manager.getSlot0(poolId); | ||
assertEq(sqrtPriceX96After, sqrtPriceX96); | ||
} | ||
} |
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,13 @@ | ||
import {IQuoter} from "v4-periphery/interfaces/IQuoter.sol"; | ||
|
||
PoolKey memory key; | ||
uint128 amountIn = 1e18; | ||
bool zeroForOne = true; | ||
uint160 MAX_SLIPPAGE = zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT; | ||
bytes memory hookData; | ||
|
||
// exact input will quote deltaAmounts[1] (output) | ||
// exact output will quote deltaAmounts[0] (input) | ||
(int128[] memory deltaAmounts, uint160 sqrtPriceX96After,) = quoter.quoteExactInputSingle( | ||
IQuoter.QuoteExactSingleParams(key, zeroForOne, address(this), amountIn, MAX_SLIPPAGE, hookData) | ||
); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.