diff --git a/src/components/walletconnect/ProposalForm/ChainWarning.tsx b/src/components/walletconnect/ProposalForm/ChainWarning.tsx index 4c5ccd7b87..2815c22f08 100644 --- a/src/components/walletconnect/ProposalForm/ChainWarning.tsx +++ b/src/components/walletconnect/ProposalForm/ChainWarning.tsx @@ -1,8 +1,7 @@ import { Alert, Typography } from '@mui/material' -import { useMemo } from 'react' +import type { AlertColor } from '@mui/material' import type { ReactElement } from 'react' import type { Web3WalletTypes } from '@walletconnect/web3wallet' -import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk' import ChainIndicator from '@/components/common/ChainIndicator' import useChains from '@/hooks/useChains' @@ -12,57 +11,54 @@ import { getEip155ChainId } from '@/services/walletconnect/utils' import css from './styles.module.css' -const ChainInformation = { - UNSUPPORTED: - 'This dApp does not support the Safe Account network. If you want to interact with it, please switch to a Safe Account on a supported network.', - WRONG: 'Please make sure that the dApp is connected to %%chain%%.', +const ChainInformation: Record = { + UNSUPPORTED: { + severity: 'error', + message: + 'This dApp does not support the Safe Account network. If you want to interact with this dApp, please switch to a Safe Account on a supported network.', + }, + WRONG: { + severity: 'info', + message: 'Please make sure that the dApp is connected to %%chain%%.', + }, } -const getSupportedChainIds = (configs: Array, proposal: Web3WalletTypes.SessionProposal): Array => { - const { requiredNamespaces, optionalNamespaces } = proposal.params - - const requiredChains = requiredNamespaces[EIP155]?.chains ?? [] - const optionalChains = optionalNamespaces[EIP155]?.chains ?? [] - - return configs - .filter((chain) => { - const eipChainId = getEip155ChainId(chain.chainId) - return requiredChains.includes(eipChainId) || optionalChains.includes(eipChainId) - }) - .map((chain) => chain.chainId) -} - -export const ChainWarning = ({ proposal }: { proposal: Web3WalletTypes.SessionProposal }): ReactElement | null => { +export const ChainWarning = ({ + proposal, + chainIds, +}: { + proposal: Web3WalletTypes.SessionProposal + chainIds: Array +}): ReactElement | null => { const { configs } = useChains() const { safe } = useSafeInfo() - const chainIds = useMemo(() => getSupportedChainIds(configs, proposal), [configs, proposal]) - const supportsSafe = chainIds.includes(safe.chainId) + const supportsCurrentChain = chainIds.includes(safe.chainId) const requiredChains = proposal.params.requiredNamespaces[EIP155]?.chains ?? [] const isCorrectChain = requiredChains.includes(getEip155ChainId(safe.chainId)) - if (supportsSafe && isCorrectChain) { + if (supportsCurrentChain && isCorrectChain) { return null } - if (supportsSafe) { + if (supportsCurrentChain) { const chainName = configs.find((chain) => chain.chainId === safe.chainId)?.chainName ?? '' - ChainInformation.WRONG = ChainInformation.WRONG.replace('%%chain%%', chainName) + ChainInformation.WRONG.message = ChainInformation.WRONG.message.replace('%%chain%%', chainName) } - const message = supportsSafe ? ChainInformation.WRONG : ChainInformation.UNSUPPORTED + const { severity, message } = supportsCurrentChain ? ChainInformation.WRONG : ChainInformation.UNSUPPORTED return ( <> - + {message} - {!supportsSafe && ( + {!supportsCurrentChain && ( <> - Supported chains + Supported networks
diff --git a/src/components/walletconnect/ProposalForm/index.tsx b/src/components/walletconnect/ProposalForm/index.tsx index 1071b3e74e..ac15a1cac1 100644 --- a/src/components/walletconnect/ProposalForm/index.tsx +++ b/src/components/walletconnect/ProposalForm/index.tsx @@ -1,5 +1,5 @@ import { Button, Checkbox, Divider, FormControlLabel, Typography } from '@mui/material' -import { useState } from 'react' +import { useMemo, useState } from 'react' import type { ReactElement } from 'react' import type { Web3WalletTypes } from '@walletconnect/web3wallet' @@ -7,6 +7,9 @@ import SafeAppIconCard from '@/components/safe-apps/SafeAppIconCard' import css from './styles.module.css' import ProposalVerification from './ProposalVerification' import { ChainWarning } from './ChainWarning' +import useChains from '@/hooks/useChains' +import useSafeInfo from '@/hooks/useSafeInfo' +import { getSupportedChainIds } from '@/services/walletconnect/utils' type ProposalFormProps = { proposal: Web3WalletTypes.SessionProposal @@ -15,12 +18,17 @@ type ProposalFormProps = { } const ProposalForm = ({ proposal, onApprove, onReject }: ProposalFormProps): ReactElement => { + const { configs } = useChains() + const { safe } = useSafeInfo() const [understandsRisk, setUnderstandsRisk] = useState(false) const { proposer } = proposal.params const { isScam } = proposal.verifyContext.verified + const chainIds = useMemo(() => getSupportedChainIds(configs, proposal.params), [configs, proposal.params]) + const supportsCurrentChain = chainIds.includes(safe.chainId) + const isHighRisk = proposal.verifyContext.verified.validation === 'INVALID' - const disabled = isScam || (isHighRisk && !understandsRisk) + const disabled = !supportsCurrentChain || isScam || (isHighRisk && !understandsRisk) return (
@@ -46,10 +54,10 @@ const ProposalForm = ({ proposal, onApprove, onReject }: ProposalFormProps): Rea
- +
- {isHighRisk && ( + {supportsCurrentChain && isHighRisk && ( setUnderstandsRisk(checked)} />} diff --git a/src/services/walletconnect/utils.ts b/src/services/walletconnect/utils.ts index 225cdc0316..ad1368d3fd 100644 --- a/src/services/walletconnect/utils.ts +++ b/src/services/walletconnect/utils.ts @@ -1,3 +1,6 @@ +import type { ChainInfo } from '@safe-global/safe-apps-sdk' +import type { ProposalTypes } from '@walletconnect/types' + import { EIP155 } from './constants' export const getEip155ChainId = (chainId: string): string => { @@ -7,3 +10,17 @@ export const getEip155ChainId = (chainId: string): string => { export const stripEip155Prefix = (eip155Address: string): string => { return eip155Address.split(':').pop() ?? '' } + +export const getSupportedChainIds = (configs: Array, params: ProposalTypes.Struct): Array => { + const { requiredNamespaces, optionalNamespaces } = params + + const requiredChains = requiredNamespaces[EIP155]?.chains ?? [] + const optionalChains = optionalNamespaces[EIP155]?.chains ?? [] + + return configs + .filter((chain) => { + const eipChainId = getEip155ChainId(chain.chainId) + return requiredChains.includes(eipChainId) || optionalChains.includes(eipChainId) + }) + .map((chain) => chain.chainId) +}