Skip to content

Commit d990e33

Browse files
committed
feat: allow specify protocol and/or chain in feed/revenue detail
1 parent f980977 commit d990e33

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

datamaxi/defillama/__init__.py

Lines changed: 25 additions & 8 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
@@ -438,7 +439,11 @@ def revenue(
438439

439440
@postprocess(num_index=4)
440441
def fee_detail(
441-
self, protocol: str, chain: str = None, daily: bool = True, pandas: bool = True
442+
self,
443+
protocol: str = None,
444+
chain: str = None,
445+
daily: bool = True,
446+
pandas: bool = True,
442447
) -> Union[List, pd.DataFrame]:
443448
"""Get fee detail for given protocol and chain
444449
@@ -448,26 +453,34 @@ def fee_detail(
448453
449454
Args:
450455
protocol (str): Protocol name
451-
chain (str): Chain name (optional)
456+
chain (str): Chain name
452457
daily (bool): Daily fee or total fee
453458
pandas (bool): Return data as pandas DataFrame
454459
455460
Returns:
456461
Timeseries of fee detail for a given protocol and chain
457462
"""
458-
check_required_parameters([[protocol, "protocol"], [daily, "daily"]])
463+
check_required_parameter(daily, "daily")
459464
params = {
460-
"protocol": protocol,
461465
"daily": str(daily).lower(),
462466
}
467+
468+
check_at_least_one_set_parameters([[protocol, "protocol"], [chain, "chain"]])
469+
if protocol is not None:
470+
params["protocol"] = protocol
471+
463472
if chain is not None:
464473
params["chain"] = chain
465474

466475
return self.query("/v1/defillama/fee/detail", params)
467476

468477
@postprocess(num_index=4)
469478
def revenue_detail(
470-
self, protocol: str, chain: str = None, daily: bool = True, pandas: bool = True
479+
self,
480+
protocol: str = None,
481+
chain: str = None,
482+
daily: bool = True,
483+
pandas: bool = True,
471484
) -> Union[List, pd.DataFrame]:
472485
"""Get revenue detail for given protocol and chain
473486
@@ -477,18 +490,22 @@ def revenue_detail(
477490
478491
Args:
479492
protocol (str): Protocol name
480-
chain (str): Chain name (optional)
493+
chain (str): Chain name
481494
daily (bool): Daily revenue or total revenue
482495
pandas (bool): Return data as pandas DataFrame
483496
484497
Returns:
485498
Timeseries of revenue detail for a given protocol and chain
486499
"""
487-
check_required_parameters([[protocol, "protocol"], [daily, "daily"]])
500+
check_required_parameter(daily, "daily")
488501
params = {
489-
"protocol": protocol,
490502
"daily": str(daily).lower(),
491503
}
504+
505+
check_at_least_one_set_parameters([[protocol, "protocol"], [chain, "chain"]])
506+
if protocol is not None:
507+
params["protocol"] = protocol
508+
492509
if chain is not None:
493510
params["chain"] = chain
494511

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ def check_required_parameters(params):
3838
check_required_parameter(p[0], p[1])
3939

4040

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

0 commit comments

Comments
 (0)