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

Fix: avoid fetching Safe Apps on all pages #3254

Merged
merged 9 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export enum SafeAppsTag {
ONRAMP = 'onramp',
}

export const WalletConnectSafeApp = IS_PRODUCTION
? {
id: 111,
url: 'https://apps-portal.safe.global/wallet-connect',
}
: {
id: 25,
url: 'https://safe-apps.dev.5afe.dev/wallet-connect',
}

// Safe Gelato relay service
export const SAFE_RELAY_SERVICE_URL_PRODUCTION =
process.env.NEXT_PUBLIC_SAFE_RELAY_SERVICE_URL_PRODUCTION || 'https://safe-client.safe.global/v1/relay'
Expand Down
20 changes: 2 additions & 18 deletions src/features/walletconnect/__tests__/WalletConnectContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ import * as useSafeWalletProvider from '@/services/safe-wallet-provider/useSafeW
jest.mock('../services/WalletConnectWallet')
jest.mock('@/services/safe-wallet-provider/useSafeWalletProvider')

jest.mock('@/hooks/safe-apps/useRemoteSafeApps', () => ({
useRemoteSafeApps: () => [
[
{
id: 111,
url: 'https://apps-portal.safe.global/wallet-connect',
name: 'WC App',
iconUrl: 'https://test.com/icon.png',
description: 'Test App Description',
},
],
undefined,
false,
],
}))

const TestComponent = () => {
const { walletConnect, error } = useContext(WalletConnectContext)
return (
Expand Down Expand Up @@ -426,10 +410,10 @@ describe('WalletConnectProvider', () => {
1,
{ method: 'fake', params: [] },
{
id: 111,
id: 25,
name: 'name',
description: 'description',
url: 'https://apps-portal.safe.global/wallet-connect',
url: 'https://safe-apps.dev.5afe.dev/wallet-connect',
iconUrl: 'iconUrl',
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { formatJsonRpcError } from '@walletconnect/jsonrpc-utils'
import useSafeInfo from '@/hooks/useSafeInfo'
import useSafeWalletProvider from '@/services/safe-wallet-provider/useSafeWalletProvider'
import { asError } from '@/services/exceptions/utils'
import { IS_PRODUCTION } from '@/config/constants'
import { SafeAppsTag } from '@/config/constants'
import { useRemoteSafeApps } from '@/hooks/safe-apps/useRemoteSafeApps'
import { IS_PRODUCTION, WalletConnectSafeApp } from '@/config/constants'
import { getPeerName, stripEip155Prefix } from '@/features/walletconnect/services/utils'
import { trackRequest } from '@/features/walletconnect//services/tracking'
import { wcPopupStore } from '@/features/walletconnect/components'
Expand All @@ -25,11 +23,6 @@ const getWrongChainError = (dappName: string): Error => {
return new Error(message)
}

const useWalletConnectApp = () => {
const [matchingApps] = useRemoteSafeApps(SafeAppsTag.WALLET_CONNECT)
return matchingApps?.[0]
}

export const WalletConnectProvider = ({ children }: { children: ReactNode }) => {
const {
safe: { chainId },
Expand All @@ -40,7 +33,6 @@ export const WalletConnectProvider = ({ children }: { children: ReactNode }) =>
const setOpen = wcPopupStore.setStore
const [error, setError] = useState<Error | null>(null)
const safeWalletProvider = useSafeWalletProvider()
const wcApp = useWalletConnectApp()

// Init WalletConnect
useEffect(() => {
Expand Down Expand Up @@ -88,8 +80,8 @@ export const WalletConnectProvider = ({ children }: { children: ReactNode }) =>

// Get response from Safe Wallet Provider
return safeWalletProvider.request(event.id, event.params.request, {
id: wcApp?.id || -1,
url: wcApp?.url || '',
id: WalletConnectSafeApp.id,
url: WalletConnectSafeApp.url,
name: getPeerName(session.peer) || 'Unknown dApp',
description: session.peer.metadata.description,
iconUrl: session.peer.metadata.icons[0],
Expand All @@ -105,7 +97,7 @@ export const WalletConnectProvider = ({ children }: { children: ReactNode }) =>
setError(asError(e))
}
})
}, [walletConnect, chainId, safeWalletProvider, wcApp])
}, [walletConnect, chainId, safeWalletProvider])

return (
<WalletConnectContext.Provider value={{ walletConnect, error, setError, open, setOpen }}>
Expand Down
7 changes: 2 additions & 5 deletions src/utils/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WalletConnectSafeApp } from '@/config/constants'
import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'

export const _replaceTemplate = (uri: string, data: Record<string, string>): string => {
Expand Down Expand Up @@ -28,9 +29,5 @@ export const getExplorerLink = (
}

export const isWalletConnectSafeApp = (url: string): boolean => {
const WC_APP_URLS = [
'https://apps-portal.safe.global/wallet-connect',
'https://safe-apps.dev.5afe.dev/wallet-connect',
]
return WC_APP_URLS.includes(url)
return url === WalletConnectSafeApp.url
}
Loading