Skip to content

Commit

Permalink
fix: ensure correct connected network (#47)
Browse files Browse the repository at this point in the history
* fix: ensure correct connected network

* chore: remove debris
  • Loading branch information
begonaalvarezd authored Aug 2, 2024
1 parent e8d0ec9 commit 01b7538
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/lib/withdraw/actions/connect-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { iscAbi, iscContractAddress } from '$lib/withdraw';
import { WTOKEN_CONTRACT_CHAIN_MAP, wIOTAAbi, wSMRAbi, wToken } from '$lib/wrap';
import { defaultEvmStores, selectedAccount, web3 } from 'svelte-web3';
import { get } from 'svelte/store';
import { addSelectedNetworkToMetamask, subscribeBalance } from '.';
import { addSelectedNetworkToMetamask, subscribeBalance, subscribeConnectedNetwork } from '.';
import { updateWithdrawStateStore, withdrawStateStore } from '../stores';

export async function connectToWallet() {
Expand Down Expand Up @@ -35,6 +35,7 @@ export async function connectToWallet() {
const wTokenContractObj = new wToken(get(withdrawStateStore)?.contractWToken);
updateWithdrawStateStore({ wTokenContractObj });

await subscribeConnectedNetwork();
await subscribeBalance();
} catch (ex) {
console.error('Failed to connect to wallet: ', ex.message);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/withdraw/actions/disconnect-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defaultEvmStores } from 'svelte-web3';

import { unsubscribeBalance } from './subscriptions';
import { unsubscribeConnectedNetwork, unsubscribeBalance } from './subscriptions';

export async function disconnectWallet(): Promise<void> {
await defaultEvmStores.disconnect();
unsubscribeConnectedNetwork();
unsubscribeBalance();
}
31 changes: 31 additions & 0 deletions src/lib/withdraw/actions/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async';
import { chainId, connected, selectedAccount } from 'svelte-web3';
import { get } from 'svelte/store';

import { updateWithdrawStateStore, withdrawStateStore } from '../stores';
import { pollAccount } from './polls';


import { selectedNetwork } from '$lib/evm-toolkit';
import { NotificationType, showNotification } from '$lib/notification';
import { disconnectWallet } from '.';

export async function subscribeBalance() {
await unsubscribeBalance();
const $withdrawStateStore = get(withdrawStateStore);
Expand All @@ -28,3 +34,28 @@ export async function unsubscribeBalance() {
balancePollingHandle: undefined,
});
}

let connectedNetworkInterval: NodeJS.Timeout | null = null;
export async function subscribeConnectedNetwork() {
connectedNetworkInterval = setInterval(async () => {
if (get(connected) || get(selectedAccount)) {
if (BigInt(get(chainId)) !== BigInt(get(selectedNetwork)?.chainID)) {
try {
await disconnectWallet();
} catch (e) {
showNotification({
type: NotificationType.Error,
message: e,
});
console.error(e);
}
}
}
}, 1000);
}

export async function unsubscribeConnectedNetwork() {
if (connectedNetworkInterval) {
clearInterval(connectedNetworkInterval);
}
}

0 comments on commit 01b7538

Please sign in to comment.