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: Update tx flow form when selecting a replacement nonce #2520

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions src/components/tx-flow/SafeTxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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,13 +35,12 @@ 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 = Math.max(safe.nonce, useRecommendedNonce() ?? 0)
const recommendedNonce = useRecommendedNonce()
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,7 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo
defaultValues: {
[TxNonceFormFieldNames.NONCE]: nonce,
},
mode: 'onTouched',
mode: 'all',
values: {
[TxNonceFormFieldNames.NONCE]: nonce,
},
Expand All @@ -122,6 +122,9 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo
required: 'Nonce is required',
// Validation must be async to allow resetting invalid values onBlur
validate: async (value) => {
// nonce is always valid so no need to validate if the input is the same
if (value === nonce) return

const newNonce = Number(value)

if (isNaN(newNonce)) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/tx/SignOrExecuteForm/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ export const useRecommendedNonce = (): number | undefined => {
const { safeAddress, safe } = useSafeInfo()

const [recommendedNonce] = useAsync(
() => {
async () => {
if (!safe.chainId || !safeAddress) return

return getRecommendedNonce(safe.chainId, safeAddress)
const recommendedNonce = await getRecommendedNonce(safe.chainId, safeAddress)

return recommendedNonce ? Math.max(safe.nonce, recommendedNonce) : undefined
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[safeAddress, safe.chainId, safe.txQueuedTag], // update when tx queue changes
Expand Down
Loading