Repository navigation
Build_Transaction
Instead of only building the input data with the build() method, the SDK can also build the full transaction with the build_transaction() method. It returns a transaction dictionary (TxParams) ready to be signed.
build_transaction() computes most of the transaction parameters (gas fees, chain id, nonce, deadline, ...), but they can also be provided. It requires the codec to have been created with a rpc endpoint or a Web3 instance.
from web3 import Web3
from uniswap_universal_router_decoder import RouterCodec
w3 = Web3(...) # your web3 instance
codec = RouterCodec(w3=w3)Let's say we want to build the full transaction to swap tokens on a V3 pool:
from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec
codec = RouterCodec(w3)
trx_params = (
codec.encode.chain().v3_swap_exact_in(
FunctionRecipient.SENDER, # the output tokens are sent to the transaction sender
amount_in, # in Wei
amount_out_min, # in Wei
[
in_token_address,
pool_fee,
out_token_address,
],
).build_transaction(
sender_address, # the 'from' address
deadline=codec.get_default_deadline(), # the swap deadline
)
)
# then sign and send it with web3
raw_transaction = w3.eth.account.sign_transaction(trx_params, account.key).raw_transaction
trx_hash = w3.eth.send_raw_transaction(raw_transaction)-
sender(mandatory): thefromaddress. -
value: the quantity of ETH in Wei sent to the UR, for example when wrapping ETH before a swap. -
trx_speed: aTransactionSpeedenum influencing the transaction rank in the block. Values areSLOW,AVERAGE,FAST(default) andFASTER. -
max_fee_per_gas_limit: if the computedmax_fee_per_gasis greater than this limit, aValueErroris raised, allowing you to stay in control. Default is 100 gwei.
How to use the trx_speed parameter:
from uniswap_universal_router_decoder import TransactionSpeed
.build_transaction(sender_address, trx_speed=TransactionSpeed.FASTER)Instead of trx_speed, custom gas fees can be provided through the priority_fee and max_fee_per_gas parameters. In that case, trx_speed must be set to None, otherwise a ValueError is raised.
Other parameters can also be provided: gas_limit, chain_id, nonce, ur_address and block_identifier.
With an AsyncRouterCodec, build_transaction() becomes asynchronous and must be awaited:
from uniswap_universal_router_decoder import AsyncRouterCodec
codec = AsyncRouterCodec(async_w3=async_w3) # your AsyncWeb3 instance
trx_params = await (
codec
.encode
.chain()
.v3_swap_exact_in(
FunctionRecipient.SENDER,
amount_in,
amount_out_min,
[in_token_address, pool_fee, out_token_address],
)
.build_transaction(sender_address, deadline=codec.get_default_deadline())
)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!