-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_watch.py
More file actions
36 lines (32 loc) · 1.18 KB
/
Copy path06_watch.py
File metadata and controls
36 lines (32 loc) · 1.18 KB
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
# SPDX-License-Identifier: MIT
"""06 - Watch a transaction until it reaches N confirmations.
Polls the explorer's /tx/:txid/status every 30s.
confirmations = current tip height - the tx's block height + 1.
python 06_watch.py <txid> [confirmations]
"""
import sys
import time
import requests
from config import api
if len(sys.argv) < 2:
sys.exit("usage: python 06_watch.py <txid> [confirmations]")
txid = sys.argv[1]
want = int(sys.argv[2]) if len(sys.argv) > 2 else 1
while True:
r = requests.get(api(f"/tx/{txid}/status"), timeout=15)
if not r.ok:
# 404 = the network has not seen this txid yet (not broadcast, still
# propagating, or a typo). Keep waiting.
print(f"{txid[:16]}... not seen yet (HTTP {r.status_code})")
else:
status = r.json()
if status.get("confirmed"):
tip = int(requests.get(api("/blocks/tip/height"), timeout=15).text)
confs = tip - status["block_height"] + 1
print(f"{txid[:16]}... {confs} confirmation(s)")
if confs >= want:
print("done.")
break
else:
print(f"{txid[:16]}... unconfirmed (in the mempool)")
time.sleep(30)