Skip to content

Commit 1e36307

Browse files
committed
feat: monitor balances lambda
1 parent 5f6d880 commit 1e36307

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

lambda/monitor-balances/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM public.ecr.aws/lambda/python:3.11
2+
3+
RUN pip install pragma-sdk
4+
5+
# Install additional dependencies
6+
RUN pip install \
7+
boto3 \
8+
requests \
9+
python-telegram-bot==20.8
10+
11+
# Copy function code
12+
COPY app.py ${LAMBDA_TASK_ROOT}
13+
14+
# Set the CMD to your handler
15+
CMD ["app.handler"]

lambda/monitor-balances/app.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import asyncio
2+
import json
3+
import os
4+
5+
import boto3
6+
import requests
7+
import telegram
8+
from pragma_sdk.onchain.client import PragmaOnChainClient
9+
from pragma_sdk.common.logging import get_pragma_sdk_logger
10+
from pragma_sdk.common.utils import felt_to_str
11+
12+
logger = get_pragma_sdk_logger()
13+
14+
# Inputs
15+
# [Optional]: Publisher names; if empty, query for all
16+
# [Optional]: Balance threshold; if empty, defaults to 100 * 10**18 Fri
17+
18+
# Behavior: Ping betteruptime iff all is good
19+
20+
SECRET_NAME = os.environ.get("SECRET_NAME")
21+
22+
23+
def handler(event, context):
24+
asyncio.run(_handler())
25+
return {"success": True}
26+
27+
28+
def _get_telegram_bot_oauth_token_from_aws():
29+
region_name = "eu-west-3"
30+
31+
# Create a Secrets Manager client
32+
session = boto3.session.Session()
33+
client = session.client(service_name="secretsmanager", region_name=region_name)
34+
get_secret_value_response = client.get_secret_value(SecretId=SECRET_NAME)
35+
return json.loads(get_secret_value_response["SecretString"])[
36+
"TELEGRAM_BOT_USER_OAUTH_TOKEN"
37+
]
38+
39+
40+
async def _handler():
41+
chat_id = os.environ.get("CHAT_ID")
42+
telegram_bot_token = os.environ.get("TELEGRAM_BOT_USER_OAUTH_TOKEN")
43+
if telegram_bot_token is None:
44+
telegram_bot_token = _get_telegram_bot_oauth_token_from_aws()
45+
network = os.environ.get("NETWORK")
46+
ignore_publishers_str = os.environ.get("IGNORE_PUBLISHERS", "")
47+
ignore_publishers = ignore_publishers_str.split(",")
48+
threshold_wei = int(os.environ.get("THRESHOLD_WEI", 100 * 10**18))
49+
bot = telegram.Bot(token=telegram_bot_token)
50+
client = PragmaOnChainClient(network)
51+
52+
publishers = [
53+
publisher
54+
for publisher in await client.get_all_publishers()
55+
if publisher not in ignore_publishers
56+
]
57+
58+
all_above_threshold = True
59+
60+
for publisher in publishers:
61+
address = await client.get_publisher_address(publisher)
62+
63+
# STRK token address for gas fees
64+
token_address = (
65+
0x04718F5A0FC34CC1AF16A1CDEE98FFB20C31F5CD61D6AB07201858F4287C938D
66+
)
67+
balance = await client.get_balance(address, token_address)
68+
69+
if balance < threshold_wei:
70+
error_message = f"Balance below threshold for publisher: {felt_to_str(publisher)}, address: {hex(address)}, balance in STRK: {balance/(10**18)}"
71+
logger.warning(error_message)
72+
all_above_threshold = False
73+
await bot.send_message(chat_id, text=error_message)
74+
75+
else:
76+
logger.info(
77+
f"Balance above threshold for publisher: {felt_to_str(publisher)}, address: {hex(address)}, balance in STRK: {balance/(10**18)}"
78+
)
79+
80+
if all_above_threshold:
81+
betteruptime_id = os.environ.get("BETTERUPTIME_ID")
82+
requests.get(f"https://betteruptime.com/api/v1/heartbeat/{betteruptime_id}")
83+
84+
85+
if __name__ == "__main__":
86+
handler(None, None)

0 commit comments

Comments
 (0)