How to conditionally sponsor a user operation
The permissionless.js library allows you to conditionally sponsor a user operation. This can be useful in situations like:
- Sponsor only first 10 transactions
- Sponsor if they have a specific NFT
The use cases can be many, but the general idea is to use the middleware.sponsorUserOperation
parameter to conditionally sponsor a user operation.
Let's take an example where you want to sponsor only the first 10 transactions.
Create the clients
First we must create the public, bundler, and (optionally) paymaster clients that will be used to interact with the SimpleAccount.
import { createPublicClient, http } from "viem"
import {
createPimlicoBundlerClient,
createPimlicoPaymasterClient,
} from "permissionless/clients/pimlico"
import { ENTRYPOINT_ADDRESS_V07 } from "permissionless"
import { decodeNonce } from "permissionless/utils"
const publicClient = createPublicClient({
transport: http("https://rpc.ankr.com/eth_sepolia"),
})
const bundlerClient = createPimlicoBundlerClient({
transport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY"),
entryPoint: ENTRYPOINT_ADDRESS_V07,
})
const paymasterClient = createPimlicoPaymasterClient({
transport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY"),
entryPoint: ENTRYPOINT_ADDRESS_V07,
})
Create an account
Now, create an account. This can any of the accounts supported by permissionless.js or custom accounts conforming to the interface. For this example, we'll use a Simple account.
import { privateKeyToSimpleSmartAccount } from "permissionless/accounts"
export const simpleSmartAccount = await privateKeyToSimpleSmartAccount(publicClient, {
privateKey: "0xPRIVATE_KEY",
entryPoint: ENTRYPOINT_ADDRESS_V07,
})
Create the smart account client with the custom paymaster logic
When creating the smartAccountClient
, we can pass in a middleware.sponsorUserOperation
function that will be called before a user operation is signed and sent.
This function can conditionally choose to add a paymaster to the user operation if the desired conditions are met.
import { sepolia } from "viem/chains"
import { createSmartAccountClient, getAccountNonce } from "permissionless"
export const smartAccountClient = createSmartAccountClient({
account: simpleSmartAccount,
entryPoint: ENTRYPOINT_ADDRESS_V07,
chain: sepolia, // or whatever chain you are using
bundlerTransport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY"),
middleware: {
gasPrice: async () => {
return (await bundlerClient.getUserOperationGasPrice()).fast // if using pimlico bundlers
},
sponsorUserOperation: async (args) => {
const transactionCount = await getAccountNonce(publicClient, {
sender: args.userOperation.sender,
entryPoint: args.entryPoint,
})
const { sequence } = decodeNonce(transactionCount)
if (sequence < 10) {
// sponsor it for the user as the sequence is less than 10
// sequence is the number of transactions sent by the user for a given key
// by default key is 0x0
return paymasterClient.sponsorUserOperation(args)
}
const gasEstimates = await bundlerClient.estimateUserOperationGas(args)
return {
...gasEstimates,
}
},
},
})