3

I managed to have this code run after few hours of searches but unfortunately, this does not produce the output I wanted which is to get the LP Pool Address in (TOKEN/BNB LP).

Given the Token Address: 0xe56842ed550ff2794f010738554db45e60730371

I wanted to get the BIN/BNB Pool Address: 0xe432afB7283A08Be24E9038C30CA6336A7cC8218.

Any ideas what could be the problem?

from web3 import Web3
from eth_abi.packed import encode_abi_packed
from eth_abi import encode_abi
import eth_abi

"""
Contract: 0xe56842ed550ff2794f010738554db45e60730371
BIN/BNB Address: 0xe432afB7283A08Be24E9038C30CA6336A7cC8218
BIN/BNB LP URL: https://bscscan.com/token/0xe432afB7283A08Be24E9038C30CA6336A7cC8218#balances
"""

CONTRACTS = {"CONTRACT": "0xe56842ed550ff2794f010738554db45e60730371",}

PANCAKE_SWAP_FACTORY = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"
PANCAKE_SWAP_ROUTER  = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
WBNB_ADDRESS = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"

hexadem_= '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
factory = PANCAKE_SWAP_FACTORY
abiEncoded_1 = encode_abi_packed(['address', 'address'], (CONTRACTS['CONTRACT'], WBNB_ADDRESS))
salt_ = Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), hexadem_])[12:]

# resPair is the address for the pancakeswap CONTRACT /WBNB pair
print("Token Contract: ", CONTRACTS)
print("BNB-LP Address: ", resPair.hex())    #-- expecting to get  0xe432afB7283A08Be24E9038C30CA6336A7cC8218

Current Output:

BNB-LP Address:  0xde173b8a63b9641a531de0fbb1c5c9eee3b4bc0c

Expected Output:

Token Contract:  0xe56842ed550ff2794f010738554db45e60730371
BNB-LP Address:  0xe432afB7283A08Be24E9038C30CA6336A7cC8218   #-- correct LP Address
TylerH
  • 20,799
  • 66
  • 75
  • 101
rbutrnz
  • 383
  • 4
  • 20

5 Answers5

5

You need to enter the two coins in alphabetical order.

pair_traded = [token_a, token_b] #token_a, token_b are the address's
pair_traded.sort()

hexadem_1 = 0xff
abiEncoded_1 = encode_abi_packed(['address', 'address'], (token_list[0], token_list[1] ))
salt_ = w3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
pair_address = w3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), pair_code_hash])[12:]
prij_naidu
  • 181
  • 7
  • Even though the parameter order is correct, the reasoning is different. The [pair contract](https://bscscan.com/address/0xe432afB7283A08Be24E9038C30CA6336A7cC8218#readContract) was created by passing arguments in the order WBNB, BIN. You can check the [transaction](https://bscscan.com/tx/0x4e79f8b1830ea35ad42bdc3622fb4914fce2bad3acc84bcdebbdfa44c9e4a119#eventlog) event logs that created the pair contract - specifically you're looking for the `PairCreated` event. So if you want to get the same pair contract address as a result, you need to pass the same input params (in the correct order). – Petr Hejda Nov 22 '21 at 18:33
  • Im getting the error: pair_address = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), pair_code_hash])[12:] NameError: name 'pair_code_hash' is not defined – rbutrnz Nov 23 '21 at 05:58
  • tried replacing: pair_code_hash into hexadem_1 and it works but the lp address is still wrong – rbutrnz Nov 23 '21 at 06:03
1

For Pancake Swap the hexadem_ should be like this I think:

hexadem_= '0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5'  ## Pancake SWAP

The one in your code is for UniSwap

Tugay
  • 2,057
  • 5
  • 17
  • 32
Oruk Kai
  • 11
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 26 '22 at 17:21
  • can the process be reverse? For example, I have the LP Address instead and I want to display the Token Address – rbutrnz Apr 28 '22 at 07:52
0
hexadem_ = '0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5' # This is pancake right hex。
factory = PANCAKE_SWAP_FACTORY
abiEncoded_1 = encode_abi_packed(['address', 'address'], (CONTRACTS['CONTRACT'], WBNB_ADDRESS))
salt_ = Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), hexadem_])[12:]

But the value I output is different from yours, my output value:

Token Contract:  {'CONTRACT': '0xe56842ed550ff2794f010738554db45e60730371'}
BNB-LP Address:  0xdbb161367d9a2a852ebeef3cbfcbf2c43b85064b
苏昌海
  • 1
  • 1
0

I figured out you need to make sure both addresses are all lowercase or all uppercase. This makes a difference when sorting. This works for me:

def compute_pool_address(self, token_address_a, token_address_b):
    pair_traded = [token_address_a.lower(), token_address_b.lower()]
    pair_traded.sort()
    hexadem = '0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5'
    abiEncoded_1 = encode_abi_packed(['address', 'address'], (pair_traded[0], pair_traded[1]))
    salt_ = self.web3.solidityKeccak(['bytes'], ['0x' + abiEncoded_1.hex()])
    abiEncoded_2 = encode_abi_packed(['address', 'bytes32'], (PANCAKE_FACTORY_ADDRESS, salt_))
    return self.web3.toChecksumAddress(self.web3.solidityKeccak(['bytes', 'bytes'], ['0xff' + abiEncoded_2.hex(), hexadem])[12:])

(self.web3 is a web3.py Web3 instance)

Quint
  • 33
  • 3
0

I had similar problems when trying to recreate the functionality of the uniswap contract. It was resolved when I used the eth_abi.abi.encode(python) function inside an expression.

Solidity code what i wanted:

keccak256(
  abi.encodePacked(
    hex'ff',
    factory,
    keccak256(abi.encode(key.token0, key.token1, key.fee)),
    POOL_INIT_CODE_HASH
  )
)

Python code which produces the same result:

encoded_internal_data = abi.encode(['address', 'address', 'uint24'], (token0, token1, fee))
key_hash = web3.solidityKeccak(['bytes'], [encoded_internal_data])
encoded_full_data = encode_abi_packed(
    ["bytes1", "address", "bytes", "bytes"],
    (HexBytes('ff'), UNISWAPV3_FACTORY_ADDRESS, key_hash, HexBytes(UNISWAPV3_POOL_INIT_CODE_HASH))
)
pair_address = web3.solidityKeccak(['bytes'], [encoded_full_data])[12:].hex()