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

feat(bridge): autoconnect wallet on load #6643

Merged
merged 2 commits into from
Jan 3, 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
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