I found some examples of SOL signTransaction on React from GitHub, but I couldn’t find an example of SPL token signTransaction. Could someone please provide an example in this case?
Hi Rayaan, thanks for your question!
Summary of your request:
- You are using the Web3Auth Plug n Play SDK (React).
- You found examples for SOL signTransaction, but you’re looking for a concrete example of signing an SPL token transaction (e.g., transfer of an SPL token) in React.
Guidance
While the Web3Auth React Solana Quick Start Example covers native SOL transactions, signing SPL token transactions requires constructing a different transaction instruction.
Here’s a high-level example for SPL Token transfer in React (using @solana/web3.js and @solana/spl-token):
import { Connection, PublicKey, Transaction } from "@solana/web3.js";
import { getAssociatedTokenAddress, createTransferInstruction } from "@solana/spl-token";
// Assume you already have the user's privateKey from Web3Auth
const connection = new Connection("https://api.devnet.solana.com");
const fromWallet = ... // user's Keypair from Web3Auth
const toPublicKey = new PublicKey("recipient-public-key");
const mint = new PublicKey("spl-token-mint-address");
const amount = 1; // Number of tokens to send
// Get associated token addresses
const fromTokenAccount = await getAssociatedTokenAddress(mint, fromWallet.publicKey);
const toTokenAccount = await getAssociatedTokenAddress(mint, toPublicKey);
// Create transfer instruction
const transferIx = createTransferInstruction(
fromTokenAccount,
toTokenAccount,
fromWallet.publicKey,
amount
);
// Create and sign transaction
const transaction = new Transaction().add(transferIx);
transaction.feePayer = fromWallet.publicKey;
transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
transaction.sign(fromWallet);
// Send transaction
const txid = await connection.sendRawTransaction(transaction.serialize());
console.log("SPL Token transfer TxID:", txid);
- Replace
fromWallet
with the Keypair derived from the private key you get from Web3Auth. - Replace
mint
with your SPL token’s mint address. - Replace
toPublicKey
with the recipient’s wallet address.
References:
Let me know if you need a more complete code sample, or if you’re running into any issues with this approach! If you’re using a different framework or SDK version, please specify, and I’ll tailor the example further.
Note: This response was generated by an AI bot and may require further verification. The Web3Auth support team has tracked this issue and will be responding as soon as possible.
Additionally, we would recommend you to join our biweekly Office Hours to get this resolved even faster. Web3Auth Developer Office Hours