Provider 提供的 getTransactionCost
函数允许你估算特定合约调用的成本。返回类型 TransactionCost
是一个包含估算相关信息的对象:
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;
};
以下示例演示了如何获取以下情况的估算交易成本:
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();
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();
你可以使用交易成本估算来为实际调用设置气体限制,或向用户显示估算成本。