-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_retires.py
96 lines (90 loc) · 3.89 KB
/
index_retires.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
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
from utils import PollingProcess, events_to_process
logger = logging.getLogger(__name__)
def _index_retires(pg_conn, _client, _chain_num):
with pg_conn.cursor() as cur:
for event in events_to_process(
cur,
"retirements",
):
(
type,
block_height,
tx_idx,
msg_idx,
_,
_,
chain_num,
timestamp,
tx_hash,
) = event[0]
normalize = {}
normalize["type"] = type
normalize["block_height"] = block_height
normalize["tx_idx"] = tx_idx
normalize["msg_idx"] = msg_idx
normalize["chain_num"] = chain_num
normalize["timestamp"] = timestamp
normalize["tx_hash"] = tx_hash
normalize["batch_denoms"] = []
normalize["amount"] = 0
for entry in event:
(_, _, _, _, key, value, _, _, _) = entry
value = value.strip('"')
if "v1alpha1.EventRetire" in entry[0]:
if key == "amount":
normalize["amount"] = normalize["amount"] + float(value)
elif key == "batch_denom":
normalize["batch_denoms"].append(value)
normalize["batch_denom"] = value # TODO remove once app fully migrated
elif key == "location":
normalize["jurisdiction"] = value
elif key == "retirer":
normalize["owner"] = value
elif "v1.EventRetire" in entry[0]:
if key == "amount":
normalize["amount"] = normalize["amount"] + float(value)
elif key == "batch_denom":
normalize["batch_denoms"].append(value)
normalize["batch_denom"] = value # TODO remove once app fully migrated
elif key == "jurisdiction":
normalize["jurisdiction"] = value
elif key == "owner":
normalize["owner"] = value
elif key == "reason":
normalize["reason"] = value
with pg_conn.cursor() as _cur:
_cur.execute(
"""SELECT TRIM(BOTH '"' FROM (tx.data -> 'tx' -> 'body' -> 'memo')::text) AS memo FROM tx WHERE block_height=%s AND chain_num=%s AND tx_idx=%s""",
(block_height, chain_num, tx_idx),
)
(memo,) = _cur.fetchone()
if not normalize.get("reason") and memo:
normalize["reason"] = memo
retirement = (
normalize["type"],
normalize["amount"],
normalize["batch_denom"], # TODO remove once app fully migrated
normalize["batch_denoms"],
normalize["jurisdiction"],
normalize["owner"],
normalize.get("reason", ""),
normalize["block_height"],
normalize["chain_num"],
normalize["tx_idx"],
normalize["msg_idx"],
normalize["timestamp"],
normalize["tx_hash"],
)
_cur.execute(
"INSERT INTO retirements (type, amount, batch_denom, batch_denoms, jurisdiction, owner, reason, block_height, chain_num, tx_idx, msg_idx, timestamp, tx_hash) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
retirement,
)
pg_conn.commit()
logger.info("retirement inserted...")
def index_retires():
p = PollingProcess(
target=_index_retires,
sleep_secs=1,
)
p.start()