Skip to main content

isGasSponsorshipError

A type guard function that checks if an error is related to gas sponsorship policies. This is useful for handling cases where a transaction was expected to be sponsored but didn’t match any sponsorship policies.

Usage

import {
  createKernelClientForWalletAccount,
  isGasSponsorshipError
} 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 kernelClient = await createKernelClientForWalletAccount({
    smartWalletAccount: walletAccount,
  });

  try {
    await kernelClient.sendTransaction({
      to: recipientAddress,
      value: parseEther("0.01"),
    });
  } catch (error) {
    if (isGasSponsorshipError(error)) {
      console.log("Transaction not sponsored - user needs to pay gas");
      // Handle by retrying without sponsorship or showing user a message
    } else {
      throw error;
    }
  }
}

Parameters

ParameterTypeDescription
errunknownThe error to check

Returns

boolean - Returns true if the error is a gas sponsorship error, false otherwise.