Skip to content

Commit

Permalink
fix: reset nonce if outdated
Browse files Browse the repository at this point in the history
resets nonce to recommended nonce if the currently selected nonce is lower than the Safe nonce.
  • Loading branch information
schmanu committed Aug 10, 2023
1 parent 9d276e0 commit 6a7060d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/components/tx-flow/common/TxNonce/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, type ReactElement, useContext, useMemo } from 'react'
import { memo, type ReactElement, useContext, useMemo, useEffect } from 'react'
import {
Autocomplete,
Box,
Expand Down Expand Up @@ -109,6 +109,16 @@ 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
useEffect(() => {
if (!isFormChanged && Number(currentFormNonce) < safe.nonce) {
formMethods.setValue(TxNonceFormFieldNames.NONCE, recommendedNonce)
}
}, [safe.nonce, recommendedNonce, currentFormNonce, isFormChanged, formMethods])

return (
<Controller
name={TxNonceFormFieldNames.NONCE}
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx/SignOrExecuteForm/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const useRecommendedNonce = (): number | undefined => {
return getRecommendedNonce(safe.chainId, safeAddress)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[safeAddress, safe.chainId, safe.txQueuedTag], // update when tx queue changes
[safeAddress, safe.chainId, safe.txQueuedTag, safe.nonce], // update when tx queue or safe nonce changes
false, // keep old recommended nonce while refreshing to avoid skeleton
)

Expand Down

0 comments on commit 6a7060d

Please sign in to comment.