Decode & Encode transaction data sent to Uniswap universal router
Project description
Uniswap Universal Router Decoder & Encoder
Description
The object of this library is to decode & encode the transaction input sent to the Uniswap universal router (UR)
(address 0xEf1c6E67703c7BD7107eed8303Fbe6EC2554BF6B
on Ethereum Mainnet).
⚠ This library has not been audited, so use at your own risk !
⚠ There is no guarantee of compatibility between 2 versions: consider forcing the version in your dependency requirements.
⚠ This project is a work in progress so not all commands are decoded yet. Below a list of the already implemented ones:
| Command Id | Function Name | Decode | Encode |
|---|---|---|---|
| 0x00 | V3_SWAP_EXACT_IN | ✅ | ❌ |
| 0x01 | V3_SWAP_EXACT_OUT | ✅ | ❌ |
| 0x02 - 0x06 | ❌ | ❌ | |
| 0x07 | placeholder | N/A | N/A |
| 0x08 | V2_SWAP_EXACT_IN | ✅ | ✅ |
| 0x09 | V2_SWAP_EXACT_OUT | ✅ | ❌ |
| 0x0a | PERMIT2_PERMIT | ✅ | ❌ |
| 0x0b | WRAP_ETH | ✅ | ✅ |
| 0x0c | UNWRAP_WETH | ✅ | ❌ |
| 0x0d | PERMIT2_TRANSFER_FROM_BATCH | ❌ | ❌ |
| 0x0e - 0x0f | placeholders | N/A | N/A |
| 0x10 - 0x1d | ❌ | ❌ | |
| 0x1e - 0x3f | placeholders | N/A | N/A |
Installation
# update pip to latest version if needed
pip install -U pip
# install the decoder from pypi.org
pip install uniswap-universal-router-decoder
Usage
The library exposes a class, RouterDecoder with several public methods that can be used to decode or encode UR data.
How to decode a transaction input
To decode a transaction input, use the decode_function_input method as follow:
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
trx_input = "0x3593564c000000000000000000 ... 90095b5c4e9f5845bba" # the trx input to decode
decoder = RouterDecoder()
decoded_trx_input = decoder.decode_function_input(trx_input)
How to decode a transaction
It's also possible to decode the whole transaction, given its hash and providing the decoder has been built with either a valid Web3 instance or the link to a rpc endpoint:
# Using a web3 instance
from web3 import Web3
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
w3 = Web3(...) # your web3 instance
decoder = RouterDecoder(w3=w3)
# Using a rpc endpoint
from web3 import Web3
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
rpc_link = "https://..." # your rpc enpoint
decoder = RouterDecoder(rpc_endpoint=rpc_link)
And then the decoder will get the transaction from the blockchain and decode it, along with its input data:
# Using a rpc endpoint
trx_hash = "0x52e63b7 ... 11b979dd9"
decoded_transaction = decoder.decode_transaction(trx_hash)
How to decode an Uniswap V3 swap path
The RouterDecoder class exposes also the static method decode_v3_path which can be used to decode a given Uniswap V3 path.
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
uniswap_v3_path = b"\xc0*\xaa9\xb2#\xfe\x8d\n\x0e ... \xd7\x89" # bytes or str hex
fn_name = "V3_SWAP_EXACT_IN" # Or V3_SWAP_EXACT_OUT
decoded_path = RouterDecoder.decode_v3_path(fn_name, uniswap_v3_path)
The result is a tuple, starting with the "in-token" and ending with the "out-token", with the pool fees between each pair.
How to encode a call to the function WRAP_ETH
This function can be used to convert eth to weth using the UR.
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
decoder = RouterDecoder()
encoded_data = decoder.encode_data_for_wrap_eth(amount_in_wei) # to convert amount_in_wei eth to weth
# then in your transaction dict:
transaction["data"] = encoded_data
# you can now sign and send the transaction to the UR
How to encode a call to the function V2_SWAP_EXACT_IN
This function can be used to swap tokens. Correct allowances must have been set before using sending such transaction.
from uniswap_universal_router_decoder.router_decoder import RouterDecoder
decoder = RouterDecoder()
encoded_data = decoder.encode_data_for_v2_swap_exact_in(
amount_in, # in Wei
min_amount_out, # in Wei
[
in_token_address,
out_token_address,
],
timestamp, # unix timestamp after which the trx will not be valid any more
)
# then in your transaction dict:
transaction["data"] = encoded_data
# you can now sign and send the transaction to the UR
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file uniswap-universal-router-decoder-0.4.0.tar.gz.
File metadata
- Download URL: uniswap-universal-router-decoder-0.4.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c50cead14ebd794f32ad31c16934f17d63c1a0a798c8e833f2765ba6a110928
|
|
| MD5 |
ad93e4658ad086e849f95a2c23de047a
|
|
| BLAKE2b-256 |
6c8aa61e4fb2a8493074735f9d9dd0edf015ded5694a15e78e5f86d8eb9dacc6
|
File details
Details for the file uniswap_universal_router_decoder-0.4.0-py3-none-any.whl.
File metadata
- Download URL: uniswap_universal_router_decoder-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5969dd6fd20a1a853bab08879bb41c80c140079af5137ba87bb88502b7877112
|
|
| MD5 |
64efdc3a507d7a6647b8944990287600
|
|
| BLAKE2b-256 |
874aeb98960e67821d686659c1cbe8cf753d4e40d1fae2502557471b3b90ab61
|