From c95fe4d0afe1de475635c5033d28d9304d918f7b Mon Sep 17 00:00:00 2001 From: Manuel Gellfart Date: Mon, 18 Sep 2023 12:20:37 +0200 Subject: [PATCH] fix: reset nonce if outdated (#2383) * fix: reset nonce if outdated resets nonce to recommended nonce if the currently selected nonce is lower than the Safe nonce. * fix: set to safe.nonce instead of recommendedNonce * refactor: move invalid nonce logic to SafeTxProvider * fix: do not setNonce for default values, keep SafeTxProvider in sync with form * fix: only update nonce on touch * refactor: remove unused code * fix: review comments * chore: add comment to useAsync deps --- src/components/tx-flow/SafeTxProvider.tsx | 4 +++- src/components/tx-flow/common/TxNonce/index.tsx | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/components/tx-flow/SafeTxProvider.tsx b/src/components/tx-flow/SafeTxProvider.tsx index cdc263d815..06c3b95574 100644 --- a/src/components/tx-flow/SafeTxProvider.tsx +++ b/src/components/tx-flow/SafeTxProvider.tsx @@ -4,6 +4,7 @@ import type { SafeTransaction } from '@safe-global/safe-core-sdk-types' import { createTx } from '@/services/tx/tx-sender' import { useRecommendedNonce, useSafeTxGas } from '../tx/SignOrExecuteForm/hooks' import { Errors, logError } from '@/services/exceptions' +import useSafeInfo from '@/hooks/useSafeInfo' export const SafeTxContext = createContext<{ safeTx?: SafeTransaction @@ -35,12 +36,13 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement => const [nonce, setNonce] = useState() const [nonceNeeded, setNonceNeeded] = useState(true) const [safeTxGas, setSafeTxGas] = useState() + const { safe } = useSafeInfo() // Signed txs cannot be updated const isSigned = safeTx && safeTx.signatures.size > 0 // Recommended nonce and safeTxGas - const recommendedNonce = useRecommendedNonce() + const recommendedNonce = Math.max(safe.nonce, useRecommendedNonce() ?? 0) const recommendedSafeTxGas = useSafeTxGas(safeTx) // Priority to external nonce, then to the recommended one diff --git a/src/components/tx-flow/common/TxNonce/index.tsx b/src/components/tx-flow/common/TxNonce/index.tsx index 3d7c481eb3..18f031f93c 100644 --- a/src/components/tx-flow/common/TxNonce/index.tsx +++ b/src/components/tx-flow/common/TxNonce/index.tsx @@ -104,7 +104,10 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo defaultValues: { [TxNonceFormFieldNames.NONCE]: nonce, }, - mode: 'all', + mode: 'onTouched', + values: { + [TxNonceFormFieldNames.NONCE]: nonce, + }, }) const resetNonce = () => {