Skip to main content

canSponsorTransaction

Checks if a transaction can be sponsored by the configured paymaster. This is useful to determine if the user will need to pay gas fees or if the transaction qualifies for gas sponsorship.

Usage

import { canSponsorTransaction } 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 canSponsorTransaction({
    walletAccount,
    transaction: {
      to: recipientAddress,
      value: parseEther("0.01"),
      data: "0x",
    },
  });

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

Parameters

You must provide either a walletAccount or a kernelClient, but not both.
ParameterTypeDescription
transaction.toHexThe recipient address
transaction.valuebigintThe value to send in wei
transaction.dataHex (optional)The transaction data
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 transaction can be sponsored, false otherwise.

Examples

Using wallet account

const canSponsor = await canSponsorTransaction({
  walletAccount,
  transaction: {
    to: "0x...",
    value: parseEther("0.1"),
  },
});

Using existing kernel client

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

const canSponsor = await canSponsorTransaction({
  kernelClient,
  transaction: {
    to: "0x...",
    value: parseEther("0.1"),
  },
});