Creating a Paymaster Proxy for Secured Sponsored Transactions
One of the biggest UX enhancements unlocked by Smart Wallet is the ability for app developers to sponsor their users’ transactions. If your app supports Smart Wallet, you can start sponsoring your users’ transactions by using standardized paymaster service communication enabled by new wallet RPC methods.The code below is also in our Wagmi Smart Wallet template.About The Hooks Used BelowThe 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.
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:
Once you choose a paymaster service provider and obtain a paymaster service URL, you can proceed to integration.ERC-7677-Compliant Paymaster ProvidersTo be compatible with Smart Wallet, the paymaster provider you choose must be ERC-7677-compliant.
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 a willSponsor 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.
The code below is built specifically for Smart Wallet. It would need to be updated to support other smart accounts.
[utils.ts]
Copy
Ask AI
import { UserOperation } from "viem/account-abstraction";import { entryPoint06Address } from "viem/account-abstraction";import {Address,BlockTag,Hex,decodeAbiParameters,decodeFunctionData,} from "viem";import { baseSepolia } from "viem/chains";import { client } from "./config";import {coinbaseSmartWalletABI,coinbaseSmartWalletProxyBytecode,coinbaseSmartWalletV1Implementation,erc1967ProxyImplementationSlot,magicSpendAddress,} from "./constants";import { myNFTABI, myNFTAddress } from "@/ABIs/myNFT";// @noErrors export async functionwillSponsor({chainId,entrypoint,userOp,}: { chainId: number; entrypoint: string; userOp:UserOperation<'0.6'> }) { // check chain id if (chainId !==baseSepolia.id) return false; // check entrypoint // not strictly needed given below check on implementation address, but leaving as example if (entrypoint.toLowerCase() !==entryPoint06Address.toLowerCase()) return false; try { // check the userOp.sender is a proxy with the expected bytecode constcode = awaitclient.getBytecode({ address:userOp.sender }); if (code !=coinbaseSmartWalletProxyBytecode) return false; // check that userOp.sender proxies to expected implementation constimplementation = awaitclient.request<{Parameters: [Address, Hex, BlockTag];ReturnType:Hex; }>({method: "eth_getStorageAt",params: [userOp.sender, erc1967ProxyImplementationSlot, "latest"], }); constimplementationAddress =decodeAbiParameters( [{ type: "address" }],implementation, )[0]; if (implementationAddress !=coinbaseSmartWalletV1Implementation) return false; // check that userOp.callData is making a call we want to sponsor constcalldata =decodeFunctionData({abi:coinbaseSmartWalletABI,data:userOp.callData, }); // keys.coinbase.com always uses executeBatch if (calldata.functionName !== "executeBatch") return false; if (!calldata.args ||calldata.args.length == 0) return false; constcalls =calldata.args[0] as {target:Address;value: bigint;data:Hex; }[]; // modify if want to allow batch calls to your contract if (calls.length > 2) return false; letcallToCheckIndex = 0; if (calls.length > 1) { // if there is more than one call, check if the first is a magic spend call if (calls[0].target.toLowerCase() !==magicSpendAddress.toLowerCase()) return false;callToCheckIndex = 1; } if (calls[callToCheckIndex].target.toLowerCase() !==myNFTAddress.toLowerCase() ) return false; constinnerCalldata =decodeFunctionData({abi:myNFTABI,data:calls[callToCheckIndex].data, }); if (innerCalldata.functionName !== "safeMint") return false; return true; } catch (e) {console.error(`willSponsor check failed: ${e}`); return false; }}
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.
For local development, you can use the same URL for the paymaster service and the proxy.We also created a minimalist proxy API
which you can use as the paymasterServiceUrl in the TransactWithPaymaster component.
How to find this code in the repository?The code above is a simplified version of the code in the
template.In the template, we create a 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.