Skip to content

Commit

Permalink
Merge TVL functions (#18)
Browse files Browse the repository at this point in the history
* feat: merge tvl functions

* fix: remove unused imports
  • Loading branch information
martinkersner authored May 20, 2024
1 parent 6dfd7f2 commit c7e0ac2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 151 deletions.
154 changes: 34 additions & 120 deletions datamaxi/defillama/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from datamaxi.api import API
from datamaxi.lib.utils import check_required_parameter
from datamaxi.lib.utils import check_required_parameters
from datamaxi.lib.utils import check_required_parameter_list
from datamaxi.lib.utils import check_at_least_one_set_parameters
from datamaxi.lib.utils import encode_string_list
from datamaxi.lib.utils import make_list
from datamaxi.lib.utils import postprocess
from datamaxi.lib.constants import BASE_URL

Expand Down Expand Up @@ -91,165 +88,82 @@ def stablecoins(self) -> List[str]:
return self.query(url_path)

@postprocess()
def tvl(self, pandas: bool = True) -> Union[List, pd.DataFrame]:
def tvl(
self, protocol: str = None, chain: str = None, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get total TVL across all chains and protocols
`GET /v1/defillama/tvl`
<https://docs.datamaxiplus.com/defillama/tvl>
Args:
protocol (str): Protocol name
chain (str): Chain name
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of total TVL
"""
url_path = "/v1/defillama/tvl"
return self.query(url_path)

@postprocess()
def protocol_tvl(
self, protocols: Union[str, List[str]] = None, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get TVL for given protocols
`GET /v1/defillama/tvl`
<https://docs.datamaxiplus.com/defillama/tvl>
Args:
protocols (Union[str, List[str]]): single protocol or multiple protocol names
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of protocol TVLs
"""
protocols = make_list(protocols)
check_required_parameter_list(protocols, "protocols")
params = {"protocols": encode_string_list(protocols)}
return self.query("/v1/defillama/tvl", params)

@postprocess()
def chain_tvl(
self, chains: Union[str, List[str]] = None, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get TVL for given chains
`GET /v1/defillama/tvl`
<https://docs.datamaxiplus.com/defillama/tvl>
Args:
chains (Union[str, List[str]]): single chain or multiple chain names
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of chain TVLs
"""
chains = make_list(chains)
check_required_parameter_list(chains, "chains")
params = {"chains": encode_string_list(chains)}
return self.query("/v1/defillama/tvl", params)

@postprocess()
def protocol_chain_tvl(
self, protocol: str, chain: str, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get TVL for given protocol and chain
`GET /v1/defillama/tvl`
<https://docs.datamaxiplus.com/defillama/tvl>
params = {}
if protocol is not None:
params["protocol"] = protocol

Args:
protocol (str): protocol name
chain (str): chain name
pandas (bool): Return data as pandas DataFrame
if chain is not None:
params["chain"] = chain

Returns:
Timeseries of protocol TVL on a given chain
"""
check_required_parameters([[protocol, "protocol"], [chain, "chain"]])
params = {"chain": chain, "protocol": protocol}
return self.query("/v1/defillama/tvl", params)
url_path = "/v1/defillama/tvl"
return self.query(url_path, params)

@postprocess(num_index=2)
def protocol_token_tvl(
self, protocol: str, usd: bool = True, pandas: bool = True
def tvl_detail(
self, protocol: str, chain: str = None, token: bool = False, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get token TVL on a given protocol
"""Get TVL detail for given protocol and chain
`GET /v1/defillama/tvl`
`GET /v1/defillama/tvl/detail`
<https://docs.datamaxiplus.com/defillama/tvl>
<https://docs.datamaxiplus.com/defillama/tvl-detail>
Args:
protocol (str): protocol name
usd (bool): Convert to USD otherwise return token amount
protocol (str): Protocol name
chain (str): Chain name
token (bool): Return token amount (return by default USD)
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of token TVL for a given protocol
Timeseries of TVL detail for a given protocol and chain
"""
check_required_parameters([[protocol, "protocol"], [usd, "usd"]])
params = {
"protocol": protocol,
"token": "true",
"usd": str(usd).lower(),
}
return self.query("/v1/defillama/tvl", params)
check_required_parameter(protocol, "protocol")
params = {"protocol": protocol}

@postprocess(num_index=2)
def protocol_chain_token_tvl(
self, protocol: str, chain: str, usd: bool = True, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get token TVL on a given protocol and chain
`GET /v1/defillama/tvl`
if chain is not None:
params["chain"] = chain

<https://docs.datamaxiplus.com/defillama/tvl>
if token:
params["token"] = str(token).lower()

Args:
protocol (str): protocol name
chain (str): chain name
usd (bool): Convert to USD otherwise return token amount
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of token TVL for a given protocol and chain
"""
check_required_parameters(
[[protocol, "protocol"], [chain, "chain"], [usd, "usd"]]
)
params = {
"protocol": protocol,
"chain": chain,
"token": "true",
"usd": str(usd).lower(),
}
return self.query("/v1/defillama/tvl", params)
url_path = "/v1/defillama/tvl/detail"
return self.query(url_path, params)

@postprocess()
def protocol_mcap(
self, protocols: Union[str, List[str]] = None, pandas: bool = True
) -> Union[List, pd.DataFrame]:
"""Get market cap for given protocols
def mcap(self, protocol: str, pandas: bool = True) -> Union[List, pd.DataFrame]:
"""Get market cap for given protocol
`GET /v1/defillama/mcap`
<https://docs.datamaxiplus.com/defillama/mcap>
Args:
protocols (Union[str, List[str]]): single protocol or multiple protocol names
protocols (str): Protocol name
pandas (bool): Return data as pandas DataFrame
Returns:
Timeseries of market cap for given protocols
"""
protocols = make_list(protocols)
check_required_parameter_list(protocols, "protocols")
check_required_parameter(protocol, "protocol")
params = {
"protocols": encode_string_list(protocols),
"protocol": protocol,
}
return self.query("/v1/defillama/mcap", params)

Expand Down
31 changes: 0 additions & 31 deletions tests/defillama/test_defillama_protocol_tvl.py

This file was deleted.

0 comments on commit c7e0ac2

Please sign in to comment.