How to decode Token Address from a given Pair Address in web3

1.8k views Asked by At

What I am trying to accomplish is to get the Contract Address 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1 given the LP Pair address 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96 in (Token/BNB) pair. My Current Code is getting me nowhere. Ideas will be very helpful.

Current Code:

from web3 import Web3, HTTPProvider
import json

bsc = 'https://bsc-dataseed.binance.org/'    
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())

address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96"    #-- LP Pair Address to check

#-- 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1  Token Address
#-- 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96  Token LP Pair Address

abi = json.loads('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[],"name":"INIT_CODE_PAIR_HASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')

PCS_FACTORY_ADDRESS="0xca143ce32fe78f1f7019d7d551a6402fc5350c73"
PCS_FACTORY_ADDRESS = Web3.toChecksumAddress(PCS_FACTORY_ADDRESS.lower() )
PCS_FACTORY_CONTRACT = web3.eth.contract(address=PCS_FACTORY_ADDRESS, abi=abi)
pair_contract = PCS_FACTORY_CONTRACT.functions.allPairs.call()
contract = web3.eth.contract(address=address , abi=abi)

token0 = contract.functions.token0().call()
token1 = contract.functions.token1().call()
print (token0)  #-- could be token Address or LP Pair Address
print (token1)  #-- could token Address or LP Pair Address

Needed Output:

Pair Address  : 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96  #-- LP Pair Address / Given input
Token Address : 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1  #-- token Address (output)
2

There are 2 answers

0
tyson.wu On BEST ANSWER

First as @Kendiro mentioned, you cannot decode a hash. Second, you cannot distinguish token0 and token1 by the nature of a contract although you are probably interested in token0 (in this case, the token) instead of token1 (wBNB).

I am not sure where you find your ABI, but you may use BSCScan API for this. The following code contains a function to return the Web3 Contract from a given address, and

Note that there are in total 3 calls to the API (one for getting contract of the LP, two for getting token contract of token0 and token1 respectively).

import requests
from web3 import Web3

apikey = 'YourApiKeyHere'

bsc = 'https://bsc-dataseed.binance.org/'    
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())


def get_contract(address):
    checksum_address = Web3.toChecksumAddress(address)
    params = {
        'module': 'contract',
        'action': 'getabi',
        'address': checksum_address,
        'apikey': apikey,
    }
    res = requests.get('https://api.bscscan.com/api', params=params)
    abi = res.json()['result']
    contract = web3.eth.contract(address=checksum_address, abi=abi)
    return contract

lp_address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96"    #-- LP Pair Address to check
lp_contract = get_contract(lp_address)

token0_address = lp_contract.functions.token0().call()
token1_address = lp_contract.functions.token1().call()

token0_contract = get_contract(token0_address)
token1_contract = get_contract(token1_address)

token0_symbol = token0_contract.functions.symbol().call()
token1_symbol = token1_contract.functions.symbol().call()

print(f'Pair Address: {lp_address}')
print(f'Token0 is {token0_symbol}: {token0_address}')
print(f'Token1 is {token1_symbol}: {token1_address}')

Output:

Pair Address: 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96
Token0 is $YSHIBAINU: 0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1
Token1 is WBNB: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c

This is the closest I could get. You may need further treatment if you want to distinguish the two tokens.

0
Kendirom On

Hash functions are designed to be irreversible. So unless you don't know all data used in hashing you can't find out what was hashed.