From 2949a18ee7ce1590e38929504c3fb27fad17d94b Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Wed, 7 Jul 2021 17:58:29 +0200 Subject: [PATCH] Add first draft of websocket listener --- main.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..c3a058b --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +import asyncio +import websockets +import json +from datetime import datetime + +def present_transaction(trans): + """ + Print the time, recipient and amount of the transaction + """ + date = datetime.fromtimestamp(int(trans["time"][:-3])) + amount = float(trans["message"]["amount"]) / (10**30) + + print(str(date) + " " + trans["message"]["account"] + " " + str(amount)) + +async def listen(): + uri = "wss://ws.mynano.ninja" + async with websockets.connect(uri) as websocket: + # Listen to confirmed transactions from Binance's hot wallet + await websocket.send(json.dumps({ + "action":"subscribe", + "topic":"confirmation", + "ack":"false", + "accounts": [ + "nano_3jwrszth46rk1mu7rmb4rhm54us8yg1gw3ipodftqtikf5yqdyr7471nsg1k" + ] + })) + + while True: + msg = await websocket.recv() + present_transaction(json.loads(msg)) + + +asyncio.get_event_loop().run_until_complete(listen())