-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbad-debt-trigger.py
42 lines (37 loc) · 1.74 KB
/
bad-debt-trigger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os, requests, datetime, locale
import asyncio, telegram
url = os.environ["DATA_URL"]
response = requests.get(url)
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
if response.status_code == 200:
data = response.json()
total_bad_debt = int(data["total"]) * -1
decimals = int(data["decimals"])
updated = int(data["updated"])
tvl = int(data["tvl"])
deposits = int(data["deposits"])
borrows = int(data["borrows"])
# ratio of bad debt to total value locked
ratio_of_bad_debt = round(total_bad_debt / tvl * 100, 2)
date = datetime.datetime.fromtimestamp(updated)
debt = locale.currency(total_bad_debt / 10**decimals, grouping=True)
tvl = locale.currency(tvl / 10**decimals, grouping=True)
deposits = locale.currency(deposits / 10**decimals, grouping=True)
borrows = locale.currency(borrows / 10**decimals, grouping=True)
threshold = int(os.getenv("DEBT_THRESHOLD", 0))
threshold_ratio = float(os.getenv("DEBT_THRESHOLD_RATIO", 100))
protocol = os.getenv("PROTOCOL", "")
message = f"⚠️ {protocol} Bad Debt ratio: {ratio_of_bad_debt}% at {date} ⚠️\nDebt: {debt}\nTVL: {tvl}\nDeposits: {deposits}\nBorrows: {borrows}"
print(message)
if (
threshold > 0 and total_bad_debt / 10**decimals > threshold
) or ratio_of_bad_debt > threshold_ratio:
bot = telegram.Bot(token=os.environ["TELEGRAM_TOKEN"])
chat_ids = os.environ["TELEGRAM_CHAT_ID"].split(",")
print(f"Sending Telegram message")
for chat_id in chat_ids:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(bot.send_message(chat_id=chat_id, text=message))
else:
print(f"Request failed with status code {response.status_code}")