From 6744fbc73edea2d4fa02bb89e4e7293c93755896 Mon Sep 17 00:00:00 2001 From: Korbinian Date: Sun, 28 Jan 2024 14:48:40 +0100 Subject: [PATCH] moved tokendropdown check, auto select destchain --- .../ChainSelector/ChainSelectorWrapper.svelte | 31 +++++++++++++------ .../TokenDropdown/TokenDropdown.svelte | 25 ++++++++------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelectorWrapper.svelte b/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelectorWrapper.svelte index 4f10f9ae1d1..791c098cac6 100644 --- a/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelectorWrapper.svelte +++ b/packages/bridge-ui-v2/src/components/ChainSelector/ChainSelectorWrapper.svelte @@ -6,9 +6,11 @@ import { destNetwork, destOptions } from '$components/Bridge/state'; import SwitchChainsButton from '$components/Bridge/SwitchChainsButton.svelte'; import { ChainSelector } from '$components/ChainSelector'; + import { OnAccount } from '$components/OnAccount'; import { OnNetwork } from '$components/OnNetwork'; import { hasBridge } from '$libs/bridge/bridges'; import { chainIdToChain, chains } from '$libs/chain'; + import { account } from '$stores/account'; import { network } from '$stores/network'; let destChainElement: ChainSelector; @@ -30,15 +32,6 @@ }); } - function onNetworkChange() { - updateDestOptions(); - const alternateChainID = getAlternateNetwork(); - if (!$destNetwork && alternateChainID) { - // if only two chains are available, set the destination chain to the other one - $destNetwork = chainIdToChain(alternateChainID); - } - } - const getAlternateNetwork = (): number | null => { if (!$network?.id) { return null; @@ -48,18 +41,35 @@ // only allow switching between two chains, if we have more we do not use this util if (chainKeys.length !== 2) { + updateDestOptions(); return null; } const alternateChainId = chainKeys.find((key) => key !== currentNetwork); if (!alternateChainId) return null; + updateDestOptions(); return alternateChainId; }; $: highlight = $destNetwork ? false : true; + const onNetworkChange = () => setAlternateNetwork(); + + const onAccountChange = () => setAlternateNetwork(); + + const setAlternateNetwork = () => { + if ($account && ($account.isConnected || $account.isConnecting)) { + const alternateChainID = getAlternateNetwork(); + if (alternateChainID) { + $destNetwork = chainIdToChain(alternateChainID); + } + } else { + $destNetwork = null; + } + }; + onMount(() => { - updateDestOptions(); + setAlternateNetwork(); }); @@ -82,3 +92,4 @@ fromToLabel={$t('common.to')} /> + diff --git a/packages/bridge-ui-v2/src/components/TokenDropdown/TokenDropdown.svelte b/packages/bridge-ui-v2/src/components/TokenDropdown/TokenDropdown.svelte index 69bcd7a71af..3f236ec75cc 100644 --- a/packages/bridge-ui-v2/src/components/TokenDropdown/TokenDropdown.svelte +++ b/packages/bridge-ui-v2/src/components/TokenDropdown/TokenDropdown.svelte @@ -65,10 +65,6 @@ warningToast({ title: $t('messages.network.required') }); return; } - if (!destChain || !destChain.id) { - warningToast({ title: $t('messages.network.required_dest') }); - return; - } // if it is an imported Token, chances are we do not yet have the bridged address // for the destination chain, so we need to fetch it @@ -76,7 +72,10 @@ // ... in the case of imported tokens, we also require the destination chain to be selected. if (!destChain) { let bridgedAddress = null; - + if (!destChain || !destChain.id) { + warningToast({ title: $t('messages.network.required_dest') }); + return; + } try { bridgedAddress = await getCrossChainAddress({ token, @@ -94,13 +93,17 @@ tokenService.updateToken(token, $account?.address as Address); } } + value = token; - const info = await getCanonicalInfoForToken({ token, srcChainId: srcChain.id, destChainId: destChain.id }); - if (info && value.addresses[srcChain.id] !== info.address) { - log('selected token is not canonical'); - $selectedTokenIsBridged = true; - } else { - $selectedTokenIsBridged = false; + + if (destChain) { + const info = await getCanonicalInfoForToken({ token, srcChainId: srcChain.id, destChainId: destChain.id }); + if (info && value.addresses[srcChain.id] !== info.address) { + log('selected token is not canonical'); + $selectedTokenIsBridged = true; + } else { + $selectedTokenIsBridged = false; + } } closeMenu(); };