Sponsor gas

Gas fees are a significant barrier to entry for new users of your app. With Smart Wallets you can remove this barrier by sponsoring gas fees for transactions via the Gas Manager.

How it works

When a user requests gas sponsorship using a configured policy, the policy engine will determine if that transaction is eligible for sponsorship. If eligible, when the user sends the transaction the Gas Manager will pay for the gas fee upfront. The Gas Manager will make a note of the sponsored cost and bill the sponsoring developer in fiat.

Prerequisites

Implementation

Required SDK version: ^v4.61.0

Use the useSendCalls hook to sponsor gas for a transaction.

The gas sponsorship policy ID can be configured:

  1. In the AlchemyAccountProvider component configuration. Used by default if configured.
  2. In the paymasterService capabilities policyId parameter. Overrides the component configuration.

Prerequisites

See the useSendCalls SDK reference for parameter descriptions.

1import { useSendCalls, useSmartAccountClient } from "@account-kit/react";
2import { config } from "./config.ts";
3
4export default function SendCalls() {
5 const { client } = useSmartAccountClient({});
6 const { sendCallsAsync } = useSendCalls({ client });
7
8 const handleSend = async () => {
9 if (!client) {
10 throw new Error("Smart account client not connected");
11 }
12
13 try {
14 const { ids } = await sendCallsAsync({
15 // OPTIONAL: configure here to override a policyId set
16 // in the `AlchemyAccountProvider` configuration.
17 capabilities: {
18 paymasterService: {
19 policyId: config.policyId,
20 },
21 },
22 calls: [
23 {
24 to: "0x0000000000000000000000000000000000000000",
25 value: "0x00",
26 data: "0x",
27 },
28 ],
29 });
30
31 console.log("Transaction sent with ID:", ids[0]);
32 } catch (error) {
33 console.error(error);
34 }
35 };
36
37 return <button onClick={handleSend}>Click to Send</button>;
38}

Advanced

Gas sponsorship also works with the prepare calls methods in the various frameworks. Usage of the capability will be the same as when using send calls. It is recommended to use prepare calls if you want to inspect the prepared call prior to prompting the user for signature.

Developers can configure multiple policy IDs for use in gas sponsorship. The backend will choose the first policy ID that where the transaction is eligible for sponsorship. This is done by passing an array of policy IDs instead of a single policy ID to the sponsor gas capability. See the wallet_prepareCalls API parameters for reference to the paymasterService.policyIds parameter.

Next steps

Build more:

Troubleshooting: