aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/launch/purchase/+page.server.js
blob: ff5efc8449beb0355d75682be0eb9fcb1e575811 (plain) (blame)
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
import { auth, get, post } from '$lib/utils';
import tickets from '$lib/tickets';
import { redirect } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { invalidateAll } from '$app/navigation';
import { browser } from '$app/environment';

export const load = async ({ parent }) => {
	let tickets = await get('/tickets');
	return { ...(await parent()), tickets };
};

export const actions = {
	internal: async ({ cookies, request }) => {
		let { ticket } = await get('/ticket');

		let { asset } = await post(
			'/assets',
			{
				name: `Launch Party Ticket ${ticket}`,
				ticker: 'LPT',
				precision: 0,
				asset_amount: '1',
				token_amount: 0,
				domain: 'coinos.io',
				filename: tickets[ticket]
			},
			auth(cookies)
		);

		await post('/ticket', { asset }, { authorization: `Bearer ${env.LAUNCH}` });

		throw redirect(307, `/launch/thanks/${asset}`);
	},

	lightning: async ({ cookies, request }) => {
		let form = await request.formData();

		let rates = await get('/rates');

		let invoice = {
			amount: 25000,
			network: 'lightning',
			prompt: false,
			rate: rates[form.get('currency')]
		};

		let user = { username: form.get('username') };

		let { uuid } = await post('/invoice', { invoice, user }, auth(cookies));

		throw redirect(307, `/invoice/${uuid}`);
	},
	bitcoin: () => console.log('YYEAH')
};

export async function POST({ request }) {
	let { address, amount, confirmed } = await request.json();
	if (!confirmed) return;

	if (request.headers.get('x-webhook') !== env.WEBHOOK) {
		throw new Error('access denied');
	}

	let username = invoices[address];
	amount = Math.round(amount / 2100);

	await post('/send', { username, asset, amount }, { authorization: `Bearer ${token}` });
}