uniswap_v3.oracle
Documentation for eth_defi.uniswap_v3.oracle Python module.
Price oracle implementation for Uniswap v3 pools.
Functions
Create a price entry based on eth_getLogs result. |
|
|
Fetch live price of Uniswap v3 pool by listening to Sync event. |
|
Feed price oracle data for a given block range using using a thread pool |
|
Feed price oracle data for a given block range. |
Classes
Hold data about tokens in the pool |
- class UniswapV3PriceOracleContext
Bases:
eth_defi.event_reader.logresult.LogContextHold data about tokens in the pool
- __init__(pool, reverse_token_order)
- Parameters
pool (eth_defi.uniswap_v3.pool.PoolDetails) –
reverse_token_order (bool) –
- Return type
None
- convert_swap_event_to_price_entry(log)
Create a price entry based on eth_getLogs result.
Called by
update_price_oracle_single_thread().- Parameters
log (dict) –
- Return type
- update_price_oracle_concurrent(oracle, json_rpc_url, pool_contract_address, start_block, end_block, reverse_token_order=False, max_workers=16)
Feed price oracle data for a given block range using using a thread pool
Example:
- Parameters
oracle (eth_defi.price_oracle.oracle.PriceOracle) – Price oracle to update
json_rpc_url (str) – JSON-RPC URL
pool_contract_address (str) – Pool contract address
start_block (int) – First block to include data for
end_block (int) – Last block to include data for (inclusive)
reverse_token_order (bool) – If pair token0 is the quote token to calculate the price.
max_workers (int) – How many threads to allocate for JSON-RPC IO.
- update_price_oracle_single_thread(oracle, web3, pool_contract_address, start_block, end_block, reverse_token_order=False)
Feed price oracle data for a given block range.
A slow single threaded implementation - suitable for testing.
Example:
- Parameters
oracle (eth_defi.price_oracle.oracle.PriceOracle) – Price oracle to update
web3 (web3.main.Web3) – Web3 connection we use to fetch event data from JSON-RPC node
pool_contract_address (str) – Pool contract address
start_block (int) – First block to include data for
end_block (int) – Last block to include data for (inclusive)
reverse_token_order (bool) – If pair token0 is the quote token to calculate the price.
- update_live_price_feed(oracle, web3, pool_contract_address, reverse_token_order=False, lookback_block_count=5)
Fetch live price of Uniswap v3 pool by listening to Sync event.
We use HTTP polling method, as HTTP polling is supported by free nodes.
Warning
We do not have bullet-proof logic to deal with minor chain reorgs. Some transactions can hop blocks and be rejected in later blocks, and we do not deal with this. This is a simple example implementation and may not suitable for production usage.
- Returns
Debug stats
- Parameters
oracle (eth_defi.price_oracle.oracle.PriceOracle) –
web3 (web3.main.Web3) –
pool_contract_address (str) –
reverse_token_order (bool) –
lookback_block_count (int) –
- Return type