forked from dialectlabs/actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.ts
163 lines (154 loc) · 4.07 KB
/
route.ts
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
import {
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
VersionedTransaction,
} from '@solana/web3.js';
import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
import {
actionSpecOpenApiPostRequestBody,
actionsSpecOpenApiGetResponse,
actionsSpecOpenApiPostResponse,
} from '../openapi';
import { prepareTransaction } from '../../shared/transaction-utils';
import { ActionGetResponse, ActionPostRequest, ActionPostResponse } from '@solana/actions';
const DONATION_DESTINATION_WALLET =
'HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg';
const DONATION_AMOUNT_SOL_OPTIONS = [0.001, 5, 10];
const DEFAULT_DONATION_AMOUNT_SOL = 0.001;
const app = new OpenAPIHono();
app.openapi(
createRoute({
method: 'get',
path: '/',
tags: ['Donate'],
responses: actionsSpecOpenApiGetResponse,
}),
(c) => {
const { icon, title, description } = getDonateInfo();
const amountParameterName = 'amount';
const response: ActionGetResponse = {
icon,
label: `${DEFAULT_DONATION_AMOUNT_SOL} SOL`,
title,
description,
links: {
actions: [
...DONATION_AMOUNT_SOL_OPTIONS.map((amount) => ({
label: `${amount} SOL`,
href: `/api/donate/${amount}`,
})),
{
href: `/api/donate/{${amountParameterName}}`,
label: 'Donate',
parameters: [
{
name: amountParameterName,
label: 'Enter a custom SOL amount',
},
],
},
],
},
};
return c.json(response, 200);
},
);
app.openapi(
createRoute({
method: 'get',
path: '/{amount}',
tags: ['Donate'],
request: {
params: z.object({
amount: z.string().openapi({
param: {
name: 'amount',
in: 'path',
},
type: 'number',
example: '1',
}),
}),
},
responses: actionsSpecOpenApiGetResponse,
}),
(c) => {
const amount = c.req.param('amount');
const { icon, title, description } = getDonateInfo();
const response: ActionGetResponse = {
icon,
label: `${amount} SOL`,
title,
description,
};
return c.json(response, 200);
},
);
app.openapi(
createRoute({
method: 'post',
path: '/{amount}',
tags: ['Donate'],
request: {
params: z.object({
amount: z
.string()
.optional()
.openapi({
param: {
name: 'amount',
in: 'path',
required: false,
},
type: 'number',
example: '1',
}),
}),
body: actionSpecOpenApiPostRequestBody,
},
responses: actionsSpecOpenApiPostResponse,
}),
async (c) => {
const amount =
c.req.param('amount') ?? DEFAULT_DONATION_AMOUNT_SOL.toString();
const { account } = (await c.req.json()) as ActionPostRequest;
const parsedAmount = parseFloat(amount);
const transaction = await prepareDonateTransaction(
new PublicKey(account),
new PublicKey(DONATION_DESTINATION_WALLET),
parsedAmount * LAMPORTS_PER_SOL,
);
const response: ActionPostResponse = {
transaction: Buffer.from(transaction.serialize()).toString('base64'),
};
return c.json(response, 200);
},
);
function getDonateInfo(): Pick<
ActionGetResponse,
'icon' | 'title' | 'description'
> {
const icon =
'https://ucarecdn.com/7aa46c85-08a4-4bc7-9376-88ec48bb1f43/-/preview/880x864/-/quality/smart/-/format/auto/';
const title = 'Donate to Alice';
const description =
'Cybersecurity Enthusiast | Support my research with a donation.';
return { icon, title, description };
}
async function prepareDonateTransaction(
sender: PublicKey,
recipient: PublicKey,
lamports: number,
): Promise<VersionedTransaction> {
const payer = new PublicKey(sender);
const instructions = [
SystemProgram.transfer({
fromPubkey: payer,
toPubkey: new PublicKey(recipient),
lamports: lamports,
}),
];
return prepareTransaction(instructions, payer);
}
export default app;