-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (63 loc) · 1.81 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
var express = require("express");
var cors = require("cors");
var app = express();
var http = require("http");
var bodyParser = require("body-parser");
const Web3 = require("web3");
const web3 = new Web3("https://api.hyperspace.node.glif.io/rpc/v1"); // hyperspace network
const router = require("./routes");
const PORT = process.env.PORT || 8000;
const server = http.createServer(app);
const CONTRACT_ADDRESS = "0xaadf05c737D6efDEbBD50C79E02d670E559dEE34"; // add your contract address here.
const CONTRACT_ABI = require("./abi.json"); // add your abi to the abi json file.
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS);
let blockNumber = 0;
async function getLastestBlock() {
return await web3.eth.getBlockNumber();
}
async function getEvents() {
let latest_block = await getLastestBlock();
blockNumber = blockNumber === 0 ? latest_block - 1 : blockNumber;
console.log(
"before - latest: ",
latest_block,
"previous block: ",
blockNumber
);
if (latest_block !== blockNumber) {
const events = await contract.getPastEvents(
"Name", // change if your looking for a different event
{ fromBlock: blockNumber + 1, toBlock: "latest" }
);
blockNumber = latest_block;
if (events.length !== 0) {
console.log(events, "events");
console.log(events[0].raw.topics, "topic");
}
console.log(
"after - before - latest: ",
latest_block,
"previous block: ",
blockNumber
);
indexData();
}
getEvents();
}
async function indexData() {
// query data logic.
getEvents();
}
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
app.use(cors());
app.use(router);
server.listen(
PORT,
() => console.log(`server has started on port ${PORT}`),
getEvents(CONTRACT_ABI, CONTRACT_ADDRESS)
);