diff --git a/apps/web/src/composables/ethers.ts b/apps/web/src/composables/ethers.ts index 0452583e4..218dd69aa 100644 --- a/apps/web/src/composables/ethers.ts +++ b/apps/web/src/composables/ethers.ts @@ -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 @@ -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) { + try { + const provider = new ethers.providers.JsonRpcProvider(rpcUrl) + const gasPrice = await provider.getGasPrice() + const gasLimit = await provider.estimateGas(unsignedTransaction as ethers.utils.Deferrable) + 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) {