Fees Calculation & Gas Estimation Logic
Fee calculation and gas estimation help you understand the costs involved in cross-chain transactions and deployments. These processes account for things like gas limits, payload size, and currency prices on the source and destination chains to ensure your operations run smoothly. Entangle provides APIs to estimate fees for bridging and deploying tokens, making it easier to plan and execute your transactions.
This page explains the logic for calculating transaction fees and estimating gas costs for cross-chain operations, including base fees, payload costs, and optimization strategies handled off-chain by the backend.
Base Transaction Fee
dstGasLimit: User-defined gas limit to be used on the destination network during the redeem transaction.
dstGasPriceInWei: Stored in the contract and updated by the backend; represents the gas price on the destination network based on
chainId
.dstNativePriceInUSDe18: Stored in the contract and updated by the backend; indicates the USD price of the native currency on the destination network, depending on
chainId
.curNativePriceInUSDe18: Stored in the contract and updated by the backend; reflects the USD price of the native currency on the source network.
Base Fee Calculation
To calculate the base fee in terms of the current native currency:
baseFeeAtCurrentNative = dstGasLimit * dstGasPriceInWei * dstNativePriceInUSDe18 / curNativePriceInUSDe18
This formula determines the amount of native currency required on the source network to execute the transaction (excluding payload), which is then compared to msg.value
.
Payload Cost Calculation
To calculate the cost related to the payload, use these parameters:
payloadLength: User-defined length of the payload in bytes.
dstPricePerByteInWei: Cost per byte of payload in the native currency on the destination network.
dstNativePriceInUSDe18: Represents the USD price of the native currency on the destination network.
curNativePriceInUSDe18: Represents the USD price of the native currency on the source network.
The payload cost calculation in the current native currency is:
payloadPriceAtCurrentNative = payloadLength * dstPricePerByteInWei * dstNativePriceInUSDe18 / curNativePriceInUSDe18
This formula calculates how much native currency is needed on the source network to cover the payload cost, which is also compared to msg.value
.
Final Calculation
To determine the total required amount, sum baseFeeAtCurrentNative
and payloadPriceAtCurrentNative
, then compare to msg.value
. Note that users may incur small losses if dstGasLimit
exceeds the actual gas consumed.
Optimization of Calculations
To optimize the process, the backend handles calculations off-chain to minimize contract updates across different destination networks:
dstGasPriceInWei
dstPricePerByteInWei
dstNativePriceInUSDe18
curNativePriceInUSDe18
Using these parameters, the backend performs these calculations:
Calculate
dstGasPriceAtCurrentNative
:dstGasPriceAtCurrentNative = dstGasPriceInWei * dstNativePriceInUSDe18 / curNativePriceInUSDe18
Calculate
dstPayloadPriceAtCurrentNative
:dstPayloadPriceAtCurrentNative = dstPricePerByteInWei * dstNativePriceInUSDe18 / curNativePriceInUSDe18
The backend then sends only the dstGasPriceAtCurrentNative
value to the contract. Optionally, it can also send dstPayloadPriceAtCurrentNative
if a dynamic price per byte is preferred; otherwise, a static price is more efficient.
Last updated
Was this helpful?