Shared function, start/end -> after/before

This commit is contained in:
Gui Heise 2022-01-19 16:48:20 -05:00
parent c4f82bdbd6
commit f0064e01b2
3 changed files with 33 additions and 36 deletions

9
cli.py
View File

@ -1,6 +1,7 @@
import logging import logging
import os import os
import sys import sys
from datetime import datetime
import click import click
@ -118,13 +119,13 @@ def fetch_all_prices():
@cli.command() @cli.command()
@click.argument("start", type=int) @click.argument("after", type=datetime)
@click.argument("end", type=int) @click.argument("before", type=datetime)
def fetch_range(start: int, end: int): def fetch_range(after: datetime, before: datetime):
inspect_db_session = get_inspect_session() inspect_db_session = get_inspect_session()
logger.info("Fetching prices") logger.info("Fetching prices")
prices = fetch_prices_range(start, end) prices = fetch_prices_range(after, before)
logger.info("Writing prices") logger.info("Writing prices")
write_prices(inspect_db_session, prices) write_prices(inspect_db_session, prices)

6
mev
View File

@ -83,11 +83,11 @@ case "$1" in
poetry run fetch-all-prices poetry run fetch-all-prices
;; ;;
fetch-range) fetch-range)
start=$2 after=$2
end=$3 before=$3
echo "Running price fetch-range" echo "Running price fetch-range"
kubectl exec -ti deploy/mev-inspect -- \ kubectl exec -ti deploy/mev-inspect -- \
poetry run fetch-range $start $end poetry run fetch-range $after $before
;; ;;
*) *)
echo "prices usage: "$1" {fetch-all}" echo "prices usage: "$1" {fetch-all}"

View File

@ -1,4 +1,4 @@
from datetime import datetime as dt from datetime import datetime
from typing import List from typing import List
from pycoingecko import CoinGeckoAPI from pycoingecko import CoinGeckoAPI
@ -11,48 +11,44 @@ def fetch_prices() -> List[Price]:
prices = [] prices = []
for token_address in TOKEN_ADDRESSES: for token_address in TOKEN_ADDRESSES:
price_data = cg.get_coin_market_chart_by_id( coingecko_price_data = cg.get_coin_market_chart_by_id(
id=COINGECKO_ID_BY_ADDRESS[token_address], id=COINGECKO_ID_BY_ADDRESS[token_address],
vs_currency="usd", vs_currency="usd",
days="max", days="max",
interval="daily", interval="daily",
) )
price_time_series = price_data["prices"] prices += _build_token_prices(coingecko_price_data, token_address)
for entry in price_time_series:
timestamp = dt.fromtimestamp(entry[0] / 1000)
token_price = entry[1]
prices.append(
Price(
timestamp=timestamp,
usd_price=token_price,
token_address=token_address,
)
)
return prices return prices
def fetch_prices_range(start: int, end: int) -> List[Price]: def fetch_prices_range(after: datetime, before: datetime) -> List[Price]:
cg = CoinGeckoAPI() cg = CoinGeckoAPI()
prices = [] prices = []
after_unix = int(after.timestamp())
before_unix = int(before.timestamp())
for token_address in TOKEN_ADDRESSES: for token_address in TOKEN_ADDRESSES:
price_data = cg.get_coin_market_chart_range_by_id( coingecko_price_data = cg.get_coin_market_chart_range_by_id(
COINGECKO_ID_BY_ADDRESS[token_address], "usd", start, end COINGECKO_ID_BY_ADDRESS[token_address], "usd", after_unix, before_unix
) )
price_time_series = price_data["prices"]
for entry in price_time_series: prices += _build_token_prices(coingecko_price_data, token_address)
timestamp = dt.fromtimestamp(entry[0] / 1000)
token_price = entry[1]
prices.append(
Price(
timestamp=timestamp,
usd_price=token_price,
token_address=token_address,
)
)
return prices 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