1

What i am trying to do this create a transcation on backend( which have pk for my wallet ) set frontend wallet as fee payer sign that transcation and send it to frontend and only if end user signs the wallet , confirm the tx and send to blockchain, This is what i have tried so far:

  const walletToSendSol = new PublicKey(wallet);
const transferTransaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: payer.publicKey,
    toPubkey:walletToSendSol ,
    lamports: LAMPORTS_PER_SOL / 80,
  })
);
transferTransaction.recentBlockhash = (
  await connection.getLatestBlockhash()
).blockhash;
transferTransaction.feePayer = walletToSendSol;
transferTransaction.sign(payer);
return new NextResponse(
  JSON.stringify({
    tx: base58.encode(
      transferTransaction.serialize({
        requireAllSignatures: false,
      })
    ),
  })
);

End user:

 const fetchTx = await fetch(`/api/getTx?wallet=${publicKey}`);
const { tx } = await fetchTx.json();
const decodedTx = base58.decode(tx);
const transcation = Transaction.from(decodedTx);
transcation.feePayer = publicKey;
const signature = await signTransaction(transcation);

but once click approve on phantom wallet nothing happens

1 Answer 1

2

It doesn't appear that you're sending the transaction, just signing it. You can use sendTransaction(transaction, connection) from the useWallet() hook in wallet adapter to handle the flow of signing and sending with a user's wallet.

After sendTransaction() is called, it's good practice to run a confirmation to ensure it landed. You can do this with the following:

const signature = await sendTransaction(transaction, connection);

await connection.confirmTransaction(signature, "confirmed");

// can also do this
const block = await connection.getLatestBlockhash("confirmed");
const result = await connection.confirmTransaction(
  {
    signature,
    ...block,
  },
  "confirmed",
);

const error = result.value.err;
if (error) {
   throw Error(error.toString());
}
1
  • yea seems like that i was missing i thought transcation are automatically sent when i click approve on my wallet , thanks for clearing my doubt
    – Rowan
    Commented May 19 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.