Skip to content

Commit

Permalink
feat: liquidity endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkersner committed Oct 22, 2024
1 parent 1d5ef5b commit e47d519
Showing 1 changed file with 102 additions and 9 deletions.
111 changes: 102 additions & 9 deletions datamaxi/datamaxi/dex.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def candle(
pandas (bool): Return data as pandas DataFrame
Returns:
Candle data in pandas DataFrame and next request function
DEX candle data in pandas DataFrame and next request function
"""
check_required_parameters(
[
Expand Down Expand Up @@ -197,10 +197,102 @@ def next_request():
else:
return res, next_request

def liquidity(
self,
chain: str,
exchange: str,
pool: str,
page: int = 1,
limit: int = 1000,
fromDateTime: str = None,
toDateTime: str = None,
sort: str = "desc",
pandas: bool = True,
) -> Union[Tuple[Dict, Callable], Tuple[pd.DataFrame, Callable]]:
"""Fetch DEX liquidity data
`GET /api/v1/dex/liquidity`
<https://docs.datamaxiplus.com/rest/dex/liquidity>
Args:
chain (str): Chain name
exchange (str): Exchange name
pool (str): Pool name
page (int): Page number
limit (int): Limit of data
fromDateTime (str): Start date and time (accepts format "2006-01-02 15:04:05" or "2006-01-02")
toDateTime (str): End date and time (accepts format "2006-01-02 15:04:05" or "2006-01-02")
sort (str): Sort order
pandas (bool): Return data as pandas DataFrame
Returns:
DEX liquidity data in pandas DataFrame and next request function
"""
check_required_parameters(
[
[chain, "chain"],
[exchange, "exchange"],
[pool, "pool"],
]
)

if page < 1:
raise ValueError("page must be greater than 0")

if limit < 1:
raise ValueError("limit must be greater than 0")

if fromDateTime is not None and toDateTime is not None:
raise ValueError(
"fromDateTime and toDateTime cannot be set at the same time"
)

if sort not in ["asc", "desc"]:
raise ValueError("sort must be either asc or desc")

params = {
"chain": chain,
"exchange": exchange,
"pool": pool,
"page": page,
"limit": limit,
"from": fromDateTime,
"to": toDateTime,
"sort": sort,
}

res = self.query("/api/v1/dex/liquidity", params)
if res["data"] is None or len(res["data"]) == 0:
raise ValueError("no data found")

def next_request():
return self.get(
chain,
exchange,
pool,
page + 1,
limit,
fromDateTime,
toDateTime,
sort,
pandas,
)

if pandas:
df = convert_data_to_data_frame(
res["data"],
["b", "l"],
)
return df, next_request
else:
return res, next_request

def chains(self) -> List[str]:
"""Fetch supported chains accepted by
[datamaxi.DexCandle.get](./#datamaxi.datamaxi.DexCandle.get)
API.
[datamaxi.Dex.candle](./#datamaxi.datamaxi.Dex.candle),
[datamaxi.Dex.trade](./#datamaxi.datamaxi.Dex.trade) and
[datamaxi.Dex.liquidity](./#datamaxi.datamaxi.Dex.liquidity).
`GET /api/v1/dex/chains`
Expand All @@ -215,8 +307,9 @@ def chains(self) -> List[str]:

def exchanges(self) -> List[str]:
"""Fetch supported exchanges accepted by
[datamaxi.DexCandle.get](./#datamaxi.datamaxi.DexCandle.get)
API.
[datamaxi.Dex.candle](./#datamaxi.datamaxi.Dex.candle),
[datamaxi.Dex.trade](./#datamaxi.datamaxi.Dex.trade) and
[datamaxi.Dex.liquidity](./#datamaxi.datamaxi.Dex.liquidity).
`GET /api/v1/dex/exchanges`
Expand All @@ -231,8 +324,9 @@ def exchanges(self) -> List[str]:

def pools(self, exchange: str = None, chain: str = None) -> List[Dict]:
"""Fetch supported pools accepted by
[datamaxi.DexCandle.get](./#datamaxi.datamaxi.DexCandle.get)
API.
[datamaxi.Dex.candle](./#datamaxi.datamaxi.Dex.candle),
[datamaxi.Dex.trade](./#datamaxi.datamaxi.Dex.trade) and
[datamaxi.Dex.liquidity](./#datamaxi.datamaxi.Dex.liquidity).
`GET /api/v1/dex/pools`
Expand All @@ -256,8 +350,7 @@ def pools(self, exchange: str = None, chain: str = None) -> List[Dict]:

def intervals(self) -> List[str]:
"""Fetch supported intervals accepted by
[datamaxi.DexCandle.get](./#datamaxi.datamaxi.DexCandle.get)
API.
[datamaxi.Dex.candle](./#datamaxi.datamaxi.Dex.candle).
`GET /api/v1/dex/intervals`
Expand Down

0 comments on commit e47d519

Please sign in to comment.