Skip to content

Commit ca08e29

Browse files
Chain/revenue access single param only (#15)
* feat: chain/revenue access single param only * feat: allow specify protocol and/or chain in feed/revenue detail * chore: flake8 * fix: flake8
1 parent a150d32 commit ca08e29

File tree

3 files changed

+75
-42
lines changed

3 files changed

+75
-42
lines changed

datamaxi/defillama/__init__.py

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datamaxi.lib.utils import check_required_parameter
55
from datamaxi.lib.utils import check_required_parameters
66
from datamaxi.lib.utils import check_required_parameter_list
7+
from datamaxi.lib.utils import check_at_least_one_set_parameters
78
from datamaxi.lib.utils import encode_string_list
89
from datamaxi.lib.utils import make_list
910
from datamaxi.lib.utils import postprocess
@@ -360,21 +361,21 @@ def stablecoin_price(
360361
@postprocess()
361362
def fee(
362363
self,
363-
protocols: Union[str, List[str]] = None,
364-
chains: Union[str, List[str]] = None,
364+
protocol: str = None,
365+
chain: str = None,
365366
daily: bool = True,
366367
pandas: bool = True,
367368
) -> Union[List, pd.DataFrame]:
368-
"""Get fee for given protocols or chains
369+
"""Get fee for given protocol or chain
369370
370371
`GET /v1/defillama/fee`
371372
372373
<https://docs.datamaxiplus.com/defillama/fee>
373374
374375
Args:
375-
protocols (Union[str, List[str]]): single protocol or multiple protocol names
376-
chains (Union[str, List[str]]): single chain or multiple chain names
377-
daily (bool): daily fee or total fee
376+
protocol (str): Protocol name
377+
chain (str): Chain name
378+
daily (bool): Daily fee or total fee
378379
pandas (bool): Return data as pandas DataFrame
379380
380381
Returns:
@@ -385,38 +386,36 @@ def fee(
385386
"daily": str(daily).lower(),
386387
}
387388

388-
if not ((protocols is None) ^ (chains is None)):
389+
if not ((protocol is None) ^ (chain is None)):
389390
raise ValueError("Either protocols or chains should be provided")
390391

391-
if protocols is not None:
392-
protocols = make_list(protocols)
393-
check_required_parameter_list(protocols, "protocols")
394-
params["protocols"] = encode_string_list(protocols)
395-
elif chains is not None:
396-
chains = make_list(chains)
397-
check_required_parameter_list(chains, "chains")
398-
params["chains"] = encode_string_list(chains)
392+
if protocol is not None:
393+
check_required_parameter(protocol, "protocol")
394+
params["protocol"] = protocol
395+
elif chain is not None:
396+
check_required_parameter(chain, "chain")
397+
params["chain"] = chain
399398

400399
return self.query("/v1/defillama/fee", params)
401400

402401
@postprocess()
403402
def revenue(
404403
self,
405-
protocols: Union[str, List[str]] = None,
406-
chains: Union[str, List[str]] = None,
404+
protocol: str = None,
405+
chain: str = None,
407406
daily: bool = True,
408407
pandas: bool = True,
409408
) -> Union[List, pd.DataFrame]:
410-
"""Get revenue for given protocols or chains
409+
"""Get revenue for given protocol or chain
411410
412411
`GET /v1/defillama/revenue`
413412
414413
<https://docs.datamaxiplus.com/defillama/revenue>
415414
416415
Args:
417-
protocols (Union[str, List[str]]): single protocol or multiple protocol names
418-
chains (Union[str, List[str]]): single chain or multiple chain names
419-
daily (bool): daily revenue or total revenue
416+
protocol (str): Protocol name
417+
chain (str): Chain name
418+
daily (bool): Daily revenue or total revenue
420419
pandas (bool): Return data as pandas DataFrame
421420
422421
Returns:
@@ -427,23 +426,25 @@ def revenue(
427426
"daily": str(daily).lower(),
428427
}
429428

430-
if not ((protocols is None) ^ (chains is None)):
429+
if not ((protocol is None) ^ (chain is None)):
431430
raise ValueError("Either protocols or chains should be provided")
432431

433-
if protocols is not None:
434-
protocols = make_list(protocols)
435-
check_required_parameter_list(protocols, "protocols")
436-
params["protocols"] = encode_string_list(protocols)
437-
elif chains is not None:
438-
chains = make_list(chains)
439-
check_required_parameter_list(chains, "chains")
440-
params["chains"] = encode_string_list(chains)
432+
if protocol is not None:
433+
check_required_parameter(protocol, "protocol")
434+
params["protocol"] = protocol
435+
elif chain is not None:
436+
check_required_parameter(chain, "chain")
437+
params["chain"] = chain
441438

442439
return self.query("/v1/defillama/revenue", params)
443440

444441
@postprocess(num_index=3)
445442
def fee_detail(
446-
self, protocol: str, chain: str = None, daily: bool = True, pandas: bool = True
443+
self,
444+
protocol: str = None,
445+
chain: str = None,
446+
daily: bool = True,
447+
pandas: bool = True,
447448
) -> Union[List, pd.DataFrame]:
448449
"""Get fee detail for given protocol and chain
449450
@@ -452,27 +453,35 @@ def fee_detail(
452453
<https://docs.datamaxiplus.com/defillama/fee-detail>
453454
454455
Args:
455-
protocol (str): protocol name
456-
chain (str): chain name (optional)
457-
daily (bool): daily fee or total fee
456+
protocol (str): Protocol name
457+
chain (str): Chain name
458+
daily (bool): Daily fee or total fee
458459
pandas (bool): Return data as pandas DataFrame
459460
460461
Returns:
461462
Timeseries of fee detail for a given protocol and chain
462463
"""
463-
check_required_parameters([[protocol, "protocol"], [daily, "daily"]])
464+
check_required_parameter(daily, "daily")
464465
params = {
465-
"protocol": protocol,
466466
"daily": str(daily).lower(),
467467
}
468+
469+
check_at_least_one_set_parameters([[protocol, "protocol"], [chain, "chain"]])
470+
if protocol is not None:
471+
params["protocol"] = protocol
472+
468473
if chain is not None:
469474
params["chain"] = chain
470475

471476
return self.query("/v1/defillama/fee/detail", params)
472477

473478
@postprocess(num_index=3)
474479
def revenue_detail(
475-
self, protocol: str, chain: str = None, daily: bool = True, pandas: bool = True
480+
self,
481+
protocol: str = None,
482+
chain: str = None,
483+
daily: bool = True,
484+
pandas: bool = True,
476485
) -> Union[List, pd.DataFrame]:
477486
"""Get revenue detail for given protocol and chain
478487
@@ -481,19 +490,23 @@ def revenue_detail(
481490
<https://docs.datamaxiplus.com/defillama/revenue-detail>
482491
483492
Args:
484-
protocol (str): protocol name
485-
chain (str): chain name (optional)
486-
daily (bool): daily revenue or total revenue
493+
protocol (str): Protocol name
494+
chain (str): Chain name
495+
daily (bool): Daily revenue or total revenue
487496
pandas (bool): Return data as pandas DataFrame
488497
489498
Returns:
490499
Timeseries of revenue detail for a given protocol and chain
491500
"""
492-
check_required_parameters([[protocol, "protocol"], [daily, "daily"]])
501+
check_required_parameter(daily, "daily")
493502
params = {
494-
"protocol": protocol,
495503
"daily": str(daily).lower(),
496504
}
505+
506+
check_at_least_one_set_parameters([[protocol, "protocol"], [chain, "chain"]])
507+
if protocol is not None:
508+
params["protocol"] = protocol
509+
497510
if chain is not None:
498511
params["chain"] = chain
499512

datamaxi/error.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ def __init__(self, params):
2828

2929
def __str__(self):
3030
return "%s is mandatory, but received empty." % (", ".join(self.params))
31+
32+
33+
class AtLeastOneParameterRequiredError(Error):
34+
def __str__(self):
35+
return "At least one parameter is required."

datamaxi/lib/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pandas as pd
44
from functools import wraps
55
from datamaxi.error import ParameterRequiredError
6+
from datamaxi.error import AtLeastOneParameterRequiredError
67

78

89
def to_float(x):
@@ -38,6 +39,20 @@ def check_required_parameters(params):
3839
check_required_parameter(p[0], p[1])
3940

4041

42+
def check_at_least_one_set_parameters(params):
43+
at_least_one_set = False
44+
for p in params:
45+
try:
46+
check_required_parameter(p[0], p[1])
47+
at_least_one_set = True
48+
break
49+
except: # noqa: E722
50+
pass
51+
52+
if not at_least_one_set:
53+
raise AtLeastOneParameterRequiredError()
54+
55+
4156
def check_required_parameter_list(values: List, name: str):
4257
if len(values) == 0:
4358
raise ParameterRequiredError([name])

0 commit comments

Comments
 (0)