-
Notifications
You must be signed in to change notification settings - Fork 23
/
route.ts
183 lines (162 loc) · 4.78 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
ActionPostResponse,
ACTIONS_CORS_HEADERS,
createPostResponse,
ActionGetResponse,
ActionPostRequest,
} from "@solana/actions";
import {
clusterApiUrl,
Connection,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import {
DEFAULT_SOL_ADDRESS,
DEFAULT_SOL_AMOUNT,
DEFAULT_RPC,
DEFAULT_TITLE,
DEFAULT_AVATOR,
DEFAULT_DESCRIPTION,
} from "./const";
export const GET = async (req: Request) => {
try {
const requestUrl = new URL(req.url);
const { toPubkey, amount } = validatedQueryParams(requestUrl);
const baseHref = new URL(
`/api/actions/donate?to=${toPubkey.toBase58()}`,
requestUrl.origin
).toString();
const payload: ActionGetResponse = {
title: DEFAULT_TITLE,
icon:
DEFAULT_AVATOR ?? new URL("/avator.jpg", requestUrl.origin).toString(),
description: DEFAULT_DESCRIPTION,
label: "Transfer", // this value will be ignored since `links.actions` exists
links: {
actions: [
{
label: `Send ${amount} SOL`, // button text
href: `${baseHref}&amount=${amount}`,
},
{
label: `Send ${amount * 5} SOL`, // button text
href: `${baseHref}&amount=${amount * 5}`,
},
{
label: `Send ${amount * 10} SOL`, // button text
href: `${baseHref}&amount=${amount * 10}`,
},
{
label: "Send SOL", // button text
href: `${baseHref}&amount={amount}`, // this href will have a text input
parameters: [
{
name: "amount", // parameter name in the `href` above
label: "Enter the amount of SOL to send", // placeholder of the text input
required: true,
},
],
},
],
},
};
return Response.json(payload, {
headers: ACTIONS_CORS_HEADERS,
});
} catch (err) {
console.log(err);
let message = "An unknown error occurred";
if (typeof err == "string") message = err;
return new Response(message, {
status: 400,
headers: ACTIONS_CORS_HEADERS,
});
}
};
// DO NOT FORGET TO INCLUDE THE `OPTIONS` HTTP METHOD
// THIS WILL ENSURE CORS WORKS FOR BLINKS
export const OPTIONS = GET;
export const POST = async (req: Request) => {
try {
const requestUrl = new URL(req.url);
const { amount, toPubkey } = validatedQueryParams(requestUrl);
const body: ActionPostRequest = await req.json();
// validate the client provided input
let account: PublicKey;
try {
account = new PublicKey(body.account);
} catch (err) {
return new Response('Invalid "account" provided', {
status: 400,
headers: ACTIONS_CORS_HEADERS,
});
}
const connection = new Connection(DEFAULT_RPC);
// ensure the receiving account will be rent exempt
const minimumBalance = await connection.getMinimumBalanceForRentExemption(
0 // note: simple accounts that just store native SOL have `0` bytes of data
);
if (amount * LAMPORTS_PER_SOL < minimumBalance) {
throw `account may not be rent exempt: ${toPubkey.toBase58()}`;
}
const transaction = new Transaction();
transaction.feePayer = account;
transaction.add(
SystemProgram.transfer({
fromPubkey: account,
toPubkey: toPubkey,
lamports: amount * LAMPORTS_PER_SOL,
})
);
// set the end user as the fee payer
transaction.feePayer = account;
transaction.recentBlockhash = (
await connection.getLatestBlockhash()
).blockhash;
const payload: ActionPostResponse = await createPostResponse({
fields: {
transaction,
message: `Send ${amount} SOL to ${toPubkey.toBase58()}`,
},
// note: no additional signers are needed
// signers: [],
});
return Response.json(payload, {
headers: ACTIONS_CORS_HEADERS,
});
} catch (err) {
console.log(err);
let message = "An unknown error occurred";
if (typeof err == "string") message = err;
return new Response(message, {
status: 400,
headers: ACTIONS_CORS_HEADERS,
});
}
};
function validatedQueryParams(requestUrl: URL) {
let toPubkey: PublicKey = DEFAULT_SOL_ADDRESS;
let amount: number = DEFAULT_SOL_AMOUNT;
try {
if (requestUrl.searchParams.get("to")) {
toPubkey = new PublicKey(requestUrl.searchParams.get("to")!);
}
} catch (err) {
throw "Invalid input query parameter: to";
}
try {
if (requestUrl.searchParams.get("amount")) {
amount = parseFloat(requestUrl.searchParams.get("amount")!);
}
if (amount <= 0) throw "amount is too small";
} catch (err) {
throw "Invalid input query parameter: amount";
}
return {
amount,
toPubkey,
};
}