Documentation Index
Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
Use this file to discover all available pages before exploring further.
This is a React-only guide.
Introduction
In this guide, we’ll show you how to create Sui transactions where a fee payer wallet pays the gas fees. We achieve this by using Sui’s sponsored transaction feature, which allows one account to pay for another account’s transaction fees.For the complete source code and working example, check out our GitHub repository.
Getting Started
Setting up the Project
We’ll use Next.js for this example so the frontend and API routes stay in one codebase. Create the project withcreate-next-app, then follow the React Quickstart Custom setup path and enable Sui. In the Dynamic dashboard, turn on Sui under Chains & Networks and add your dev origin under Security → Allowed Origins.
If you already have a Next.js app, add the Dynamic React SDK using the same quickstart.
Setting Up the Fee Payer Wallet
You’ll need a wallet that will pay for gas fees on behalf of your users:- Create a Sui wallet and add some funds to it (for paying gas fees)
- Add its private key to your
.env.localfile:
.env.local
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 sponsored transactions. Create a file atapp/api/gas/route.ts:
app/api/gas/route.ts
How It Works
Our API route:- Receives a transaction from the frontend (already built by the user’s wallet)
- Validates the fee payer private key and creates the keypair
- Fetches SUI coins from the fee payer wallet for gas payment
- Creates a sponsored transaction by setting the gas owner and payment
- Signs the transaction with the fee payer’s key
- Returns the sponsored transaction bytes and sponsor signature
Client Implementation
Creating the Frontend Component
Now let’s create a UI for users to send USDC tokens without paying gas. Createapp/components/Send.tsx:
app/components/Send.tsx
How the Gasless Transaction Works
The gasless transaction flow works as follows:- Transaction Creation: The user’s wallet creates a transaction for transferring USDC
- Transaction Building: The transaction is built with only the transaction kind (no gas payment)
- Sponsorship Request: The frontend sends the transaction to our API for sponsorship
- Gas Payment Setup: The server adds gas payment from the fee payer wallet
- Dual Signing: Both the user and the fee payer sign the transaction
- Execution: The transaction is executed on the Sui network
Simple Explanation
Think of gasless transactions like having someone else pay for your Uber ride. Here’s what happens:- You (the user) want to send USDC to someone
- Your app (the fee payer) pays the gas fees for you
- Sui network processes the transaction using your app’s gas payment
- You only pay for the USDC you’re sending, not the gas fees
Advanced Usage
Supporting Different Token Types
You can easily modify the component to support different token types by changing theUSDC_COIN_TYPE constant:
Gas Estimation
You can add gas estimation before executing transactions:Security Considerations
- Rate Limiting: Implement rate limiting to prevent abuse of your fee payer wallet
- Transaction Validation: Validate all transaction parameters on the server side
- Gas Limits: Set appropriate gas limits to prevent excessive spending
- Monitoring: Monitor your fee payer wallet balance and transaction patterns
- Private Key Security: Never expose your fee payer private key in client-side code
Testing
To test the implementation:- Make sure you have USDC tokens in your wallet on Sui testnet
- Set up your fee payer wallet with sufficient SUI for gas fees
- Connect your wallet using Dynamic
- Enter a recipient address and amount
- Execute the transaction
Conclusion
Congratulations! You’ve successfully implemented gasless transactions on Sui 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 to protect your fee payer wallet
- Transaction validation and approval workflows
- Gas cost monitoring and alerts
- Support for different Sui networks (mainnet, testnet, devnet)
- Support for other token types and complex transactions