-
Notifications
You must be signed in to change notification settings - Fork 12
Fix Swap V4 and code enhancement #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
uniswap_universal_router.py
Outdated
| def __init__(self, wallet_address, private_key, provider, web3): | ||
| self.w3=web3 | ||
| self.w3 = web3 | ||
| self.w3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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
V3 Swap:

Swap transaction sent! Tx hash: f6d30ca34005b3427fb0416e7c4ec01fe39a87aa73df0eb15e880749068dfd08