Skip to content

Commit

Permalink
Add .toFixed to ETH gas fee calc to avoid precision errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed May 10, 2023
1 parent c680e86 commit 563a4dc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function useEthers() {
// const gasEstimateInEth = ethers.utils.formatEther(gasEstimate)
const fee = maxPriorityFeePerGasInWei?.mul(gasEstimate).add(maxFeePerGasInWei)
const feeInWei = ethers.utils.formatEther(fee)
const feeInEth = (parseInt(feeInWei) / 10**18).toString()
const feeInEth = ((parseInt(feeInWei) / 10**18).toFixed(8)).toString()
return {
gasEstimate,
fee: feeInEth
Expand All @@ -73,6 +73,33 @@ export default function useEthers() {
}
}

/**
* Estimate gas fee using legacy methodology
* @returns string in ETH
* @deprecated
* @see estimateEIP1559GasFee
*/
async function estimateLegacyGasFee(rpcUrl: string, unsignedTransaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>) {
try {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl)
const gasPrice = await provider.getGasPrice()
const gasLimit = await provider.estimateGas(unsignedTransaction as ethers.utils.Deferrable<ethers.providers.TransactionRequest>)
const fee = gasPrice.mul(gasLimit)
const feeInWei = ethers.utils.formatEther(fee)
const feeInEth = (parseInt(feeInWei) / 10**18).toFixed(8).toString()
return {
gasLimit,
fee: feeInEth
}
} catch (err) {
console.error('There was an error in estimateGasFee :>> ', err)
return {
gasLimit: '0',
fee: '0'
}
}
}

async function getEthersAddress (providerString: ProviderString) {
const provider = availableProviders.value[providerString as keyof BrowserProviders]
if (provider) {
Expand Down

0 comments on commit 563a4dc

Please sign in to comment.