forked from ProjectOpenSea/metadata-api-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
314 lines (285 loc) · 9.96 KB
/
index.js
File metadata and controls
314 lines (285 loc) · 9.96 KB
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
import express from "express";
import path from "path";
import { domainToASCII, fileURLToPath } from "url";
import pkgMoment from "moment";
const { moment } = pkgMoment;
import HOST from "./src/constants.js";
import db from "./src/database.js";
import abiNFT from "./src/abiNFT.js";
import abiProtocol from "./src/abiProtocol.js";
import env from "dotenv";
import axios from "axios";
import ethers from "ethers";
import Moralis from "moralis-v1/node.js";
//why this line : https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope#:~:text=The%20__dirname%20or%20__,directory%20name%20of%20the%20pathclea.
env.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const serverUrl = process.env.SERVER_URL || "https://xqzpsbn9rkym.usemoralis.com:2053/server";
const appId = process.env.APP_ID || "WcthTo0h24NbgFE4ExuuDaVR67isyWtiK9kJAEt9";
const masterKey = process.env.MASTER_KEY || "DqSTHsNyalXVdubxKMXGf0Uaq5iTGXI4oWB5hfyy";
const ADDRESSPROTOCOL = process.env.ADDRESSPROTOCOL || "0xe2790A1F0b412EA3880a5B93Ae6bd4F966C20CED";
const ADDRESSNFT = process.env.ADDRESSNFT || "0x5B51857C8220Ac230fb93aA0087587fD4229eE8d";
const PORT = process.env.PORT || 5000;
const API_KEY = process.env.MORALISV2_API_KEY;
console.log(API_KEY)
const TRANSFER = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
const ADDRESS0 = "0x0000000000000000000000000000000000000000000000000000000000000000";
const STAKE_STARTED = "0xf1b726f9b240a431d5a1d157ace1cf0be2de5c6c909df71d85852f34c069a542";
const STAKE_ENDEDAT = "0xf187ddd32321a75ef62b9b415856a4e6da260b575772cb6e14dde1b37439ab29";
const STAKED_RESUMEDAT = "0x39e4283688f7cedd360def9191f85b9a3207636f1a7a2831a20222bc085678b3"
const iface = new ethers.utils.Interface(abiNFT);
async function refreshMetadata(_tokenId){
const options = {
method: 'GET',
url: `https://deep-index.moralis.io/api/v2/nft/${ADDRESSNFT}/${_tokenId}/metadata/resync?chain=mumbai&flag=uri&mode=async`,
headers: {
Accept: 'application/json',
'X-API-Key': 'xTVK25vKf4vxBe8db3OC18Ox0HMtGjxMNJaI6zFzEoXVetpPn0E2qCcJNj1sunGO'
}
};
axios
.request(options)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});
}
function ownerOf(_tokenId) {
const options = {
method: "POST",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSNFT}/function?chain=mumbai&function_name=ownerOf`,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-API-Key": API_KEY,
},
data: { abi: abiNFT, params: { tokenId: _tokenId } },
};
return axios
.request(options)
.then(function (response) {
let addressTo = ethers.utils.hexZeroPad(response.data, 32);
return filterDeposits(addressTo);
})
.catch(function (error) {
console.error(error);
});
}
function filterDeposits(addressTo) {
const options = {
method: "GET",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSPROTOCOL}/logs?chain=mumbai&topic0=${TRANSFER}&topic1=${ADDRESS0}&topic2=${addressTo}&limit=500`,
headers: {
Accept: "application/json",
"X-API-Key": API_KEY,
},
};
return axios
.request(options)
.then(function (response) {
return response.data;
})
.catch(function (error) {
console.error(error);
});
}
function isActive(account) {
const options = {
method: "POST",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSPROTOCOL}/function?chain=mumbai&function_name=isStaking`,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-API-Key": API_KEY,
},
// to give params you need to pass only the name of the param and the value of the param as it's on the ABI or Smart Contract
data: { abi: abiProtocol, params: { _account: account } },
};
return axios
.request(options)
.then(function (response) {
return response.data
})
.catch(function (error) {
console.error(error);
});
}
function stakeStartedAt(addressTo) {
let _addressTo = ethers.utils.hexZeroPad(addressTo.toString(), 32);
const options = {
method: "GET",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSPROTOCOL}/logs?chain=mumbai&topic0=${STAKE_STARTED}&topic1=${_addressTo}&limit=500`,
headers: {
Accept: "application/json",
"X-API-Key": API_KEY,
},
};
return axios
.request(options)
.then(function (response) {
return response.data;
})
.catch(function (error) {
console.error(error);
});
}
function stakeEndedAt(addressTo) {
console.log(addressTo);
let _addressTo = ethers.utils.hexZeroPad(addressTo, 32);
const options = {
method: "GET",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSPROTOCOL}/logs?chain=mumbai&topic0=${STAKE_ENDEDAT}&topic1=${_addressTo}&limit=500`,
headers: {
Accept: "application/json",
"X-API-Key": API_KEY,
},
};
return axios
.request(options)
.then(function (response) {
return response.data;
})
.catch(function (error) {
console.error(error);
});
}
function stakeResumedAt(addressTo) {
const options = {
method: "GET",
url: `https://deep-index.moralis.io/api/v2/${ADDRESSPROTOCOL}/logs?chain=mumbai&topic0=${STAKED_RESUMEDAT}&topic1=${addressTo}&limit=500`,
headers: {
Accept: "application/json",
"X-API-Key": API_KEY,
},
};
return axios
.request(options)
.then(function (response) {
return response.data;
})
.catch(function (error) {
console.error(error);
});
}
function levelOfToken (days) {
let _days = days <= 1 ? 1 : days <= 30 ? 2 : days <= 90 ? 3 : days <= 180 ? 4 : days >=365 ? 5: 4;
return _days;
//1, 30, 90, 180, 365
}
const app = express()
.set("port", PORT)
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs");
// Static public files
app.use(express.static(path.join(__dirname, "public")));
app.get("/", function (req, res) {
res.send("Get ready for OpenSea!");
});
app.get("/api/token/:token_id", async function (req, res) {
const tokenId = parseInt(req.params.token_id).toString();
let stakeTimeArray = await ownerOf(tokenId);
//refreshMetadata(tokenId);
let _result = stakeTimeArray.result[stakeTimeArray.result.length - 1];
//Block timestamp are in dateNowISO8601, you need to convert it using new Date, to get in epoch time, toISOString() converts the Date into a string in ISO 8601 format.
//console.log(firstDeposit.toISOString(), dateNow.toISOString());
let firstDeposit = new Date(
_result.block_timestamp
);
let dateNow = new Date();
let timeStaked = ((dateNow - firstDeposit) / (1000 * 3600 * 24)).toFixed();
console.log(timeStaked);
// this parses the Address whe're looking for, and return the adress of the owner in an array
// handle like this _address[0]
let _address = ethers.utils.defaultAbiCoder.decode(
["address"],
_result.topic2
);
let _addressToDeposits = Moralis.Object.extend("addressToDeposits");
let _query = new Moralis.Query(_addressToDeposits);
_query.equalTo("address", _address[0]);
let _resultQuery = await _query.find();
console.log(await isActive(_address[0]));
// When _resultQuery is empty, the user has not deposited yet thus the _resultQuery.length is 0
if (_resultQuery.length > 0) {
if (await isActive(_address[0])) {
// is user is Active then calculate how many days have pass since the first deposit
let _ParseObjectSubclass = _resultQuery[0];
console.log(_ParseObjectSubclass.id);
let _addressToDeposits = Moralis.Object.extend("addressToDeposits");
let _query = new Moralis.Query(_addressToDeposits);
_query.get(_ParseObjectSubclass.id).then(
(_address) => {
_address.set("days", timeStaked);
_address.set("daysCounter", timeStaked)
//Remember to use the save function to save the changes to the Parse server
_address.save();
},
(error) => {
console.log(error);
}
);
}else{
// if the guy is not Active..
let _ParseObjectSubclass = _resultQuery[0];
console.log(_ParseObjectSubclass.id);
let _addressToDeposits = Moralis.Object.extend("addressToDeposits");
let _query = new Moralis.Query(_addressToDeposits);
_query.get(_ParseObjectSubclass.id).then(
async(_address) => {
console.log(_address.get("days"));
console.log(_address.get("daysCounter"))
console.log(await stakeEndedAt(_address.get("address")));
//Remember to use the save function to save the changes to the Parse server
_address.save();
},
(error) => {
console.log(error);
}
);
}
} else {
// if the data does't exist, create a new object
let _addressToDeposits = Moralis.Object.extend("addressToDeposits");
let _newAdressInstance = new _addressToDeposits();
_newAdressInstance = new _addressToDeposits({
address: _address[0],
days: timeStaked,
startDate: firstDeposit,
endDate: "-1",
daysCounter: timeStaked,
});
_newAdressInstance.save().then(
(_newAdressInstance) => {
console.log("New Object created with Object Id", _newAdressInstance.id);
},
(error) => {
console.log(error);
}
);
}
//30, 60, 90, 120, 150
let level = await levelOfToken(timeStaked);
const nft = db[level];
const data = {
name: nft.name,
attributes: {
type: nft.type,
stakeTime: timeStaked, //nft.stakeTime,
transparent: nft.transparent,
fruits: nft.fruits,
},
image: `${HOST}/images/${level}.png`,
};
res.send(data);
});
const startServer = async () => {
await Moralis.start({ serverUrl, appId, masterKey });
console.log("Moralis server started");
app.listen(app.get("port"), function () {
console.log("Node app is running on port", app.get("port"));
});
};
startServer();