freeearth
freeearth

Reputation: 101

Compute uniswap pair address via python

I`m trying to compute (off-line, i.e. without an http requests) address of an Uniswap pair, with python, web3 and eth-abi libraries, based on this solidity example.

address factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address token0 = 0xCAFE000000000000000000000000000000000000; // change me!
address token1 = 0xF00D000000000000000000000000000000000000; // change me!

address pair = address(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  keccak256(abi.encodePacked(token0, token1)),
  hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));

Have some thoughts:

hexadem_ ='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
hexadem_1 = 0xff
abiEncoded_1 = encode_abi_packed(['address', 'address'], (  token_0, token_1 ))
salt_ = web3.Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed(['bytes', 'address', 'bytes32'], (bytes(hexadem_1), factory, salt_))
resPair = web3.Web3.solidityKeccak(['bytes','bytes'], ['0x' +abiEncoded_2.hex(), hexadem_])

Can somebody suggest me, what is wrong, which way it should be considered?

Upvotes: 2

Views: 4432

Answers (2)

prij_naidu
prij_naidu

Reputation: 181

Firstly, resPair is too long to be a contract address. The rules can be explained by reading this: https://eips.ethereum.org/EIPS/eip-1014

Also make sure you enter the two addresses in alphabetical order. Use the .sort() function to do this.

Here is the correct code:

hexadem_ ='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
hexadem_1 = 0xff
factory = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f' 
abiEncoded_1 = encode_abi_packed(['address', 'address'], ('0x7825e833d495f3d1c28872415a4aee339d26ac88', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' )) #these two addresses to be changed as fit. Currently they are TLOS and WETH
salt_ = w3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = w3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), hexadem_])[12:]
resPair

Upvotes: 5

Mokhtar Tlili
Mokhtar Tlili

Reputation: 938

You are using wrong hexadem_ Use this one:

hexadem_ = 0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5

See line 298 in the contract source of PancakeRouter https://bscscan.com/address/0x10ED43C718714eb63d5aA57B78B54704E256024E#code

Upvotes: 0

Related Questions