-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
233 lines (211 loc) · 7.58 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
import express, { json } from "express";
import dotenv from 'dotenv';
import knex from "knex";
import cors from "cors";
import bcrypt from "bcrypt";
import fetch from "node-fetch";
import { hashPassword, sendError, loginUser, registerUser, parseCollectionData, parseItemData, addCollectionToWatchlist, removeCollectionFromWatchlist, parseWatchlistItems, calculateOwners, isCollectionInWatchlist } from "./helpers.js";
const PORT = 3500;
dotenv.config();
const API_KEY = process.env.API_KEY
// const watchlist = [
// {
// image_url:"https://i.seadn.io/gae/_zidXBb2QsMBD6OYdjED63tczeXDUr1ah7zvhSSLHQjU4BF-H-lUexkLJ76_ahmbkkItEiH738jVPG88DOFVdt4GX377cvNNgCyzFT4?w=500&auto=format",
// collection_name:"Alien Frens",
// slug:"alienfrensnft",
// floor_price:"0.089",
// owners:"6124"
// },
// {
// image_url:"https://i.seadn.io/gae/Ju9CkWtV-1Okvf45wo8UctR-M9He2PjILP0oOvxE89AyiPPGtrR3gysu1Zgy0hjd2xKIgjJJtWIc0ybj4Vd7wv8t3pxDGHoJBzDB?w=500&auto=format",
// collection_name:"Bored Ape Yacht Club",
// slug:"boredapeyachtclub",
// floor_price:"33.92",
// owners:"5581"
// },
// {
// image_url:"https://i.seadn.io/gae/7B0qai02OdHA8P_EOVK672qUliyjQdQDGNrACxs7WnTgZAkJa_wWURnIFKeOh5VTf8cfTqW3wQpozGedaC9mteKphEOtztls02RlWQ?w=500&auto=format",
// collection_name:"Doodles",
// slug:"doodles-official",
// floor_price:"1.48",
// owners:"5548"
// },
// {
// image_url:"https://i.seadn.io/gae/seJEwLWJP3RAXrxboeG11qbc_MYrxwVrsxGH0s0qxvF68hefOjf5qrPSKkIknUTYzfvinOUPWbYBdM8VEtGEE980Qv2ti_GGd86OWQ?w=500&auto=format",
// collection_name:"Deadfellaz",
// slug:"deadfellaz",
// floor_price:"0.17",
// owners:"6522"
// },
// {
// image_url:"https://i.seadn.io/gcs/files/b1c9ed2e584b4f6e418bf1ca15311844.jpg?w=500&auto=format",
// collection_name:"Opepen",
// slug:"opepen-edition",
// floor_price:"0.69",
// owners:"4191"
// },
// ]
// const collections = [
// {
// image_url:"https://i.seadn.io/gae/_zidXBb2QsMBD6OYdjED63tczeXDUr1ah7zvhSSLHQjU4BF-H-lUexkLJ76_ahmbkkItEiH738jVPG88DOFVdt4GX377cvNNgCyzFT4?w=500&auto=format",
// collection_name:"Alien Frens",
// slug:"alienfrensnft",
// floor_price:"0.089",
// owners:"6124"
// },
// {
// image_url:"https://i.seadn.io/gae/Ju9CkWtV-1Okvf45wo8UctR-M9He2PjILP0oOvxE89AyiPPGtrR3gysu1Zgy0hjd2xKIgjJJtWIc0ybj4Vd7wv8t3pxDGHoJBzDB?w=500&auto=format",
// collection_name:"Bored Ape Yacht Club",
// slug:"boredapeyachtclub",
// floor_price:"33.92",
// owners:"5581"
// },
// {
// image_url:"https://i.seadn.io/gae/7B0qai02OdHA8P_EOVK672qUliyjQdQDGNrACxs7WnTgZAkJa_wWURnIFKeOh5VTf8cfTqW3wQpozGedaC9mteKphEOtztls02RlWQ?w=500&auto=format",
// collection_name:"Doodles",
// slug:"doodles-official",
// floor_price:"1.48",
// owners:"5548"
// },
// {
// image_url:"https://i.seadn.io/gae/seJEwLWJP3RAXrxboeG11qbc_MYrxwVrsxGH0s0qxvF68hefOjf5qrPSKkIknUTYzfvinOUPWbYBdM8VEtGEE980Qv2ti_GGd86OWQ?w=500&auto=format",
// collection_name:"Deadfellaz",
// slug:"deadfellaz",
// floor_price:"0.17",
// owners:"6522"
// },
// {
// image_url:"https://i.seadn.io/gcs/files/b1c9ed2e584b4f6e418bf1ca15311844.jpg?w=500&auto=format",
// collection_name:"Opepen",
// slug:"opepen-edition",
// floor_price:"0.69",
// owners:"4191"
// },
// {
// image_url:"https://i.seadn.io/gcs/files/4bce6187fea476154b311dafaf327c89.png?w=500&auto=format",
// collection_name:"Moonbirds",
// slug:"proof-moonbirds",
// floor_price:"1.245",
// owners:"6234"
// },
// {
// image_url:"https://i.seadn.io/gae/J2iIgy5_gmA8IS6sXGKGZeFVZwhldQylk7w7fLepTE9S7ICPCn_dlo8kypX8Ju0N6wvLVOKsbP_7bNGd8cpKmWhFQmqMXOC8q2sOdqw?w=500&auto=format",
// collection_name:"mfers",
// slug:"mfers",
// floor_price:"0.45",
// owners:"5191"
// },
// ]
const app = express()
app.use(json())
app.use(cors())
const db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
port : 5432,
user : process.env.USER,
password : '',
database : process.env.DATABASE
}
});
// Index page returns simple json
app.get("/", (req, res) => {
res.json("Hiii");
})
app.post("/login", (req, res) => {
const {username, password} = req.body;
if (!username || !password) {
return sendError(res, 400, "Invalid username or password")
}
loginUser(username, password, db, bcrypt, req, res)
})
app.post("/register", (req, res) => {
const {username, password, confirmation} = req.body;
if (!username || !password || !confirmation || password !== confirmation) {
return sendError(res, 400, "Invalid username or password")
}
hashPassword(password, bcrypt)
.then((hash) => {
registerUser(username, hash, db, req, res);
})
.catch((error) => {
return sendError(res, 500, "Error hashing password")
});
});
app.get("/collection/:collectionSlug/:startToken?", (req, res) => {
const {collectionSlug} = req.params;
let {startToken} = req.params;
const withMetadata = true;
const NFTlimit = 20;
if (!startToken || isNaN(startToken)) {
startToken = 0;
}
fetch('https://eth-mainnet.g.alchemy.com/nft/v2/' + API_KEY + '/getNFTsForCollection?collectionSlug=' + collectionSlug + '&withMetadata=' + withMetadata + '&startToken=' + startToken + '&limit=' + NFTlimit)
.then(resp => {
if (!resp.ok) {
throw new Error(`Fetch error: ${resp.status} - ${resp.statusText}`);
}
return resp.json();
})
.then(data => {
parseCollectionData(data.nfts, API_KEY)
.then(lightweight_data => res.json(lightweight_data))
})
.catch(error => {
console.error("Error searching collection:", error);
return sendError(res, 500, "An error occurred while searching the collection")
});
})
app.get("/:collectionHash/:tokenId", (req, res) => {
const {collectionHash, tokenId} = req.params;
const tokenType = "ERC721";
fetch('https://eth-mainnet.g.alchemy.com/nft/v2/'+ API_KEY +'/getNFTMetadata?contractAddress=' + collectionHash + '&tokenId=' + tokenId + '&tokenType='+ tokenType +'&refreshCache=false')
.then(resp => {
if (!resp.ok) {
throw new Error(`Fetch error: ${resp.status} - ${resp.statusText}`);
}
return resp.json();
})
.then(data => {
const lightweight_data = parseItemData(data)
res.json(lightweight_data)
})
.catch(error => {
console.error("Error searching item:", error);
return sendError(res, 500, "An error occurred while searching the item")
});
})
app.post("/add-to-watchlist", (req, res) => {
const {collection, username} = req.body;
addCollectionToWatchlist(collection, username, db, res)
})
app.delete("/remove-from-watchlist", (req, res) => {
const {collection, username} = req.body;
removeCollectionFromWatchlist(collection, username, db, res)
})
app.post("/fetch-watchlist", (req, res) => {
const {username} = req.body;
db.select("collections")
.from("watchlist")
.where("username", username)
.then(data => {
if (data && data[0] && data[0].collections) {
parseWatchlistItems(data[0].collections, API_KEY)
.then(lightweight_data => res.json(lightweight_data))
} else {
res.json("watchlist_empty")
}
})
})
app.post("/is-collection-in-watchlist", async (req, res) => {
const {username, collection_address} = req.body;
const collectionInWatchlist = await isCollectionInWatchlist(collection_address, username, db, res)
res.json(collectionInWatchlist)
})
app.get("/fetch-collections", (req, res) => {
db.select("*")
.from("collections")
.then(data => res.json(data))
})
app.listen(PORT, () => console.log("server is listening on port:", PORT));