-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
263 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { | ||
ActionGetResponse, | ||
ActionPostRequest, | ||
ActionPostResponse, | ||
createActionHeaders, | ||
createPostResponse, | ||
} from "@solana/actions"; | ||
import { createActionRoutes } from "../../utils/action-handler"; | ||
import { PayMeQuerySchema } from "./schema"; | ||
import { createQueryParser } from "../../utils/validation"; | ||
import { VersionedTransaction } from "@solana/web3.js"; | ||
import { fetchBlink } from "../../utils/fetch"; | ||
import { | ||
getConnection, | ||
hydrateTransactionMessage, | ||
} from "../../utils/connection"; | ||
import { createTransferInstruction } from "@solana/spl-token"; | ||
|
||
async function handleGet(req: Request): Promise<ActionGetResponse> { | ||
const requestUrl = new URL(req.url); | ||
const baseHref = new URL("/api/actions/memo", requestUrl.origin).toString(); | ||
|
||
return { | ||
type: "action", | ||
title: "Actions Example - Pay Me", | ||
icon: new URL("/solana_devs.jpg", requestUrl.origin).toString(), | ||
description: "Append a payment instruction to a recipient", | ||
label: "Write", | ||
links: { | ||
actions: [ | ||
{ | ||
label: "Pay Me", | ||
href: `${baseHref}?mint={mint}&recipient={recipient}&amountUsd={amountUsd}&blink={blink}`, | ||
parameters: [ | ||
{ | ||
type: "text", | ||
name: "mint", | ||
label: "Mint", | ||
required: false, | ||
}, | ||
{ | ||
type: "text", | ||
name: "recipient", | ||
label: "Recipient", | ||
required: true, | ||
}, | ||
{ | ||
type: "number", | ||
name: "amountUsd", | ||
label: "Amount USD", | ||
required: true, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
const parseQueryParams = createQueryParser(PayMeQuerySchema); | ||
|
||
async function handlePost(req: Request): Promise<ActionPostResponse> { | ||
const requestUrl = new URL(req.url); | ||
const { blink, amountUsd, mint, payer, recipient } = | ||
parseQueryParams(requestUrl); | ||
|
||
const body: ActionPostRequest = await req.json(); | ||
|
||
// get a quote for the amount in USD | ||
const jupPrice: { data: { [key: string]: { price: number } } } = await ( | ||
await fetch(`https://api.jup.ag/price/v2?ids=${mint.toBase58()}`) | ||
).json(); | ||
|
||
const amountInMint = Math.ceil( | ||
jupPrice.data[mint.toBase58()].price * amountUsd, | ||
); | ||
|
||
const txResponseBody = await fetchBlink(blink, body.account); | ||
const connection = getConnection(); | ||
const tx = VersionedTransaction.deserialize( | ||
Buffer.from(txResponseBody.transaction, "base64"), | ||
); | ||
const txMessage = await hydrateTransactionMessage(tx, connection); | ||
|
||
const finalTx = new VersionedTransaction(txMessage.compileToV0Message()); | ||
|
||
return createPostResponse({ | ||
fields: { | ||
transaction: finalTx, | ||
message: `Payed ${amountUsd} in ${mint.toBase58()} to ${recipient.toBase58()}"`, | ||
}, | ||
}); | ||
} | ||
|
||
export const { GET, POST, OPTIONS } = createActionRoutes( | ||
{ | ||
GET: handleGet, | ||
POST: handlePost, | ||
}, | ||
createActionHeaders(), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { z } from "zod"; | ||
import { | ||
publicKeySchema, | ||
numberFromStringSchema, | ||
} from "../../utils/validation"; | ||
|
||
export const PayMeQuerySchema = z.object({ | ||
blink: z.string(), | ||
amountUsd: numberFromStringSchema(), | ||
mint: publicKeySchema.default( | ||
// USDC | ||
"EPjFWdd5AufqSSqeM2gEi2U9jFw27yN25g8yLWkxfV8rauYU", | ||
), | ||
payer: publicKeySchema, | ||
recipient: publicKeySchema, | ||
}); | ||
|
||
export type PayMeQuery = z.infer<typeof PayMeQuerySchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 10 additions & 4 deletions
14
examples/next-js/src/app/api/actions/transfer-sol/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { z } from "zod"; | ||
import { PublicKey } from "@solana/web3.js"; | ||
import { DEFAULT_SOL_AMOUNT } from "./const"; | ||
import { numberFromStringSchema, publicKeySchema } from "../../utils/validation"; | ||
import { | ||
blinkSchema, | ||
insertionTypeSchema, | ||
numberFromStringSchema, | ||
publicKeySchema, | ||
} from "../../utils/validation"; | ||
|
||
// Define the input type (what comes from URL params) | ||
export const TransferSolQuerySchema = z.object({ | ||
to: publicKeySchema, | ||
amount: numberFromStringSchema({ min: 0 }), | ||
blink: blinkSchema, | ||
// insertion one of "prepend", "append" | ||
insertion: insertionTypeSchema, | ||
}); | ||
|
||
// Type representing the parsed and transformed data | ||
export type TransferSolQuery = z.infer<typeof TransferSolQuerySchema>; | ||
export type TransferSolQuery = z.infer<typeof TransferSolQuerySchema>; |
Oops, something went wrong.