diff --git a/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelector.svelte b/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelector.svelte index 5a300baedd1..103b2d58c26 100644 --- a/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelector.svelte +++ b/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelector.svelte @@ -1,14 +1,14 @@ + + + + diff --git a/packages/bridge-ui-v2/src/components/SwitchChainModal/index.ts b/packages/bridge-ui-v2/src/components/SwitchChainModal/index.ts new file mode 100644 index 00000000000..3c64b6751d6 --- /dev/null +++ b/packages/bridge-ui-v2/src/components/SwitchChainModal/index.ts @@ -0,0 +1 @@ +export { default as SwitchChainModal } from './SwitchChainModal.svelte'; diff --git a/packages/bridge-ui-v2/src/i18n/en.json b/packages/bridge-ui-v2/src/i18n/en.json index 252ca7c56bc..ac2bdecebf4 100644 --- a/packages/bridge-ui-v2/src/i18n/en.json +++ b/packages/bridge-ui-v2/src/i18n/en.json @@ -214,6 +214,10 @@ "connecting": "Connecting" } }, + "switch_modal": { + "title": "Not on the right network", + "description": "Your current network is not supported. Please select one:" + }, "messages": { "account": { "required": "Please connect your wallet.", diff --git a/packages/bridge-ui-v2/src/libs/util/chainToIconMap.ts b/packages/bridge-ui-v2/src/libs/util/chainToIconMap.ts new file mode 100644 index 00000000000..1d5aaeff588 --- /dev/null +++ b/packages/bridge-ui-v2/src/libs/util/chainToIconMap.ts @@ -0,0 +1,10 @@ +import type { ComponentType } from 'svelte'; + +import { EthIcon, TaikoIcon } from '$components/Icon'; +import { PUBLIC_L1_CHAIN_ID, PUBLIC_L2_CHAIN_ID } from '$env/static/public'; + +export const chainToIconMap: Record = { + [PUBLIC_L1_CHAIN_ID]: EthIcon, + [PUBLIC_L2_CHAIN_ID]: TaikoIcon, + // TODO: L3 +}; diff --git a/packages/bridge-ui-v2/src/libs/wagmi/watcher.ts b/packages/bridge-ui-v2/src/libs/wagmi/watcher.ts index c4d29fb461e..3e73cb1eb33 100644 --- a/packages/bridge-ui-v2/src/libs/wagmi/watcher.ts +++ b/packages/bridge-ui-v2/src/libs/wagmi/watcher.ts @@ -1,7 +1,9 @@ import { watchAccount, watchNetwork /*, watchPublicClient, watchWalletClient*/ } from '@wagmi/core'; +import { isSupportedChain } from '$libs/chain'; import { getLogger } from '$libs/util/logger'; import { account } from '$stores/account'; +import { switchChainModal } from '$stores/modal'; import { network } from '$stores/network'; const log = getLogger('wagmi:watcher'); @@ -17,9 +19,19 @@ export function startWatching() { unWatchNetwork = watchNetwork((data) => { log('Network changed', data); + const { chain } = data; + + // We need to check if the chain is supported, and if not + // we present the user with a modal to switch networks. + if (chain && !isSupportedChain(BigInt(chain.id))) { + log('Unsupported chain', chain); + switchChainModal.set(true); + return; + } + // When we switch networks, we are actually selecting // the source chain. - network.set(data.chain); + network.set(chain); }); // Action for subscribing to account changes. diff --git a/packages/bridge-ui-v2/src/routes/+layout.svelte b/packages/bridge-ui-v2/src/routes/+layout.svelte index 76f0e336502..7cba2d5243e 100644 --- a/packages/bridge-ui-v2/src/routes/+layout.svelte +++ b/packages/bridge-ui-v2/src/routes/+layout.svelte @@ -8,6 +8,7 @@ import { Header } from '$components/Header'; import { NotificationToast } from '$components/NotificationToast'; import { SideNavigation } from '$components/SideNavigation'; + import SwitchChainModal from '$components/SwitchChainModal/SwitchChainModal.svelte'; import { startWatching, stopWatching } from '$libs/wagmi'; onMount(startWatching); @@ -30,3 +31,5 @@ + + diff --git a/packages/bridge-ui-v2/src/stores/modal.ts b/packages/bridge-ui-v2/src/stores/modal.ts new file mode 100644 index 00000000000..2b1ad8a8ad2 --- /dev/null +++ b/packages/bridge-ui-v2/src/stores/modal.ts @@ -0,0 +1,5 @@ +import { writable } from 'svelte/store'; + +// We make this global because we need to be able to +// open and close the modal from anywhere in the app +export const switchChainModal = writable(false);