-
Notifications
You must be signed in to change notification settings - Fork 15
/
check_tx.py
83 lines (65 loc) · 2.2 KB
/
check_tx.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
check_tx.py
A Demo script that takes a transaction id as input, and sends back a json with it's status
- unknown
- in mempool (shows timestamp, from, to, amount, first 50 chars of openfield)
- in ledger (shows timestamp, from, to, amount, first 50 chars of openfield as well as number of confirmations)
This script is supposed to be run in the node directory, it needs access to both ledger.db and mempool.db
"""
import sqlite3, sys, json
import options
# Default ledger path
ledger_path = "static/ledger.db"
# Default mempool path
mempool_path = "mempool.db"
def list_to_tx(result):
"""
Converts the query result into a dict
"""
result[4] = result[4][:50]
keys = ["Timestamp", "Address", "Recipient", "Amount", "Openfield", "Block"]
return dict(zip(keys,result))
def is_in_mempool(txid):
"""
If txid is in mempool, sends back details of the tx
"""
mempool = sqlite3.connect(mempool_path)
mempool.text_factory = str
m = mempool.cursor()
m.execute("SELECT timestamp, address, recipient, amount, openfield FROM transactions WHERE signature like ?;", (txid+"%",))
result = m.fetchone()
if result:
return (True, list_to_tx(list(result)))
else:
return (False, None)
def is_in_ledger(txid):
"""
If txid is in ledger, sends back details of the tx and number of confirmations
"""
ledger = sqlite3.connect(ledger_path)
ledger.text_factory = str
m = ledger.cursor()
m.execute("SELECT timestamp, address, recipient, amount, openfield, block_height FROM transactions WHERE signature like ?;", (txid+"%",))
result = m.fetchone()
if result:
m.execute("SELECT block_height FROM transactions ORDER BY block_height desc LIMIT 1")
last = m.fetchone()
return (True, list_to_tx(list(result)), last[0])
else:
return (False, None, None)
if __name__ == "__main__":
if len(sys.argv) != 2:
txid = input("No argument detected, please insert command manually\n")
else:
txid = sys.argv[1]
res = {"TxId":txid, "Status":"Unknown"}
isit, details = is_in_mempool(txid)
if isit:
res["Status"] = "Mempool"
res.update(details)
isit, details, lastblock = is_in_ledger(txid)
if isit:
res["Status"] = "Confirmed"
res.update(details)
res["Confirmations"] = lastblock - res["Block"]
print(json.dumps(res))