Sunday, 1 May 2022

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

I wanted to get the Token Address based on the LP Pair Address. 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
Token Address : 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1  #-- token Address (output)


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

No comments:

Post a Comment