Before we have a user sign a UserOp to be sent to the bundler, it is important that they understand how much gas they will pay.
When you have a UserOp, you can create an estimation object with the following code:
const userOpEstimated = await opts.etherspotPrimeSdk.estimate(); const totalGas = await opts.etherspotPrimeSdk.totalGasEstimated(userOpEstimated);
userOpEstimated is the esitmated gas details for the current batch.
totalGas defines how much gas is needed.
Then to calculate how much gas in total is needed we will do:
gas * gas price
We want to get how much ETH you need to pay in gas, then get the USD price from some API like CoinGecko. Then multiply that gas with the USD price.
Here is an example of how we do this in the code:
const gas = estimated.totalGas.mul(estimated.maxFeePerGas); const nativeTokenAddress = getContract(chainId, "NATIVE_TOKEN"); const executionFeeUsd = getUsd(gas, nativeTokenAddress, false, infoTokens);