uniswap_v2.oracle

Documentation for eth_defi.uniswap_v2.oracle Python module.

Price oracle implementation for Uniswap v2 pools.

Functions

convert_sync_log_result_to_price_entry(log)

Create a price entry based on Sync eth_getLogs result.

update_live_price_feed(oracle, web3, ...[, ...])

Fetch live price of Uniswap v2 pool by listening to Sync event.

update_price_oracle_with_sync_events_single_thread(...)

Feed price oracle data for a given block range.

Classes

UniswapV2PriceOracleContext

Hold data about tokens in in the pool

class UniswapV2PriceOracleContext

Bases: eth_defi.event_reader.logresult.LogContext

Hold data about tokens in in the pool

__init__(pair, reverse_token_order)
Parameters
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

eth_defi.price_oracle.oracle.PriceEntry

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
Return type

collections.Counter