diff --git a/src/components/tx-flow/SafeTxProvider.tsx b/src/components/tx-flow/SafeTxProvider.tsx index cdc263d815..da2d33580e 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 @@ -36,6 +37,8 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement => const [nonceNeeded, setNonceNeeded] = useState(true) const [safeTxGas, setSafeTxGas] = useState() + const { safe } = useSafeInfo() + // Signed txs cannot be updated const isSigned = safeTx && safeTx.signatures.size > 0 @@ -43,8 +46,9 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement => const recommendedNonce = useRecommendedNonce() const recommendedSafeTxGas = useSafeTxGas(safeTx) - // Priority to external nonce, then to the recommended one - const finalNonce = isSigned ? safeTx?.data.nonce : nonce ?? recommendedNonce ?? safeTx?.data.nonce + // Priority to external nonce if valid, then to the recommended one + const isNonceValid = nonce !== undefined && nonce >= safe.nonce + const finalNonce = isSigned ? safeTx?.data.nonce : isNonceValid ? nonce : recommendedNonce ?? safeTx?.data.nonce const finalSafeTxGas = isSigned ? safeTx?.data.safeTxGas : safeTxGas ?? recommendedSafeTxGas ?? safeTx?.data.safeTxGas // Update the tx when the nonce or safeTxGas change diff --git a/src/components/tx-flow/common/TxNonce/index.tsx b/src/components/tx-flow/common/TxNonce/index.tsx index d1619148ae..c9b1c37ffc 100644 --- a/src/components/tx-flow/common/TxNonce/index.tsx +++ b/src/components/tx-flow/common/TxNonce/index.tsx @@ -109,15 +109,10 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo formMethods.setValue(TxNonceFormFieldNames.NONCE, recommendedNonce) } - const currentFormNonce = formMethods.watch(TxNonceFormFieldNames.NONCE) - const isFormChanged = formMethods.formState.isDirty - - // Update to recommended nonce if the current nonce is unchanged and invalid after update + // keep the formdata up-to-date when the actually used nonce updates useEffect(() => { - if (!isFormChanged && Number(currentFormNonce) < safe.nonce) { - formMethods.setValue(TxNonceFormFieldNames.NONCE, safe.nonce.toString()) - } - }, [safe.nonce, currentFormNonce, isFormChanged, formMethods]) + formMethods.setValue(TxNonceFormFieldNames.NONCE, nonce) + }, [nonce, formMethods]) return (