Sponsoring Gas
With account abstraction, you can pay gas for users so they don't have to acquire native tokens in order to interact with your DApp.
When you sponsor gas through ZeroDev, there are two ways to pay for the gas:
-
Put down your credit card. We front the gas for your users, and then at the end of the billing cycle (once a month) we charge your credit card.
-
Buy gas credits from us.
Setting up gas sponsoring policies
To avoid over-spending gas on sponsoring, you must set up gas-sponsoring policies. Sign up on the ZeroDev dashboard if you haven't already, then set up gas policies.
Note that you MUST set up a gas policy to begin sponsoring. Without setting up a gas policy, there won't be any gas sponsored.
API
When setting up an account, you can specify a getPaymasterData
function in paymaster
when you create the account client.
The getPaymasterData
function essentially takes a UserOp and then returns a UserOp with the paymasterAndData
field set. For example, if you are using the ZeroDev paymaster, use the createZeroDevPaymasterClient
helper function:
import { http } from "viem"
import { createZeroDevPaymasterClient, createKernelAccountClient } from "@zerodev/sdk"
import { getEntryPoint } from "@zerodev/sdk/constants"
const entryPoint = getEntryPoint("0.7")
const paymasterClient = createZeroDevPaymasterClient({
chain,
// Get this RPC from ZeroDev dashboard
transport: http(PAYMASTER_RPC),
})
const kernelClient = createKernelAccountClient({
paymaster: {
getPaymasterData: (userOperation) => {
return paymasterClient.sponsorUserOperation({
userOperation,
})
}
}
// other args...
})
What happens when you reach the sponsorship limit?
If you have reached the sponsorship limit, either because of the policies you set up or because you have reached an account-level limit, sending UserOp will fail.
If, instead of failing, you want the UserOp to proceed but use the user's own native tokens (e.g. ETH), then you can set up your paymaster
middleware like this:
import { GetPaymasterDataReturnType } from "viem/account-abstraction"
const kernelClient = createKernelAccountClient({
// other args...
paymaster: {
getPaymasterData: async (userOperation) => {
try {
return await paymasterClient.sponsorUserOperation({ userOperation })
} catch (error) {
return {} as GetPaymasterDataReturnType
}
},
},
})
UltraRelay
UltraRelay is a new relay solution that functions as a combination of ERC-4337 bundlers and paymasters, as a single entity. UltraRelay is significantly more efficient than regular ERC-4337 bundlers and paymasters. In our benchmarks, UltraRelay achieves:
- 30% less gas than ERC-4337 bundlers
- 20% lower latency than ERC-4337 bundlers
This makes UltraRelay the best solution for gas sponsorship.
API
To use UltraRelay, simply update your createKernelAccountClient
config as follows:
- Append
?provider=ULTRA_RELAY
to your bundler RPC. - (optional) Do NOT set the paymaster middleware, which will save you some latency.
- (optional) Set a no-op gas estimation middleware, which will save you even more latency.
Here's how it looks like in code:
const kernelClient = createKernelAccountClient({
account,
chain,
bundlerTransport: http(BUNDLER_RPC+`?provider=ULTRA_RELAY`),
userOperation: {
estimateFeesPerGas: async ({ bundlerClient }) => {
return {
maxFeePerGas: BigInt(0),
maxPriorityFeePerGas: BigInt(0),
}
},
},
})