-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
483 lines (441 loc) · 16 KB
/
index.js
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
require('dotenv').config()
const axios = require('axios')
const { exception } = require('console');
const { knownExtended } = require('tar');
var knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./defi-gov-watch_db.sqlite"
}
});
const subgrapBaseUrl = 'https://api.thegraph.com/subgraphs/name'
const subgraphs = { 'uniswap': '/ianlapham/uniswapv2', 'curve': '/aragon/aragon-mainnet' }
const hubBaseUrl = 'https://hub.snapshot.page/api'
const hubs = { 'spaces': '/spaces', 'proposals': '/proposals', 'proposal': '/proposal/' }
const tgBaseUrl = `https://api.telegram.org/bot${process.env.TG_BOT_API_KEY}`
const tgActions = { 'send': '/sendMessage', 'update': '/getUpdates' }
const queryInterval = 60 * 5 //seconds
//#region classes
class Space {
constructor(id, name, network, symbol, createdAt = Date.now(), isWatched = false) {
this.id = id;
this.name = name;
this.network = network;
this.symbol = symbol;
this.createdAt = createdAt;
this.isWatched = isWatched
}
}
class Proposal {
constructor(id, space, address, start, end, name, body, createdAt = Date.now(), isWatched = false) {
this.id = id
this.space = space
this.address = address
this.start = start
this.end = end
this.name = name
this.body = body
this.createdAt = createdAt
this.isWatched = isWatched
}
}
class ProposalChoice {
constructor(id, proposal, name, order, createdAt = Date.now()) {
this.id = id
this.proposal = proposal
this.name = name
this.order = order
this.createdAt = createdAt
}
}
class VoteCount {
constructor(id, proposalChoice, count, createdAt = Date.now(), tempChoice = null) {
this.id = id
this.proposalChoice = proposalChoice
this.count = count
this.createdAt = createdAt
this.tempChoice = tempChoice
}
}
class Alert {
constructor(title, body) {
this.title = title
this.body = body
}
}
//#endregion
//#region db init
knex.schema.hasTable('networks').then(function (exists) {
if (!exists) {
const res = knex.schema.createTable('networks', function (t) {
t.int('id').primary();
t.int('name');
t.dateTime('createdAt');
});
knex('networks').insert(
{ id: 1, name: 'Ethereum mainnet' },
{ id: 56, name: 'Binance smart chain' },
{ id: 4, name: 'Ethereum Rinkeby' },
{ id: 100, name: 'xDai' },
{ id: 42, name: 'Ethereum Kovan' },
{ id: 7, name: 'ThaiChain' },
{ id: 61, name: 'Ethereum Classic mainnet' },
{ id: 97, name: 'Binance smart chain testnet' },
{ id: 137, name: 'Matic Mainnet' },
{ id: 420, name: 'Optimistic Ethereum' },
{ id: 32659, name: 'Fusion mainnet' },
{ id: 80001, name: 'Matic Mumbai' },
)
return res;
}
});
knex.schema.hasTable('spaces').then(function (exists) {
if (!exists) {
return knex.schema.createTable('spaces', function (t) {
t.string('id').primary();
t.string('name');
t.int('network').references('networks.id');
t.string('symbol');
t.dateTime('createdAt');
t.boolean('isWatched');
});
}
});
knex.schema.hasTable('proposals').then(function (exists) {
if (!exists) {
return knex.schema.createTable('proposals', function (t) {
t.string('id').primary();
t.int('space').references('spaces.id')
t.string('address');
t.dateTime('start');
t.dateTime('end');
t.string('name')
t.text('body')
t.dateTime('createdAt');
t.boolean('isWatched');
});
}
});
knex.schema.hasTable('proposalChoices').then(function (exists) {
if (!exists) {
return knex.schema.createTable('proposalChoices', function (t) {
t.increments('id').primary();
t.int('proposal').references('proposals.id');
t.string('name');
t.int('order');
t.dateTime('createdAt');
});
}
});
knex.schema.hasTable('voteCounts').then(function (exists) {
if (!exists) {
return knex.schema.createTable('voteCounts', function (t) {
t.increments('id').primary();
t.int('proposalChoice').references('proposalChoices.id')
t.int('count');
t.dateTime('createdAt');
});
}
});
//#endregion
//#region db accessors
const getSnapshotSpaces = async () => {
const res = knex('spaces').select()
if (res.length > 0)
return res.map(x => new Space(x.id, x.name, x.network, x.symbol, x.createdAt, x.isWatched))
return res
}
const addSnapshotSpace = async (pSpace) => {
const res = knex('spaces').insert({
id: pSpace.id,
name: pSpace.name,
network: pSpace.network,
symbol: pSpace.symbol,
createdAt: pSpace.createdAt,
isWatched: pSpace.isWatched
})
return res
}
const delSnapshotSpace = async (pSpace) => {
const res = knex('spaces').where('id', pSpace.id).del()
return res
}
const getWatchedSpaces = async () => {
const res = knex('spaces').where('isWatched', true).select()
if (res.length > 0)
return res.map(x => new Space(x.id, x.name, x.network, x.symbol, x.createdAt, x.isWatched))
return res
}
const getSnapshotProposals = async (pSpace = null) => {
const res = pSpace ? knex('proposals').where('space', pSpace.id).select() : knex('proposals').select()
if (res.length > 0)
return res.map(x => new Proposal(x.id, x.space, x.address, x.start, x.end, x.name, x.body, x.createdAt, x.isWatched))
return res
}
const addSnapshotProposal = async (pProposal) => {
const res = knex('proposals').insert({
id: pProposal.id,
space: pProposal.space,
address: pProposal.address,
start: pProposal.start,
end: pProposal.end,
name: pProposal.name,
body: pProposal.body,
createdAt: pProposal.createdAt,
isWatched: pProposal.isWatched
})
return res
}
const delSnapshotProposal = async (pProposal) => {
const res = knex('proposals').where('id', pProposal.id).del()
return res
}
const getWatchedProposals = async () => {
const res = knex('proposals').where('isWatched', true).select()
if (res.length > 0)
return res.map(x => new Proposal(x.id, x.space, x.address, x.start, x.end, x.name, x.body, x.createdAt, x.isWatched))
return res
}
const getSnapshotProposalChoices = async (pProposal = null) => {
const res = pProposal ? knex('proposalChoices').where('proposal', pProposal.id).select() : knex('proposalChoices').select()
if (res.length > 0)
return res.map(x => new ProposalChoice(x.id, x.proposal, x.name, x.order, x.createdAt))
return res
}
const addSnapshotProposalChoice = async (pProposalChoice) => {
const res = knex('proposalChoices').insert({
proposal: pProposalChoice.proposal,
name: pProposalChoice.name,
order: pProposalChoice.order,
createdAt: pProposalChoice.createdAt,
})
return res
}
const delSnapshotProposalChoice = async (pProposalChoice) => {
const res = knex('proposalChoices').where('id', pProposalChoice.id).del()
return res
}
const getVoteCount = async (pProposalChoice = null, pIslastOnly = false) => {
try {
let res = []
if (pProposalChoice && pIslastOnly) {
res = await knex('voteCounts').where('proposalChoice', pProposalChoice.id).orderBy('createdAt', 'desc').limit(1).select()
} else if (pProposalChoice) {
res = await knex('voteCounts').where('proposalChoice', pProposalChoice.id).select()
} else if (pIslastOnly) {
res = await knex('voteCounts').orderBy('createdAt', 'desc').limit(1).select()
} else {
res = await knex('voteCounts').select()
}
if (res.length > 0)
return res.map(x => new VoteCount(x.id, x.proposalChoice, x.count, new Date(x.createdAt)))
return res
} catch (e) {
throw new exception("Error getting VoteCount from db", e)
}
}
const addVoteCount = async (pVoteCount) => {
const res = knex('voteCounts').insert({
proposalChoice: pVoteCount.proposalChoice,
count: pVoteCount.count,
createdAt: pVoteCount.createdAt,
})
return res
}
const delVoteCount = async (pVoteCount) => {
const res = knex('voteCounts').where('id', pVoteCount.id).del()
return res
}
//#endregion
//#region Remote fetches
const fetchSnashotSpaces = async () => {
try {
const res = await axios.get(hubBaseUrl + hubs['spaces'])
const spaces = []
for (const resItem in res.data) {
spaces.push(new Space(resItem,
res.data[resItem].name,
res.data[resItem].network,
res.data[resItem].symbol))
}
return spaces
}
catch (e) {
console.error("Error fetching Spaces", e)
}
}
const fetchSnapshotProposals = async (pSpace) => {
try {
const res = await axios.get(hubBaseUrl + '/' + pSpace.id + hubs['proposals'])
const proposals = []
for (const resItem in res.data) {
proposals.push(new Proposal(resItem,
pSpace.id,
res.data[resItem].address,
new Date(res.data[resItem].msg.payload.start * 1000),
new Date(res.data[resItem].msg.payload.end * 1000),
res.data[resItem].msg.payload.name,
res.data[resItem].msg.payload.body))
}
return proposals
}
catch (e) {
console.error("Error fetching Proposals", e)
}
}
const fetchSnashotProposalChoices = async (pProposal) => {
try {
const res = await axios.get(hubBaseUrl + '/' + pProposal.space + hubs['proposals'])
const proposalChoices = []
for (let item in res.data) {
if (item == pProposal.id) {
res.data[item].msg.payload.choices.forEach((x, i) => {
proposalChoices.push(new ProposalChoice(
null,
pProposal.id,
x,
i + 1,
))
})
}
}
return proposalChoices;
}
catch (e) {
console.error('Error Fetching Proposal Choices', e)
}
}
const fetchSnashotVoteCounts = async (pProposal) => {
try {
const res = await axios.get(hubBaseUrl + '/' + pProposal.space + hubs['proposal'] + pProposal.id)
const votes = []
const voteCounts = []
for (item in res.data) {
if (res.data[item].msg.type === 'vote') {
const vote = res.data[item].msg.payload.choice
votes[vote] = votes[vote] ? votes[vote] + 1 : 1
}
}
for (let vote in votes) {
if (vote != undefined) {
voteCounts.push(new VoteCount(null, null, votes[vote], Date.now(), vote))
}
}
return voteCounts;
}
catch (e) {
console.error('Error Fetching Proposal Choices', e)
}
}
//#endregion
//#region Alerts
const sendAlert = async (pAlert) => {
consoleAlert(pAlert);
tgAlert(pAlert);
}
const tgAlert = async (pAlert) => {
const res = await axios.post(tgBaseUrl + tgActions['send'], {
'chat_id': process.env.TG_CHAT_ID,
'text': pAlert.title + '\n\n' + pAlert.body
})
return res
}
const consoleAlert = async (pAlert) => {
console.log(pAlert.title, pAlert.body)
}
//#endregion
//#region Logic
const updateSnapshotSpaces = async () => {
// Fetch the spaces from remote
const spaces = await fetchSnashotSpaces()
// Get the existing Spaces from DB
const existingSpaces = await getSnapshotSpaces()
//Compare
const addedSpaces = spaces.filter(x => !existingSpaces.map(y => y.id).includes(x.id));
const removedSpaces = existingSpaces.filter(x => !spaces.map(y => y.id).includes(x.id));
//Alert and add new spaces
if (addedSpaces.length > 0) {
sendAlert(new Alert(`Found ${addedSpaces.length} recently added space(s)`, `${addedSpaces.map(x => x.name).join(', ')}`));
addedSpaces.forEach(async x => await addSnapshotSpace(x))
}
//Alert and remove old spaces
if (removedSpaces.length > 0) {
sendAlert(new Alert(`Found ${removedSpaces.length} recently removed space(s)`, `${removedSpaces.map(x => x.name).join(', ')}`));
removedSpaces.forEach(async x => await delSnapshotSpace(x))
}
}
const updateProposals = async (pSpace) => {
//Fetch the proposals from remote
const proposals = await fetchSnapshotProposals(pSpace)
//Get the existing Proposals from DB
const existingProposals = await getSnapshotProposals(pSpace)
//Compare
const addedProposals = proposals.filter(x => !existingProposals.map(y => y.id).includes(x.id));
const removedProposals = existingProposals.filter(x => !proposals.map(y => y.id).includes(x.id));
//Alert and add new proposals
if (addedProposals.length > 0) {
sendAlert(new Alert(`Found ${addedProposals.length} recently added proposal(s) for ${pSpace.name}`, `${addedProposals.map(x => x.name).join(',\n')}`));
for (let addedProposal of addedProposals) {
await addSnapshotProposal(addedProposal)
await updateProposalChoices(addedProposal)
}
}
//Alert and remove old proposals
if (removedProposals.length > 0) {
sendAlert(new Alert(`Found ${removedProposals.length} recently removed proposal(s) for ${pSpace.name}`, `${removedProposals.map(x => x.name).join(',\n')}`));
removedProposals.forEach(async x => await delSnapshotProposal(x))
}
}
const updateProposalChoices = async (pProposal) => {
//Fetch remote proposalChoices
const proposalChoices = await fetchSnashotProposalChoices(pProposal)
//Insert db
proposalChoices.forEach(async x => await addSnapshotProposalChoice(x))
}
const updateVoteCounts = async (pProposal) => {
//Fetch remote voteCounts
const newVoteCounts = await fetchSnashotVoteCounts(pProposal)
//Get proposalChoices from db
const proposalChoices = await getSnapshotProposalChoices(pProposal)
//Typically 1 voteCount per proposalChoice
for (let newVoteCount of newVoteCounts) {
//Match the voteCount to it's proposalChoice
const proposalChoice = proposalChoices.filter(x => x.order == newVoteCount.tempChoice)[0]
//If we don't have proposalChoices for the current proposal, update them and they will be voteCounted on the next run
if (!proposalChoice) {
// await updateProposalChoices(pProposal)
return
}
//Replace the tempChoice with the actual proposalChoice (snapshot uses the order of the choice as it's Id)
newVoteCount.proposalChoice = proposalChoice.id
//Look for the last votesCounts on this proposalChoice
const existingProposalChoiceVoteCounts = await getVoteCount(proposalChoice, true)
//If there is none, or the count is different, add ours
if (existingProposalChoiceVoteCounts.length < 1 || newVoteCount.count !== existingProposalChoiceVoteCounts[0].count) {
addVoteCount(newVoteCount)
}
}
};
//#endregion
(async () => {
try {
setInterval(async () => {
updateSnapshotSpaces();
const spaces = await getWatchedSpaces();
for (let space of spaces) {
updateProposals(space);
}
const watchedProposals = await getWatchedProposals();
for (let proposal of watchedProposals) {
updateVoteCounts(proposal);
}
//Proposals that just ended
//Proposals that end soon
//Proposals that just reached quorum
//Proposals that changed by % over threshold
//Proposals that ...
}, queryInterval * 1000)
} catch (e) {
console.error(e)
}
})();