Skip to content

Estimating Contract Call Cost

The getTransactionCost function provided by the Provider allows you to estimate the cost of a specific contract call. The return type, TransactionCost, is an object containing relevant information for the estimation:

ts
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;
};
See code in context

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

1. Single contract call transaction:

ts
const cost = await contract.functions
  .return_context_amount()
  .callParams({
    forward: [100, baseAssetId],
  })
  .getTransactionCost();

expect(cost.minFee).toBeDefined();
expect(cost.maxFee).toBeDefined();
expect(cost.gasPrice).toBeDefined();
expect(cost.gasUsed).toBeDefined();
expect(cost.gasPrice).toBeDefined();
See code in context

2. Multiple contract calls transaction:

ts

const scope = contract.multiCall([
  contract.functions.return_context_amount().callParams({
    forward: [100, baseAssetId],
  }),
  contract.functions.return_context_amount().callParams({
    forward: [300, baseAssetId],
  }),
]);

const cost = await scope.getTransactionCost();

expect(cost.minFee).toBeDefined();
expect(cost.maxFee).toBeDefined();
expect(cost.gasPrice).toBeDefined();
expect(cost.gasUsed).toBeDefined();
expect(cost.gasPrice).toBeDefined();
See code in context

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