Web3.py check liquidity pool balance

Question:

I’m trying to compute the balance of a lp address given the address of the token.
So I have this function:

web3 = Web3(Web3.HTTPProvider("https://bsc-dataseed.binance.org/"))

def CheckLiquidity(TokenAddress, web3):
   LPAddress = GetLiquidityAddress(TokenAddress) #returns the web3.toChecksumAddress()
   balance = web3.eth.get_balance(LPAddress)
   bnbBalance = web3.fromWei(balance, 'ether)

Only problem that this returns me 0… for every contract I tried. I also manually checked on bsc scan the wbnb balance in the pool and is not 0.
Can someone help me please?

Asked By: mandiatodos

||

Answers:

There is an example in Web3 Ethereum Defi package.

    pair = get_deployed_contract(web3, "UniswapV2Pair.json", pair_address)
    token_a, token_b, timestamp = pair.functions.getReserves().call()

    # Check we got the liquidity
    assert token_a == 10 * 10**18
    assert token_b == 17_000 * 10**18

See full example.

Answered By: Mikko Ohtamaa

"That’s how I ended up doing it yesterday. But I can’t understand why web3.eth.get_balance() works fine with wallet addresses and not with LP addresses. "

Because the pair holds WETH not ETH.

Answered By: Emil Skoglund
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.