Repository navigation
Encode_Permit2_Permit
The Universal Router can be given a token allowance through the Permit2 contract (0x000000000022D473030F116dDEE9F6B43aC78BA3) using an off-chain signature instead of a regular on-chain approve. This is done with the PERMIT2_PERMIT function.
⚠ The Permit2 contract itself must first be approved through the token contract.
An rpc access is required to fetch the current Permit2 allowance and nonce:
from uniswap_universal_router_decoder import RouterCodec
rpc_endpoint = "https://..." # your rpc endpoint
codec = RouterCodec(rpc_endpoint=rpc_endpoint) # or RouterCodec(w3=w3)The Permit2 nonce, needed to build the permit, can be fetched like that:
amount, expiration, nonce = codec.fetch_permit2_allowance(account.address, token_address)data, signable_message = codec.create_permit2_signable_message(
token_address, # the token the allowance is given for
amount, # the allowance amount in Wei, max = 2 ** 160 - 1
expiration, # the unix timestamp at which the allowance becomes invalid
nonce, # the Permit2 nonce, see above
ur_address, # the spender: the UR checksum address
deadline, # the deadline of the permit signature
chain_id, # the chain id, ex: 1 for Ethereum Mainnet
)The signable message must be signed with your account:
signed_message = account.sign_message(signable_message) # where account is your LocalAccountThen the permit can be encoded:
encoded_data = codec.encode.chain().permit2_permit(data, signed_message).build(deadline)
# then in your transaction dict:
transaction["data"] = encoded_data
# you can now sign and send the transaction to the URThe permit can also be chained with a swap in the same transaction. See How to chain several commands in the same 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!