[Ethereum] Web3: Get token address from pair address

bsccpp-ethereumpythonweb3js

I am studying all listed pairs on Pancakeswap. I get the pair address from

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

with n cycling from 0 to the current number of listed pairs.
This works fine.
When I have a pair address, how can I determine the underlying pair?

For example, for RAZOR the pair address is: 0x65d83d57c52d10e233bd4fed21fb9184ce00e861.
Using

contract = w3.eth.contract(address=Web3.toChecksumAddress(pair_contract) , abi=PAIRS_ABI)
symbol = contract.functions.name().call()

I only get the name of the LP – Tokens (Pancake LPs).
What I want to have is the address of the token itelf (for Razor it would be 0x50de6856358cc35f3a9a57eaaa34bd4cb707d2cd)

Can you help me how to achieve this?

Right now I use python and Web3.

Thank you very much

Best Answer

You get the tokens that make up the trading pair by calling following lines on the pair contract:

token0 = contract.functions.token0().call()
token1 = contract.functions.token1().call()

I used the code format you defined in your question above. In your example, the Razor token could be either token0 or token1, depending on the way that pair contract was initialized.

Related Topic