Comments (21)
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.
@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.
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 tokenHow 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.
@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.
@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.
@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.
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.
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.
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.
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.
from uniswap-python.
I also lost you on how to get the owner and why it's the same owner for both tokens/addresses.
from uniswap-python.
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.
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.
@msujithk also, I am getting
AttributeError: 'Uniswap' object has no attribute 'erc20_contract'
Where is that method?
from uniswap-python.
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.
@joseddg92 is there anyway for me to message you about the code above?
from uniswap-python.
@joseddg92 Does your code get the amount of lp tokens for a given address
from uniswap-python.
@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.
joseddg92 no such thing as sorx or fx or etc, cepux, say etc any nmw s perfect
from uniswap-python.
@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)
- Uniswap v4 support HOT 6
- Slippage limit does not seem to hold, or price impact is incorrect HOT 4
- unable to swap HOT 7
- uni的py库好像不支持avax HOT 1
- execution reverted on Sepolia HOT 2
- domain uniswap-python.com is not resolving HOT 4
- "execution reverted: Insufficient WETH9" on Polygon HOT 4
- Could not transact with/call contract function, is contract deployed correctly and chain synced? HOT 2
- execution reverted by new coin (even after liquidity added) HOT 2
- Exception has occurred: TypeError swapped quantity must be an integer HOT 1
- How can i get history statistic? HOT 3
- about historical data; HOT 1
- uniswap.make_trade_output() - Arguments Type HOT 1
- web3.exceptions.ContractLogicError: execution reverted: Unexpected error uniswapv3 EUROC USDC pair HOT 8
- More helpful error messages for `ContractLogicError: execution reverted`
- Getting price based on pool?
- How to fetch or calculate price of an asset? HOT 4
- Fails when swapping USDT=>WBNB HOT 1
- eth_sendTransaction does not exist/is not available after calling mint_liquidity method.
- How to use with the Base chain? HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uniswap-python.