Skip to content

Commit

Permalink
quoter doc
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jan 8, 2024
1 parent d511e09 commit 41adb7f
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 87 deletions.
9 changes: 9 additions & 0 deletions src/keywords.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"trade",
"swapping"
],
"/quoter": [
"quoter",
"quoting",
"exact input",
"exact output",
"single",
"multi",
"multihop"
],
"/initialize": [
"pool",
"initialize",
Expand Down
88 changes: 88 additions & 0 deletions src/pages/quoter/Quoter.sol
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);
}
}
13 changes: 13 additions & 0 deletions src/pages/quoter/QuoterSnippet.solsnippet
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)
);
16 changes: 0 additions & 16 deletions src/pages/quoter/TemplateSnippet.solsnippet

This file was deleted.

Loading

0 comments on commit 41adb7f

Please sign in to comment.