Skip to content
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

fix: reset nonce if outdated #2383

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions 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 @@ -36,15 +37,18 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement =>
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 recommendedSafeTxGas = useSafeTxGas(safeTx)

// Priority to external nonce, then to the recommended one
katspaugh marked this conversation as resolved.
Show resolved Hide resolved
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
katspaugh marked this conversation as resolved.
Show resolved Hide resolved
const finalSafeTxGas = isSigned ? safeTx?.data.safeTxGas : safeTxGas ?? recommendedSafeTxGas ?? safeTx?.data.safeTxGas

// Update the tx when the nonce or safeTxGas change
Expand Down
7 changes: 6 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,11 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo
formMethods.setValue(TxNonceFormFieldNames.NONCE, recommendedNonce)
}

// keep the formdata up-to-date when the actually used nonce updates
useEffect(() => {
formMethods.setValue(TxNonceFormFieldNames.NONCE, nonce)
}, [nonce, 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
katspaugh marked this conversation as resolved.
Show resolved Hide resolved
false, // keep old recommended nonce while refreshing to avoid skeleton
)

Expand Down