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