Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Cost Estimation

Icon LinkEstimating Contract Call Cost

The FunctionInvocationScope.getTransactionCost Icon Link method allows you to estimate the cost of a specific contract call. The return type, TransactionCost, is an object containing relevant information for the estimation:

export type TransactionCost = {
  gasPrice: BN;
  gasUsed: BN;
  minGas: BN;
  minFee: BN;
  maxFee: BN;
  maxGas: BN;
  receipts: TransactionResultReceipt[];
  outputVariables: number;
  missingContractIds: string[];
  estimatedPredicates: TransactionRequestInput[];
  requiredQuantities: CoinQuantity[];
  addedSignatures: number;
  dryRunStatus?: DryRunStatus;
  updateMaxFee?: boolean;
};

The following example demonstrates how to get the estimated transaction cost for:

Icon Link1. Single contract call transaction:

const cost = await contract.functions
  .return_context_amount()
  .callParams({
    forward: [100, provider.getBaseAssetId()],
  })
  .getTransactionCost();
 
console.log('costs', cost);

Icon Link2. Multiple contract calls transaction:

const scope = contract.multiCall([
  contract.functions.return_context_amount().callParams({
    forward: [100, provider.getBaseAssetId()],
  }),
  contract.functions.return_context_amount().callParams({
    forward: [300, provider.getBaseAssetId()],
  }),
]);
 
const txCost = await scope.getTransactionCost();
 
console.log('costs', txCost);

You can use the transaction cost estimation to set the gas limit for an actual call or display the estimated cost to the user.