Prime SDK examples
Show gas fee in fiat
With account abstraction we want to give users who are new to blockchain as simple a user experience as possible.
Showing the gas fee in USD rather than the native token will make it a lot easier to understand for newer users, and we can do this in a couple of simple steps.
First we start by estimating the current batch of UserOps. This will be the units of gas used.
const estimation = await primeSdk.estimate();
Then we want to get the gas in wei for this batch.
const totalGas = await primeSdk.totalGasEstimated(estimation);
And multiply this by the current gas fees on the network.
const gas = totalGas.mul(estimation.maxFeePerGas as BigNumberish);
Next fix the formatting using the formatEther from ethers.
const gasInMatic = ethers.utils.formatEther(gas);
Then get the current price of the native token to the network.
const rateData = await primeSdk.fetchExchangeRates({tokens: [ethers.constants.AddressZero], chainId: 137});
Finally, multiply the current token price by the gas in the token to get the gas price in USD for this transaction.
const priceInUsd = usdRate.usd * (+gasInMatic);