Skip to content

Commit

Permalink
combine IQuoter structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm committed Dec 18, 2023
1 parent 67772de commit fbb6d1b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
22 changes: 7 additions & 15 deletions contracts/interfaces/IQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ interface IQuoter {
bytes hookData;
}

struct QuoteExactInputParams {
Currency currencyIn;
struct QuoteExactParams {
Currency exactCurrency;
PathKey[] path;
address recipient;
uint128 amountIn;
}

struct QuoteExactOutputParams {
Currency currencyOut;
PathKey[] path;
address recipient;
uint128 amountOut;
uint160 sqrtPriceLimitX96;
uint128 exactAmount;
}

/// @notice Returns the delta amounts for a given exact input swap of a single pool
Expand All @@ -67,11 +59,11 @@ interface IQuoter {
/// currencyIn The input currency of the swap
/// path The path of the swap encoded as PathKeys that contains currency, fee, tickSpacing, and hook info
/// recipient The intended recipient of the output tokens
/// amountIn The desired input amount
/// exactAmount The desired input amount
/// @return deltaAmounts Delta amounts along the path resulted from the swap
/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path
/// @return initializedTicksLoadedList List of the initialized ticks that the swap loaded for each pool in the path
function quoteExactInput(QuoteExactInputParams memory params)
function quoteExactInput(QuoteExactParams memory params)
external
returns (
int128[] memory deltaAmounts,
Expand Down Expand Up @@ -99,11 +91,11 @@ interface IQuoter {
/// currencyOut The output currency of the swap
/// path The path of the swap encoded as PathKeys that contains currency, fee, tickSpacing, and hook info
/// recipient The intended recipient of the output tokens
/// amountOut The desired output amount
/// exactAmount The desired output amount
/// @return deltaAmounts Delta amounts along the path resulted from the swap
/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path
/// @return initializedTicksLoadedList List of the initialized ticks that the swap loaded for each pool in the path
function quoteExactOutput(QuoteExactOutputParams memory params)
function quoteExactOutput(QuoteExactParams memory params)
external
returns (
int128[] memory deltaAmounts,
Expand Down
16 changes: 8 additions & 8 deletions contracts/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract Quoter is IQuoter, ILockCallback {
}

/// @inheritdoc IQuoter
function quoteExactInput(QuoteExactInputParams memory params)
function quoteExactInput(QuoteExactParams memory params)
external
returns (
int128[] memory deltaAmounts,
Expand Down Expand Up @@ -87,7 +87,7 @@ contract Quoter is IQuoter, ILockCallback {
}

/// @inheritdoc IQuoter
function quoteExactOutput(QuoteExactOutputParams memory params)
function quoteExactOutput(QuoteExactParams memory params)
public
override
returns (
Expand Down Expand Up @@ -167,7 +167,7 @@ contract Quoter is IQuoter, ILockCallback {
}

/// @dev quote an ExactInput swap along a path of tokens, then revert with the result
function _quoteExactInput(QuoteExactInputParams memory params) public selfOnly returns (bytes memory) {
function _quoteExactInput(QuoteExactParams memory params) public selfOnly returns (bytes memory) {
uint256 pathLength = params.path.length;

int128[] memory deltaAmounts = new int128[](pathLength + 1);
Expand All @@ -178,13 +178,13 @@ contract Quoter is IQuoter, ILockCallback {

for (uint256 i = 0; i < pathLength; i++) {
(PoolKey memory poolKey, bool zeroForOne) =
PathKeyLib.getPoolAndSwapDirection(params.path[i], i == 0 ? params.currencyIn : prevCurrencyOut);
PathKeyLib.getPoolAndSwapDirection(params.path[i], i == 0 ? params.exactCurrency : prevCurrencyOut);
(, int24 tickBefore,) = manager.getSlot0(poolKey.toId());

(BalanceDelta curDeltas, uint160 sqrtPriceX96After, int24 tickAfter) = _swap(
poolKey,
zeroForOne,
int256(int128(i == 0 ? params.amountIn : prevAmountOut)),
int256(int128(i == 0 ? params.exactAmount : prevAmountOut)),
0,
params.path[i].hookData
);
Expand Down Expand Up @@ -232,7 +232,7 @@ contract Quoter is IQuoter, ILockCallback {
}

/// @dev quote an ExactOutput swap along a path of tokens, then revert with the result
function _quoteExactOutput(QuoteExactOutputParams memory params) public selfOnly returns (bytes memory) {
function _quoteExactOutput(QuoteExactParams memory params) public selfOnly returns (bytes memory) {
uint256 pathLength = params.path.length;

int128[] memory deltaAmounts = new int128[](pathLength + 1);
Expand All @@ -243,15 +243,15 @@ contract Quoter is IQuoter, ILockCallback {

for (uint256 i = pathLength; i > 0; i--) {
(PoolKey memory poolKey, bool oneForZero) = PathKeyLib.getPoolAndSwapDirection(
params.path[i - 1], i == pathLength ? params.currencyOut : prevCurrencyIn
params.path[i - 1], i == pathLength ? params.exactCurrency : prevCurrencyIn
);

(, int24 tickBefore,) = manager.getSlot0(poolKey.toId());

(BalanceDelta curDeltas, uint160 sqrtPriceX96After, int24 tickAfter) = _swap(
poolKey,
!oneForZero,
-int256(int128(i == pathLength ? params.amountOut : prevAmountIn)),
-int256(int128(i == pathLength ? params.exactAmount : prevAmountIn)),
0,
params.path[i - 1].hookData
);
Expand Down
54 changes: 27 additions & 27 deletions test/Quoter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactInput_0to2_2TicksLoaded() public {
tokenPath.push(token0);
tokenPath.push(token2);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10000);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10000);

(
int128[] memory deltaAmounts,
Expand All @@ -144,7 +144,7 @@ contract QuoterTest is Test, Deployers {

// The swap amount is set such that the active tick after the swap is -120.
// -120 is an initialized tick for this pool. We check that we don't count it.
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 6200);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 6200);

(
int128[] memory deltaAmounts,
Expand All @@ -163,7 +163,7 @@ contract QuoterTest is Test, Deployers {

// The swap amount is set such that the active tick after the swap is -60.
// -60 is an initialized tick for this pool. We check that we don't count it.
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 4000);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 4000);

(
int128[] memory deltaAmounts,
Expand All @@ -179,7 +179,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactInput_0to2_0TickLoaded_startingNotInitialized() public {
tokenPath.push(token0);
tokenPath.push(token2);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10);

(
int128[] memory deltaAmounts,
Expand All @@ -196,7 +196,7 @@ contract QuoterTest is Test, Deployers {
setupPoolWithZeroTickInitialized(key02);
tokenPath.push(token0);
tokenPath.push(token2);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10);

(
int128[] memory deltaAmounts,
Expand All @@ -212,7 +212,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactInput_2to0_2TicksLoaded() public {
tokenPath.push(token2);
tokenPath.push(token0);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10000);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10000);

(
int128[] memory deltaAmounts,
Expand All @@ -231,7 +231,7 @@ contract QuoterTest is Test, Deployers {

// The swap amount is set such that the active tick after the swap is 120.
// 120 is an initialized tick for this pool. We check that we don't count it.
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 6250);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 6250);

(
int128[] memory deltaAmounts,
Expand All @@ -248,7 +248,7 @@ contract QuoterTest is Test, Deployers {
setupPoolWithZeroTickInitialized(key02);
tokenPath.push(token2);
tokenPath.push(token0);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 200);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 200);

// Tick 0 initialized. Tick after = 1
(
Expand All @@ -266,7 +266,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactInput_2to0_0TickLoaded_startingNotInitialized() public {
tokenPath.push(token2);
tokenPath.push(token0);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 103);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 103);

(
int128[] memory deltaAmounts,
Expand All @@ -282,7 +282,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactInput_2to1() public {
tokenPath.push(token2);
tokenPath.push(token1);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10000);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10000);

(
int128[] memory deltaAmounts,
Expand All @@ -298,7 +298,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token0);
tokenPath.push(token2);
tokenPath.push(token1);
IQuoter.QuoteExactInputParams memory params = getExactInputParams(tokenPath, 10000);
IQuoter.QuoteExactParams memory params = getExactInputParams(tokenPath, 10000);

(
int128[] memory deltaAmounts,
Expand Down Expand Up @@ -352,7 +352,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactOutput_0to2_2TicksLoaded() public {
tokenPath.push(token0);
tokenPath.push(token2);
IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 15000);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 15000);

(
int128[] memory deltaAmounts,
Expand All @@ -369,7 +369,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token0);
tokenPath.push(token2);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 6143);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 6143);

(
int128[] memory deltaAmounts,
Expand All @@ -386,7 +386,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token0);
tokenPath.push(token2);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 4000);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 4000);

(
int128[] memory deltaAmounts,
Expand All @@ -404,7 +404,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token0);
tokenPath.push(token2);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 100);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 100);

// Tick 0 initialized. Tick after = 1
(
Expand All @@ -422,7 +422,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token0);
tokenPath.push(token2);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 10);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 10);

(
int128[] memory deltaAmounts,
Expand All @@ -438,7 +438,7 @@ contract QuoterTest is Test, Deployers {
function testQuoter_quoteExactOutput_2to0_2TicksLoaded() public {
tokenPath.push(token2);
tokenPath.push(token0);
IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 15000);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 15000);

(
int128[] memory deltaAmounts,
Expand All @@ -456,7 +456,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token2);
tokenPath.push(token0);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 6223);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 6223);

(
int128[] memory deltaAmounts,
Expand All @@ -474,7 +474,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token2);
tokenPath.push(token0);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 6000);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 6000);
(
int128[] memory deltaAmounts,
uint160[] memory sqrtPriceX96AfterList,
Expand All @@ -491,7 +491,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token2);
tokenPath.push(token1);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 9871);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 9871);

(
int128[] memory deltaAmounts,
Expand All @@ -510,7 +510,7 @@ contract QuoterTest is Test, Deployers {
tokenPath.push(token2);
tokenPath.push(token1);

IQuoter.QuoteExactOutputParams memory params = getExactOutputParams(tokenPath, 9745);
IQuoter.QuoteExactParams memory params = getExactOutputParams(tokenPath, 9745);

(
int128[] memory deltaAmounts,
Expand Down Expand Up @@ -631,32 +631,32 @@ contract QuoterTest is Test, Deployers {
function getExactInputParams(MockERC20[] memory _tokenPath, uint256 amountIn)
internal
view
returns (IQuoter.QuoteExactInputParams memory params)
returns (IQuoter.QuoteExactParams memory params)
{
PathKey[] memory path = new PathKey[](_tokenPath.length - 1);
for (uint256 i = 0; i < _tokenPath.length - 1; i++) {
path[i] = PathKey(Currency.wrap(address(_tokenPath[i + 1])), 3000, 60, IHooks(address(0)), bytes(""));
}

params.currencyIn = Currency.wrap(address(_tokenPath[0]));
params.exactCurrency = Currency.wrap(address(_tokenPath[0]));
params.path = path;
params.recipient = address(this);
params.amountIn = uint128(amountIn);
params.exactAmount = uint128(amountIn);
}

function getExactOutputParams(MockERC20[] memory _tokenPath, uint256 amountOut)
internal
view
returns (IQuoter.QuoteExactOutputParams memory params)
returns (IQuoter.QuoteExactParams memory params)
{
PathKey[] memory path = new PathKey[](_tokenPath.length - 1);
for (uint256 i = _tokenPath.length - 1; i > 0; i--) {
path[i - 1] = PathKey(Currency.wrap(address(_tokenPath[i - 1])), 3000, 60, IHooks(address(0)), bytes(""));
}

params.currencyOut = Currency.wrap(address(_tokenPath[_tokenPath.length - 1]));
params.exactCurrency = Currency.wrap(address(_tokenPath[_tokenPath.length - 1]));
params.path = path;
params.recipient = address(this);
params.amountOut = uint128(amountOut);
params.exactAmount = uint128(amountOut);
}
}

0 comments on commit fbb6d1b

Please sign in to comment.