-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·114 lines (100 loc) · 2.69 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
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
const express = require('express');
const cors = require('cors');
const redis = require('redis');
const axios = require('axios');
const main = require('./main');
const enumerable = require('./constants/polygon-mainnet-enumerable.json');
const indexable = require('./constants/openq-local-indexable.json');
const fetchCoinGeckoPrices = require('./utils/fetchCoinGeckoPrices.js');
const fetchCachedToken = require('./utils/fetchCachedToken.js');
const metadata = require('./metadata');
const metadataByToken = require('./metadataByToken');
require('dotenv').config();
// Prepare Redis
let client;
if(process.env.DEPLOY_ENV === 'local') {
const redisUrl = `redis://${process.env.REDIS_USERNAME}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`;
client = redis.createClient({
url: redisUrl,
});
}
else{
const redisUrl = `rediss://${process.env.REDIS_USERNAME}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`;
client = redis.createClient({
url: redisUrl,
socket: {
tls: true,
rejectUnauthorized: false,
cert: '...'
}
});
}
client.on('error', err => {
console.log(err);
});
const PORT = 8081;
const app = express();
app.use(express.json());
app.use(cors({ origin: process.env.ORIGIN_URL }));
app.post('/tvl', async (req, res) => {
try {
const result = await main(
req,
client,
fetchCoinGeckoPrices,
fetchCachedToken
);
res.json(result);
} catch (error) {
res.status(403).json(error);
}
});
app.get('/tokenMetadata', async (req, res) => {
try {
const tokenMetadata = await metadata(req, client);
const withPath = tokenMetadata.map((token) => {
if ((!token.path && !token.logoURI) || token.path?.match(/github.com/) || token.path?.match(/drive.google/) || token.logoURI?.match(/github.com/) || token.logoURI?.match(/drive.google/)) {
return { ...token, path: '/crypto-logos/ERC20.svg', logoURI: '/crypto-logos/ERC20.svg' };
}
return token;
});
res.json(withPath);
} catch (err) {
res.status(500).json(error);
}
});
app.get('/tokenMetadata/:address', async (req, res) => {
try {
const singleTokenMetadata = await metadataByToken(client, req.params.address);
res.json(singleTokenMetadata);
} catch (error) {
res.status(500).send({ message: error.message });
}
});
app.get("/staticMetadata/:type", async (req, res) => {
const { type } = req.params;
switch (type) {
case "enumerable": {
try {
res.json(enumerable.tokens);
}
catch (err) {
res.status(500);
}
}
break;
case 'indexable': {
try {
res.json(indexable);
}
catch (err) {
res.status(500);
}
}
break;
default:
res.status(404);
}
});
app.listen(PORT);
console.log(`OpenQ-Coin-API on ${PORT}`);