Giter Site home page Giter Site logo

Comments (21)

msujithk avatar msujithk commented on May 18, 2024 2

I added my own function to the library to get amount of tokens in liquidity pool and to get the price of the input token in eth. Here is the code,

def get_token_price_in_eth(self, owner, weth_address,token) -> Wei:
    weth_address = self.w3.toChecksumAddress(weth_address)#WETH
    erc20_weth = self.erc20_contract(weth_address)
    owner = self.w3.toChecksumAddress(owner)#Uni:Token input exchange ex: UniV2:DAI
    weth_balance: int = erc20_weth.functions.balanceOf(owner).call()
    weth_balance = float(self.w3.fromWei(weth_balance,'ether') )
    print (f'WETH quantity in Uniswap Pool = {weth_balance}')
    token_address = self.w3.toChecksumAddress(token) # Like DAI
    erc20 = self.erc20_contract(token_address)
    token_balance: int = erc20.functions.balanceOf(owner).call()
    gwei = 1000000000
    token_balance = float(token_balance / gwei)
    print (f'Token balance in Uniswap Pool = {token_balance}')
    return float(weth_balance/token_balance)  # price of token

from uniswap-python.

jhb10c avatar jhb10c commented on May 18, 2024 2

@Mreyna3, @8ty8t, are you looking to check LP tokens for V2 or V3 uniswap. For V2, you can check the total LP from the pairs contract function totalSupply(). I think V3 should be roughly similar. If you are still looking for a solution. I can type one up.

from uniswap-python.

Prijwal-Naidu avatar Prijwal-Naidu commented on May 18, 2024 1

I added my own function to the library to get amount of tokens in liquidity pool and to get the price of the input token in eth. Here is the code,

def get_token_price_in_eth(self, owner, weth_address,token) -> Wei:
    weth_address = self.w3.toChecksumAddress(weth_address)#WETH
    erc20_weth = self.erc20_contract(weth_address)
    owner = self.w3.toChecksumAddress(owner)#Uni:Token input exchange ex: UniV2:DAI
    weth_balance: int = erc20_weth.functions.balanceOf(owner).call()
    weth_balance = float(self.w3.fromWei(weth_balance,'ether') )
    print (f'WETH quantity in Uniswap Pool = {weth_balance}')
    token_address = self.w3.toChecksumAddress(token) # Like DAI
    erc20 = self.erc20_contract(token_address)
    token_balance: int = erc20.functions.balanceOf(owner).call()
    gwei = 1000000000
    token_balance = float(token_balance / gwei)
    print (f'Token balance in Uniswap Pool = {token_balance}')
    return float(weth_balance/token_balance)  # price of token

How do you find that "owner" knowing only token address?

You can generate the token pair using the router address of uniswap (or sushiswap), and the two token addresses. I answered this question on stackexchange: https://stackoverflow.com/questions/66710238/compute-uniswap-pair-address-via-python/67156507#67156507

from uniswap-python.

ErikBjare avatar ErikBjare commented on May 18, 2024

@cmackeen Yeah, I was lazy and didn't implement get_ex_token_balance for Uniswap v2 since I didn't personally have any need for it.

It should be easy to implement in a similar fashion to what @msujithk suggested (although I haven't tested it), so feel free to submit a PR!

from uniswap-python.

yukarinoki avatar yukarinoki commented on May 18, 2024

@ErikBjare I'd like to work on this issue, is there anyone else working on this issue? Otherwise, would you like to assign me this issue? (Or should I send a pull request without an assign?)

from uniswap-python.

ErikBjare avatar ErikBjare commented on May 18, 2024

@yukarinoki To my knowledge no one is working on it. Go ahead and do your thing, I'll assign you to the issue :)

from uniswap-python.

camperrr avatar camperrr commented on May 18, 2024

I added my own function to the library to get amount of tokens in liquidity pool and to get the price of the input token in eth. Here is the code,

def get_token_price_in_eth(self, owner, weth_address,token) -> Wei:
    weth_address = self.w3.toChecksumAddress(weth_address)#WETH
    erc20_weth = self.erc20_contract(weth_address)
    owner = self.w3.toChecksumAddress(owner)#Uni:Token input exchange ex: UniV2:DAI
    weth_balance: int = erc20_weth.functions.balanceOf(owner).call()
    weth_balance = float(self.w3.fromWei(weth_balance,'ether') )
    print (f'WETH quantity in Uniswap Pool = {weth_balance}')
    token_address = self.w3.toChecksumAddress(token) # Like DAI
    erc20 = self.erc20_contract(token_address)
    token_balance: int = erc20.functions.balanceOf(owner).call()
    gwei = 1000000000
    token_balance = float(token_balance / gwei)
    print (f'Token balance in Uniswap Pool = {token_balance}')
    return float(weth_balance/token_balance)  # price of token

How do you find that "owner" knowing only token address?

from uniswap-python.

ledzgio avatar ledzgio commented on May 18, 2024

Any news about this? I'd like to have a way to get the amount of tokens provided as liquidity in a specific uniswap pool.

Is there a way, given the amount of LP UNI-V2 tokens, how much was deposited as liquidity?

from uniswap-python.

SaltySousChef avatar SaltySousChef commented on May 18, 2024

When you create a pair the object returned by the sdk has "tokenAmounts" and "liquidityToken" as two identifiers you can call query. "tokenAmounts" contains the contract addresses for the two tokens you are trying to trade and their relative value and "liquidityToken" contains the contract address for the liquidity pool. To get the balance/rate from the liquidity pool create an instance of the ERC20 contract using its ABI and the address of the token and then call balanceOf on the liquidity pool.

from uniswap-python.

0hax avatar 0hax commented on May 18, 2024
    token_balance: int = erc20.functions.balanceOf(owner).call()
    gwei = 1000000000
    token_balance = float(token_balance / gwei)

Hi,
Can you explain why you are dividing the balance of the token by gwei ? I can't understand this.

from uniswap-python.

SaltySousChef avatar SaltySousChef commented on May 18, 2024

from uniswap-python.

freaker2k7 avatar freaker2k7 commented on May 18, 2024

I also lost you on how to get the owner and why it's the same owner for both tokens/addresses.

from uniswap-python.

Prijwal-Naidu avatar Prijwal-Naidu commented on May 18, 2024

I also lost you on how to get the owner and why it's the same owner for both tokens/addresses.

The 'owner' key is the address for the token pair which is unique for each Exchange.

from uniswap-python.

joseddg92 avatar joseddg92 commented on May 18, 2024

So, the function shared on this post can be used to get the liquidity in v2? I am looking to use this in BSC chain.

I already got the fix for constants.py for the BSC chain, and I have also copied the function suggested here, but I do not understand the parameters. Suppose I want to check the liquidity of CAKE in pancakeswap. All I have is the CAKE contract address.

What is owner, weth_addrees and token?

from uniswap-python.

joseddg92 avatar joseddg92 commented on May 18, 2024

@msujithk also, I am getting

AttributeError: 'Uniswap' object has no attribute 'erc20_contract'

Where is that method?

from uniswap-python.

joseddg92 avatar joseddg92 commented on May 18, 2024

Sorry for my last two comments, got it figured out.

Working code for BSC:

    def get_pool_info(self, owner, weth_address, token) -> Wei:
        weth_address = self.w3.toChecksumAddress(weth_address)  # WETH
        erc20_weth= _load_contract_erc20(self.w3, weth_address)

        owner = self.w3.toChecksumAddress(owner)
        weth_balance: int = erc20_weth.functions.balanceOf(owner).call()
        weth_balance = float(self.w3.fromWei(weth_balance, 'ether'))
        print(f'WETH quantity in Uniswap Pool = {weth_balance}')
        token_address = self.w3.toChecksumAddress(token)  # Like DAI
        erc20 = _load_contract_erc20(self.w3, token_address)

        token_balance: int = erc20.functions.balanceOf(owner).call()
        gwei = 1000000000
        token_balance = float(token_balance / gwei)
        print(f'Token balance in Uniswap Pool = {token_balance}')
        
        return 0.0 if token_balance == 0 else float(weth_balance / token_balance)  # price of token

Used like this:

# For BSC network
CONTRACTS = {
    "no_liq": "0x0Fc701755e825C488439Df26393505C4AC9e6Fc1",
    "CAKE": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"
}

PANCAKE_SWAP_FACTORY= "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"
PANCAKE_SWAP_ROUTER = "0x10ED43C718714eb63d5aA57B78B54704E256024E"

for name, addr in CONTRACTS.items():
    pancakeswap.get_pool_info(
        owner=PANCAKE_SWAP_ROUTER,
        weth_address=addr,
        token=addr
    )

from uniswap-python.

8ty8t avatar 8ty8t commented on May 18, 2024

@joseddg92 is there anyway for me to message you about the code above?

from uniswap-python.

Mreyna3 avatar Mreyna3 commented on May 18, 2024

@joseddg92 Does your code get the amount of lp tokens for a given address

from uniswap-python.

hoangduyloc avatar hoangduyloc commented on May 18, 2024

@joseddg92 method is just good for tracking small amounts of tokens, it can not work for most of the tokens. @8ty8t, @Mreyna3. I think we need to figure out some other ways to successfully tracking the liquid pool.

from uniswap-python.

asdfzxh8 avatar asdfzxh8 commented on May 18, 2024

joseddg92 no such thing as sorx or fx or etc, cepux, say etc any nmw s perfect

from uniswap-python.

0x6d656d6573 avatar 0x6d656d6573 commented on May 18, 2024

@Mreyna3, @8ty8t, are you looking to check LP tokens for V2 or V3 uniswap. For V2, you can check the total LP from the pairs contract function totalSupply(). I think V3 should be roughly similar. If you are still looking for a solution. I can type one up.

Where can I get the abi for this?

from uniswap-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.