I have been working behind the scenes to develop the Uniswap V4 pool helper. If you’re interested, you can follow the recent commits to the degenbot repository.
It’s largely done, so let’s take a brief tour.
Libraries
The Uniswap V4 core contracts are built on the V3 foundation. In many cases, the V4 libraries use the same contract and function names from V3.
You can find these new libraries in the degenbot package under the degenbot.uniswap.v4_libraries module.
There are some subtle differences though, which we should understand.
Delta Sign Convention
I covered this in Part III, but it is worth repeating.
The sign convention from Uniswap V3 has been reversed. When interacting with a V3 pool, you would specify a positive amount for an exact input swap and a negative amount for an exact output swap.
When interacting with a V4 pool, you specify a negative amount for an exact input swap and a positive amount for an exact output swap. This convention is also extended across the V4 libraries, so I needed to develop a separate set of utility libraries to ensure that subtle V3-V4 differences would not corrupt results for either paradigm.
The pool helper uses a dataclass named SwapDelta to track swap calculation results. The name matches the struct used by the contract. I have extended the dataclass with amount_in
and amount_out
methods, which you can call to translate automatically. When we start doing mixed V3 and V4 pool arbitrage, we will appreciate the convenience.
Validation For Input and Return Values
It can be difficult to port Solidity contract code to Python, especially when underflow/overflow must be modeled. Many of the V3 and V4 contracts use the gas-optimized Yul blocks which do unchecked math, followed by an underflow/overflow check, or perform integer conversion through the SafeCast library.
Keep reading with a 7-day free trial
Subscribe to Degen Code to keep reading this post and get 7 days of free access to the full post archives.