Skip to content

Conversation

@OuthBack
Copy link

@OuthBack OuthBack commented Jul 21, 2025

Enhance Uniswap Universal Router Integration

Summary

This Pull Request introduces enhancements to the Uniswap universal router integration, fix the V4 Swap and there are some code formatter changes.

Motivation

Uniswap V4 Support: Implement comprehensive logic for V4 exact input swaps, including correct handling of token0/token1 order and zeroForOne flags.

Key Changes

Added injection of web3.middleware.ExtraDataToPOAMiddleware to handle RPC providers that omit extraData from blocks, crucial for Polygon and other PoA chains.

Uniswap V4 Swap Logic:

  • Implemented logic within make_trade to correctly determine token0, token1, and zeroForOne based on token addresses for V4 pool keys. Because the token0 is always the smallest address and token1 is always the biggest.

  • Updated v4_swap encoding to dynamically use the zeroForOne flag and correctly specify take_all and settle_all recipients.

  • Extracted token balance checking logic into a new check_balance method for better reusability and clarity.

  • Updated check_permit2_allowance and create_permit_signature to use token0 for consistency with V4 swap logic.

Dependency Update:

  • Ensured RouterCodec is initialized with the web3 instance (RouterCodec(self.w3)) for fix an error ocourring due code version.

  • Changed signed_tx.rawTransaction to signed_tx.raw_transaction to reflect changes in the web3.py SignedTransaction object structure.

Visual Evidence

POLYGON

V4 Swap:
Swap transaction sent! Tx hash: f95248991a1c03a184a517c9665e2a97b4879dcf022acc91c1d5bb39bdfdd826

image

V3 Swap:
Swap transaction sent! Tx hash: f6d30ca34005b3427fb0416e7c4ec01fe39a87aa73df0eb15e880749068dfd08
image

@snarflakes snarflakes marked this pull request as ready for review July 21, 2025 17:39
def __init__(self, wallet_address, private_key, provider, web3):
self.w3=web3
self.w3 = web3
self.w3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)
Copy link
Author

@OuthBack OuthBack Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

necessary for I run into Polygon chain

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! does adding this code still enable base/arbitrum and other non POA swaps to succeed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check other blockchains, just Polygon. I can just add this middleware only when is Polygon.

Comment on lines +212 to +221
if from_token <= to_token:
token0 = from_token
token1 = to_token
zeroForOne = True
else:
token0 = to_token
token1 = from_token
zeroForOne = False

token0 = Web3.to_checksum_address(token0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just using token0 and token1 for v4, it is still using from_token and to_token in v3

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for ordernate the tokens from lowest to highest addreess

@OuthBack OuthBack requested a review from snarflakes July 24, 2025 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants