Skip to content

Commit

Permalink
Move get_pool_volume() to pool_data
Browse files Browse the repository at this point in the history
- replace pool_data.queries with folder
- move pool_volume to pool_data.queries
- support get_pool_volume() using pool address
  • Loading branch information
nagakingg committed Oct 27, 2023
1 parent 98a6f81 commit 048c0e8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 52 deletions.
15 changes: 12 additions & 3 deletions changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ Removed

Added
-----
- Added network.curve_prices
- Added ApiResultError to exceptions
- Added vol_limited_arb.pool_volume.get_pool_volume()
- Added Curve Prices API volume query (network.curve_prices)
- Added pool_data.get_pool_volume() to fetch historical pool volume from
Curve Prices API

Changed
-------
- Volume multipliers now computed individually for each asset pair
- Replaced pool_data.queries with folder
- Pool volume and volume limiting are only supported for Ethereum pools
pending updates to Curve Prices API

Deprecated
----------
- RAI3CRV pool is currently unsupported by simulation pipelines. It will
be reimplemented along with Stableswap-NG pools.

2 changes: 1 addition & 1 deletion curvesim/pipelines/vol_limited_arb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
40 changes: 4 additions & 36 deletions curvesim/pool_data/__init__.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions curvesim/pool_data/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Functions for fetching data about Curve pools.
"""
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,28 +13,34 @@
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.
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
Expand All @@ -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()
Expand Down
19 changes: 15 additions & 4 deletions test/integration/test_get_pool_volume.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
2 changes: 1 addition & 1 deletion test/unit/test_pool_volume.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 048c0e8

Please sign in to comment.