useWriteContracts
and useCapabilities
hooks used below rely on new wallet RPC and are not yet supported in most wallets.
It is recommended to have a fallback function if your app supports wallets other than Smart Wallet.
Using Wagmi/Viem in a Next.js app
Choose a paymaster service provider
As a prerequisite, you’ll need to obtain a paymaster service URL from a paymaster service provider. We recommend the Coinbase Developer Platform paymaster, currently offering up to $15k in gas credits as part of the Base Gasless Campaign. Once you have signed up for Coinbase Developer Platform, you get your Paymaster service URL by navigating to Onchain Tools > Paymaster as shown below:
Validate UserOperation
The policies on many paymaster services are quite simple and limited. As your API will be exposed on the web, you want to make sure in cannot abused: called to sponsor transaction you do not want to fund. The checks below are a bit tedious, but highly recommended to be safe. See “Trust and Validation” here for more on this. The goal of this section is to write awillSponsor
function to add some extra validation if needed.
[Simplifying willSponsor with Allowlisting]
willSponsor
can be simplified or removed entirely if your paymaster service supports allowlisting which
contracts and function calls should be sponsored. Coinbase Developer Platform supports this.twoslash [utils.ts]
twoslash [constants.ts]
twoslash [myNFT.ts]
twoslash [config.ts]
Protect Your Paymaster Service URLAs you can see in the Paymaster transaction component,
we use a proxy to protect the paymaster service URL, because it is exposed on the client side.
paymasterServiceUrl
in the TransactWithPaymaster
component.
Send EIP-5792 requests with a paymaster service capability
Once you have your paymaster service set up, you can now pass its URL along to Wagmi’suseWriteContracts
hook.
Using Your Proxy URLIf you set up a proxy in your app’s backend as recommended in step (2) above, you’ll want to pass in the proxy URL you created.
twoslash [page.tsx]
twoslash [myNFT.ts]
TransactWithPaymaster
component that uses the useWriteContracts
hook to send a transaction with a paymaster.
The TransactWithPaymaster
component is used in the page.tsx
file.
That’s it! Smart Wallet will handle the rest. If your paymaster service is able to sponsor the transaction,
in the UI Smart Wallet will indicate to your user that the transaction is sponsored.