Gasless Transactions on Solana
Introduction
In this guide, we’ll show you how to create Solana transactions where a fee payer wallet pays the gas fees instead of your users. We achieve this by partially signing the transaction on the server and sending it to the client for final signing.
Getting Started
Setting up the Project
We’ll use Next.js for this example since we can then keep the frontend and the API route together. To get started, create a new project with:
If you already have a Next.js app, simply follow our quickstart guide to add the Dynamic SDK.
Setting Up the Fee Payer Wallet
You’ll need a wallet that will pay for gas fees on behalf of your users:
- Create a Solana wallet and add some funds to it (for paying gas fees)
- Add its private key (string format) to your
.env.local
file:
Never share your private key or commit it to your code repository. Always use environment variables and add them to your .gitignore
.
Server Implementation
Creating the API Route
Now we’ll create an API route that will prepare partially-signed transactions. Create a file at app/api/gas/route.ts
:
How It Works
Our API route:
- Receives details about the transfer (who’s sending, receiving, and how much)
- Creates a transaction with the instructions
- Sets our server wallet as the fee payer
- Partially signs the transaction with the fee payer wallet
- Returns the transaction to the frontend
Client Implementation
Creating the Frontend Component
Now let’s create a simple UI for users to send tokens without paying gas. Create app/components/Send.tsx
:
For styling, create the Send.css file and you can find the styles we used here.
Using the Component
Finally, add the component to your main page in app/page.tsx
:
Conclusion
Congratulations! You’ve successfully implemented gasless transactions on Solana using Dynamic’s SDK. This approach creates a superior user experience by allowing your users to perform transactions without worrying about gas fees.
Next Steps
Consider implementing rate limiting or additional verification to protect your fee payer wallet from abuse.
To get the complete source code, check out our GitHub repository.