Repository navigation
Encode_V2_Swap
Let's say we want to swap tokens on a Uniswap V2 pool through the Universal Router (UR).
⚠ A correct token allowance must have been set before sending such a transaction, either directly to the UR or through the Permit2 flow.
No rpc access is required to encode a swap:
from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()To swap tokens with an exact in_amount:
from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec
codec = RouterCodec()
encoded_data = codec.encode.chain().v2_swap_exact_in(
FunctionRecipient.SENDER, # the output tokens are sent to the transaction sender
amount_in, # the exact amount of tokens to send, in Wei
amount_out_min, # the minimum amount of tokens to receive, in Wei
[
in_token_address, # checksum address of the token sent to the UR
out_token_address, # checksum address of the token received from the UR
],
).build(codec.get_default_deadline()) # unix timestamp after which the trx is not valid any more
# then in your transaction dict:
transaction["data"] = encoded_data
# you can now sign and send the transaction to the URWhen the exact in_amount is not known when the transaction is built, typically because it comes from a previous swap output, the v2_swap_exact_in_from_balance() method can be used. The input tokens are then taken from the UR balance, so no amount_in is needed:
encoded_data = codec.encode.chain().v2_swap_exact_in_from_balance(
FunctionRecipient.SENDER, # the output tokens are sent to the transaction sender
amount_out_min, # the minimum amount of tokens to receive, in Wei
[
in_token_address, # checksum address of the token sent to the UR
out_token_address, # checksum address of the token received from the UR
],
).build(codec.get_default_deadline()) # unix timestamp after which the trx is not valid any moreThis is typically chained after a V2_SWAP_EXACT_IN or V3_SWAP_EXACT_IN command, so that the second swap uses the tokens bought by the first one. See How to chain several commands in the same transaction ?.
To swap tokens with an exact out_amount:
encoded_data = codec.encode.chain().v2_swap_exact_out(
FunctionRecipient.SENDER, # the input tokens are taken from the transaction sender
amount_out, # the exact amount of tokens to receive, in Wei
amount_in_max, # the maximum amount of tokens to send, in Wei
[
in_token_address, # checksum address of the token sent to the UR
out_token_address, # checksum address of the token received from the UR
],
).build(codec.get_default_deadline()) # unix timestamp after which the trx is not valid any moreTo swap through several pools, see How to encode a multi-hop swap ?.
Only the input data is built here. See How to build a full transaction to the Universal Router ? to build the whole transaction.
A full working example is available in this tutorial.
If you find this library useful, consider a donation and be part of the Supporter Community! :)
- ☕️☕️☕️ https://www.buymeacoffee.com/elnaril ☕️☕️☕️
- Ethereum compatible: 0xe32A121C89121bF8B829D6d88BBEA89e5d88f002
- Sponsoring: https://github.com/sponsors/Elnaril
Thank you to all supporters!! 🙏
Also, consider ⭐ starring ⭐ the repository and following the GitHub account and/or 𝕏 to get the next lib updates!