-1

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

0

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.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 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. – mandiatodos Apr 09 '22 at 14:45
  • Because you need to ask the correct balance from smart contract internal accounting. If you do not understand this please study the smart contract source code until you understand them wholly. – Mikko Ohtamaa Apr 09 '22 at 17:05
0

"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.

  • Welcome to SO and thank your for your contribution. You don't have to retype the previous comment. Just answer by leaving your own comment. You may want to refer to the other poster(s) using the '@' sign. See the Help link next to comment input field. – Hermann Schachner Oct 03 '22 at 06:39