Skip to content

Instantly share code, notes, and snippets.

@gaburipeach
Created April 22, 2023 23:12
Show Gist options
  • Select an option

  • Save gaburipeach/0d2d14a2d50eb302c15129985813914f to your computer and use it in GitHub Desktop.

Select an option

Save gaburipeach/0d2d14a2d50eb302c15129985813914f to your computer and use it in GitHub Desktop.
watcher.py
import asyncio
import logging
import requests_async
import json
import pandas as pd
from scimitar.config import init_and_get_config
import numpy as np
import matplotlib.pyplot as plt
import time
import scimitar_utils.telegram
import scimitar.config.telegram
old = pd.DataFrame()
PAUSE = 25
address = "zzz"
token = "zzz"
async def loop():
global old
logging.info("Beginning: ")
counter = 0
while True:
try:
z = await requests_async.get(
"https://api.etherscan.io/api?module=account&action=tokentx&address={address}&page=1&startblock=0&endblock=27025780&sort=desc&apikey={token}".format(
address=address, token=token
)
)
except:
await scimitar_utils.telegram.send_message(
"Error scraping... timing out and resetting",
chat_id=scimitar.config.telegram.TEST_CHANNEL_ID,
)
df = pd.DataFrame(json.loads(z.content)["result"])
df["amount"] = df.value.astype("float") / df.tokenDecimal.astype("int64").apply(
lambda x: 10**x
)
df["date"] = pd.to_datetime(df.timeStamp, unit="s")
df["tx_id"] = df.hash.apply(lambda x: "https://etherscan.io/tx/{}".format(x))
try:
df = df.loc[~df["tx_id"].isin(old["tx_id"])]
except Exception as e:
old = df
logging.info("error")
logging.info(e)
continue
# For thing in df send alert
if counter > 0:
for _, item in df.iterrows():
message = """
==== Voyager Transfer ====
Time: {date} UTC
Token: {token}
Amount: {amt}
Txid: {txid}
""".format(
date=item.date,
token=item.tokenSymbol,
amt=item.amount,
txid=item.tx_id,
)
await scimitar_utils.telegram.send_message(
message,
chat_id=scimitar.config.telegram.SCIMITAR_WATCH_CHANNEL_ID,
)
old = pd.concat([old, df], ignore_index=True)
counter += 1
logging.info("sleeping...")
await asyncio.sleep(PAUSE)
if __name__ == "__main__":
init_and_get_config("aws", "production")
logging.info("starting...")
asyncio.run(loop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment