Sponsor gas fees for Solana transactions allowing users to transact without SOL balance
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.
Sponsoring transactions on Solana involves the following steps:
1
Set up a fee payer wallet
Create a fee payer wallet in your backend to pay for users’ gas fees.
2
Prepare and sign the transaction
Prepare a transaction with a custom fee payer, sign it with the user’s wallet, and send it to your
backend.
3
Verify and complete the transaction
Verify the transaction, sign it with the fee payer wallet, and broadcast it
to the network.
To prepare transactions with a fee payer, we recommend using the
@solana/web3.js library.
On Solana, rent refunds from Associated Token Account (ATA) closures go to the account owner
rather than the fee payer. This can be exploited to drain gas sponsorship funds through repeated
transactions opening and closing ATAs. See Security best
practices for
details and mitigation strategies before enabling sponsorship.
To start, create a fee payer wallet in your backend to sponsor transactions sent by users. You can either:
Generate a new keypair directly:
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);
Or create a Solana wallet to act as your fee payer for better security and key management.
Ensure you fund this wallet with SOL to pay for transaction fees.
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.