-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
344 lines (305 loc) · 13 KB
/
index.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Required Packages:
const cors = require('cors');
const express = require('express');
const admin = require('firebase-admin');
const requestIp = require('request-ip');
const swagger = require('swagger-ui-express');
const functions = require('firebase-functions');
// Imports:
import { ethers } from 'ethers';
import weaver from 'weaverfi';
import * as fn from './functions';
// Type Imports:
import type { Chain, Address, Hash } from 'weaverfi';
import type { Application, Request, Response, NextFunction } from 'express';
// Fetching Swagger Docs Setup JSON File:
const swaggerDocs: JSON = require('../static/swagger.json');
// Initializing Firebase App:
admin.initializeApp();
// Initializing Express Server:
const api: Application = express();
api.use(cors());
api.use(express.static('functions/static'));
// General Settings:
const localTesting: boolean = false; // Set this to `true` to test the API locally instead of deploying it.
const localTestingPort: number = 3_000; // This is the port used to locally host the API during testing.
const dbPrices: boolean = true; // Set this to `true` to fetch token prices from Firebase (production only).
const dbKeyCache: boolean = true; // Set this to `true` to fetch cached key info from Firebase (production only).
const rateLimited: boolean = true; // Set this to `true` to rate limit all endpoints with a few exceptions.
const minInstances = 0; // Set this to the number of function instances you want to keep warm (decreases spin-up time but increases cost).
const maxInstances = 100; // Set this to the maximum number of function instances you would like to have (stops excessive scaling during peak usage).
// 3PI Key Management Settings:
const contractAddresses: Partial<Record<Chain, Address>> = {
op: '0xF50D1cAF40E1dE56198F262ACA4A3745De0A88dC',
poly: '0xF50D1cAF40E1dE56198F262ACA4A3745De0A88dC'
}
const rateLimitTimespanInMs: number = 86_400_000;
const newKeyCooldown: boolean = true;
const freeTierID: number = 0;
const apiTiers: Record<number, { rateLimit: number }> = {
0: { rateLimit: 100 },
1: { rateLimit: 1_000 },
2: { rateLimit: 3_000 },
3: { rateLimit: 8_000 }
}
/* ========================================================================================================================================================================= */
// Swagger Documentation Endpoint:
api.use('/docs', swagger.serve, swagger.setup(swaggerDocs));
// Teapot:
api.get(`/teapot`, async (req: Request, res: Response) => {
fn.sendError('teapot', res);
});
// Logging Middleware:
api.use(async (req: Request, res: Response, next: NextFunction) => {
if(fn.isValidRoute(req.path)) {
if(rateLimited && (process.env.WHITELIST === undefined || req.headers.origin === undefined || (req.headers.origin && !process.env.WHITELIST.split(' ').includes(req.headers.origin)))) {
const apiKey = req.query.key;
if(apiKey) {
if(typeof apiKey === 'string') {
const timestampInMs = Date.now();
const keyInfo = await fn.getKeyInfo(apiKey, contractAddresses, admin, timestampInMs, { useDB: dbKeyCache });
if(keyInfo.valid) {
const coolingDown = newKeyCooldown ? timestampInMs < ((keyInfo.startTime * 1000) + rateLimitTimespanInMs) : false;
const rateLimit = coolingDown ? apiTiers[keyInfo.tierId].rateLimit * ((timestampInMs - (keyInfo.startTime * 1000)) / rateLimitTimespanInMs) : apiTiers[keyInfo.tierId].rateLimit;
if(!localTesting) {
// Getting client's IP & hashing it if using free tier:
let clientIp = requestIp.getClientIp(req);
const hashedIP = keyInfo.tierId === freeTierID ? ethers.utils.keccak256(Buffer.from(typeof clientIp !== 'string' ? 'no-ip' : clientIp)) as Hash : undefined;
// Determining if client is rate-limited:
const ipRateLimitReached = hashedIP ? await fn.isUserRateLimited(admin, hashedIP, rateLimit, rateLimitTimespanInMs) : false;
if(!ipRateLimitReached) {
const keyDoc = await fn.fetchKeyDocDB(admin, keyInfo.hash);
if(keyDoc) {
if(keyDoc.lastTimestamp.toMillis() < (timestampInMs - rateLimitTimespanInMs)) {
const usageHistory = { timestamp: keyDoc.lastTimestamp, queries: keyDoc.queries };
await fn.updateKeyDocDB(admin, keyInfo.hash, { usage: admin.firestore.FieldValue.arrayUnion(usageHistory), lastTimestamp: admin.firestore.FieldValue.serverTimestamp(), queries: 1 });
fn.logUsage(req);
next();
} else if(keyDoc.queries >= rateLimit) {
if(coolingDown) {
fn.sendError('rateLimitedRampingUp', res);
} else {
fn.sendError('rateLimited', res);
}
} else {
await fn.updateKeyDocDB(admin, keyInfo.hash, { queries: admin.firestore.FieldValue.increment(1) });
fn.logUsage(req);
next();
}
} else {
await fn.setKeyDocDB(admin, keyInfo.hash, { lastTimestamp: admin.firestore.FieldValue.serverTimestamp(), queries: 1, usage: [] });
fn.logUsage(req);
next();
}
} else {
if(coolingDown) {
fn.sendError('rateLimitedRampingUp', res);
} else {
fn.sendError('rateLimited', res);
}
}
}
} else {
fn.sendError('invalidAuth', res);
}
} else {
fn.sendError('invalidAuth', res);
}
} else {
fn.sendError('missingAuth', res);
}
} else {
fn.logUsage(req);
next();
}
} else {
next();
}
});
/* ========================================================================================================================================================================= */
// Chain List Endpoint:
api.get('/chains', (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver.getAllChainInfo());
});
// Project List Endpoint:
api.get('/projects', (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver.getAllProjects());
});
// Token List Endpoint:
api.get('/tokens', (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver.getAllTokens());
});
/* ========================================================================================================================================================================= */
// All Token Prices Endpoint:
api.get('/tokenPrices', async (req: Request, res: Response) => {
try {
if(!localTesting && dbPrices) {
fn.sendResponse(req, res, await fn.fetchTokenPricesDB(admin));
} else {
fn.sendResponse(req, res, await weaver.getAllTokenPrices());
}
} catch(err) {
fn.sendError('internalError', res, err);
}
});
// Native Token Prices Endpoint:
api.get('/nativeTokenPrices', async (req: Request, res: Response) => {
try {
if(!localTesting && dbPrices) {
fn.sendResponse(req, res, await fn.fetchNativeTokenPricesDB(admin));
} else {
fn.sendResponse(req, res, await weaver.getNativeTokenPrices());
}
} catch(err) {
fn.sendError('internalError', res, err);
}
});
/* ========================================================================================================================================================================= */
// Chain-Specific Endpoints:
weaver.getAllChains().forEach(chain => {
// Chain Info Endpoint:
api.get(`/${chain}/info`, (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver[chain].getInfo());
});
// Project List Endpoint:
api.get(`/${chain}/projects`, (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver[chain].getProjects());
});
// Token List Endpoint:
api.get(`/${chain}/tokens`, (req: Request, res: Response) => {
fn.sendResponse(req, res, weaver[chain].getTokens());
});
// Gas Estimates Endpoint:
api.get(`/${chain}/gas`, async (req: Request, res: Response) => {
fn.sendResponse(req, res, await weaver[chain].getGasEstimates());
});
// Token Prices Endpoint:
api.get(`/${chain}/tokenPrices`, async (req: Request, res: Response) => {
try {
if(!localTesting && dbPrices) {
fn.sendResponse(req, res, await fn.fetchChainTokenPricesDB(admin, chain));
} else {
fn.sendResponse(req, res, await weaver[chain].getTokenPrices());
}
} catch(err) {
fn.sendError('internalError', res, err);
}
});
// Token Price Endpoint:
api.get(`/${chain}/tokenPrice`, async (req: Request, res: Response) => {
let address = req.query.address as string | undefined;
let decimals = req.query.decimals ? parseInt(req.query.decimals as string) : undefined;
let tokenInfo = { chain, address, decimals, price: 0 };
if(address) {
try {
if(!localTesting && dbPrices) {
let tokenPrice = await fn.fetchTokenPriceDB(admin, chain, address);
if(tokenPrice) { tokenInfo.price = tokenPrice; }
}
if(tokenInfo.price === 0 && decimals !== undefined) {
if(weaver[chain].isAddress(address as Address)) {
tokenInfo.price = await weaver[chain].getTokenPrice(address as Address, decimals);
} else {
fn.sendError('invalidAddress', res);
}
}
fn.sendResponse(req, res, tokenInfo);
} catch(err) {
fn.sendError('internalError', res, err);
}
} else {
fn.sendError('missingAddress', res);
}
});
// Wallet Balance Endpoint:
api.get(`/${chain}/wallet`, async (req: Request, res: Response) => {
let address = req.query.address as string | undefined;
if(address) {
try {
if(!localTesting && dbPrices) { await fn.fetchTokenPricesDB(admin); }
if(weaver[chain].isAddress(address as Address)) {
fn.sendResponse(req, res, await weaver[chain].getWalletBalance(address as Address));
} else {
fn.sendError('invalidAddress', res);
}
} catch(err) {
fn.sendError('internalError', res, err);
}
} else {
fn.sendError('missingAddress', res);
}
});
// Project Balance Endpoint:
api.get(`/${chain}/project`, async (req: Request, res: Response) => {
let project = req.query.name as string | undefined;
let address = req.query.address as string | undefined;
if(project) {
if(weaver[chain].getProjects().includes(project)) {
if(address) {
try {
if(!localTesting && dbPrices) { await fn.fetchTokenPricesDB(admin); }
if(weaver[chain].isAddress(address as Address)) {
fn.sendResponse(req, res, await weaver[chain].getProjectBalance(address as Address, project));
} else {
fn.sendError('invalidAddress', res);
}
} catch(err) {
fn.sendError('internalError', res, err);
}
} else {
fn.sendError('missingAddress', res);
}
} else {
fn.sendError('invalidProject', res);
}
} else {
fn.sendError('missingProject', res);
}
});
// NFT Balance Endpoint:
api.get(`/${chain}/nfts`, async (req: Request, res: Response) => {
let address = req.query.address as string | undefined;
if(address) {
try {
if(weaver[chain].isAddress(address as Address)) {
fn.sendResponse(req, res, await weaver[chain].getNFTBalance(address as Address));
} else {
fn.sendError('invalidAddress', res);
}
} catch(err) {
fn.sendError('internalError', res, err);
}
} else {
fn.sendError('missingAddress', res);
}
});
// Token Price History Endpoint:
if(!localTesting && dbPrices) {
api.get(`/${chain}/tokenPriceHistory`, async (req: Request, res: Response) => {
let address = req.query.address as string | undefined;
if(address) {
try {
let priceHistory = await fn.fetchTokenPriceHistoryDB(admin, chain, address);
fn.sendResponse(req, res, { chain, address, priceHistory });
} catch(err) {
fn.sendError('internalError', res, err);
}
} else {
fn.sendError('missingAddress', res);
}
});
}
});
/* ========================================================================================================================================================================= */
// 404 Response:
api.all('*', async (req: Request, res: Response) => {
fn.sendError('routeError', res);
});
/* ========================================================================================================================================================================= */
// Local Development:
if(localTesting) {
api.listen(localTestingPort, () => { console.info(`API Up on http://127.0.0.1:${localTestingPort}`); });
// Production Deployment:
} else {
exports.api = functions.runWith({ memory: '1GB', timeoutSeconds: 180, minInstances: minInstances, maxInstances: maxInstances }).https.onRequest(api);
}