Skip to content

Commit

Permalink
fix: reset nonce if outdated (#2383)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
schmanu authored Sep 18, 2023
1 parent e200a0e commit c95fe4d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/components/tx-flow/SafeTxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,12 +36,13 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement =>
const [nonce, setNonce] = useState<number>()
const [nonceNeeded, setNonceNeeded] = useState<boolean>(true)
const [safeTxGas, setSafeTxGas] = useState<number>()
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
Expand Down
5 changes: 4 additions & 1 deletion src/components/tx-flow/common/TxNonce/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit c95fe4d

Please sign in to comment.