-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
140 lines (115 loc) · 3.09 KB
/
main.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
const { ethers } = require("ethers");
const { PrismaClient } = require("@prisma/client");
const express = require("express");
const app = express();
const prisma = new PrismaClient();
const { ETHEREUM_RPC_URL } = require("./constants");
const provider = new ethers.providers.JsonRpcProvider(ETHEREUM_RPC_URL);
const retryDelay = 10000;
const date = new Date();
date.setDate(date.getDate() - 89);
const endTime = Math.floor(date.getTime() / 1000);
console.log(endTime);
async function storeData(to, from, timestamp) {
if (to == null || from == null || timestamp == null) return;
var address = await prisma.ContractAddresses.findUnique({
where: { address: to.toLowerCase() },
});
if (address == null) {
address = await prisma.ContractAddresses.create({
data: { address: to.toLowerCase() },
});
}
await prisma.InteractedAddresses.create({
data: {
address: from.toLowerCase(),
timestamp: timestamp,
ContractAddresses: {
connect: {
id: address.id,
},
},
},
});
}
async function fetchDataWithRetry(tx, timeStamp) {
let retries = 0;
const maxRetries = 3;
while (retries < maxRetries) {
try {
const receipt = await provider.getTransactionReceipt(tx);
if (receipt == null) return;
await storeData(receipt.to, receipt.from, timeStamp);
break;
} catch (error) {
console.error(error);
retries++;
await delay(retryDelay);
}
}
}
async function listenForNewBlock() {
console.log("new block");
provider.on("block", async (blockNumber) => {
let block;
let retries = 0;
const maxRetries = 3;
while (retries < maxRetries) {
try {
block = await provider.getBlock(blockNumber);
break;
} catch (error) {
console.error(error);
retries++;
await delay(retryDelay);
}
}
if (block) {
for (const tx of block.transactions) {
await fetchDataWithRetry(tx, block.timestamp);
}
}
});
}
async function main() {
// const latestBlock = await provider.getBlockNumber();
const latestBlock = 16922046
for (var i = latestBlock; i > 0; i--) {
let block;
let retries = 0;
const maxRetries = 3;
while (retries < maxRetries) {
try {
block = await provider.getBlock(i);
break;
} catch (error) {
console.error(error);
retries++;
await delay(retryDelay);
}
}
if (!block) continue;
if (block.timestamp < endTime) {
return listenForNewBlock();
}
for (const tx of block.transactions) {
await fetchDataWithRetry(tx, block.timestamp);
}
}
return listenForNewBlock();
}
main();
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
app.get("/address/:address", async (req, res) => {
const address = req.params.address;
const addressData = await prisma.ContractAddresses.findUnique({
where: { address: address.toLowerCase() },
include: { InteractedAddresses: true },
});
res.send(addressData);
});
app.listen(3000, () => {
console.log("server started");
});