Skip to content

Commit

Permalink
refactor PathKeyLib
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm committed Dec 4, 2023
1 parent 238a23e commit 5daedb0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
6 changes: 3 additions & 3 deletions contracts/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {PoolIdLibrary} from "@uniswap/v4-core/contracts/types/PoolId.sol";
import {
SwapType,
SwapInfo,
SwapIntention,
ExactInputSingleParams,
ExactInputSingleBatchParams,
ExactInputParams,
Expand All @@ -22,6 +21,7 @@ import {
} from "../libraries/SwapIntention.sol";
import {IQuoter} from "../interfaces/IQuoter.sol";
import {PoolTicksCounter} from "../libraries/PoolTicksCounter.sol";
import {PathKeyLib} from "../libraries/PathKey.sol";

contract Quoter is IQuoter {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -256,7 +256,7 @@ contract Quoter is IQuoter {

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

(BalanceDelta curDeltas, uint160 sqrtPriceX96After, int24 tickAfter) = _swap(
Expand Down Expand Up @@ -308,7 +308,7 @@ contract Quoter is IQuoter {
uint128 prevAmountIn;

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

Expand Down
16 changes: 16 additions & 0 deletions contracts/libraries/PathKey.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;

import {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";

struct PathKey {
Currency intermediateCurrency;
Expand All @@ -12,3 +13,18 @@ struct PathKey {
IHooks hooks;
bytes hookData;
}

library PathKeyLib {
function getPoolAndSwapDirection(PathKey memory params, Currency currencyIn)
internal
pure
returns (PoolKey memory poolKey, bool zeroForOne)
{
(Currency currency0, Currency currency1) = currencyIn < params.intermediateCurrency
? (currencyIn, params.intermediateCurrency)
: (params.intermediateCurrency, currencyIn);

zeroForOne = currencyIn == currency0;
poolKey = PoolKey(currency0, currency1, params.fee, params.tickSpacing, params.hooks);
}
}
17 changes: 2 additions & 15 deletions contracts/libraries/SwapIntention.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.20;

import {PathKey} from "./PathKey.sol";
import {PathKey, PathKeyLib} from "./PathKey.sol";
import {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
Expand Down Expand Up @@ -70,17 +70,4 @@ struct ExactOutputParams {
uint160 sqrtPriceLimitX96;
}

library SwapIntention {
function getPoolAndSwapDirection(PathKey memory params, Currency currencyIn)
internal
pure
returns (PoolKey memory poolKey, bool zeroForOne)
{
(Currency currency0, Currency currency1) = currencyIn < params.intermediateCurrency
? (currencyIn, params.intermediateCurrency)
: (params.intermediateCurrency, currencyIn);

zeroForOne = currencyIn == currency0;
poolKey = PoolKey(currency0, currency1, params.fee, params.tickSpacing, params.hooks);
}
}
library SwapIntention {}
12 changes: 11 additions & 1 deletion test/Quoter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import "../contracts/libraries/SwapIntention.sol";
import {
SwapType,
SwapInfo,
ExactInputSingleParams,
ExactInputSingleBatchParams,
ExactInputParams,
ExactOutputSingleParams,
ExactOutputSingleBatchParams,
ExactOutputParams
} from "../contracts/libraries/SwapIntention.sol";
import {PathKey} from "../contracts/libraries/PathKey.sol";
import {IQuoter} from "../contracts/interfaces/IQuoter.sol";
import {Quoter} from "../contracts/lens/Quoter.sol";
import {LiquidityAmounts} from "../contracts/libraries/LiquidityAmounts.sol";
Expand Down

0 comments on commit 5daedb0

Please sign in to comment.