Skip to content

Commit

Permalink
Merge pull request #9 from itzmestar/yields
Browse files Browse the repository at this point in the history
Yields
  • Loading branch information
itzmestar authored Apr 11, 2024
2 parents 9121517 + a82a38c commit 41b54b1
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 5 deletions.
2 changes: 1 addition & 1 deletion defillama/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'DeFiLlama'
__description__ = 'Unofficial DeFi Llama API client.'
__url__ = 'https://github.com/itzmestar/DeFiLlama'
__version__ = '2.0.0'
__version__ = '2.1.0'
__build__ = 0x010001
__author__ = 'Tarique Anwer'
__author_email__ = 'itzmetariq@gmail.com'
Expand Down
107 changes: 103 additions & 4 deletions defillama/defillama.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

BASE_URL = "https://api.llama.fi"


# --------- Constants --------- #


Expand All @@ -23,7 +24,7 @@ def __init__(self):
"""
self.session = requests.Session()

def _send_message(self, method, endpoint, params=None, data=None):
def _send_message(self, method, endpoint, params=None, data=None, full_url=False):
"""
Send API request.
:param method: HTTP method (get, post, delete, etc.)
Expand All @@ -33,18 +34,20 @@ def _send_message(self, method, endpoint, params=None, data=None):
:return: dict/list: JSON response
"""
url = BASE_URL + endpoint
if full_url:
url = endpoint
response = self.session.request(method, url, params=params,
data=data, timeout=30)
json=data, timeout=30)
return response.json()

def _get(self, endpoint, params=None):
def _get(self, endpoint, params=None, full_url=False):
"""
Get API request
:param endpoint: Endpoint (to be added to base URL)
:param params: HTTP request parameters
:return:
"""
return self._send_message('GET', endpoint, params=params)
return self._send_message('GET', endpoint, params=params, full_url=full_url)

def get_all_protocols(self):
"""
Expand Down Expand Up @@ -113,3 +116,99 @@ def get_chains_current_tvl(self):
path = f'/v2/chains'

return self._get(path)

# ##### Yields EPs ###### #

def get_pools(self):
"""
Get the latest data for all pools
"""
path = 'https://yields.llama.fi/pools'

return self._get(path, full_url=True)

def get_pool(self, pool: str):
"""
Get the historical APY & TVL data for a pool
"""
path = f'https://yields.llama.fi/chart/{pool}'

return self._get(path, full_url=True)

# ##### Volumes EPs ###### #

def get_dexs(self, excludeTotalDataChart=True, excludeTotalDataChartBreakdown=True, dataType='dailyVolume'):
"""
list all dexs
"""
path = '/overview/dexs'
params = {
'excludeTotalDataChart': excludeTotalDataChart,
'excludeTotalDataChartBreakdown': excludeTotalDataChartBreakdown,
'dataType': dataType
}

return self._get(path, params=params)

def get_chain_dexs(self, chain, excludeTotalDataChart=True, excludeTotalDataChartBreakdown=True, dataType='dailyVolume'):
"""
list all dexs filter by chain
"""
path = f'/overview/dexs/{chain}'
params = {
'excludeTotalDataChart': excludeTotalDataChart,
'excludeTotalDataChartBreakdown': excludeTotalDataChartBreakdown,
'dataType': dataType
}

return self._get(path, params=params)

def get_dex_summary(self, protocol, excludeTotalDataChart=True, excludeTotalDataChartBreakdown=True, dataType='dailyVolume'):
"""
get summary of dex volume with historical data
"""
path = f'/summary/dexs/{protocol}'
params = {
'excludeTotalDataChart': excludeTotalDataChart,
'excludeTotalDataChartBreakdown': excludeTotalDataChartBreakdown,
'dataType': dataType
}

return self._get(path, params=params)

def get_options_dexs(self, excludeTotalDataChart=True, excludeTotalDataChartBreakdown=True, dataType='dailyVolume'):
"""
list all options dexs
"""
path = '/overview/options'
params = {
'excludeTotalDataChart': excludeTotalDataChart,
'excludeTotalDataChartBreakdown': excludeTotalDataChartBreakdown,
'dataType': dataType
}

return self._get(path, params=params)

def get_chain_options_dexs(self, chain, excludeTotalDataChart=True, excludeTotalDataChartBreakdown=True, dataType='dailyVolume'):
"""
list all options dexs
"""
path = f'/overview/options/{chain}'
params = {
'excludeTotalDataChart': excludeTotalDataChart,
'excludeTotalDataChartBreakdown': excludeTotalDataChartBreakdown,
'dataType': dataType
}

return self._get(path, params=params)

def get_options_dex_summary(self, protocol, dataType='dailyPremiumVolume'):
"""
get summary of option dex volume with historical data
"""
path = f'/summary/options/{protocol}'
params = {
'dataType': dataType
}

return self._get(path, params=params)
55 changes: 55 additions & 0 deletions tests/test_defillama.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,58 @@ def test_get_chains_current_tvl(self, llama):
assert 'chainId' in data
break

def test_get_pools(self, llama):
response = llama.get_pools()
assert type(response) is dict
assert 'status' in response
assert 'data' in response

def test_get_pool(self, llama):
response = llama.get_pool('747c1d2a-c668-4682-b9f9-296708a3dd90')
assert type(response) is dict
assert 'status' in response
assert 'data' in response

def test_get_dexs(self, llama):
response = llama.get_dexs()
assert type(response) is dict
assert 'totalDataChart' in response
assert 'totalDataChartBreakdown' in response
assert 'protocols' in response

def test_get_chain_dexs(self, llama):
response = llama.get_dexs('ethereum')
assert type(response) is dict
assert 'totalDataChart' in response
assert 'totalDataChartBreakdown' in response
assert 'protocols' in response

def test_get_dex_summary(self, llama):
response = llama.get_dex_summary('uniswap')
assert type(response) is dict
assert 'id' in response
assert 'name' in response
assert 'url' in response
assert 'totalDataChart' in response

def test_get_options_dexs(self, llama):
response = llama.get_options_dexs()
assert type(response) is dict
assert 'totalDataChart' in response
assert 'totalDataChartBreakdown' in response
assert 'protocols' in response

def test_get_chain_options_dexs(self, llama):
response = llama.get_chain_options_dexs('ethereum')
assert type(response) is dict
assert 'totalDataChart' in response
assert 'totalDataChartBreakdown' in response
assert 'protocols' in response

def test_get_options_dex_summary(self, llama):
response = llama.get_options_dex_summary('lyra')
assert type(response) is dict
assert 'id' in response
assert 'name' in response
assert 'url' in response
assert 'totalDataChart' in response

0 comments on commit 41b54b1

Please sign in to comment.