Skip to content

Instantly share code, notes, and snippets.

@nazariyv
Last active June 6, 2021 20:23
Show Gist options
  • Save nazariyv/62be83ee221c057dbdcd30fca24293e9 to your computer and use it in GitHub Desktop.
Save nazariyv/62be83ee221c057dbdcd30fca24293e9 to your computer and use it in GitHub Desktop.
Uniswap v2 Python CREATE2
# references
# 1. https://uniswap.org/docs/v2/javascript-SDK/getting-pair-addresses/
# 2. https://ethereum.stackexchange.com/questions/90539/how-to-get-the-non-packed-soliditykeccak-hash-in-python-web3
# deps
# Python 3.9.5
# 1. eth-abi==2.1.1
# 2. web3==5.18.0
def get_pair_address(token0: str, token1: str):
import eth_abi
from web3 import Web3
init_code_hash = (
"0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f"
)
# intuitively that is how it should be sorted in Uniswap contract
if int(token0, 16) < int(token1, 16):
temp = token0
token0 = token1
token1 = temp
abi_encoded = eth_abi.encode_abi(["address", "address"], [token0, token1])
hash = Web3.solidityKeccak(["bytes"], ["0x" + abi_encoded.hex()])
return hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment