-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
236 lines (196 loc) · 7.39 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
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
require('dotenv').config()
const axios = require('axios').default;
const express = require("express");
const rateLimit = require("express-rate-limit");
const app = express();
const port = process.env.PORT || 3000;
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");
const cors = require('cors');
app.use(cors());
// Limiter
const limiter = rateLimit({
windowMs: 1000 // 1 second
})
// Appl to all requests
app.use(limiter)
// Port
let server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
})
server.timeout = 60_000;
// Routes
// All NFTs based on user address
app.get("/Moralis/WalletNFTs/:chain/:address", async (req, res) => {
console.log('\nRequest to /Moralis/WalletNFTs/:chain/:address');
try {
if (!Moralis.Core.isStarted) {
await Moralis.start({
apiKey: process.env.MORALIS_API_KEY
});
}
const chain = EvmChain[req.params.chain];
const address = req.params.address;
const response = await Moralis.EvmApi.nft.getWalletNFTs({
address,
chain
});
console.log('Request fulfilled\n');
res.send(response.toJSON());
} catch (err) {
return res.status(500).json({
success: false,
message: err.message,
})
}
});
// Filtered NFTs based on chain, user address and collection address
app.get("/Moralis/WalletNFTs/:chain/:address/:collection", async (req, res) => {
console.log('\nRequest to /Moralis/WalletNFTs/:chain/:address/:collection');
try {
if (!Moralis.Core.isStarted) {
await Moralis.start({
apiKey: process.env.MORALIS_API_KEY
});
}
const chain = EvmChain[req.params.chain];
const address = req.params.address;
const response = await Moralis.EvmApi.nft.getWalletNFTs({
address,
chain,
});
let responseJSON = response.toJSON();
const filteredResponse = responseJSON.result.filter(x => {
return x.token_address.toLowerCase() === req.params.collection.toLowerCase()
})
console.log('Request fulfilled\n');
res.send(filteredResponse)
} catch (err) {
return res.status(500).json({
success: false,
message: err.message,
})
}
});
// Get price of a specific ERC20 token based on chain and token address
app.get("/Moralis/TokenPrice/:chain/:address/:assetPriceId", async (req, res) => {
console.log('\nRequest to /Moralis/TokenPrice/:chain/:address/:assetPriceId');
try {
if (!Moralis.Core.isStarted) {
await Moralis.start({
apiKey: process.env.MORALIS_API_KEY
});
}
const chain = EvmChain[req.params.chain];
const address = req.params.address;
const moralisResponse = await Moralis.EvmApi.token.getTokenPrice({
address,
chain,
});
// Get asset price id
let assetPriceId = req.params.assetPriceId.toLowerCase();
requestURL = "https://api.coingecko.com/api/v3/coins/list"
let correctId = false;
let coinList;
coinList = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
for (var e in coinList) {
if (coinList[e].id == assetPriceId) {
correctId = true
}
}
// Get asset price over usd
let requestedPrice;
requestURL = 'https://api.coingecko.com/api/v3/simple/price?ids=' + assetPriceId + '&vs_currencies=' + 'usd' + '&precision=full';
requestedPrice = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
const response = { price: moralisResponse.toJSON().usdPrice / requestedPrice[assetPriceId].usd }
console.log('Request fulfilled\n');
res.send(response)
} catch (err) {
return res.status(500).json({
success: false,
message: err.message,
})
}
});
// Get opensea collection Infos based on collection name
app.get("/QuarryDraw/OpenseaCollection/:collectionName", async (req, res) => {
console.log('\nRequest to /QuarryDraw/OpenseaCollection/:collectionName');
try {
let requestURL = "https://api.opensea.io/api/v1/collection/" + req.params.collectionName;
let response;
response = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
console.log('Request fulfilled\n');
res.send(response)
} catch (err) {
return res.status(500).json({
success: false,
message: err.message,
})
}
});
// Get opensea collection floor and/or marketcap based on collection name and valued by asset
app.get("/QuarryDraw/OpenseaCollection/:collectionName/:assetPriceId/:floor/:marketcap", async (req, res) => {
console.log('\nRequest to /QuarryDraw/OpenseaCollection/:collectionName/:assetPriceId/:floor/:marketcap')
let requestURL;
try {
// Get asset price id
let assetPriceId = req.params.assetPriceId.toLowerCase();
requestURL = "https://api.coingecko.com/api/v3/coins/list"
let correctId = false;
let coinList;
coinList = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
for (var e in coinList) {
if (coinList[e].id == assetPriceId) {
correctId = true
}
}
if (!correctId) throw { message: "WRONG_ID, availability at https://api.coingecko.com/api/v3/coins/list" };
// Get asset price over eth
let assetPriceInEth;
requestURL = 'https://api.coingecko.com/api/v3/simple/price?ids=' + assetPriceId + '&vs_currencies=' + 'eth' + '&precision=full';
assetPriceInEth = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
// Get opensea floor price
let collectionName = ((req.params.collectionName).toLowerCase()).replace(" ", "-");
let collectionInfo;
requestURL = 'https://api.opensea.io/api/v1/collection/' + collectionName;
collectionInfo = await axios.get(requestURL)
.then(function (res) {
console.log("Correct server response");
return res.data;
});
// Caluculate floor and marketcap based on asset price
let response = {
// floor: 0,
// marketcap: 0
}
if (req.params.floor == 'true') response.floor = collectionInfo.collection.stats.floor_price / assetPriceInEth[assetPriceId].eth
if (req.params.marketcap == 'true') response.marketcap = collectionInfo.collection.stats.market_cap / assetPriceInEth[assetPriceId].eth
console.log('Request fulfilled\n');
res.send(response);
} catch (err) {
return res.status(500).json({
success: false,
message: err.message,
})
}
});