Skip to content

Commit

Permalink
fix: Hide nonce for spending limit txs
Browse files Browse the repository at this point in the history
  • Loading branch information
usame-algan committed Jul 12, 2023
1 parent cbb24e3 commit dfee18f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/components/tx-flow/SafeTxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const SafeTxContext = createContext<{

nonce?: number
setNonce: Dispatch<SetStateAction<number | undefined>>
nonceNeeded?: boolean
setNonceNeeded: Dispatch<SetStateAction<boolean>>

safeTxGas?: number
setSafeTxGas: Dispatch<SetStateAction<number | undefined>>
Expand All @@ -22,6 +24,7 @@ export const SafeTxContext = createContext<{
setSafeTx: () => {},
setSafeTxError: () => {},
setNonce: () => {},
setNonceNeeded: () => {},
setSafeTxGas: () => {},
})

Expand All @@ -31,6 +34,7 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement =>
const [safeTx, setSafeTx] = useState<SafeTransaction>()
const [safeTxError, setSafeTxError] = useState<Error>()
const [nonce, setNonce] = useState<number>()
const [nonceNeeded, setNonceNeeded] = useState<boolean>(true)
const [safeTxGas, setSafeTxGas] = useState<number>()

// Signed txs cannot be updated
Expand Down Expand Up @@ -63,6 +67,8 @@ const SafeTxProvider = ({ children }: { children: ReactNode }): ReactElement =>
setSafeTxError,
nonce: finalNonce,
setNonce,
nonceNeeded,
setNonceNeeded,
safeTxGas: finalSafeTxGas,
setSafeTxGas,
recommendedNonce,
Expand Down
54 changes: 35 additions & 19 deletions src/components/tx-flow/common/TxLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ComponentType, type ReactElement, type ReactNode, useEffect, useState } from 'react'
import { type ComponentType, type ReactElement, type ReactNode, useContext, useEffect, useState } from 'react'
import { Box, Container, Grid, Typography, Button, Paper, SvgIcon, IconButton, useMediaQuery } from '@mui/material'
import { useTheme } from '@mui/material/styles'
import type { TransactionSummary } from '@safe-global/safe-gateway-typescript-sdk'
import classnames from 'classnames'
import { ProgressBar } from '@/components/common/ProgressBar'
import SafeTxProvider from '../../SafeTxProvider'
import SafeTxProvider, { SafeTxContext } from '../../SafeTxProvider'
import { TxInfoProvider } from '@/components/tx-flow/TxInfoProvider'
import TxNonce from '../TxNonce'
import TxStatusWidget from '../TxStatusWidget'
Expand All @@ -15,6 +15,38 @@ import { RedefineMessage } from '@/components/tx/security/redefine'
import { TxSecurityProvider } from '@/components/tx/security/shared/TxSecurityContext'
import ChainIndicator from '@/components/common/ChainIndicator'

const TxLayoutHeader = ({
hideNonce,
icon,
subtitle,
}: {
hideNonce: TxLayoutProps['hideNonce']
icon: TxLayoutProps['icon']
subtitle: TxLayoutProps['subtitle']
}) => {
const { nonceNeeded } = useContext(SafeTxContext)

if (hideNonce && !icon && !subtitle) return null

return (
<Box className={css.headerInner}>
<Box display="flex" alignItems="center">
{icon && (
<div className={css.icon}>
<SvgIcon component={icon} inheritViewBox />
</div>
)}

<Typography variant="h4" component="div" fontWeight="bold">
{subtitle}
</Typography>
</Box>

{!hideNonce && nonceNeeded && <TxNonce />}
</Box>
)
}

type TxLayoutProps = {
title: ReactNode
children: ReactNode
Expand Down Expand Up @@ -87,23 +119,7 @@ const TxLayout = ({
<ProgressBar value={progress} />
</Box>

{!hideNonce || icon || subtitle ? (
<Box className={css.headerInner}>
<Box display="flex" alignItems="center">
{icon && (
<div className={css.icon}>
<SvgIcon component={icon} inheritViewBox />
</div>
)}

<Typography variant="h4" component="div" fontWeight="bold">
{subtitle}
</Typography>
</Box>

{!hideNonce && <TxNonce />}
</Box>
) : null}
<TxLayoutHeader subtitle={subtitle} icon={icon} hideNonce={hideNonce} />
</Paper>

<div className={css.step}>
Expand Down
5 changes: 5 additions & 0 deletions src/components/tx-flow/common/TxStatusWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import css from './styles.module.css'
import CloseIcon from '@mui/icons-material/Close'
import useWallet from '@/hooks/wallets/useWallet'
import SafeLogo from '@/public/images/logo-no-text.svg'
import { useContext } from 'react'
import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider'

const confirmedMessage = (threshold: number, confirmations: number) => {
return (
Expand All @@ -33,6 +35,7 @@ const TxStatusWidget = ({
}) => {
const wallet = useWallet()
const { safe } = useSafeInfo()
const { nonceNeeded } = useContext(SafeTxContext)
const { threshold } = safe

const { executionInfo = undefined } = txSummary || {}
Expand Down Expand Up @@ -73,6 +76,8 @@ const TxStatusWidget = ({
<ListItemText primaryTypographyProps={{ fontWeight: 700 }}>
{isBatch ? (
'Create batch'
) : !nonceNeeded ? (
'Confirmed'
) : (
<>
{confirmedMessage(threshold, confirmationsSubmitted)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement, SyntheticEvent } from 'react'
import { useMemo, useState } from 'react'
import { useContext, useMemo, useState } from 'react'
import type { BigNumberish, BytesLike } from 'ethers'
import { Button, CardActions, Typography } from '@mui/material'
import SendToBlock from '@/components/tx-flow/flows/TokenTransfer/SendToBlock'
Expand All @@ -22,6 +22,7 @@ import useOnboard from '@/hooks/wallets/useOnboard'
import { WrongChainWarning } from '@/components/tx/WrongChainWarning'
import { asError } from '@/services/exceptions/utils'
import TxCard from '@/components/tx-flow/common/TxCard'
import { TxModalContext } from '@/components/tx-flow'

export type SpendingLimitTxParams = {
safeAddress: string
Expand All @@ -43,6 +44,7 @@ const ReviewSpendingLimitTx = ({
}): ReactElement => {
const [isSubmittable, setIsSubmittable] = useState<boolean>(true)
const [submitError, setSubmitError] = useState<Error | undefined>()
const { setTxFlow } = useContext(TxModalContext)
const currentChain = useCurrentChain()
const onboard = useOnboard()
const { safe, safeAddress } = useSafeInfo()
Expand Down Expand Up @@ -88,7 +90,7 @@ const ReviewSpendingLimitTx = ({

try {
await dispatchSpendingLimitTxExecution(txParams, txOptions, onboard, safe.chainId, safeAddress)

setTxFlow(undefined)
onSubmit()
} catch (_err) {
const err = asError(_err)
Expand Down
5 changes: 5 additions & 0 deletions src/components/tx/SpendingLimitRow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { HelpCenterArticle } from '@/config/constants'

import css from './styles.module.css'
import { TokenAmountFields } from '@/components/common/TokenAmountInput'
import { useContext } from 'react'
import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider'

const SpendingLimitRow = ({
availableAmount,
Expand All @@ -22,6 +24,7 @@ const SpendingLimitRow = ({
}) => {
const { control, trigger } = useFormContext()
const isOnlySpendLimitBeneficiary = useIsOnlySpendingLimitBeneficiary()
const { setNonceNeeded } = useContext(SafeTxContext)

const formattedAmount = safeFormatUnits(availableAmount, selectedToken?.decimals)

Expand All @@ -40,6 +43,8 @@ const SpendingLimitRow = ({
onChange={(e) => {
onChange(e)

setNonceNeeded(e.target.value === TokenTransferType.multiSig)

// Validate only after the field is changed
setTimeout(() => {
trigger(TokenAmountFields.amount)
Expand Down

0 comments on commit dfee18f

Please sign in to comment.