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 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 Providers
To 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.
twoslash [utils.ts]
Copy
Ask AI
// @errors: 2305// @noErrorsimport { 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 function willSponsor({ 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 const code = await client.getBytecode({ address: userOp.sender }); if (code != coinbaseSmartWalletProxyBytecode) return false; // check that userOp.sender proxies to expected implementation const implementation = await client.request<{ Parameters: [Address, Hex, BlockTag]; ReturnType: Hex; }>({ method: "eth_getStorageAt", params: [userOp.sender, erc1967ProxyImplementationSlot, "latest"], }); const implementationAddress = decodeAbiParameters( [{ type: "address" }], implementation, )[0]; if (implementationAddress != coinbaseSmartWalletV1Implementation) return false; // check that userOp.callData is making a call we want to sponsor const calldata = 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; const calls = 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; let callToCheckIndex = 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; const innerCalldata = 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; }}
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.