-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (190 loc) · 5.34 KB
/
server.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
const express = require("express");
const ethers = require("ethers");
const cors = require("cors");
const { contractABI } = require("./constant");
const { db } = require("./config");
require("custom-env").env(true);
const contract_address = process.env.CONTRACT_ADDRESS;
const provider = new ethers.getDefaultProvider(process.env.PROVIDER);
const contract = new ethers.Contract(contract_address, contractABI, provider);
provider.pollingInterval = 1000;
const app = express();
const port = 5000;
app.use(
cors({
origin: "*",
})
);
app.use(express.json());
const uu = async () => {
contract.on("EligibleIds", (id, eligible_prize) => {
const obj = {
id: ethers.toNumber(id),
is_eligible: eligible_prize.is_eligible,
prize_amount: ethers.toNumber(eligible_prize.prize_amount),
is_claimed: eligible_prize.is_claimed,
from_claiming: eligible_prize.from_claiming,
};
//console.log(obj);
//get all eligible ids
//uniswap resurrect use the same event
db.collection(process.env.COLLECTION)
.doc(String(ethers.toNumber(id)))
.set(obj)
.then(() => {
console.log("data created");
})
.catch((err) => {
console.log(err);
});
});
contract.on("ExpiredBounty", (id) => {
//update existing id to from_claiming = true
db.collection(process.env.COLLECTION)
.doc(String(ethers.toNumber(id)))
.update({ from_claiming: true })
.then(() => {
console.log("updated");
})
.catch((err) => {
console.log(err);
});
});
contract.on("ClaimBounty", (id, amount) => {
//update existing id to is_claimed = true
db.collection(process.env.COLLECTION)
.doc(String(ethers.toNumber(id)))
.update({ is_claimed: true })
.then(() => {
console.log("updated");
})
.catch((err) => {
console.log(err);
});
});
};
uu();
app.post("/logs", async (req, res) => {
try {
const idRef = db.collection(process.env.COLLECTION);
const response = await idRef.get();
const respArr = [];
response.forEach((doc) => {
respArr.push(doc.data());
});
res.send(respArr);
} catch (error) {
res.send(error);
}
});
//wait to render claim
app.post("/claim", async (req, res) => {
try {
//update existing firebase id, after read on-chain data
const updatedIdOnChain = await contract.idIsEligible(req.body.id);
const obj = {
id: req.body.id,
is_eligible: updatedIdOnChain.is_eligible,
prize_amount: ethers.toNumber(updatedIdOnChain.prize_amount),
is_claimed: updatedIdOnChain.is_claimed,
from_claiming: updatedIdOnChain.from_claiming,
};
await db
.collection(process.env.COLLECTION)
.doc(String(req.body.id))
.update(obj);
const idRef = db.collection(process.env.COLLECTION);
const response = await idRef.get();
const respArr = [];
response.forEach((doc) => {
respArr.push(doc.data());
});
res.send(respArr);
console.log("updated");
} catch (error) {
res.send(error);
}
});
//get only the unclaimed id
app.get("/logs/unclaimed", async (req, res) => {
try {
const idRef = db.collection(process.env.COLLECTION);
const response = await idRef.get();
const respArr = [];
response.forEach((doc) => {
if (!doc.data().is_claimed && !doc.data().from_claiming)
respArr.push(doc.data());
});
res.send(respArr);
} catch (error) {
res.send(error);
}
});
//get only the claimed id
app.get("/logs/claimed", async (req, res) => {
try {
const idRef = db.collection(process.env.COLLECTION);
const response = await idRef.get();
const respArr = [];
response.forEach((doc) => {
if (doc.data().is_claimed) respArr.push(doc.data());
});
res.send(respArr);
} catch (error) {
res.send(error);
}
});
//get only the the expired ids
app.get("/logs/expired", async (req, res) => {
try {
const idRef = db.collection(process.env.COLLECTION);
const response = await idRef.get();
const respArr = [];
response.forEach((doc) => {
if (doc.data().from_claiming) respArr.push(doc.data());
});
res.send(respArr);
} catch (error) {
res.send(error);
}
});
//force update in case something wrong happen to this logger
app.get("/forceupdate", async (req, res) => {
try {
const filter = contract.filters.EligibleIds();
const events = await contract.queryFilter(filter);
const eventIds = events.map((item, idx) => {
const logs = contract.interface.parseLog({
data: item.data,
topics: item.topics,
});
return ethers.toNumber(logs.args.id);
});
eventIds.forEach(async (item, index) => {
const getDataObject = await contract.idIsEligible(item);
const obj = {
id: item,
is_eligible: getDataObject.is_eligible,
prize_amount: ethers.toNumber(getDataObject.prize_amount),
is_claimed: getDataObject.is_claimed,
from_claiming: getDataObject.from_claiming,
};
await db
.collection(process.env.COLLECTION)
.doc(String(item))
.set(obj)
.then(() => {
console.log("data created");
})
.catch((err) => {
console.log(err);
});
});
res.send("ok");
} catch (error) {
res.send(error);
}
});
app.listen(port, () => {
console.log("listening on:", port);
});