Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
435 lines (296 loc) · 13.2 KB
/
Copy pathserver.js
File metadata and controls
435 lines (296 loc) · 13.2 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
const express = require('express')
const db = require('./models/index')
const QRCode = require('qrcode')
const config = require('./config.json')
const main_url = config.url
const fs = require('fs/promises')
// Solana pay imports
const { clusterApiUrl, Connection, Keypair, PublicKey, Transaction } = require('@solana/web3.js')
const BigNumber = require('bignumber.js')
const { createTransferCheckedInstruction, getAccount, getAssociatedTokenAddress, getMint, getOrCreateAssociatedTokenAccount, amountToUiAmount } = require('@solana/spl-token')
const { TEN, encodeURL, findReference, validateTransfer, FindReferenceError, ValidateTransferError } = require('@solana/pay')
// Coneection
const cluster = 'mainnet-beta'
const endpoint = clusterApiUrl(cluster);
const connection = new Connection(endpoint, 'confirmed');
//Token
const splToken = new PublicKey('DwWQHDiyLauoh3pUih7X6G1TrqQnAjgpZ1W7ufrJQcb9');
//wallet
const MERCHANT_KEYPAIR = Keypair.fromSecretKey(new Uint8Array(JSON.parse(config.pkey)))
const MERCHANT_WALLET = new PublicKey(MERCHANT_KEYPAIR.publicKey)
//const MERCHANT_WALLET = new PublicKey("FFzWfCaNva5R3FcmtADtxmbMA97rVhgM2GWhmzo2k5RB")
// Annf2Hqk8xsfRQcGL3j4WRQXJhx4umM3uV4jv56qzWMK
// 6NhgBfVm9imA1h6Kd8HabdbiRAXf77Xcxh189dHZHMwx
const app = express()
app.use(express.json())
app.use(express.static('public'));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/rsvp/:tank', (req, res) => {
let tank = req.query.tank
if (['1', '2', '3'].includes(tank)) {
var fishtank = tank
res.render('index', { fishtank: fishtank })
} else {
res.render('index', { fishtank: NaN })
}
})
app.get('/api/get/fishtanks', (req, res) => {
const fishtanks = ['1', '2', '3', '4']
res.send(fishtanks)
})
app.get('/lol', (req, res) => {
res.redirect('https://www.youtube.com/watch?v=a3Z7zEc7AXQ')
})
app.get('/api/get/fishtank/:id', async (req, res) => {
try {
let parts = req.header("date").split("/");
let date = formatDate(new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10) - 1,
parseInt(parts[0], 10)))
console.log(date)
const reservations = await db.rsvp.findAndCountAll({ where: { fishtank: req.params.id, date: date }, raw: true })
const checkouts = await db.checkout.findAndCountAll({ where: { fishtank: req.params.id, date: date }, raw: true })
let result = []
await reservations.rows.forEach(async (rows, i) => {
result.push({
period: await reservations.rows[i].period,
wallet: await reservations.rows[i].wallet,
})
})
await checkouts.rows.forEach(async (rows, i) => {
result.push({
period: await checkouts.rows[i].period,
wallet: await checkouts.rows[i].wallet,
})
})
res.send(await result)
} catch (error) {
console.log(error)
}
})
app.get('/files/qrcodes/:file', (req, res) => {
try {
res.sendFile(__dirname + '/files/qrcodes/' + req.params.file)
} catch (error) {
console.log(error)
res.send({ error: "No reference found" })
}
})
// TESTING
app.post('/api/post/rsvp', async (req, res) => {
try {
await db.rsvp.create({ name: req.body.name, wallet: req.body.wallet, hash: req.body.hash, fishtank: req.body.fishtank, date: req.body.date, period: req.body.period })
} catch (error) {
console.log(error)
}
res.sendStatus(200)
})
// ^^^ TESTING
app.post('/api/post/checkout', async (req, res) => {
try {
let parts = req.body.date.split("/");
let date = formatDate(new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10) - 1,
parseInt(parts[0], 10)))
const fechaProporcionada = new Date(date); fechaProporcionada.setHours(0, 0, 0, 0);
const fechaActual = new Date(); fechaActual.setHours(0, 0, 0, 0);
if (fechaProporcionada.getTime() < fechaActual.getTime()) {
return res.sendStatus(400);
}
let check_period_checkout = await db.checkout.findOne({ where: { period: req.body.period, fishtank: req.body.fishtank, date: date } })
let check_period_rsvp = await db.rsvp.findOne({ where: { period: req.body.period, fishtank: req.body.fishtank, date: date } })
if (check_period_checkout || check_period_rsvp) { return res.sendStatus(400); }
const checkout = await db.checkout.create({ fishtank: req.body.fishtank, date: date, period: req.body.period, status: 'started' })
let url = encodeURL({ link: main_url + '?id=' + checkout.checkoutId })
//let qr = createQR(url, 512, 'white', 'black').download(__dirname + '/files/qrcodes/' + checkout.checkoutId + '.svg')
QRCode.toFile(__dirname + '/files/qrcodes/' + checkout.checkoutId + '.png', url.href, { errorCorrectionLevel: 'M' })
res.send({ checkoutId: checkout.checkoutId, qr: main_url + '/files/qrcodes/' + checkout.checkoutId + '.png', url: 'solana:' + main_url + '?id=' + checkout.checkoutId, url2: url })
} catch (error) {
console.log(error)
res.sendStatus(500)
}
})
// Solana pay
app.get('/', (req, res) => {
const icon = main_url + '/logo.png'
res.send({ "label": "Fishtanks Reservations", "icon": icon })
})
app.get('/logo.png', (req, res) => {
res.sendFile(__dirname + '/files/logo.png')
})
app.post('/', async (req, res) => {
try {
// We pass the reference to use in the query
const reference = req.query.id
if (!reference) {
res.status(400).send({ error: "No reference provided" })
return console.log('No reference')
}
// We pass the selected items in the query, calculate the expected cost
const amount = await calculatePrice(reference)
if (!amount) {
res.status(400).send({ error: "No checkout detected" })
return console.log('No checkout reference')
}
// We pass the buyer's public key in JSON body
const account = req.body.account
if (!account) {
res.status(400).send({ error: "No account provided" })
return console.log('No account provided')
}
const buyerPublicKey = new PublicKey(account)
const shopPublicKey = MERCHANT_WALLET
// Get details about the token
const Mint = await getMint(connection, splToken)
// Get the buyer's USDC token account address
const buyerUsdcAddress = await getAssociatedTokenAddress(splToken, buyerPublicKey)
// Get the shop's USDC token account address
const shopAddress = await getAssociatedTokenAddress(splToken, shopPublicKey)
const feepayerAccount = await getAssociatedTokenAddress(Mint.address, MERCHANT_KEYPAIR.publicKey)
// Get a recent blockhash to include in the transaction
const lastblock = await connection.getLatestBlockhash('finalized')
const transaction = new Transaction({
blockhash: lastblock.blockhash,
// The buyer pays the transaction fee
//feePayer: shopAddress,
lastValidBlockHeight: lastblock.lastValidBlockHeight
})
// Create the instruction to send USDC from the buyer to the shop
const transferInstruction = createTransferCheckedInstruction(
buyerUsdcAddress, // source
splToken, // mint (token address)
shopAddress, // destination
buyerPublicKey, // owner of source address
amount, // amount to transfer (in units of the USDC token)
1, // decimals of the USDC token
)
// Add the reference to the instruction as a key
// This will mean this transaction is returned when we query for the reference
const pubkey = new Keypair().publicKey
transferInstruction.keys.push({
pubkey: pubkey,
isSigner: false,
isWritable: false,
})
// Add the instruction to the transaction
transaction.add(transferInstruction)
transaction.sign(MERCHANT_KEYPAIR)
// Serialize the transaction and convert to base64 to return it
const serializedTransaction = transaction.serialize({
// We will need the buyer to sign this transaction after it's returned to them
requireAllSignatures: false
})
const base64 = serializedTransaction.toString('base64')
// Insert into database: reference, amount
await db.checkout.update({ status: 'pending', pubkey: pubkey.toString() }, { where: { checkoutId: req.query.id } })
// Return the serialized transaction
res.send({
transaction: base64,
message: "Thanks for your order! 🍪",
})
} catch (err) {
console.log(err);
res.status(500).send({ error: 'error creating transaction', })
return
}
});
async function calculatePrice(id) {
let checkout = await db.checkout.findOne({ where: { checkoutId: id } })
let amount = config.price
if (!await checkout) { amount = null }
return amount;
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
async function checkstatus() {
try {
// 10 min = 600000 // 40 sec = 40000
let time = 600000
// Started
let started = await db.checkout.findAndCountAll({ where: { status: 'started' }, raw: true })
await started.rows.forEach(async (rows, i) => {
let now = Date.now()
if (now - new Date(await started.rows[i].createdAt).getTime() >= time) {
await db.checkout.destroy({ where: { checkoutId: await started.rows[i].checkoutId } });
console.log(await started.rows[i].checkoutId + ' Deleted');
await fs.rm(__dirname + '/files/qrcodes/' + await started.rows[i].checkoutId + '.png')
};
})
// Pending
let pending = await db.checkout.findAndCountAll({ where: { status: 'pending' }, raw: true })
await pending.rows.forEach(async (rows, i) => {
try {
let amount = await calculatePrice(await pending.rows[i].checkoutId) * 0.1
if (!amount) { return }
let reference = new PublicKey(await pending.rows[i].pubkey)
// Check if there is any transaction for the reference
const signatureInfo = await findReference(connection, reference, { finality: 'confirmed' })
// Validate that the transaction has the expected recipient, amount and SPL token
await validateTransfer(
connection,
signatureInfo.signature,
{
recipient: MERCHANT_WALLET,//await getAssociatedTokenAddress(splToken, MERCHANT_WALLET, true,),
amount,
splToken: splToken,
reference,
},
{ commitment: 'confirmed' }
).then(async (value) => {
try {
await db.rsvp.create({ wallet: await pending.rows[i].pubkey, checkoutId: await pending.rows[i].checkoutId, fishtank: await pending.rows[i].fishtank, date: await pending.rows[i].date, period: await pending.rows[i].period })
await db.checkout.destroy({ where: { checkoutId: await pending.rows[i].checkoutId } });
await fs.rm(__dirname + '/files/qrcodes/' + await pending.rows[i].checkoutId + '.png')
//email: pending.rows[i].email,
} catch (error) {
console.log(error)
}
}, (reason) => {
console.log(reason)
}
)
} catch (e) {
if (e instanceof FindReferenceError) {
return
}
if (e instanceof ValidateTransferError) {
// Transaction is invalid
console.log('Transaction is invalid', e)
return;
}
console.log(e)
}
let now = Date.now()
if (now - new Date(await pending.rows[i].createdAt).getTime() >= time) {
await db.checkout.destroy({ where: { checkoutId: await pending.rows[i].checkoutId } });
console.log(await pending.rows[i].checkoutId + ' Deleted');
await fs.rm(__dirname + '/files/qrcodes/' + await pending.rows[i].checkoutId + '.png')
};
})
} catch (err) {
console.log(err)
}
setTimeout(arguments.callee, 10000)
}
// alter: true
//{force: true}
db.sequelize.sync().then(() => {
app.listen(config.port, () => {
console.log(main_url)
checkstatus()
console.log('Started!')
})
})