Skip to content

checkBalanceAgainstTransaction

Check if a user's balance is enough to send a transaction with the specified gas and storage limits to the specified contract. The balance is enough if the user can cover the up-front payment of both execution and storage, or if these costs are sponsored by the contract.

Usage

example.ts
import { publicClient } from "./client";

const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0,
});
Output: {
isBalanceEnough: true,
willPayCollateral: true,
willPayTxFee: true
}

Returns

  • isBalanceEnough: boolean indicate balance is enough for gas fee and collateral storage
  • willPayCollateral: boolean false if the transaction is eligible for storage collateral sponsorship, true otherwise.
  • willPayTxFee: boolean false if the transaction is eligible for gas sponsorship, true otherwise.

Parameters

accountAddress

  • Type: Address
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...", 
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0,
});

contractAddress

  • Type: Address
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...", 
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0,
});

gasLimit

  • Type: number
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000, 
  gasPrice: 10000000000,
  storageLimit: 0,
});

gasPrice

  • Type: number
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000, 
  storageLimit: 0,
});

storageLimit

const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0, 
});

epochTag (optional)

  • Type: latest_state| latest_confirmed| latest_checkpoint | earliest
  • Defaut: latest_state
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0,
  epochTag: "latest_state", 
});

epochNumber (optional)

  • Type: bigint
const data = await publicClient.checkBalanceAgainstTransaction({
  accountAddress: "cfx:...",
  contractAddress: "cfx:...",
  gasLimit: 21000,
  gasPrice: 10000000000,
  storageLimit: 0,
  epochNumber: 1n, 
});

JSON-RPC Method

cfx_checkBalanceAgainstTransaction