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

chore(bridge-ui-v2): pre-select destination chain #14850

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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`);
KorbinianK marked this conversation as resolved.
Show resolved Hide resolved
}
return chain;
};

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