aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/launch/purchase/+page.server.js
blob: 3317b42017d499def0c86bfa05d4563f775f8d03 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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');
	let { user } = await parent();
	let hasTicket = !!user?.accounts.find((a) => tickets.includes(a.asset));
  if (hasTicket) throw redirect(307, '/launch/ticket');
	return { tickets };
};

export const actions = {
	internal: async ({ cookies, request }) => {
		let form = await request.formData();
		let username = form.get('username');

		let { address } = await post(
			'/invoice',
			{ invoice: { network: 'liquid' }, user: { username } },
			auth(cookies)
		);

		let { ticket } = await get('/ticket');

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

		await post('/send', { amount: 25000, username: 'launch' }, 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,
			memo: 'launch',
			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}` });
}