-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.ts
75 lines (64 loc) · 1.49 KB
/
start.ts
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
import {
attestedEventSignature,
getAndUpdateAllRelevantLogs,
provider,
registeredEventSignatureV1,
registeredEventSignatureV2,
revokedEventSignature,
revokedOffchainEventSignature,
timestampEventSignature,
} from "./utils";
import { startGraph } from "./graph";
import { ethers } from "ethers";
require("dotenv").config();
let running = false;
let timeout: NodeJS.Timeout | null = null;
const POLLING_INTERVAL = process.env.POLLING_INTERVAL
? Number(process.env.POLLING_INTERVAL)
: 60000;
const DISABLE_LISTENER = process.env.DISABLE_LISTENER;
export async function update() {
if (running) {
return;
}
try {
running = true;
await getAndUpdateAllRelevantLogs();
} catch (e) {
console.log("Error!", e);
}
running = false;
}
function setGoTimeout() {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
console.log("Timeout occurred, calling go function");
go();
}, POLLING_INTERVAL);
}
async function go() {
await update();
setGoTimeout();
}
const filter = {
topics: [
[
ethers.utils.id(registeredEventSignatureV1),
ethers.utils.id(registeredEventSignatureV2),
ethers.utils.id(attestedEventSignature),
ethers.utils.id(revokedEventSignature),
ethers.utils.id(timestampEventSignature),
ethers.utils.id(revokedOffchainEventSignature),
],
],
};
if (!DISABLE_LISTENER) {
provider.on(filter, async (log: ethers.providers.Log) => {
go();
});
}
go();
setGoTimeout();
startGraph();