Build a Sponsored Transaction Component
Overview
Gasless transactions are the future of user onboarding. When users interact with your app for the first time, they shouldn’t need to pay gas just to get started. By using a Paymaster with Smart Wallets on Base, you can sponsor gas fees for onchain interactions—creating a frictionless, seamless experience for your users. In this guide, you’ll learn how to build a reusable component that makes sponsored transactions possible, from allowlisting your contract to wiring up the frontend with Wagmi, Viem, and OnchainKit.
Prerequisites
Before you get started, make sure you have the following ready:
- A deployed contract (on Base Mainnet or Base Sepolia)
- Viem v2.23.12
- Wagmi v2.14.15
- A Coinbase Developer Platform (CDP) account
- OnchainKit
Please use the specified versions of Wagmi and Viem for full compatibility with Base Wallet.
Allowlist Your Contract
To sponsor gas for your application, you first need to allowlist the contract and function you want to support.
- Go to https://portal.cdp.coinbase.com/
- Navigate to Onchain Tools > Paymaster in the left-hand sidebar.
- Under the Configuration tab, click Enable Paymaster, then select Add.
- Input your contract address. For this example, we’ll use:
- Specify the function signature:
startGame(uint256)
- Click Save to finalize the configuration.
Set Up Your Constants File
Let’s set up a file to store important contract data.
Create a file at app/utils/constants.ts
:
This file holds your contract address, entry fee, and the Paymaster URL that enables gas sponsorship.
Add the Contract ABI
We’ll need the contract ABI to encode the correct function calls.
- Create a new directory:
app/utils/abis
- Add a file called
GuessGameABI.ts
inside it. - Paste the ABI you retrieved from a block explorer like Basescan or Blockscout.
You can find verified contract ABIs on Basescan. Navigate to the contract address, go to the Contract tab, and copy the ABI.
Build the Sponsored Transaction Component
Let’s build a reusable PlayButton
component that triggers a sponsored transaction using Wagmi + Viem.
Create a new file: app/components/PlayButton.tsx
Import the necessary modules:
Set up your state and wallet connection:
Now implement the transaction logic using Wagmi’s sendCalls
:
Render the button UI:
You’re Ready!
You’ve now created a gasless transaction component that connects to your smart contract, handles wallet logic, and leverages the Coinbase Paymaster to sponsor transactions.
Use this setup as a foundation for other gasless interactions in your dapp. Adjust the contract address, ABI, and function calls to match your specific needs.
Next step: Add logic to display user progress or game state once a transaction is confirmed.
Happy building! 🚀