|
5 | 5 | import {
|
6 | 6 | ActionPostResponse,
|
7 | 7 | createPostResponse,
|
8 |
| - MEMO_PROGRAM_ID, |
9 | 8 | ActionGetResponse,
|
10 |
| - ActionPostRequest, |
11 | 9 | createActionHeaders,
|
12 |
| - ActionError, |
| 10 | + Action, |
| 11 | + ActionPostRequest, |
13 | 12 | } from "@solana/actions";
|
14 | 13 | import {
|
15 |
| - clusterApiUrl, |
16 |
| - ComputeBudgetProgram, |
17 |
| - Connection, |
18 | 14 | PublicKey,
|
19 |
| - Transaction, |
20 |
| - TransactionInstruction, |
| 15 | + TransactionMessage, |
| 16 | + VersionedTransaction, |
21 | 17 | } from "@solana/web3.js";
|
| 18 | +import { createActionRoutes } from "../../utils/action-handler"; |
| 19 | +import { getConnection } from "../../utils/connection"; |
| 20 | +import { MemoQuerySchema } from "./schema"; |
| 21 | + |
| 22 | +// Create memo program instruction |
| 23 | +import { createMemoInstruction } from "./instruction"; |
22 | 24 |
|
23 |
| -// create the standard headers for this route (including CORS) |
24 | 25 | const headers = createActionHeaders();
|
25 | 26 |
|
26 |
| -export const GET = async (req: Request) => { |
27 |
| - const payload: ActionGetResponse = { |
| 27 | +async function handleGet(req: Request): Promise<ActionGetResponse> { |
| 28 | + const requestUrl = new URL(req.url); |
| 29 | + const baseHref = new URL("/api/actions/memo", requestUrl.origin).toString(); |
| 30 | + |
| 31 | + return { |
28 | 32 | type: "action",
|
29 |
| - title: "Actions Example - Simple On-chain Memo", |
30 |
| - icon: new URL("/solana_devs.jpg", new URL(req.url).origin).toString(), |
31 |
| - description: "Send a message on-chain using a Memo", |
32 |
| - label: "Send Memo", |
| 33 | + title: "Actions Example - Write Memo", |
| 34 | + icon: new URL("/solana_devs.jpg", requestUrl.origin).toString(), |
| 35 | + description: "Write a message to the Solana network", |
| 36 | + label: "Write", |
| 37 | + links: { |
| 38 | + actions: [ |
| 39 | + { |
| 40 | + label: "Write Message", |
| 41 | + href: `${baseHref}?message={message}`, |
| 42 | + parameters: [ |
| 43 | + { |
| 44 | + type: "textarea", |
| 45 | + name: "message", |
| 46 | + label: "Enter your message", |
| 47 | + required: true, |
| 48 | + min: 1, |
| 49 | + max: 256, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
33 | 55 | };
|
| 56 | +} |
| 57 | + |
| 58 | +async function handlePost(req: Request): Promise<ActionPostResponse> { |
| 59 | + const requestUrl = new URL(req.url); |
| 60 | + const { message } = parseQueryParams(requestUrl); |
| 61 | + |
| 62 | + const body: ActionPostRequest = await req.json(); |
| 63 | + const account = new PublicKey(body.account); |
| 64 | + |
| 65 | + const connection = getConnection(); |
| 66 | + const { blockhash } = await connection.getLatestBlockhash(); |
34 | 67 |
|
35 |
| - return Response.json(payload, { |
36 |
| - headers, |
| 68 | + // Create the memo instruction |
| 69 | + const memoInstruction = createMemoInstruction(message, [account]); |
| 70 | + |
| 71 | + // Create a versioned transaction |
| 72 | + const messageV0 = new TransactionMessage({ |
| 73 | + payerKey: account, |
| 74 | + recentBlockhash: blockhash, |
| 75 | + instructions: [memoInstruction], |
| 76 | + }).compileToV0Message(); |
| 77 | + |
| 78 | + const transaction = new VersionedTransaction(messageV0); |
| 79 | + |
| 80 | + return createPostResponse({ |
| 81 | + fields: { |
| 82 | + transaction, |
| 83 | + message: `Write memo: "${message}"`, |
| 84 | + }, |
37 | 85 | });
|
38 |
| -}; |
39 |
| - |
40 |
| -// DO NOT FORGET TO INCLUDE THE `OPTIONS` HTTP METHOD |
41 |
| -// THIS WILL ENSURE CORS WORKS FOR BLINKS |
42 |
| -export const OPTIONS = async () => Response.json(null, { headers }); |
43 |
| - |
44 |
| -export const POST = async (req: Request) => { |
45 |
| - try { |
46 |
| - const body: ActionPostRequest = await req.json(); |
47 |
| - |
48 |
| - let account: PublicKey; |
49 |
| - try { |
50 |
| - account = new PublicKey(body.account); |
51 |
| - } catch (err) { |
52 |
| - throw 'Invalid "account" provided'; |
53 |
| - } |
54 |
| - |
55 |
| - const connection = new Connection( |
56 |
| - process.env.SOLANA_RPC! || clusterApiUrl("devnet"), |
57 |
| - ); |
58 |
| - |
59 |
| - const transaction = new Transaction().add( |
60 |
| - // note: `createPostResponse` requires at least 1 non-memo instruction |
61 |
| - ComputeBudgetProgram.setComputeUnitPrice({ |
62 |
| - microLamports: 1000, |
63 |
| - }), |
64 |
| - new TransactionInstruction({ |
65 |
| - programId: new PublicKey(MEMO_PROGRAM_ID), |
66 |
| - data: Buffer.from("this is a simple memo message2", "utf8"), |
67 |
| - keys: [], |
68 |
| - }), |
69 |
| - ); |
70 |
| - |
71 |
| - // set the end user as the fee payer |
72 |
| - transaction.feePayer = account; |
73 |
| - |
74 |
| - transaction.recentBlockhash = ( |
75 |
| - await connection.getLatestBlockhash() |
76 |
| - ).blockhash; |
77 |
| - |
78 |
| - const payload: ActionPostResponse = await createPostResponse({ |
79 |
| - fields: { |
80 |
| - transaction, |
81 |
| - message: "Post this memo on-chain", |
82 |
| - }, |
83 |
| - // no additional signers are required for this transaction |
84 |
| - // signers: [], |
85 |
| - }); |
86 |
| - |
87 |
| - return Response.json(payload, { |
88 |
| - headers, |
89 |
| - }); |
90 |
| - } catch (err) { |
91 |
| - console.log(err); |
92 |
| - let actionError: ActionError = { message: "An unknown error occurred" }; |
93 |
| - if (typeof err == "string") actionError.message = err; |
94 |
| - return Response.json(actionError, { |
95 |
| - status: 400, |
96 |
| - headers, |
97 |
| - }); |
| 86 | +} |
| 87 | + |
| 88 | +function parseQueryParams(requestUrl: URL) { |
| 89 | + const result = MemoQuerySchema.safeParse( |
| 90 | + Object.fromEntries(requestUrl.searchParams) |
| 91 | + ); |
| 92 | + |
| 93 | + if (!result.success) { |
| 94 | + throw result.error; |
98 | 95 | }
|
99 |
| -}; |
| 96 | + |
| 97 | + return result.data; |
| 98 | +} |
| 99 | + |
| 100 | +export const { GET, POST, OPTIONS } = createActionRoutes( |
| 101 | + { |
| 102 | + GET: handleGet, |
| 103 | + POST: handlePost, |
| 104 | + }, |
| 105 | + headers |
| 106 | +); |
0 commit comments