uniswap_v2.oracle
Documentation for eth_defi.uniswap_v2.oracle Python module.
Price oracle implementation for Uniswap v2 pools.
Functions
Create a price entry based on Sync eth_getLogs result. |
|
|
Fetch live price of Uniswap v2 pool by listening to Sync event. |
Feed price oracle data for a given block range. |
Classes
Hold data about tokens in in the pool |
- class UniswapV2PriceOracleContext
Bases:
eth_defi.event_reader.logresult.LogContextHold data about tokens in in the pool
- __init__(pair, reverse_token_order)
- Parameters
pair (eth_defi.uniswap_v2.pair.PairDetails) –
reverse_token_order (bool) –
- Return type
None
- convert_sync_log_result_to_price_entry(log)
Create a price entry based on Sync eth_getLogs result.
Called by
update_price_oracle_with_sync_events_single_thread().- Parameters
log (dict) –
- Return type
- update_price_oracle_with_sync_events_single_thread(oracle, web3, pair_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 Sync event data from JSON-RPC node
start_block (int) – First block to include data for
end_block (int) – Last block to include data for (inclusive)
reverse_token_order – If pair token0 is the quote token to calculate the price.
pair_contract_address (str) –
- update_live_price_feed(oracle, web3, pair_contract_address, reverse_token_order=False, lookback_block_count=5)
Fetch live price of Uniswap v2 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) –
pair_contract_address (str) –
lookback_block_count (int) –
- Return type