Skip to content

Commit

Permalink
fix: redirect to old deployment for legacy Safes (#1096)
Browse files Browse the repository at this point in the history
* fix: redirect to old deployment for legacy Safes

* fix: add tests

* fix: update param

* fix: test + adjust early return

* fix: extract constant + use `query` for Safe

* fix: remove unnecessary dependency
  • Loading branch information
iamacook authored Nov 8, 2022
1 parent 68bde18 commit 61cbc36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
38 changes: 36 additions & 2 deletions src/hooks/__tests__/useSafeNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jest.mock('../../hooks/useSafeInfo')
// mock router
jest.mock('next/router', () => ({
useRouter: jest.fn(() => ({
query: { safe: 'rin:0x123' },
query: { safe: 'eth:0x123' },
})),
}))

Expand Down Expand Up @@ -56,13 +56,43 @@ describe('useSafeNotifications', () => {
link: {
href: {
pathname: '/settings/setup',
query: { safe: 'rin:0x123' },
query: { safe: 'eth:0x123' },
},
title: 'Update Safe',
},
})
})

it('should show a notification for legacy Safes', async () => {
// mock useSafeInfo to return a SafeInfo with an outdated version
;(useSafeInfo as jest.Mock).mockReturnValue({
safe: {
implementation: { value: '0x123' },
implementationVersionState: 'OUTDATED',
version: '1.0.0',
},
safeAddress: '0x123',
})

// render the hook
const { result } = renderHook(() => useSafeNotifications())

// await
await act(async () => Promise.resolve())

// check that the notification was shown
expect(result.current).toBeUndefined()
expect(showNotification).toHaveBeenCalledWith({
variant: 'warning',
message: `Safe version 1.0.0 is not supported by this web app anymore. You can update your Safe via the old web app here.`,
groupKey: 'safe-outdated-version',
link: {
href: 'https://gnosis-safe.io/app/eth:0x123/settings/details?no-redirect=true',
title: 'Update Safe',
},
})
})

it('should not show a notification when the Safe version is up to date', async () => {
;(useSafeInfo as jest.Mock).mockReturnValue({
safe: {
Expand All @@ -89,6 +119,8 @@ describe('useSafeNotifications', () => {
;(useSafeInfo as jest.Mock).mockReturnValue({
safe: {
implementation: { value: '0x123' },
implementationVersionState: 'UP_TO_DATE',
version: '1.3.0',
},
})
jest.spyOn(contracts, 'isValidMasterCopy').mockImplementation((...args: any[]) => Promise.resolve(false))
Expand Down Expand Up @@ -117,6 +149,8 @@ describe('useSafeNotifications', () => {
;(useSafeInfo as jest.Mock).mockReturnValue({
safe: {
implementation: { value: '0x456' },
implementationVersionState: 'UP_TO_DATE',
version: '1.3.0',
},
})
jest.spyOn(contracts, 'isValidMasterCopy').mockImplementation((...args: any[]) => Promise.resolve(true))
Expand Down
21 changes: 12 additions & 9 deletions src/hooks/useSafeNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import useSafeInfo from './useSafeInfo'
import { useAppDispatch } from '@/store'
import { AppRoutes } from '@/config/routes'
import useAsync from './useAsync'
import { isOldestVersion, isValidMasterCopy } from '@/services/contracts/safeContracts'
import { isValidMasterCopy } from '@/services/contracts/safeContracts'
import { useRouter } from 'next/router'
import { isValidSafeVersion } from './coreSDK/safeCoreSDK'

const OLD_URL = 'https://gnosis-safe.io/app'

const CLI_LINK = {
href: 'https://github.com/5afe/safe-cli',
Expand All @@ -31,26 +34,26 @@ const useSafeNotifications = (): void => {
return
}

const isOldSafe = isOldestVersion(version)
const isOldSafe = !isValidSafeVersion(version)

const id = dispatch(
showNotification({
variant: 'warning',
groupKey: 'safe-outdated-version',

message: isOldSafe
? `Safe version ${version} is not supported by this web app anymore. We recommend using the command line interface instead.`
? `Safe version ${version} is not supported by this web app anymore. You can update your Safe via the old web app here.`
: `Your Safe version ${version} is out of date. Please update it.`,

link: isOldSafe
? CLI_LINK
: {
href: {
link: {
href: isOldSafe
? `${OLD_URL}/${query.safe}/settings/details?no-redirect=true`
: {
pathname: AppRoutes.settings.setup,
query: { safe: query.safe },
},
title: 'Update Safe',
},
title: 'Update Safe',
},
}),
)

Expand Down
2 changes: 1 addition & 1 deletion src/services/contracts/safeContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const getSpecificGnosisSafeContractInstance = (safe: SafeInfo) => {
})
}

export const isOldestVersion = (safeVersion: string): boolean => {
const isOldestVersion = (safeVersion: string): boolean => {
return semverSatisfies(safeVersion, '<=1.0.0')
}

Expand Down

0 comments on commit 61cbc36

Please sign in to comment.