From 6f99d4ec786e1013b6f41008cb3700e1db25037a Mon Sep 17 00:00:00 2001 From: naga Date: Thu, 26 Oct 2023 16:03:59 -0400 Subject: [PATCH] Move get_pool_volume() to pool_data - replace pool_data.queries with folder - move pool_volume to pool_data.queries - support get_pool_volume() using pool address --- ...5_203748_nagakingg_curve_prices_volume.rst | 3 +- .../pipelines/vol_limited_arb/__init__.py | 2 +- curvesim/pool_data/__init__.py | 40 ++----------------- curvesim/pool_data/queries/__init__.py | 3 ++ .../{queries.py => queries/metadata.py} | 38 ++++++++++++++++-- .../queries}/pool_volume.py | 19 +++++++-- test/integration/test_get_pool_volume.py | 19 +++++++-- test/unit/test_pool_volume.py | 2 +- 8 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 curvesim/pool_data/queries/__init__.py rename curvesim/pool_data/{queries.py => queries/metadata.py} (57%) rename curvesim/{pipelines/vol_limited_arb => pool_data/queries}/pool_volume.py (83%) diff --git a/changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst b/changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst index ae1b24e47..2dba45e78 100644 --- a/changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst +++ b/changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst @@ -11,8 +11,9 @@ Added ----- - Added network.curve_prices - Added ApiResultError to exceptions -- Added vol_limited_arb.pool_volume.get_pool_volume() +- Added pool_data.get_pool_volume() Changed ------- - Volume multipliers now computed individually for each asset pair +- Replaced pool_data.queries with folder diff --git a/curvesim/pipelines/vol_limited_arb/__init__.py b/curvesim/pipelines/vol_limited_arb/__init__.py index 556810394..e7b03b09e 100644 --- a/curvesim/pipelines/vol_limited_arb/__init__.py +++ b/curvesim/pipelines/vol_limited_arb/__init__.py @@ -9,10 +9,10 @@ from curvesim.logging import get_logger from curvesim.metrics import init_metrics, make_results from curvesim.pool import get_sim_pool +from curvesim.pool_data import get_pool_volume from .. import run_pipeline from ..common import DEFAULT_METRICS -from .pool_volume import get_pool_volume from .strategy import VolumeLimitedStrategy logger = get_logger(__name__) diff --git a/curvesim/pool_data/__init__.py b/curvesim/pool_data/__init__.py index 384545364..a00749a80 100644 --- a/curvesim/pool_data/__init__.py +++ b/curvesim/pool_data/__init__.py @@ -1,44 +1,12 @@ """ -Tools for fetching pool state and metadata. +Tools for fetching pool metadata and volume. Currently supports stableswap pools, metapools, rebasing (RAI) metapools, and 2-token cryptopools. """ -__all__ = [ - "from_address", - "get_metadata", -] +__all__ = ["get_metadata", "get_pool_volume"] -from curvesim.pool_data.metadata import PoolMetaData -from .queries import from_address - - -def get_metadata( - address, - chain="mainnet", - env="prod", - end_ts=None, -): - """ - Pulls pool state and metadata from daily snapshot. - - Parameters - ---------- - address : str - Pool address prefixed with “0x”. - - chain : str - Chain/layer2 identifier, e.g. “mainnet”, “arbitrum”, “optimism". - - Returns - ------- - :class:`~curvesim.pool_data.metadata.PoolMetaDataInterface` - - """ - # TODO: validate function arguments - metadata_dict = from_address(address, chain, env=env, end_ts=end_ts) - metadata = PoolMetaData(metadata_dict) - - return metadata +from .queries.metadata import get_metadata +from .queries.pool_volume import get_pool_volume diff --git a/curvesim/pool_data/queries/__init__.py b/curvesim/pool_data/queries/__init__.py new file mode 100644 index 000000000..e732838e2 --- /dev/null +++ b/curvesim/pool_data/queries/__init__.py @@ -0,0 +1,3 @@ +""" +Functions for fetching data about Curve pools. +""" diff --git a/curvesim/pool_data/queries.py b/curvesim/pool_data/queries/metadata.py similarity index 57% rename from curvesim/pool_data/queries.py rename to curvesim/pool_data/queries/metadata.py index e61bb7135..c4b967b6e 100644 --- a/curvesim/pool_data/queries.py +++ b/curvesim/pool_data/queries/metadata.py @@ -1,8 +1,11 @@ +""" +Functions to get pool metadata for Curve pools. +""" +from curvesim.network.subgraph import pool_snapshot_sync, symbol_address_sync +from curvesim.network.web3 import underlying_coin_info_sync +from curvesim.pool_data.metadata import PoolMetaData from curvesim.utils import get_event_loop -from ..network.subgraph import pool_snapshot_sync, symbol_address_sync -from ..network.web3 import underlying_coin_info_sync - def from_address(address, chain, env="prod", end_ts=None): """ @@ -50,3 +53,32 @@ def from_symbol(symbol, chain, env): data = from_address(address, chain, env) return data + + +def get_metadata( + address, + chain="mainnet", + env="prod", + end_ts=None, +): + """ + Pulls pool state and metadata from daily snapshot. + + Parameters + ---------- + address : str + Pool address prefixed with “0x”. + + chain : str + Chain/layer2 identifier, e.g. “mainnet”, “arbitrum”, “optimism". + + Returns + ------- + :class:`~curvesim.pool_data.metadata.PoolMetaDataInterface` + + """ + # TODO: validate function arguments + metadata_dict = from_address(address, chain, env=env, end_ts=end_ts) + metadata = PoolMetaData(metadata_dict) + + return metadata diff --git a/curvesim/pipelines/vol_limited_arb/pool_volume.py b/curvesim/pool_data/queries/pool_volume.py similarity index 83% rename from curvesim/pipelines/vol_limited_arb/pool_volume.py rename to curvesim/pool_data/queries/pool_volume.py index ce2c721f6..65a2f09d0 100644 --- a/curvesim/pipelines/vol_limited_arb/pool_volume.py +++ b/curvesim/pool_data/queries/pool_volume.py @@ -4,7 +4,7 @@ from datetime import datetime, timezone from math import comb -from typing import List, Optional, Tuple +from typing import List, Optional, Tuple, Union from pandas import DataFrame, Series @@ -13,21 +13,24 @@ from curvesim.pool_data.metadata import PoolMetaDataInterface from curvesim.utils import get_event_loop, get_pairs +from .metadata import get_metadata + logger = get_logger(__name__) def get_pool_volume( - pool_metadata: PoolMetaDataInterface, + metadata_or_address: Union[PoolMetaDataInterface, str], days: int = 60, end: Optional[int] = None, + chain: Optional[str] = "mainnet", ) -> DataFrame: """ Gets historical daily volume for each pair of coins traded in a Curve pool. Parameters ---------- - pool_metadata: PoolMetaDataInterface - Pool metadata for the pool of interest. + metadata_or_address: PoolMetaDataInterface or str + Pool metadata or pool address to fetch metadata. days: int, defaults to 60 Number of days to pull volume data for. @@ -35,6 +38,9 @@ def get_pool_volume( end: int, defaults to start of current date Posix timestamp of the last time to pull data for. + chain: str, default "mainnet" + Chain to use if pool address is provided to fetch metadata. + Returns ------- DataFrame @@ -44,6 +50,11 @@ def get_pool_volume( logger.info("Fetching historical pool volume...") + if isinstance(metadata_or_address, str): + pool_metadata: PoolMetaDataInterface = get_metadata(metadata_or_address, chain) + else: + pool_metadata = metadata_or_address + pair_data = _get_pair_data(pool_metadata) start_ts, end_ts = _process_timestamps(days, end) loop = get_event_loop() diff --git a/test/integration/test_get_pool_volume.py b/test/integration/test_get_pool_volume.py index 9ba7bd883..bb8f726bf 100644 --- a/test/integration/test_get_pool_volume.py +++ b/test/integration/test_get_pool_volume.py @@ -1,4 +1,4 @@ -from curvesim.pipelines.vol_limited_arb.pool_volume import get_pool_volume +from curvesim.pool_data import get_pool_volume from curvesim.pool_data.metadata import PoolMetaData from curvesim.utils import get_pairs @@ -21,6 +21,17 @@ def test_get_pool_volume(): for metadata in metadata_list: pool_metadata = PoolMetaData(metadata) - volumes = get_pool_volume(pool_metadata, days=2, end=1698292800) - assert len(volumes) == 2 - assert volumes.columns.to_list() == get_pairs(pool_metadata.coin_names) + + # Test using metadata + volumes1 = get_pool_volume(pool_metadata, days=2, end=1698292800) + assert len(volumes1) == 2 + assert volumes1.columns.to_list() == get_pairs(pool_metadata.coin_names) + + # Test using address and chain + address = pool_metadata.address + chain = pool_metadata.chain + volumes2 = get_pool_volume(address, chain=chain, days=2, end=1698292800) + assert len(volumes2) == 2 + assert volumes2.columns.to_list() == get_pairs(pool_metadata.coin_names) + + assert all(volumes1 == volumes2) diff --git a/test/unit/test_pool_volume.py b/test/unit/test_pool_volume.py index 1c4f3a6ca..dddf5e5f7 100644 --- a/test/unit/test_pool_volume.py +++ b/test/unit/test_pool_volume.py @@ -1,6 +1,6 @@ from math import comb -from curvesim.pipelines.vol_limited_arb.pool_volume import _get_pair_data +from curvesim.pool_data.queries.pool_volume import _get_pair_data class DummyMetadata: