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:
export type TransactionCost = {
requiredQuantities: CoinQuantity[];
receipts: TransactionResultReceipt[];
minGasPrice: BN;
gasPrice: BN;
minGas: BN;
maxGas: BN;
gasUsed: BN;
minFee: BN;
maxFee: BN;
usedFee: BN;
};
The following example demonstrate how to get the estimated transaction cost for:
const { minGasPrice } = provider.getGasConfig();
const cost = await contract.functions
.return_context_amount()
.callParams({
forward: [100, BaseAssetId],
})
.txParams({
gasPrice: minGasPrice,
})
.getTransactionCost();
expect(cost.minFee).toBeDefined();
expect(cost.maxFee).toBeDefined();
expect(cost.gasPrice).toBeDefined();
expect(cost.gasUsed).toBeDefined();
expect(cost.minGasPrice).toBeDefined();
const { minGasPrice } = provider.getGasConfig();
const scope = contract
.multiCall([
contract.functions.return_context_amount().callParams({
forward: [100, BaseAssetId],
}),
contract.functions.return_context_amount().callParams({
forward: [300, BaseAssetId],
}),
])
.txParams({
gasPrice: minGasPrice,
});
const cost = await scope.getTransactionCost();
expect(cost.minFee).toBeDefined();
expect(cost.maxFee).toBeDefined();
expect(cost.gasPrice).toBeDefined();
expect(cost.gasUsed).toBeDefined();
expect(cost.minGasPrice).toBeDefined();
You can use the transaction cost estimation to set the gas limit for an actual call or display the estimated cost to the user.