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

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
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)
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
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: Convert to timestamp and lookup
  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.1.0.tar.gz (212.3 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.1.0-py3-none-any.whl (214.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: web3_oracle-0.1.0.tar.gz
  • Upload date:
  • Size: 212.3 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.1.0.tar.gz
Algorithm Hash digest
SHA256 9c3f92466dd1c1c12a82bfed12a674a8098a85f8f1d1077edb8248d759aa4948
MD5 9c3da97ee05404eb785da535eda6bd35
BLAKE2b-256 03d9833f889c77ec75851620c7ceab7a07c6290f7f8687461dd53a9c552f8b5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: web3_oracle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 214.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f39c67c421212388daf5c4cbc3a1cb936e1f038ff1f0dc29536f07739f856787
MD5 f86d4379350dd7f338b2ef84fc12ba39
BLAKE2b-256 bb963b412bd547084678a64840cf2531cfde13701471a7ecb5577bc0d6723df2

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