-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
257 lines (235 loc) · 6.76 KB
/
app.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
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
import { BigNumber, ethers } from "ethers";
import { verifyMessage } from "ethers/lib/utils";
import express from "express";
import { connect } from "mongoose";
import cors from "cors";
import AddressInfo from "./models/AddressInfo";
import ClaimInfo from "./models/ClaimInfo";
import ContractAddress from "./models/ContractAddress";
import RegistrationInfo from "./models/registrationInfo";
// @ts-ignore
import { mongoHost, mongoPort, mongoDb } from "./config";
import { aprRouter } from "./apr";
const app = express();
/* Middleware */
// parse body as json
app.use(express.json());
// Enable CORS - allow any host to talk to this api
// TODO - Should CORS setup be more strict?
app.use(cors());
/* MongoDB */
const uri: string = `mongodb://${mongoHost}:${mongoPort}/${mongoDb}`;
connect(uri, (err: any) => {
if (err) {
console.log(err.message);
} else {
console.log("Successfully Connected!");
}
});
/**
* Get merkle leaf for an address
*/
app.get("/claimInfo/:rawAddress", async (request, response, next) => {
const { rawAddress } = request.params;
if (!rawAddress) {
response.status(400).json({ error: "missing address" });
return;
}
// verify address
let checksummedAddress: string;
try {
checksummedAddress = ethers.utils.getAddress(rawAddress);
} catch (e) {
response.status(400).json({ error: "invalid address" });
return;
}
try {
const doc = await ClaimInfo.findOne({ address: checksummedAddress });
if (doc) {
console.log(
`Found claimInfo entry: ${doc.address}, ${doc.chainId} ${doc.leaf}`
);
response.json({
chainId: doc.chainId,
leaf: doc.leaf,
});
return;
} else {
console.log(`No doc found for address ${checksummedAddress}`);
response.status(404).json({ error: "No claim found for address" });
return;
}
} catch (e) {
console.log(`Failed to retrieve claim info for `);
response.status(500).json({ error: "Failed to query database" });
}
});
app.get("/contract/:chainId/:contractName", async (request, response, next) => {
const { chainId, contractName } = request.params;
if (!chainId) {
response.status(400).json({ error: "missing chainId" });
return;
}
if (!contractName) {
response.status(400).json({ error: "missing contractName" });
return;
}
let numericChainId: number;
try {
numericChainId = parseInt(chainId, 10);
} catch (e) {
response.status(400).json({ error: "Invalid chainId" });
return;
}
try {
const doc = await ContractAddress.findOne({
chainId: numericChainId,
name: contractName.toLowerCase(),
});
if (doc) {
response.json({
address: doc.address,
});
} else {
response.status(404).json({ error: "Not found" });
}
} catch (e) {
console.log(`Failed to retrieve contract info: ${e}`);
response.status(500).json({ error: "Failed to query database" });
}
});
/**
* Get info about registration phase
* -> When does current registration phase end
* -> When will next phase start
*/
app.get("/registrationInfo", async (request, response, next) => {
try {
const doc = await RegistrationInfo.findOne();
response.send(doc);
} catch (err) {
console.log(`Failed to get settings from mongodb`);
response.send("Error");
}
});
/**
* Get info about an address
*
* Response body:
* {
* chainId: <desired payout chainId, defaults to mainnet>,
* nextAmount: <expected amount for next airdrop phase>
* }
*/
app.get("/address/:rawAddress", async (request, response, next) => {
const { rawAddress } = request.params;
if (!rawAddress) {
response.status(400).json({ error: "missing address" });
return;
}
// verify address
let checksummedAddress: string;
try {
checksummedAddress = ethers.utils.getAddress(rawAddress);
} catch (e) {
response.status(400).json({ error: "invalid address" });
return;
}
// default values when no info in database
// 4 for Rinkeby, 1 for mainnet, 31337 for hardhat test
let chainId = 1;
let nextAmount = BigNumber.from(0);
try {
const doc = await AddressInfo.findOne({ address: checksummedAddress });
if (doc) {
console.log(
`Found entry: ${doc.address}, ${doc.chainId}, ${doc.nextAmount}`
);
chainId = doc.chainId || chainId;
nextAmount = doc.nextAmount ? BigNumber.from(doc.nextAmount) : nextAmount;
} else {
console.log(`No doc found for address ${checksummedAddress}`);
}
response.json({
chainId,
nextAmount,
});
} catch (e) {
console.log(`Failed to retrieve address info`);
response.status(500).json({ error: "Failed to query database" });
}
});
/**
* Set the payout chainId for an address.
*
* Expected payload in body:
* {
* "chainId": 123,
* "signature": "signature string"
* }
*/
app.post("/address/:rawAddress", async (request, response, next) => {
const { rawAddress } = request.params;
const { chainId, signature } = request.body;
// verify address
if (!rawAddress) {
response.status(400).json({ error: "missing address" });
return;
}
let checksummedAddress: string;
try {
checksummedAddress = ethers.utils.getAddress(rawAddress);
} catch (e) {
response.status(400).json({ error: "invalid address" });
return;
}
// verify chainId
if (!chainId) {
response.status(400).json({ error: "missing chainId" });
return;
}
if (typeof chainId !== "number") {
response.status(400).json({ error: "invalid chainId" });
return;
}
// verify signature
if (!signature) {
response.status(400).json({ error: "missing signature" });
return;
}
if (typeof signature !== "string") {
response.status(400).json({ error: "invalid signature" });
return;
}
const signedMessage = `Set chainId for ${checksummedAddress} to ${chainId}`;
let signerAddress: string;
try {
signerAddress = verifyMessage(signedMessage, signature);
} catch (e) {
response.status(400).json({ error: "signature not matching" });
return;
}
if (signerAddress !== checksummedAddress) {
response.status(400).json({ error: "signature not matching" });
return;
}
// store new chainId
try {
const res = await AddressInfo.updateOne(
{ address: checksummedAddress },
{ chainId: chainId },
{ upsert: true }
);
res.n; // Number of documents matched
res.nModified; // Number of documents modified
console.log(`Found ${res.n} entries, updated ${res.nModified} entries.`);
response.json({ success: true });
} catch (err) {
console.log(`failed to set chainId: ${err.message}`);
response.status(500).json({ error: "Failed to update database" });
}
});
// APR Router
app.use("/apr", aprRouter);
app.get("/", (req, res) => res.send("Fairdrop backend server!"));
export default app;