Skip to content

Commit

Permalink
feat: add 1.3 factor increase on gas estimate to match MM
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Oct 1, 2023
1 parent ebdefab commit 44e81fb
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions libs/shared/providers/src/gas/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { contracts } from '@origin/shared/contracts';
import { useQuery } from '@tanstack/react-query';
import { fetchFeeData, readContract } from '@wagmi/core';
import { formatUnits } from 'viem';
import { useBlockNumber } from 'wagmi';

const GAS_MARGIN = 1.3;

export const useGasPrice = (gasAmount = 0n) => {
const { data: blockNumber } = useBlockNumber();

return useQuery({
queryKey: ['useGasPrice', gasAmount?.toString()],
queryKey: ['useGasPrice', gasAmount?.toString(), blockNumber?.toString()],
queryFn: async () => {
const [price, data] = await Promise.all([
readContract({
Expand All @@ -17,12 +22,23 @@ export const useGasPrice = (gasAmount = 0n) => {
]);

const gweiUsd = +formatUnits(price, 6) * 1e-9;
const gasPrice = +formatUnits(data.gasPrice, 9);
const gasPrice =
+formatUnits(data.gasPrice, 9) +
+formatUnits(data.maxPriorityFeePerGas, 9);
const maxGasPrice = +formatUnits(data.maxFeePerGas, 9);
const gasCostUsd = Number(gasAmount) * gasPrice * gweiUsd;
const maxGasCost = Number(gasAmount) * maxGasPrice * gweiUsd;
const gasCostGwei = Number(gasAmount) * GAS_MARGIN * gasPrice;
const gasCostUsd = gasCostGwei * gweiUsd;
const maxGasCostGwei = Number(gasAmount) * GAS_MARGIN * maxGasPrice;
const maxGasCostUsd = maxGasCostGwei * gweiUsd;

return { gweiUsd, gasPrice, gasCostUsd, maxGasCost };
return {
gweiUsd,
gasPrice,
gasCostUsd,
gasCostGwei,
maxGasCostUsd,
maxGasCostGwei,
};
},
});
};

0 comments on commit 44e81fb

Please sign in to comment.