diff --git a/cli.py b/cli.py index f67441d..e480ea8 100644 --- a/cli.py +++ b/cli.py @@ -1,6 +1,7 @@ import logging import os import sys +from datetime import datetime import click @@ -8,7 +9,7 @@ from mev_inspect.concurrency import coro from mev_inspect.crud.prices import write_prices from mev_inspect.db import get_inspect_session, get_trace_session from mev_inspect.inspector import MEVInspector -from mev_inspect.prices import fetch_prices +from mev_inspect.prices import fetch_prices, fetch_prices_range RPC_URL_ENV = "RPC_URL" @@ -117,6 +118,19 @@ def fetch_all_prices(): write_prices(inspect_db_session, prices) +@cli.command() +@click.argument("after", type=click.DateTime(formats=["%Y-%m-%d", "%m-%d-%Y"])) +@click.argument("before", type=click.DateTime(formats=["%Y-%m-%d", "%m-%d-%Y"])) +def fetch_range(after: datetime, before: datetime): + inspect_db_session = get_inspect_session() + + logger.info("Fetching prices") + prices = fetch_prices_range(after, before) + + logger.info("Writing prices") + write_prices(inspect_db_session, prices) + + def get_rpc_url() -> str: return os.environ["RPC_URL"] diff --git a/mev b/mev index d42e8ee..eb81fc6 100755 --- a/mev +++ b/mev @@ -82,7 +82,14 @@ case "$1" in kubectl exec -ti deploy/mev-inspect -- \ poetry run fetch-all-prices ;; - *) + fetch-range) + after=$2 + before=$3 + echo "Running price fetch-range" + kubectl exec -ti deploy/mev-inspect -- \ + poetry run fetch-range $after $before + ;; + *) echo "prices usage: "$1" {fetch-all}" exit 1 esac diff --git a/mev_inspect/prices.py b/mev_inspect/prices.py index e69394c..62b3508 100644 --- a/mev_inspect/prices.py +++ b/mev_inspect/prices.py @@ -1,4 +1,4 @@ -from datetime import datetime as dt +from datetime import datetime from typing import List from pycoingecko import CoinGeckoAPI @@ -7,27 +7,48 @@ from mev_inspect.schemas.prices import COINGECKO_ID_BY_ADDRESS, TOKEN_ADDRESSES, def fetch_prices() -> List[Price]: - cg = CoinGeckoAPI() + coingecko_api = CoinGeckoAPI() prices = [] for token_address in TOKEN_ADDRESSES: - price_data = cg.get_coin_market_chart_by_id( + coingecko_price_data = coingecko_api.get_coin_market_chart_by_id( id=COINGECKO_ID_BY_ADDRESS[token_address], vs_currency="usd", days="max", interval="daily", ) - price_time_series = price_data["prices"] - - for entry in price_time_series: - timestamp = dt.fromtimestamp(entry[0] / 100) - token_price = entry[1] - prices.append( - Price( - timestamp=timestamp, - usd_price=token_price, - token_address=token_address, - ) - ) + prices += _build_token_prices(coingecko_price_data, token_address) return prices + + +def fetch_prices_range(after: datetime, before: datetime) -> List[Price]: + coingecko_api = CoinGeckoAPI() + prices = [] + after_unix = int(after.timestamp()) + before_unix = int(before.timestamp()) + + for token_address in TOKEN_ADDRESSES: + coingecko_price_data = coingecko_api.get_coin_market_chart_range_by_id( + COINGECKO_ID_BY_ADDRESS[token_address], "usd", after_unix, before_unix + ) + + prices += _build_token_prices(coingecko_price_data, token_address) + + return prices + + +def _build_token_prices(coingecko_price_data, token_address) -> List[Price]: + time_series = coingecko_price_data["prices"] + prices = [] + for entry in time_series: + timestamp = datetime.fromtimestamp(entry[0] / 1000) + token_price = entry[1] + prices.append( + Price( + timestamp=timestamp, + usd_price=token_price, + token_address=token_address, + ) + ) + return prices diff --git a/pyproject.toml b/pyproject.toml index f2f9f17..d616834 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ inspect-many-blocks = 'cli:inspect_many_blocks_command' enqueue-many-blocks = 'cli:enqueue_many_blocks_command' fetch-block = 'cli:fetch_block_command' fetch-all-prices = 'cli:fetch_all_prices' +fetch-range = 'cli:fetch_range' [tool.black] exclude = '''