Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7950

I need to implement onchain function into post API for blinks

$
0
0
    import {  ActionPostResponse,  ACTIONS_CORS_HEADERS,  createPostResponse,  ActionGetResponse,  ActionPostRequest as BaseActionPostRequest,} from "@solana/actions";import {  clusterApiUrl,  Connection,  PublicKey,  Transaction,  TransactionInstruction,} from "@solana/web3.js";import fetch from "node-fetch";import BN from "bn.js";import type { NextApiRequest, NextApiResponse } from "next";import * as anchor from "@project-serum/anchor";import * as idl from "../../../../public/idl.json";const connection = new anchor.web3.Connection(  process.env.SOLANA_RPC! || anchor.web3.clusterApiUrl("devnet"),"confirmed");const programId = new PublicKey("AAkrLC6jXnorRDGFHAzEkoWjt1ZPaZ3wHx7Y8dnxn7Bt");const keypair = // const provider = new anchor.AnchorProvider(connection, new anchor.Wallet(), {//   preflightCommitment: "confirmed",// });// anchor.setProvider(provider);const program = new anchor.Program(idl as anchor.Idl, programId, provider);interface ActionPostRequest extends BaseActionPostRequest {  submission_id: number;}const postHandler = async (req: NextApiRequest, res: NextApiResponse) => {  try {    const body: ActionPostRequest = req.body;    console.log(body);    console.log(req.query);    let submission_id = body.submission_id || req.query.submission_id;   console.log(typeof(submission_id));    let account: PublicKey;    try {      account = new PublicKey(body.account);    } catch (err) {      return res.status(400).json({ error: 'Invalid "account" provided' });    }    const transaction = new Transaction();    // const instruction = await program.methods    //   .submitVote(new BN(submission_id))    //   .accounts({    //     user: account,    //   })    //   .instruction();    // transaction.add(instruction);    transaction.feePayer = account;    transaction.recentBlockhash = (      await connection.getLatestBlockhash()    ).blockhash;    // const payload: ActionPostResponse = await createPostResponse({    //   fields: {    //     transaction,    //     message: `Submit a vote for submission ${submission_id}`,    //   },    // });    // res.status(200).json();  } catch (err) {    console.error(err);    let message = "An unknown error occurred";    if (typeof err === "string") message = err;    res.status(400).json({ error: message });  }};export default async (req: NextApiRequest, res: NextApiResponse) => {  if (req.method === "GET") {    await getHandler(req, res);  } else if (req.method === "POST") {    await postHandler(req, res);  } else {    res.setHeader("Allow", ["GET", "POST"]);    res.status(405).end(`Method ${req.method} Not Allowed`);  }};

I an trying to implement a blink where i have to call the below function I need to call submit Vote and the thing i need to take care is that the fee should be paid by user who is initiating the transaction so in that case how do i implement that function in the post request that's where i need help in

 use anchor_lang::prelude::*;use solana_program::entrypoint::ProgramResult;declare_id!("AAkrLC6jXnorRDGFHAzEkoWjt1ZPaZ3wHx7Y8dnxn7Bt");#[program]pub mod voting_blinks {    use super::*;    pub fn submit_vote(ctx: Context<SubmitVote>, submission_id: u64) -> ProgramResult {        // Emit an event        emit!(VoteSubmitted {            submission_id: submission_id,            user_wallet: *ctx.accounts.user.key,        });        Ok(())    }}#[derive(Accounts)]pub struct SubmitVote<'info> {    #[account(mut)]    pub user: Signer<'info>,}#[event]pub struct VoteSubmitted {    pub submission_id: u64,    pub user_wallet: Pubkey,}

Viewing all articles
Browse latest Browse all 7950

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>