Integrating Base Paymaster for Gasless Transactions in a Wagmi Project
This guide covers the steps to add Base Paymaster support for gasless transactions in an existing Wagmi project. It focuses on configuring wagmi.ts, adding Base-specific information, configuring the Coinbase Developer Platform (CDP), and implementing Wagmi’s experimental hooks for onchain actions.
Select the chain, Base or Base Sepolia, you’d like to sponsor transactions on. Then copy the RPC URL in the Paymaster & Bundler endpoint section.
Security
This guide uses environment variables to store the Paymaster & Bundler endpoint obtained from cdp.portal.coinbase.com. The most secure way to do this is by using a proxy. For the purposes of this guide, the endpoint is hardcoded into our project file. For production, we highly recommend using a proxy service.
Add this key to your .env file as NEXT_PUBLIC_CDP_PAYMASTER or set up a proxy service for production applications.
In the Contract allowlist section, add the smart contract addresses you want to interact with using the Base Paymaster. Give each contract a name and be sure to include the specific functions as well as the contract address. Then click Add.
WalletConnect Project ID
Base Wallet (FKA Coinbase Smart Wallet) requires a WalletConnect project ID to work. If you don’t have one, please obtain one (free) from their website.
For the onchain actions (minting, etc.) of your application, use Wagmi’s experimental hooks to manage wallet connection, check for paymaster capabilities, and execute onchain actions with you whitelisted contracts.
useCapabilities: Retrieves the capabilities supported by the connected wallet, such as paymasterService for gasless transactions.
useWriteContracts: Executes onchain write actions, here used to call the mintTo function on the NFT contract.
Here’s an example for a onchain action to mint an NFT: