Skip to content

Commit

Permalink
fix: Move logic inside useRecommendedNonce
Browse files Browse the repository at this point in the history
  • Loading branch information
usame-algan committed Sep 21, 2023
1 parent 542f025 commit 83daec9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 2 additions & 5 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,18 +35,16 @@ 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 safeRecommendedNonce = recommendedNonce ? Math.max(safe.nonce, recommendedNonce) : undefined
const recommendedSafeTxGas = useSafeTxGas(safeTx)

// Priority to external nonce, then to the recommended one
const finalNonce = isSigned ? safeTx?.data.nonce : nonce ?? safeRecommendedNonce ?? safeTx?.data.nonce
const finalNonce = isSigned ? safeTx?.data.nonce : 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
Expand Down Expand Up @@ -78,7 +75,7 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement =>
setNonceNeeded,
safeTxGas: finalSafeTxGas,
setSafeTxGas,
recommendedNonce: safeRecommendedNonce,
recommendedNonce,
}}
>
{children}
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

0 comments on commit 83daec9

Please sign in to comment.