3

Directly trade with pool can save at least 16,000 gas, which is $5.68 at 114Gwei gas price in ehtereum mainnet for a single transaction.The reduced gas usage is from finding pool offline.

But I don't find document about trading directly with pool in Uniswap V3 doc, only with router in it.

  1. Find pool in Uniswap V3 router trade
  2. Directly trade with pool using 1inch aggregator
1
  • Did you find a way how directly interact with a pool?
    – user42664
    Commented May 29, 2023 at 7:53

2 Answers 2

4

Check out the Charm Finance github for an example of how to do so. You need to call pool.swap() with the correct parameters, and have a swap callback function inside your contract. I don't believe it's possible to swap directly with the pool outside of a smart contract built to do so.

I have some good experience working with the pool directly, so here is some info, but as always you should read the docs and the code yourself instead of just relying on me. The Uniswap v3 code says:

    /// @notice Swap token0 for token1, or token1 for token0
    /// @dev The caller of this method receives a callback in the form of IUniswapV3SwapCallback#uniswapV3SwapCallback
    /// @param recipient The address to receive the output of the swap
    /// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
    /// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
    /// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
    /// value after the swap. If one for zero, the price cannot be greater than this value after the swap
    /// @param data Any data to be passed through to the callback
    /// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
    /// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
    function swap(
        address recipient,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96,
        bytes calldata data
    ) external returns (int256 amount0, int256 amount1);

Recipient should be address(this) if you want the contract to have the tokens, or msg.sender if you want the caller to have the tokens. zeroForOne represents whether you're trading token0 for token1 or vice versa.

amountSpecified is how much of that token you are swapping for the other one. If it's positive then you're doing exactInput (i.e. sending amountSpecified tokens for whatever amount of amountOut you get) and if it's negative you're doing exactOutput (i.e. amountSpecified represents the amount of tokens you're getting out, for an unknown amount of input tokens).

sqrtPriceLimitX96 is basically max slippage you'll allow in the swap. If you're swapping t0 for t1 it needs to be higher than the current sqrtPriceLimitX96, lower if you're swapping the other way.

Unless you're using callback data for something (I doubt you are) you can just set data to be an empty string.

Relevant UniswapV3 docs: IUniswapV3SwapCallback

5
  • There isn't specific thing about how to construct params of pool.swap(), just simple tests github.com/charmfinance/alpha-vaults-contracts/search?q=swap
    – CryptoOnly
    Commented Feb 9, 2022 at 3:25
  • Added some more info to my answer.
    – Meriadoc
    Commented Feb 9, 2022 at 3:43
  • How to calculate sqrtPriceLimitX96 to ensure I get at least amountOutMinimum given inputAmount in exactInput using pool.swap()? I don't find it in Single Swaps Doc where I can use amountOutMinimum to specify it.
    – CryptoOnly
    Commented Feb 9, 2022 at 10:42
  • "I don't believe it's possible to swap directly with the pool outside of a smart contract built to do so". Instead of believing you should go to the sources. the swap() function (in v3 core) is declared as "external", so any account (EOA/contract) can call this function.
    – Nulik
    Commented Jun 7, 2022 at 0:11
  • Any account can call it, but EOA's/smart contracts not built to do so will have their action reverted. The pool will try to call swapCallback on the calling account and fail, causing transaction revert.
    – Meriadoc
    Commented Jun 8, 2022 at 1:42
0

Call exactInput or exactInputSingle from the pool that represents the pair in which you wanna perform a swap.

1
  • You may not have understood the problem
    – Jay Ptah
    Commented Mar 29, 2023 at 3:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.