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)
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).
Output:
This is the closest I could get. You may need further treatment if you want to distinguish the two tokens.