-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from ethereum-optimism/01-08-feat_show_gas_est…
…imation_values feat: show gas estimation values
- Loading branch information
Showing
3 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {useQuery} from '@tanstack/react-query'; | ||
import {Address, Hex} from 'viem'; | ||
import {estimateL1Fee} from 'viem/op-stack'; | ||
import {useEstimateGas, usePublicClient, useGasPrice} from 'wagmi'; | ||
|
||
export const useGasEstimation = ({ | ||
to, | ||
data, | ||
account, | ||
chainId, | ||
}: { | ||
to: Address; | ||
data: Hex; | ||
account: Address; | ||
chainId: number; | ||
}) => { | ||
const publicClient = usePublicClient({ | ||
chainId, | ||
}); | ||
|
||
const { | ||
data: estimatedL1Fee, | ||
isLoading: isL1FeeEstimationLoading, | ||
error: l1FeeEstimationError, | ||
} = useQuery({ | ||
queryKey: ['estimateL1Fee', chainId, to, account, data], | ||
queryFn: () => { | ||
// @ts-expect-error | ||
return estimateL1Fee(publicClient, { | ||
chainId, | ||
to, | ||
account, | ||
data, | ||
}); | ||
}, | ||
}); | ||
|
||
const { | ||
data: estimatedL2Gas, | ||
isLoading: isL2GasEstimationLoading, | ||
error: l2GasEstimationError, | ||
} = useEstimateGas({ | ||
chainId, | ||
to, | ||
account, | ||
data, | ||
}); | ||
|
||
const { | ||
data: l2GasPrice, | ||
isLoading: isL2GasPriceLoading, | ||
error: l2GasPriceError, | ||
} = useGasPrice({ | ||
chainId, | ||
}); | ||
|
||
const isLoading = | ||
isL1FeeEstimationLoading || isL2GasEstimationLoading || isL2GasPriceLoading; | ||
|
||
const error = | ||
l1FeeEstimationError || | ||
l2GasEstimationError || | ||
l2GasPriceError || | ||
undefined; | ||
|
||
const areValuesAvailable = estimatedL1Fee && estimatedL2Gas && l2GasPrice; | ||
|
||
if (isLoading || !areValuesAvailable) { | ||
return { | ||
data: undefined, | ||
error: null, | ||
isLoading: true, | ||
}; | ||
} | ||
|
||
if (error) { | ||
return { | ||
data: undefined, | ||
error: new Error('Gas estimation failed'), | ||
isLoading: false, | ||
}; | ||
} | ||
|
||
const totalFee = estimatedL1Fee + estimatedL2Gas * l2GasPrice; | ||
|
||
return { | ||
data: { | ||
totalFee: totalFee, | ||
estimatedL1Fee: estimatedL1Fee, | ||
estimatedL2Gas: estimatedL2Gas, | ||
l2GasPrice: l2GasPrice, | ||
}, | ||
error: null, | ||
isLoading: false, | ||
}; | ||
}; |