Skip to content

Commit

Permalink
fix(bridge-ui): toggle chain (#395)
Browse files Browse the repository at this point in the history
* toggle chain fix

* chain dropdown
  • Loading branch information
cyberhorsey authored Dec 9, 2022
1 parent c895430 commit 2d45b9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
12 changes: 9 additions & 3 deletions packages/bridge-ui/src/components/ChainDropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { _ } from "svelte-i18n";
import ChevDown from "./icons/ChevDown.svelte";
import { fromChain } from "../store/chain";
import { fromChain, toChain } from "../store/chain";
import MetaMask from "./icons/MetaMask.svelte";
import { switchEthereumChain } from "../utils/switchEthereumChain";
import { ethereum } from "../store/ethereum";
Expand All @@ -15,6 +15,12 @@
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
fromChain.set(chain);
if (chain === CHAIN_MAINNET) {
toChain.set(CHAIN_TKO);
} else {
toChain.set(CHAIN_MAINNET);
}
signer.set(provider.getSigner());
};
</script>
Expand Down Expand Up @@ -45,7 +51,7 @@
>
<svelte:component this={CHAIN_MAINNET.icon} height={24} />
<span class="pl-1.5 text-left flex-1">{CHAIN_MAINNET.name}</span>
<MetaMask />
<MetaMask />
</button>
</li>
<li>
Expand All @@ -57,7 +63,7 @@
>
<svelte:component this={CHAIN_TKO.icon} height={24} />
<span class="pl-1.5 text-left flex-1">{CHAIN_TKO.name}</span>
<MetaMask />
<MetaMask />
</button>
</li>
</ul>
Expand Down
26 changes: 18 additions & 8 deletions packages/bridge-ui/src/components/form/SelectChain.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
import ArrowRight from "../icons/ArrowRight.svelte";
import { fromChain, toChain } from "../../store/chain";
import { CHAIN_MAINNET, CHAIN_TKO } from "../../domain/chain";
import { ethereum } from "../../store/ethereum";
import { signer } from "../../store/signer";
import { switchEthereumChain } from "../../utils/switchEthereumChain";
import { ethers } from "ethers";
function toggleChains() {
fromChain.update((val) =>
val === CHAIN_MAINNET ? CHAIN_TKO : CHAIN_MAINNET
);
toChain.update((val) =>
val === CHAIN_MAINNET ? CHAIN_TKO : CHAIN_MAINNET
);
}
const toggleChains = async () => {
const chain = $fromChain === CHAIN_MAINNET ? CHAIN_TKO : CHAIN_MAINNET;
await switchEthereumChain($ethereum, chain);
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
fromChain.set(chain);
if (chain === CHAIN_MAINNET) {
toChain.set(CHAIN_TKO);
} else {
toChain.set(CHAIN_MAINNET);
}
signer.set(provider.getSigner());
};
</script>

<div class="flex items-center justify-between w-full p-8 py-6 text-lg">
Expand Down

0 comments on commit 2d45b9c

Please sign in to comment.