-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
329 lines (259 loc) · 10.1 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import json
import time
import os
from datetime import datetime
from dotenv import load_dotenv
import dateutil.parser
import requests
load_dotenv()
# Configure Sentry
try:
SENTRY_URL = os.environ["SENTRY_DSN"]
print("Configuring Sentry with DSN {}...".format(SENTRY_URL))
import sentry_sdk
sentry_sdk.init(SENTRY_URL)
except KeyError as e:
print("SENTRY_DSN not found in environment, not configuring Sentry")
pass # Ignore sentry initilization errors
# Configure Discord
try:
DISCORD_WEBHOOK = os.environ["DISCORD_WEBHOOK"]
if len(DISCORD_WEBHOOK) < 5:
raise KeyError
except KeyError as e:
raise Exception("DISCORD_WEBHOOK envar not found! You must set a DISCORD_WEBHOOK for things to work properly.")
CONTRACT = 'mainnet/KT1WZ1HJyx5wPt96ZTjtWPotoPUk7pXNPfT2'
# CONTRACT = 'florencenet/KT1E3aVbNwX5AwpSQ151dp3Qg4Wf9mGEs3ex'
KNOWN_CONTRACTS = {
'KT1E3aVbNwX5AwpSQ151dp3Qg4Wf9mGEs3ex': "The Kolibri DAO"
}
vote_map = {
0: "YAY",
1: "NAY",
2: "ABSTAIN"
}
def send_discord(payload):
print("Submitting webhook...")
response = requests.post(DISCORD_WEBHOOK, json=payload)
response.raise_for_status()
if int(response.headers['x-ratelimit-remaining']) == 0:
rate_limit_reset = float(response.headers['x-ratelimit-reset-after']) + 1
print("Waiting for discord rate limits...({} sec)".format(rate_limit_reset))
time.sleep(rate_limit_reset)
time.sleep(1)
return response
def fetch_all_history():
operations = []
last_id = None
while True:
print("Looking back at id {}".format(last_id))
params = {
'status': 'applied',
'entrypoints': 'vote,propose,executeTimelock,endVoting,cancelTimelock',
# 'with_storage_diff': True # TODO: Parse storage diff to see `endVoting` outcome?
}
if last_id is not None:
params['last_id'] = last_id
response = requests.get(
'https://api.better-call.dev/v1/contract/{}/operations'.format(CONTRACT),
params=params
)
applied_ops = response.json()
operations += applied_ops['operations']
if 'last_id' not in applied_ops:
break
last_id = applied_ops['last_id']
return operations
def fetch_contract_activity(since=None):
params = {
'status': 'applied',
'entrypoints': 'vote,propose,executeTimelock,endVoting,cancelTimelock'
}
if since is not None:
params['from'] = since
response = requests.get(
'https://api.better-call.dev/v1/contract/{}/operations'.format(CONTRACT),
params=params
)
applied_ops = response.json()
return applied_ops
def parse_operations_to_map(operations):
op_map = {}
for op in operations[::-1]:
if op['counter'] not in op_map:
op_map[op['counter']] = [op]
else:
op_map[op['counter']].append(op)
return op_map
def shorten_address(address):
return address[:5] + "..." + address[-5:]
def handle_vote_operation(operations):
vote_call = find_op(operations, 'vote')
vote_value = int(vote_call['parameters'][0]['value'])
voter = vote_call['source']
vote_callback_call = find_op(operations, 'voteCallback')
vote_amount = "{:,.2f}".format(
int(vote_callback_call['parameters'][0]['children'][2]['value']) / (10 ** 18)
)
voter_link = '**[{}](<https://tzkt.io/{}>)**'.format(
shorten_address(voter),
voter
)
if vote_value == 0:
formatted_vote = '<:blobyes:883220230896242718> **YAY**'
elif vote_value == 1:
formatted_vote = '<:blobno:883220231101763604> **NAY**'
else:
formatted_vote = '<:shrug:812037587908034590> **ABSTAIN**'
tx_link = 'https://better-call.dev/mainnet/opg/{}'.format(vote_call['hash'])
payload = {
"content": ":ballot_box: {} voted {} with **{} kDAO** | **[TX](<{}>)**".format(
voter_link,
formatted_vote,
vote_amount,
tx_link
)
}
send_discord(payload)
def handle_propose_operation(operations):
propose_call = find_op(operations, 'propose')
proposer_address = propose_call['source']
proposal_title = propose_call['parameters'][0]['children'][0]['value']
proposal_description_link = propose_call['parameters'][0]['children'][1]['value']
proposal_lambda = propose_call['parameters'][0]['children'][3]['value']
address_link = '**[{}](https://tzkt.io/{})**'.format(
shorten_address(proposer_address),
proposer_address
)
payload = {
"content": ":office_worker: :scales: {} submitted a new proposal to the DAO! **[Link](https://governance.kolibri.finance)**".format(address_link),
"embeds": [
{
"color": 4111763,
"fields": [
{
"name": "Title",
"value": proposal_title
},
{
"name": "Description",
"value": "[Link To Description]({})".format(proposal_description_link)
},
{
"name": "Lambda",
"value": "```{}```".format(
(proposal_lambda[:1024 - 9] + '...') if len(proposal_lambda) > (1024 - 9) else proposal_lambda
)
}
],
"thumbnail": {
"url": "https://services.tzkt.io/v1/avatars/{}".format(proposer_address)
}
}
]
}
send_discord(payload)
def handle_execute_timelock_operation(operations):
non_execute_operations = []
for operation in operations:
if operation['entrypoint'] != 'executeTimelock':
non_execute_operations.append(operation)
formatted_operations = []
for operation in non_execute_operations:
formatted_operations.append(
"Called `%{}` on [{}](<https://better-call.dev/mainnet/{}/operations>)".format(
operation['entrypoint'],
KNOWN_CONTRACTS.get(operation['destination'], operation['destination']),
operation['destination']
)
)
execute_call = find_op(operations, 'executeTimelock')
executor = execute_call['source']
payload = {
"content": ':timer_clock: **[{}](https://tzkt.io/{})** Executed the proposal in the Kolibri DAO timelock!'.format(
shorten_address(executor),
executor
),
"embeds": [{
"title": "Executed Operations",
"description": '\n'.join(formatted_operations),
"color": 4111763
}]
}
send_discord(payload)
def handle_end_voting_operation(operations):
end_voting_call = find_op(operations, 'endVoting')
ender = end_voting_call['source']
# transfer_call = find_op(operations, 'transfer')
address_link = '**[{}](<https://tzkt.io/{}>)**'.format(
shorten_address(ender),
ender
)
payload = {
'content': ':lock: {} Closed voting (if things passed, they moved to the timelock and escrow was returned) **[TX](<https://better-call.dev/mainnet/opg/{}>)**'.format(
address_link,
end_voting_call['hash']
)
}
send_discord(payload)
def handle_new_operations(operations):
op_map = parse_operations_to_map(operations)
for op_id, operations in op_map.items():
entrypoints = set([x['entrypoint'] for x in operations])
if 'vote' in entrypoints and 'voteCallback' in entrypoints:
handle_vote_operation(operations)
elif 'propose' in entrypoints and 'transfer' in entrypoints:
handle_propose_operation(operations)
elif 'executeTimelock' in entrypoints:
handle_execute_timelock_operation(operations)
elif 'endVoting' in entrypoints:
handle_end_voting_operation(operations)
else:
print("Unknown operation", entrypoints)
def latest_timestamp_from_operations(operations):
initial_event_timestamp = operations[0]['timestamp']
initial_event_datetime = dateutil.parser.parse(initial_event_timestamp)
latest_event_timestamp = int(initial_event_datetime.timestamp()) * 1000
return latest_event_timestamp
def find_op(ops, entrypoint):
return next(op for op in ops if op['entrypoint'] == entrypoint)
def watch_for_changes():
if os.path.exists('.shared/previous-state.json'):
print("Found .shared/previous-state.json, bootstrapping from that!")
with open('.shared/previous-state.json') as f:
previous_state = json.loads(f.read())
print("Previous state - {}".format(previous_state))
latest_event_timestamp = previous_state['latest-timestamp']
else:
print("No previous state found! Starting fresh...")
bcd_payload = fetch_contract_activity()
latest_event_timestamp = latest_timestamp_from_operations(bcd_payload['operations'])
while True:
search_timestamp = latest_event_timestamp + 1000 # Add a second to only look at future events
try:
new_activity = fetch_contract_activity(since=search_timestamp)
except Exception as e:
sentry_sdk.capture_exception(e)
print("Exception processing contract activity! Sleeping for a bit and trying again...")
time.sleep(10)
continue
new_operations = new_activity['operations']
if len(new_operations) != 0:
handle_new_operations(new_operations)
latest_event_timestamp = latest_timestamp_from_operations(new_operations)
if not os.path.exists('.shared/previous-state.json'):
if not os.path.exists('.shared'):
os.makedirs('.shared')
with open('.shared/previous-state.json', 'w') as f:
payload = json.dumps({
"latest-timestamp": latest_event_timestamp,
"updated": str(datetime.now())
})
f.write(payload)
else:
print("[{}] No new activity, looping...".format(datetime.now()))
time.sleep(30)
if __name__ == "__main__":
watch_for_changes()
# all_operations = fetch_all_history()
# handle_new_operations(all_operations)