get_onchain_price
Documentation for eth_defi.uniswap_v3.price.get_onchain_price function.
- get_onchain_price(web3, pool_contract_address, *, block_identifier=None, reverse_token_order=False)[source]
Get the current price of a Uniswap v3 pool.
Reads Uniswap v3 “slot 0” price.
This is the current price according to Uniswap team explanation, which we assume is the mid-price
See mid price
To read the latest ETH-USDC price on Polygon:
import os from web3 import Web3, HTTPProvider from eth_defi.uniswap_v3.price import get_onchain_price json_rpc_url = os.environ["JSON_RPC_POLYGON"] web3 = Web3(HTTPProvider(json_rpc_url)) # ETH-USDC 5 BPS pool address # https://tradingstrategy.ai/trading-view/polygon/uniswap-v3/eth-usdc-fee-5 pool_address = "0x45dda9cb7c25131df268515131f647d726f50608" price = get_onchain_price(web3, pool_address, reverse_token_order=True) print(f"ETH price is {price:.2f} USD")
- Parameters
web3 (web3.main.Web3) – Web3 instance
pool_contract_address (str) – Contract address of the Uniswap v3 pool
block_identifier (Optional[int]) –
A specific block to query price.
Block number or block hash.
reverse_token_order (bool) –
For switching the pair ticker around to make it human readable.
If set, assume quote token is token0, and the human price is 1/price
If not set assumes base token is token0
- Returns
Current price in human-readable Decimal format.