Create your own BLINK [Solana Tutorial]

date
Jul 9, 2024
slug
create-solana-blink
status
Published
tags
Solana
NodeJs
summary
Tính năng rất hay của Solana để có thể sử dụng ở tất cả các nơi trên X mà không cần kết nối nhiều
type
Post
const port = 3000; import express from "express"; import bodyParser from "body-parser"; import cors from "cors"; import { Connection, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, clusterApiUrl, } from "@solana/web3.js"; const app = express(); const corsOptions = { origin: "*", credentials: true, //access-control-allow-credentials:true optionSuccessStatus: 200, }; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cors(corsOptions)); app.get("/api/donate", (req, res) => { const response = { icon: "https://cdn-icons-png.flaticon.com/128/10880/10880476.png", description: "Donation", title: "Please donate me", label: "Donate me", error: "Blink don't not work", links: { actions: [ { label: "Donald Trump (1.57x) - 0.0001 SOL", href: "http://localhost:3000/api/donate?amount=0.0001", }, { label: "Joe Biden (3.75x) - 0.0002 SOL", href: "http://localhost:3000/api/donate?amount=0.0002", }, { label: "Kamala Harris (8x) - 0.0003 SOL", href: "http://localhost:3000/api/donate?amount=0.0003", }, { label: "Michelle Obama (17x) - 0.0004 SOL", href: "http://localhost:3000/api/donate?amount=0.0004", }, { label: "Robert F. Kennedy Jr. (36x) - 0.0005 SOL", href: "http://localhost:3000/api/donate?amount=0.0005", }, ], }, }; res.json(response); }); app.post("/api/donate", async (req, res) => { const connection = new Connection( "", ); const receiptAddress = ""; const address = req.body.account; const amount = req.query.amount || 0.0001; const ixs = SystemProgram.transfer({ fromPubkey: new PublicKey(address), toPubkey: new PublicKey(receiptAddress), lamports: amount * LAMPORTS_PER_SOL, }); const tx = new Transaction(); tx.add(ixs); tx.feePayer = new PublicKey(address); tx.recentBlockhash = ( await connection.getLatestBlockhash({ commitment: "finalized" }) ).blockhash; const serializeTrx = tx .serialize({ requireAllSignatures: false }) .toString("base64"); const response = { transaction: serializeTrx, message: address, }; res.json(response); }); app.options("/api/donate", async (req, res) => { const response = { transaction: "", message: "Options ", }; res.json(response); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });