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

[Multichain] fix: Check all fallbackHandler deployments [SW-236] #4281

Merged
merged 4 commits into from
Oct 1, 2024
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
47 changes: 47 additions & 0 deletions src/components/settings/FallbackHandler/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TWAP_FALLBACK_HANDLER } from '@/features/swap/helpers/utils'
import { chainBuilder } from '@/tests/builders/chains'
import { render, waitFor } from '@/tests/test-utils'

Expand Down Expand Up @@ -238,4 +239,50 @@ describe('FallbackHandler', () => {

expect(fbHandler.container).toBeEmptyDOMElement()
})

it('should display a message in case it is a TWAP fallback handler', () => {
jest.spyOn(useSafeInfoHook, 'default').mockImplementation(
() =>
({
safe: {
version: '1.3.0',
chainId: '1',
fallbackHandler: {
value: TWAP_FALLBACK_HANDLER,
},
},
} as unknown as ReturnType<typeof useSafeInfoHook.default>),
)

const { getByText } = render(<FallbackHandler />)

expect(
getByText(
"This is CoW's fallback handler. It is needed for this Safe to be able to use the TWAP feature for Swaps.",
),
).toBeInTheDocument()
})

it('should not display a message in case it is a TWAP fallback handler on an unsupported network', () => {
jest.spyOn(useSafeInfoHook, 'default').mockImplementation(
() =>
({
safe: {
version: '1.3.0',
chainId: '10',
fallbackHandler: {
value: TWAP_FALLBACK_HANDLER,
},
},
} as unknown as ReturnType<typeof useSafeInfoHook.default>),
)

const { queryByText } = render(<FallbackHandler />)

expect(
queryByText(
"This is CoW's fallback handler. It is needed for this Safe to be able to use the TWAP feature for Swaps.",
),
).not.toBeInTheDocument()
})
})
19 changes: 11 additions & 8 deletions src/components/settings/FallbackHandler/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TWAP_FALLBACK_HANDLER } from '@/features/swap/helpers/utils'
import { TWAP_FALLBACK_HANDLER, TWAP_FALLBACK_HANDLER_NETWORKS } from '@/features/swap/helpers/utils'
import { getCompatibilityFallbackHandlerDeployments } from '@safe-global/safe-deployments'
import NextLink from 'next/link'
import { Typography, Box, Grid, Paper, Link, Alert } from '@mui/material'
import semverSatisfies from 'semver/functions/satisfies'
Expand All @@ -7,7 +8,6 @@ import type { ReactElement } from 'react'

import EthHashInfo from '@/components/common/EthHashInfo'
import useSafeInfo from '@/hooks/useSafeInfo'
import { getFallbackHandlerContractDeployment } from '@/services/contracts/deployments'
import { HelpCenterArticle } from '@/config/constants'
import ExternalLink from '@/components/common/ExternalLink'
import { useTxBuilderApp } from '@/hooks/safe-apps/useTxBuilderApp'
Expand All @@ -22,11 +22,12 @@ export const FallbackHandler = (): ReactElement | null => {

const supportsFallbackHandler = !!safe.version && semverSatisfies(safe.version, FALLBACK_HANDLER_VERSION)

const fallbackHandlerDeployment = useMemo(() => {
if (!chain) {
const fallbackHandlerDeployments = useMemo(() => {
if (!chain || !safe.version) {
return undefined
}
return getFallbackHandlerContractDeployment(chain, safe.version)

return getCompatibilityFallbackHandlerDeployments({ network: chain?.chainId, version: safe.version })
}, [safe.version, chain])

if (!supportsFallbackHandler) {
Expand All @@ -35,8 +36,10 @@ export const FallbackHandler = (): ReactElement | null => {

const hasFallbackHandler = !!safe.fallbackHandler
const isOfficial =
hasFallbackHandler && safe.fallbackHandler?.value === fallbackHandlerDeployment?.networkAddresses[safe.chainId]
const isTWAPFallbackHandler = safe.fallbackHandler?.value === TWAP_FALLBACK_HANDLER
safe.fallbackHandler &&
fallbackHandlerDeployments?.networkAddresses[safe.chainId].includes(safe.fallbackHandler.value)
const isTWAPFallbackHandler =
safe.fallbackHandler?.value === TWAP_FALLBACK_HANDLER && TWAP_FALLBACK_HANDLER_NETWORKS.includes(safe.chainId)

const warning = !hasFallbackHandler ? (
<>
Expand Down Expand Up @@ -101,7 +104,7 @@ export const FallbackHandler = (): ReactElement | null => {
{safe.fallbackHandler && (
<EthHashInfo
shortAddress={false}
name={safe.fallbackHandler.name || fallbackHandlerDeployment?.contractName}
name={safe.fallbackHandler.name || fallbackHandlerDeployments?.contractName}
address={safe.fallbackHandler.value}
customAvatar={safe.fallbackHandler.logoUri}
showCopyButton
Expand Down
3 changes: 3 additions & 0 deletions src/features/swap/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function asDecimal(amount: number | bigint, decimals: number): number {

export const TWAP_FALLBACK_HANDLER = '0x2f55e8b20D0B9FEFA187AA7d00B6Cbe563605bF5'

// https://github.com/cowprotocol/composable-cow/blob/main/networks.json
export const TWAP_FALLBACK_HANDLER_NETWORKS = ['1', '100', '11155111', '42161']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We could also use a mapping chainId -> address here. But maybe we can change it if there are ever multiple addresses.


export const getExecutionPrice = (
order: Pick<SwapOrder, 'executedSellAmount' | 'executedBuyAmount' | 'buyToken' | 'sellToken'>,
): number => {
Expand Down
Loading