Skip to content

Commit

Permalink
feat(bridge): autoconnect wallet on load (#6643)
Browse files Browse the repository at this point in the history
Co-authored-by: dave | d1onys1us <13951458+d1onys1us@users.noreply.github.com>
  • Loading branch information
shadab-taiko and d1onys1us committed Jan 3, 2023
1 parent 409e7a4 commit 1332aa2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/bridge-ui/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
const {
chains: wagmiChains,
provider,
webSocketProvider,
} = configureChains(
[mainnet, taiko],
[
Expand All @@ -83,6 +82,7 @@
);
$wagmiClient = createClient({
autoConnect: true,
provider,
connectors: [
new MetaMaskConnector({
Expand Down
6 changes: 4 additions & 2 deletions packages/bridge-ui/src/components/AddressDropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
let addressAvatarImgData: string = "";
let tokenBalance: string = "";
onMount(async () => {
await setAddress($signer);
onMount(() => {
(async () => {
await setAddress($signer);
})();
});
$: getUserBalance($signer);
Expand Down
53 changes: 28 additions & 25 deletions packages/bridge-ui/src/components/buttons/Connect.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { onDestroy } from "svelte";
import { BigNumber, ethers } from "ethers";
import { onDestroy, onMount } from "svelte";
import { signer } from "../../store/signer";
import { _ } from "svelte-i18n";
import {
Expand All @@ -9,7 +8,9 @@
fetchSigner,
watchAccount,
watchNetwork,
ConnectorNotFoundError
ConnectorNotFoundError,
getNetwork,
getAccount
} from "@wagmi/core";
import { CHAIN_MAINNET, CHAIN_TKO } from "../../domain/chain";
Expand Down Expand Up @@ -38,30 +39,32 @@
}
};
let unwatchNetwork;
let unwatchAccount;
async function setSigner() {
const wagmiSigner = await fetchSigner();
signer.set(wagmiSigner);
return wagmiSigner;
}
async function onConnect() {
const { chain } = getNetwork();
await setSigner();
await changeChain(chain.id);
watchNetwork(
async (network) => await changeChain(network.chain.id)
);
watchAccount(async () => {
const s = await setSigner();
transactions.set(
await $transactioner.GetAllByAddress(await s.getAddress())
);
});
}
async function connectWithConnector(connector: Connector) {
try {
const { chain } = await wagmiConnect({ connector });
await setSigner();
await changeChain(chain.id);
unwatchNetwork = watchNetwork(
async (network) => await changeChain(network.chain.id)
);
unwatchAccount = watchAccount(async () => {
const s = await setSigner();
transactions.set(
await $transactioner.GetAllByAddress(await s.getAddress())
);
});
successToast("Connected");
await wagmiConnect({ connector });
await onConnect();
successToast("Connected");
} catch(error) {
if(error instanceof ConnectorNotFoundError) {
errorToast(`${connector.name} not installed`);
Expand All @@ -77,12 +80,12 @@
"coinbase wallet": CoinbaseWallet,
};
onDestroy(() => {
if (unwatchNetwork) {
unwatchNetwork();
}
if (unwatchAccount) {
unwatchAccount();
onMount(() => {
const account = getAccount();
if(account.isConnected) {
(async () => {
await onConnect();
})();
}
});
</script>
Expand Down

0 comments on commit 1332aa2

Please sign in to comment.