Skip to content

Commit

Permalink
chore(bridge-ui-v2): pre-select destination chain (#14850)
Browse files Browse the repository at this point in the history
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
KorbinianK and davidtaikocha authored Sep 29, 2023
1 parent e2360c9 commit 87ed466
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/bridge-ui-v2/src/components/Bridge/Bridge.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { routingContractsMap } from '$bridgeConfig';
import { chainConfig } from '$chainConfig';
import { FlatAlert } from '$components/Alert';
import ChainSelectorWrapper from '$components/Bridge/ChainSelectorWrapper.svelte';
import { Button } from '$components/Button';
import { Card } from '$components/Card';
import { ChainSelectorWrapper } from '$components/ChainSelector';
import { successToast, warningToast } from '$components/NotificationToast';
import { errorToast, infoToast } from '$components/NotificationToast/NotificationToast.svelte';
import { OnAccount } from '$components/OnAccount';
Expand Down Expand Up @@ -327,7 +327,7 @@
</script>

{#if $activeBridge === BridgeTypes.FUNGIBLE}
<Card class="w-full md:w-[524px]" title={$t('bridge.title.default')} text={$t('bridge.description')}>
<Card class="w-full md:w-[524px]" title={$t('bridge.title.default')} text={$t('bridge.description.default')}>
<div class="space-y-[30px]">
<div class="f-between-center gap-4">
<ChainSelectorWrapper />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script lang="ts">
import { onMount } from 'svelte';
import { chainConfig } from '$chainConfig';
import { destNetwork, destOptions } from '$components/Bridge/state';
import SwitchChainsButton from '$components/Bridge/SwitchChainsButton.svelte';
import { ChainSelector } from '$components/ChainSelector';
import { OnNetwork } from '$components/OnNetwork';
import { hasBridge } from '$libs/bridge/bridges';
import { chains } from '$libs/chain';
import { chainIdToChain, chains } from '$libs/chain';
import { network } from '$stores/network';
function handleSourceChange(): void {
Expand All @@ -28,8 +29,30 @@
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;
}
const currentNetwork: number = Number($network.id);
const chainKeys: number[] = Object.keys(chainConfig).map(Number);
// only allow switching between two chains, if we have more we do not use this util
if (chainKeys.length !== 2) {
return null;
}
const alternateChainId = chainKeys.find((key) => key !== currentNetwork);
if (!alternateChainId) return null;
return alternateChainId;
};
onMount(() => {
updateDestOptions();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as ChainSelector } from './ChainSelector.svelte';
export { default as ChainSelectorWrapper } from './ChainSelectorWrapper.svelte';
8 changes: 8 additions & 0 deletions packages/bridge-ui-v2/src/libs/chain/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function mapChainConfigToChain(chainId: string, chainConfig: ChainConfigMap[numb
};
}

export const chainIdToChain = (chainId: number): Chain => {
const chain = chains.find((chain) => chain.id === chainId);
if (!chain) {
throw new Error(`Chain with id ${chainId} not found`);
}
return chain;
};

export const chains: Chain[] = Object.entries(chainConfig).map(([chainId, chainConfig]) =>
mapChainConfigToChain(chainId, chainConfig),
);
Expand Down

1 comment on commit 87ed466

@vercel
Copy link

@vercel vercel bot commented on 87ed466 Sep 29, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

bridge-ui-v2-internal – ./packages/bridge-ui-v2

bridge-ui-v2-internal-taikoxyz.vercel.app
bridge-ui-v2-internal.vercel.app
bridge-ui-v2-internal-git-main-taikoxyz.vercel.app

Please sign in to comment.