With embedded wallets, your app can sponsor gas fees for transactions on Solana, allowing users to transact without a SOL balance.
This is done by configuring the feePayer property of the sponsored transaction to be a fee payer wallet that your app manages to pay users’ gas fees.
To start, create a fee payer wallet in your backend to sponsor transactions sent by users. You can either:
Generate a new keypair directly:
Copy
Ask AI
import {Keypair} from '@solana/web3.js';import bs58 from 'bs58';// Generate a new keypairconst feePayerWallet = new Keypair();const feePayerAddress = feePayerWallet.publicKey.toBase58();const feePayerPrivateKey = bs58.encode(feePayerWallet.secretKey);// Make sure to store the private key securely; it should never leave your serverconsole.log('Fee Payer Address:', feePayerAddress);console.log('Fee Payer Private Key:', feePayerPrivateKey);
When implementing transaction sponsorship, be mindful of these security considerations:
Verify Transaction Contents
Always verify the transaction contents in your backend before signing with the fee payer. Ensure
there are no unauthorized fund transfers.
Rate Limiting
Implement rate limiting to prevent abuse of your sponsorship service. Consider limits per user,
per session, or per wallet.
Amount Validation
Validate the transaction amount if applicable. Consider setting maximum sponsorship amounts to
prevent excessive spending.
Program ID Whitelisting
Only sponsor transactions for specific whitelisted program IDs that your app interacts with to
prevent abuse.
Be extremely careful with your fee payer wallet’s private key. Never expose it in client-side code
or store it in unsecured environments. Consider using environment variables, secret management
services, or HSMs to securely store private keys in production.