Skip to main content

canSponsorUserOperation

Checks if a user operation can be sponsored by the configured paymaster. Works for both single transactions and batch transactions. This is useful to determine if the user will need to pay gas fees or if the operation qualifies for gas sponsorship.

Usage

import { canSponsorUserOperation } from "@dynamic-labs-sdk/zerodev";
import { isEvmWalletAccount } from "@dynamic-labs-sdk/evm";
import { getPrimaryWalletAccount } from "@dynamic-labs-sdk/client";
import { parseEther } from "viem";

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isEvmWalletAccount(walletAccount)) {
  const canSponsor = await canSponsorUserOperation({
    walletAccount,
    calls: [
      {
        to: recipientAddress,
        value: parseEther("0.01"),
        data: "0x",
      },
    ],
  });

  if (canSponsor) {
    console.log("Operation will be sponsored - no gas fees required!");
  } else {
    console.log("Operation cannot be sponsored - user pays gas fees");
  }
}

Parameters

You must provide either a walletAccount or a kernelClient, but not both.
ParameterTypeDescription
callsBatchCall[]Array of calls to execute (single or multiple)
calls[].toHexThe recipient address for this call
calls[].valuebigintThe value to send in wei for this call
calls[].dataHex (optional)The transaction data for this call
walletAccountEvmWalletAccount (optional)The wallet account. Required if no kernelClient is provided
kernelClientKernelClient (optional)An existing kernel client. Required if no walletAccount is provided

Returns

Promise<boolean> - Returns true if the user operation can be sponsored, false otherwise.

Examples

Single transaction with wallet account

const canSponsor = await canSponsorUserOperation({
  walletAccount,
  calls: [
    {
      to: "0x...",
      value: parseEther("0.1"),
    },
  ],
});

Batch transaction

const canSponsor = await canSponsorUserOperation({
  walletAccount,
  calls: [
    { to: "0xRecipient1...", value: parseEther("0.1") },
    { to: "0xRecipient2...", value: parseEther("0.2") },
    { to: "0xRecipient3...", value: parseEther("0.3") },
  ],
});

Using existing kernel client

const kernelClient = await createKernelClientForWalletAccount({
  smartWalletAccount: walletAccount,
});

const canSponsor = await canSponsorUserOperation({
  kernelClient,
  calls: [
    {
      to: "0x...",
      value: parseEther("0.1"),
    },
  ],
});