-
Notifications
You must be signed in to change notification settings - Fork 64
refactor: accept transactionRequests[] in TransactionFee and useGas #2237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { isLegacyMeteorologyFeeData } from '~/core/resources/gas/classification' | |
| import { useEstimateApprovalGasLimit } from '~/core/resources/gas/estimateApprovalGasLimit'; | ||
| import { useEstimateSwapGasLimit } from '~/core/resources/gas/estimateSwapGasLimit'; | ||
| import { useOptimismL1SecurityFee } from '~/core/resources/gas/optimismL1SecurityFee'; | ||
| import { estimateTransactionsGasLimit } from '~/core/resources/transactions/simulation'; | ||
| import { useCurrentCurrencyStore, useGasStore } from '~/core/state'; | ||
| import { useNetworkStore } from '~/core/state/networks/networks'; | ||
| import { ParsedAsset, ParsedSearchAsset } from '~/core/types/assets'; | ||
|
|
@@ -365,27 +366,72 @@ const useGas = ({ | |
| }; | ||
| }; | ||
|
|
||
| const toSimulationTransaction = ( | ||
| tx: TransactionRequest, | ||
| ): { from: string; to: string; value: string; data: string } => ({ | ||
| from: tx.from?.toString() ?? '', | ||
| to: tx.to?.toString() ?? '', | ||
| value: tx.value?.toString() ?? '0', | ||
| data: tx.data?.toString() ?? '0x', | ||
| }); | ||
|
|
||
| /** | ||
| * Gas estimation for one or more transactions. | ||
| * Single tx: uses provider estimateGas. Batch: sums gas from simulation API. | ||
| */ | ||
| export const useTransactionGas = ({ | ||
| chainId, | ||
| address, | ||
| defaultSpeed, | ||
| transactionRequest, | ||
| transactionRequests, | ||
| }: { | ||
| chainId: ChainId; | ||
| address?: Address; | ||
| defaultSpeed?: GasSpeed; | ||
| transactionRequest: TransactionRequest; | ||
| transactionRequests: TransactionRequest[]; | ||
| }) => { | ||
| const { data: estimatedGasLimit } = useEstimateGasLimit({ | ||
| chainId, | ||
| transactionRequest: useDebounce(transactionRequest, 500), | ||
| const debouncedRequests = useDebounce(transactionRequests, 500); | ||
| const isSingle = (debouncedRequests?.length ?? 0) <= 1; | ||
|
|
||
| const { data: singleGasLimit } = useEstimateGasLimit( | ||
| { | ||
| chainId, | ||
| transactionRequest: debouncedRequests?.[0] ?? {}, | ||
| }, | ||
| { enabled: !!chainId && isSingle && !!debouncedRequests?.length }, | ||
| ); | ||
|
|
||
| const { data: batchGasLimit } = useQuery({ | ||
| queryKey: [ | ||
| 'estimateBatchGasLimit', | ||
| chainId, | ||
| address, | ||
| debouncedRequests | ||
| ?.map( | ||
| (r) => | ||
| `${r.from ?? ''}-${r.to}-${r.value?.toString() ?? '0'}-${r.data}`, | ||
| ) | ||
| .join(','), | ||
| ], | ||
| queryFn: async () => { | ||
| if (!debouncedRequests?.length || isSingle) return undefined; | ||
| const steps = debouncedRequests.map((tx, i) => ({ | ||
| transaction: toSimulationTransaction(tx), | ||
| label: `Call ${i + 1}`, | ||
| })); | ||
| return estimateTransactionsGasLimit({ chainId, steps }); | ||
| }, | ||
| enabled: !!chainId && !isSingle && !!debouncedRequests?.length, | ||
| }); | ||
|
|
||
| const estimatedGasLimit = isSingle ? singleGasLimit : batchGasLimit; | ||
|
|
||
| return useGas({ | ||
| chainId, | ||
| address, | ||
| defaultSpeed, | ||
| estimatedGasLimit, | ||
| transactionRequest, | ||
| estimatedGasLimit: estimatedGasLimit ?? undefined, | ||
| transactionRequest: debouncedRequests?.[0] ?? null, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unintended side effects on gas. I think we want to avoid conflating using sequential simulation for display values, vs gas estimates; we really need to estimate the batch as a whole.
|
||
| enabled: true, | ||
| }); | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback values are concerning here because they differ from the transaction submission flow. If missing here, the simulation would fail and the UI handles gracefully, but could still be destructive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd want to use the selected address instead here, I think. I assume the stack won't let you send to a null address with
'', but maybefromas the default fortoas well