-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Curve Prices API for pool volume
- Add network.curve_prices - Add ApiResultError to exceptions - Add vol_limited_arb.pool_volume.get_pool_volume() - Compute volume multiplier per asset pair - Remove volume multiplier modes - Remove pipelines.utils - Remove PriceVolume.total_volumes() - Remove volume from PoolDataCache
- Loading branch information
Showing
8 changed files
with
259 additions
and
200 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
changelog.d/20231025_203748_nagakingg_curve_prices_volume.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Removed | ||
------- | ||
- Removed volume multiplier modes | ||
- Removed pipelines.utils | ||
- Removed PriceVolume.total_volumes() | ||
- Removed volume from PoolDataCache | ||
|
||
Added | ||
----- | ||
- Added network.curve_prices | ||
- Added ApiResultError to exceptions | ||
- Added vol_limited_arb.pool_volume.get_pool_volume() | ||
|
||
Changed | ||
------- | ||
- Volume multipliers now computed individually for each asset pair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
""" | ||
Network connector for Curve Prices API. | ||
""" | ||
|
||
from typing import List | ||
|
||
from eth_utils import to_checksum_address | ||
from pandas import DataFrame, to_datetime | ||
|
||
from curvesim.exceptions import ApiResultError, CurvesimValueError | ||
|
||
from .http import HTTP | ||
from .utils import sync | ||
|
||
URL = "https://prices.curve.fi/v1/" | ||
|
||
CHAIN_ALIASES = {"mainnet": "ethereum"} | ||
|
||
|
||
async def _get_pool_pair_volume( | ||
pool_address, | ||
base_token_address, | ||
quote_token_address, | ||
start_ts, | ||
end_ts, | ||
*, | ||
chain="ethereum", | ||
interval="day", | ||
): | ||
chain = _chain_from_alias(chain) | ||
pool_address = to_checksum_address(pool_address) | ||
|
||
url = URL + f"volume/{chain}/{pool_address}" | ||
params = { | ||
"main_token": quote_token_address, | ||
"reference_token": base_token_address, | ||
"start": start_ts, | ||
"end": end_ts, | ||
"interval": interval, | ||
} | ||
r = await HTTP.get(url, params=params) | ||
|
||
try: | ||
data = r["data"] | ||
except KeyError as e: | ||
raise ApiResultError( | ||
"No historical volume returned for\n" | ||
f"Pool: '{pool_address}', Chain: '{chain}',\n" | ||
f"Tokens: (base: {base_token_address}, quote: {quote_token_address}),\n" | ||
f"Timestamps: (start: {start_ts}, end: {end_ts})" | ||
) from e | ||
|
||
return data | ||
|
||
|
||
async def get_pool_pair_volume( | ||
pool_address: str, | ||
base_token_address: str, | ||
quote_token_address: str, | ||
start_ts: int, | ||
end_ts: int, | ||
*, | ||
chain: str = "ethereum", | ||
interval: str = "day", | ||
) -> DataFrame: | ||
""" | ||
Gets historical daily volume for a pair of coins traded in a Curve pool. | ||
Parameters | ||
---------- | ||
pool_address: str | ||
The Curve pool's address. | ||
base_token_address: str | ||
Address for the base token. | ||
quote_token_address: str | ||
Address for the quote token. Volumes are returned in units of | ||
start_ts: int | ||
Posix timestamp (UTC) for start of query period. | ||
end_ts: int | ||
Posix timestamp (UTC) for end of query period. | ||
chain: str, default "ethereum" | ||
The pool's blockchain (note: currently only "ethereum" supported) | ||
interval: str, default "day" | ||
The sampling interval for the data. Available values: week, day, hour | ||
Returns | ||
------- | ||
DataFrame | ||
Rows: DateTimeIndex; Columns: volume, fees | ||
""" | ||
data: List[dict] = await _get_pool_pair_volume( | ||
pool_address, | ||
base_token_address, | ||
quote_token_address, | ||
start_ts, | ||
end_ts, | ||
chain=chain, | ||
interval=interval, | ||
) | ||
|
||
df = DataFrame(data, columns=["timestamp", "volume", "fees"]) | ||
df["timestamp"] = to_datetime(df["timestamp"], unit="s") | ||
df.set_index("timestamp", inplace=True) | ||
return df | ||
|
||
|
||
def _chain_from_alias(chain): | ||
if chain in CHAIN_ALIASES: | ||
chain = CHAIN_ALIASES.get(chain, chain) | ||
|
||
if chain != "ethereum": | ||
raise CurvesimValueError( | ||
"Curve Prices API currently only supports Ethereum chain." | ||
) | ||
|
||
return chain | ||
|
||
|
||
get_pool_pair_volume_sync = sync(get_pool_pair_volume) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.