Skip to main content

A package to retrieve token prices by timestamp

Project description

Web3 Oracle

A Python package for retrieving token prices by timestamp, datetime, or Ethereum block number.

Features

  • Get token prices by timestamp or datetime
  • Get token prices by Ethereum block number
  • Fast ETH price lookup by block number using direct mapping
  • Fast BTC price lookup by Ethereum block number
  • Reference prices for altcoins and less common tokens
  • Unified token price lookup with automatic source selection
  • Command-line interface

Installation

pip install web3-oracle

Testing

To test the complete package functionality:

# Run complete test suite
python run_tests.py
# OR
make test

# Quick tests only (faster)
python run_tests.py --quick
# OR 
make test-quick

The test suite verifies:

  • ✅ ETH/BTC price lookup by timestamp
  • ✅ ETH/BTC price lookup by block number (with different rounding logic)
  • ✅ Altcoin reference prices
  • ✅ CLI functionality
  • ✅ Performance benchmarks

Usage

Basic Usage

from web3_oracle import Oracle
from datetime import datetime

# Initialize the oracle
oracle = Oracle()

# Get price by timestamp
eth_price = oracle.get_price("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", 1620518400)

# Get price by datetime
dt = datetime(2022, 1, 15, 12, 0, 0)
btc_price = oracle.get_price_by_datetime("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599", dt)

# Get reference price for less common tokens
uni_price = oracle.get_reference_price("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

# Get Ethereum price by block number (WETH/WBTC only)
eth_block_price = oracle.get_price_by_block("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", 15000000)

# Fast ETH price lookup by block (rounded to nearest 10,000)
fast_eth_price = oracle.fast_price(14350000)  # Will use price at block 14350000

# Fast BTC price lookup by block (rounded to nearest 10,000)
fast_btc_price = oracle.fast_btc_price(18039284)  # Will use price at block 18039284

# Unified token price lookup (automatically chooses optimal source) - RECOMMENDED
token_price = oracle.get_token_price(
    token_address="0x1f9840a85d5af5bf1d1762f925bdaddc4201f984",  # UNI token
    block_number=15000000  # Optional
)

Command Line Interface

# Get price by timestamp
web3-oracle get_price 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 1620518400

# Get price by date and time
web3-oracle get_price_by_date 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 "2021-05-08" "12:00:00"

# Get reference price for a token
web3-oracle get_reference_price 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984

# Get price by block number (WETH/WBTC only)
web3-oracle get_price_by_block 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 15000000

# Fast ETH price lookup
web3-oracle fast_price 14350000

# Fast BTC price lookup
web3-oracle fast_btc_price 18039284

# Unified token price lookup (automatically selects best source)
web3-oracle get_token_price 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 --block 15000000
web3-oracle get_token_price 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984

# List all available tokens
web3-oracle list_tokens

Data Sources

The package uses several data sources:

  • ethereum_prices.csv: Historical ETH prices by timestamp
  • bitcoin_prices.csv: Historical BTC prices by timestamp
  • stable_prices.csv: Historical stablecoin prices by timestamp
  • altcoins_prices.csv: Reference prices for altcoins and less common tokens
  • eth_block_prices.csv: Direct mapping of Ethereum block numbers to ETH prices
  • btc_block_prices.csv: Direct mapping of Ethereum block numbers to BTC prices

Tools

The package includes tools for indexing Ethereum blocks and creating price mappings:

# Generate ETH block to price direct mapping
python -m web3_oracle.tools.index_eth_blocks

# Generate BTC price mapping for Ethereum blocks
python -m web3_oracle.tools.index_btc_blocks

Priority Order for Token Prices

The unified get_token_price method uses the following priority order:

  1. For WETH with block number: Direct block price mapping (fastest)
  2. For WBTC with block number: Direct block price mapping (fast)
  3. For any token with reference price: Reference price from altcoins data
  4. For any token with block number: Not supported (use timestamp or reference price)
  5. For any token with timestamp: Regular timestamp lookup

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web3_oracle-0.3.0.tar.gz (235.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web3_oracle-0.3.0-py3-none-any.whl (242.4 kB view details)

Uploaded Python 3

File details

Details for the file web3_oracle-0.3.0.tar.gz.

File metadata

  • Download URL: web3_oracle-0.3.0.tar.gz
  • Upload date:
  • Size: 235.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for web3_oracle-0.3.0.tar.gz
Algorithm Hash digest
SHA256 768cd419cce2bd6e6b091a3d8b542a32e1ebd6142bc4e49dab22e0023e66d822
MD5 d248dc89e023167fa261951037f36259
BLAKE2b-256 0fa50389a5b3c968a1d1448196b31167d7b357ee269fc833b4164e05f92d46f0

See more details on using hashes here.

File details

Details for the file web3_oracle-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: web3_oracle-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 242.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for web3_oracle-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c49e0d92b57c1c193828293fd859ec8fad96a8a144417b03c01637fd1967747e
MD5 129cb704ee014fb65c33f8098d579d29
BLAKE2b-256 40aec73519114ef74ae993e937d231ff420afcaf632a9f5d0c955be7b29a673a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page