Skip to content

Commit

Permalink
Get onchain price of Uniswap v3 pool (#138)
Browse files Browse the repository at this point in the history
* Add util func to get onchain price of uni v3 pool

* Add test and changelog
  • Loading branch information
hieuh25 committed Jul 22, 2023
1 parent c20f829 commit a34dcb9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.21.9

- Add utility function `get_onchain_price()` to ask on-chain price of a
Uniswap v3 pool at any given block number

# 0.21.8

- Add test coverage for `extract_timestamps_json_rpc_lazy`
Expand Down
7 changes: 6 additions & 1 deletion eth_defi/uniswap_v3/pool.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Uniswap v3 pool data."""
from decimal import Decimal
from dataclasses import dataclass
from decimal import Decimal
from typing import Union

from eth_typing import HexAddress
from web3 import Web3
from web3.contract import Contract

from eth_defi.abi import get_deployed_contract
from eth_defi.token import TokenDetails, fetch_erc20_details
Expand Down Expand Up @@ -33,6 +34,9 @@ class PoolDetails:
#: Pool fee as % multiplier, 1 = 100%
fee: float

#: Pool contract proxy
pool: Contract

def __repr__(self):
return f"Pool {self.address} is {self.token0.symbol}-{self.token1.symbol}, with the fee {self.fee * 100:.04f}%"

Expand Down Expand Up @@ -72,6 +76,7 @@ def fetch_pool_details(web3, pool_contact_address: Union[str, HexAddress]) -> Po
token1,
raw_fee,
raw_fee / 1_000_000,
pool,
)


Expand Down
23 changes: 23 additions & 0 deletions eth_defi/uniswap_v3/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from decimal import Decimal

from eth_typing import HexAddress
from web3 import Web3

from eth_defi.uniswap_v3.deployment import UniswapV3Deployment
from eth_defi.uniswap_v3.pool import fetch_pool_details
from eth_defi.uniswap_v3.utils import encode_path


Expand Down Expand Up @@ -216,3 +218,24 @@ def estimate_sell_received_amount(
return amount, current_block

return amount


def get_onchain_price(
web3: Web3,
pool_contract_address: str,
*,
block_identifier: int | None = None,
reverse_token_order: bool = False,
):
"""Get the current price of a Uniswap pool.
:param web3: Web3 instance
:param pool_contract_address: Contract address of the pool
:param block_identifier: A specific block to query price
:param reverse_token_order: If set, assume quote token is token0
:return: Current price
"""
pool_details = fetch_pool_details(web3, pool_contract_address)
_, tick, *_ = pool_details.pool.functions.slot0().call(block_identifier=block_identifier)

return pool_details.convert_price_to_human(tick, reverse_token_order)
19 changes: 19 additions & 0 deletions tests/uniswap_v3/test_uniswap_v3_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
UniswapV3PriceHelper,
estimate_buy_received_amount,
estimate_sell_received_amount,
get_onchain_price,
)
from eth_defi.uniswap_v3.utils import get_default_tick_range

Expand Down Expand Up @@ -313,3 +314,21 @@ def test_estimate_sell_received_cash(
)
assert usdc_received / 1e18 == pytest.approx(14159.565580618213)
assert block_number > 0


def test_get_onchain_price(web3, weth_usdc_uniswap_pool: str):
"""Test get onchain price of a pool."""

price = get_onchain_price(
web3,
weth_usdc_uniswap_pool,
)

assert price == pytest.approx(Decimal(1699.9057541866793))

reverse_price = get_onchain_price(
web3,
weth_usdc_uniswap_pool,
reverse_token_order=True,
)
assert reverse_price == pytest.approx(Decimal(0.00058826790693372))

0 comments on commit a34dcb9

Please sign in to comment.