Skip to content

Commit

Permalink
Merge pull request #24 from trungtt198x/iota
Browse files Browse the repository at this point in the history
Fix TVL from GeckoTerminal
  • Loading branch information
trungtt198x authored Sep 10, 2024
2 parents 7f3d7dd + ed77f88 commit 7754f43
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
58 changes: 53 additions & 5 deletions helpers/smr_market_data/smd_geckoterminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async def get_geckoterminal_data():
geckoterminal_url = f"https://api.geckoterminal.com/api/v2/networks/{geckoterminal_ticker}/pools"
headers = {"accept": "application/json"}
total_defi_volume_usd_h24 = 0
total_reserve_in_usd = 0
total_defi_tx_24h = 0
page = 1

Expand All @@ -45,9 +44,7 @@ async def get_geckoterminal_data():

for entry in defi_volume_data:
h24_volume = float(entry["attributes"]["volume_usd"]["h24"])
reserve_in_usd = float(entry["attributes"]["reserve_in_usd"])
total_defi_volume_usd_h24 += h24_volume
total_reserve_in_usd += reserve_in_usd

# Extract transactions data for h24
transactions_h24 = entry["attributes"]["transactions"]["h24"]
Expand All @@ -59,8 +56,8 @@ async def get_geckoterminal_data():
logger.debug("Total USD 24h Volume for all pools: %s", total_defi_volume_usd_h24)
logger.debug("Total 24h Defi Transactions for IOTA EVM: %s", total_defi_tx_24h)

if total_defi_volume_usd_h24 > 0 and total_defi_tx_24h > 0 and total_reserve_in_usd > 0:
return {"defi_total_volume": total_defi_volume_usd_h24, "total_defi_tx_24h": total_defi_tx_24h, "total_reserve_in_usd": total_reserve_in_usd}
if total_defi_volume_usd_h24 > 0 and total_defi_tx_24h > 0:
return {"defi_total_volume": total_defi_volume_usd_h24, "total_defi_tx_24h": total_defi_tx_24h}
else:
logger.debug("IOTA Total Volume or Total Transactions not found in the response.")
elif defi_volume.status_code == 404:
Expand All @@ -78,3 +75,54 @@ async def get_geckoterminal_data():
logger.error("Request Exception occurred: %s", err)
except Exception as e:
logger.error("An unexpected error occurred: %s", e)

async def get_geckoterminal_data_tvl():
"""
Get GeckoTerminal Defi Volume data for IOTA EVM.
Returns:
dict: Dictionary containing IOTA EVM's total 24h volume and total 24h transactions.
"""
logger.info("Getting GeckoTerminal Defi Volume data for IOTA EVM")
geckoterminal_url = f"https://api.geckoterminal.com/api/v2/networks/{geckoterminal_ticker}/pools"
headers = {"accept": "application/json"}
total_reserve_in_usd = 0
page = 1

try:
while True:
# Make a request to the GeckoTerminal API with the current page number
defi_volume = requests.get(geckoterminal_url + f"?page={page}", headers=headers, timeout=10)
defi_volume.raise_for_status()

if defi_volume.status_code == 200:
defi_volume_json = defi_volume.json()
# Extract and parse the JSON response
defi_volume_data = defi_volume_json.get("data", [])

if len(defi_volume_data.length) == 0:
logger.info("Pagination ends at page: %d", page)
break

for entry in defi_volume_data:
reserve_in_usd = float(entry["attributes"]["reserve_in_usd"])
total_reserve_in_usd += reserve_in_usd

elif defi_volume.status_code == 404:
logger.info("Pagination ends at page: %d", page)
break
else:
logger.error("Unexpected status code: %s", defi_volume.status_code)
break

page += 1
return total_reserve_in_usd

except requests.exceptions.Timeout:
logger.error("GeckoTerminal API request timed out.")
except requests.exceptions.HTTPError as errh:
logger.error("HTTP Error occurred: %s", errh)
except requests.exceptions.RequestException as err:
logger.error("Request Exception occurred: %s", err)
except Exception as e:
logger.error("An unexpected error occurred: %s", e)
5 changes: 3 additions & 2 deletions helpers/smr_market_data_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from helpers.smr_market_data.smd_coingecko import get_coingecko_exchange_data
from helpers.smr_market_data.smd_coingecko import get_coingecko_24h_trading_volume
from helpers.smr_market_data.smd_shimmer import get_shimmer_data
from helpers.smr_market_data.smd_geckoterminal import get_geckoterminal_data
from helpers.smr_market_data.smd_geckoterminal import get_geckoterminal_data, get_geckoterminal_data_tvl
from helpers.smr_market_data.smd_defillama import get_defillama_data


Expand Down Expand Up @@ -355,10 +355,11 @@ async def build_embed():
coingecko_24h_vol = await get_coingecko_24h_trading_volume()
defillama_data = await get_defillama_data()
geckoterminal_data = await get_geckoterminal_data()
geckoterminal_tvl = await get_geckoterminal_data_tvl()
shimmer_data = await get_shimmer_data()
total_defi_tx_24h = geckoterminal_data["total_defi_tx_24h"]
defi_total_volume = geckoterminal_data['defi_total_volume']
geckoterminal_tvl = geckoterminal_data['total_reserve_in_usd']

iota_rank = defillama_data["iota_rank"]
discord_timestamp = await generate_discord_timestamp()

Expand Down

0 comments on commit 7754f43

Please sign in to comment.