How to sponsor gas for a User Operation
Gas fees are a significant barrier to entry for new user of your app. With Account Kit you can remove this barrier by sponsoring gas fees for transactions via the Gas Manager. This guide explains how to sponsor gas by creating a gas policy, linking it to your client, and sending sponsored UserOperations
(UOs) from a smart account.
After setting up Account Kit in your project, follow these steps to sponsor gas.
Create a Gas Manager policy
A gas manager policy is a set of rules that define which UOs are eligible for gas sponsorship. You can control which operations are eligible for sponsorship by defining rules:
- Spending rules: limit the amount of money or the number of user ops that can be sponsored by this policy
- Allowlist: restrict wallet addresses that are eligible for sponsorship. The policy will only sponsor gas for UOs that were sent by addresses on this list.
- Blocklist: ban certain addresses from receiving sponsorship under this policy
- Policy duration: define the duration of your policy and the sponsorship expiry period. This is the period for which the Gas Manager signature (paymaster data) will remain valid once it is generated.
To learn more about policy configuration, refer to the guide on setting up a gas manager policy.
Once you have decided on policy rules for your app, create a policy in the Gas Manager dashboard.
Now you should have a Gas policy created with a policy id you can use to sponsor gas for your users.
Set the Policy ID globally
When creating your Account Kit config, you can optionally pass in a Gas Policy ID. This will enable all UOs sent by the sendUserOperation
method to be sponsored by the policy you created.
Copy it and then replace the GAS_MANAGER_POLICY_ID
in the snippet below.
import { createConfig } from "@account-kit/core";
import { sepolia, alchemy } from "@account-kit/infra";
export const config = createConfig({
transport: alchemy({ apiKey: "ALCHEMY_API_KEY" }),
chain: sepolia,
policyId: "GAS_MANAGER_POLICY_ID",
});
Now you can follow the guide for Sending user operations to send sponsored UOs from your smart account!
Set the gas policy ID per Client
If you want more control over which UOs are sponsored, then you can set the policy ID on a specific instance of the Smart Account Client
returned by the watchSmartAccountClient
function.
import { watchSmartAccountClient } from "@account-kit/core";
import { config } from "./config";
// How you actually store this state variable
// depends on the framework you're using
let clientState;
// The watch smart account client will handle all of the possible state changes
// that can impact this client:
// - Signer status
// - Account instantiation
// - Chain changes
const clientSubscription = watchSmartAccountClient(
{
type: "LightAccount",
policyId: "GAS_MANAGER_POLICY_ID",
},
config
)((clientState_) => {
clientState = clientState_;
});
// .. do stuff with the client state
This client is now configured to sponsor gas using your gas policy. You can now send sponsored UOs from this client using the sendUserOperation
method.