Merge pull request #239 from flashbots/prices-range
Add prices fetch-range
This commit is contained in:
commit
7bdc8b68ef
16
cli.py
16
cli.py
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
@ -8,7 +9,7 @@ from mev_inspect.concurrency import coro
|
|||||||
from mev_inspect.crud.prices import write_prices
|
from mev_inspect.crud.prices import write_prices
|
||||||
from mev_inspect.db import get_inspect_session, get_trace_session
|
from mev_inspect.db import get_inspect_session, get_trace_session
|
||||||
from mev_inspect.inspector import MEVInspector
|
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"
|
RPC_URL_ENV = "RPC_URL"
|
||||||
|
|
||||||
@ -117,6 +118,19 @@ def fetch_all_prices():
|
|||||||
write_prices(inspect_db_session, 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:
|
def get_rpc_url() -> str:
|
||||||
return os.environ["RPC_URL"]
|
return os.environ["RPC_URL"]
|
||||||
|
|
||||||
|
9
mev
9
mev
@ -82,7 +82,14 @@ case "$1" in
|
|||||||
kubectl exec -ti deploy/mev-inspect -- \
|
kubectl exec -ti deploy/mev-inspect -- \
|
||||||
poetry run fetch-all-prices
|
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}"
|
echo "prices usage: "$1" {fetch-all}"
|
||||||
exit 1
|
exit 1
|
||||||
esac
|
esac
|
||||||
|
@ -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
|
||||||
@ -7,27 +7,48 @@ from mev_inspect.schemas.prices import COINGECKO_ID_BY_ADDRESS, TOKEN_ADDRESSES,
|
|||||||
|
|
||||||
|
|
||||||
def fetch_prices() -> List[Price]:
|
def fetch_prices() -> List[Price]:
|
||||||
cg = CoinGeckoAPI()
|
coingecko_api = CoinGeckoAPI()
|
||||||
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 = coingecko_api.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] / 100)
|
|
||||||
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(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
|
||||||
|
@ -39,6 +39,7 @@ inspect-many-blocks = 'cli:inspect_many_blocks_command'
|
|||||||
enqueue-many-blocks = 'cli:enqueue_many_blocks_command'
|
enqueue-many-blocks = 'cli:enqueue_many_blocks_command'
|
||||||
fetch-block = 'cli:fetch_block_command'
|
fetch-block = 'cli:fetch_block_command'
|
||||||
fetch-all-prices = 'cli:fetch_all_prices'
|
fetch-all-prices = 'cli:fetch_all_prices'
|
||||||
|
fetch-range = 'cli:fetch_range'
|
||||||
|
|
||||||
[tool.black]
|
[tool.black]
|
||||||
exclude = '''
|
exclude = '''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user