Skip to content

Instantly share code, notes, and snippets.

@rusintez
Last active June 28, 2024 08:17
Show Gist options
  • Save rusintez/c583db7577fc258570dfa2dbdb73be7e to your computer and use it in GitHub Desktop.
Save rusintez/c583db7577fc258570dfa2dbdb73be7e to your computer and use it in GitHub Desktop.

Create Solana Action

  • Wrapper for solana actions / solana pay
  • Uses Hono (setup to be deployed to cloudflare)

Installation

npm install gist:c583db7577fc258570dfa2dbdb73be7e

Usage

import { createAction, createTx } from "createaction-ts";

export default createAction({
  title: "Support",
  description: "Support",
  icon: "https://img.icons8.com/?size=48&id=VA1My7dhyBQU&format=png",
  label: "Donate 1 lamport"
}, async (account) => ({
  transaction: await createTx(new PublicKey(account), [
    SystemProgram.transfer({
      fromPubkey: new PublicKey(account),
      toPubkey: new PublicKey("9ySNVUz6cGrdeAYFSeuKSwL5b48UJCbDyyufexDq1Fu"),
      lamports: 1,
    }),
  ]),
}));
import {
ActionsSpecGetResponse,
ActionsSpecPostResponse,
} from "solanaactions-ts";
import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { strictObject, string } from "zod";
import { cors } from "hono/cors";
export const createAction = (
params: ActionsSpecGetResponse,
handle: (account: string) => Promise<ActionsSpecPostResponse>
) =>
new Hono()
.use("/*", cors())
.get("/", ({ json }) => json(params))
.post(
"/",
zValidator("json", strictObject({ account: string() })),
({ json, req }) =>
handle(req.valid("json").account)
.then((res) => json(res))
.catch((err) => json(err, 500))
);
const toBase64 = (bytes: Uint8Array) =>
btoa(Array.from(bytes, (c) => String.fromCharCode(c)).join(""));
export const createTx = async (
payerKey: PublicKey,
instructions: TransactionInstruction[]
) =>
toBase64(
new VersionedTransaction(
new TransactionMessage({
payerKey,
recentBlockhash: await new Connection(clusterApiUrl("mainnet-beta"))
.getLatestBlockhash({ commitment: "max" })
.then((res) => res.blockhash),
instructions,
}).compileToV0Message()
).serialize()
);
{
"version": "0.0.1",
"name": "createaction-ts",
"main": "createaction.ts",
"dependencies": {
"@hono/zod-validator": "^0.2.2",
"@solana/web3.js": "^1.93.2",
"hono": "^4.4.9",
"solanaactions-ts": "gist:f6012a42dc0a360d2f999f7a647dc83c",
"zod": "^3.23.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment