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 <[email protected]>
  • Loading branch information
KorbinianK and davidtaikocha committed Oct 2, 2023
1 parent 745c196 commit e900dd9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/bridge-ui-v2/src/components/Bridge/Bridge.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,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
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';
let destChainElement: ChainSelector;
Expand All @@ -30,8 +31,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

0 comments on commit e900dd9

Please sign in to comment.