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

fix(bridge-ui): set to null when chainID is not one of the two supported, so prevchain can be checked #14468

Merged
merged 2 commits into from
Aug 14, 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
38 changes: 38 additions & 0 deletions packages/bridge-ui/src/utils/switchNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
import { get } from 'svelte/store';
import { switchNetwork as wagmiSwitchNetwork } from 'wagmi/actions';

import { srcChain } from '../store/chain';
import { Deferred } from './Deferred';

export async function switchNetwork(chainId: number) {
const prevChainId = get(srcChain)?.id;

if (prevChainId === chainId) return;

await wagmiSwitchNetwork({ chainId });

// What are we doing here? we have a watcher waiting for network changes.
// When this happens this watcher is called and takes care of setting
// the signer and chains in the store. We are actually waiting here
// for these stores to change due to some race conditions in the UI.
// There will be a better design around this in alpha-4: fewer stores
// and '$:' tags. They're evil.
dionysuzx marked this conversation as resolved.
Show resolved Hide resolved
const deferred = new Deferred<void>();

// This will prevent an unlikely infinite loop
const starting = Date.now();
const timeout = 5000; // TODO: config?

const waitForNetworkChange = () => {
const srcChainId = get(srcChain)?.id;

if (srcChainId && srcChainId !== prevChainId) {
// We have finally set the chain in the store. We're done here.
deferred.resolve();
} else if (Date.now() > starting + timeout) {
// Wait, what???
dionysuzx marked this conversation as resolved.
Show resolved Hide resolved
dionysuzx marked this conversation as resolved.
Show resolved Hide resolved
deferred.reject(new Error('timeout switching network'));
} else {
setTimeout(waitForNetworkChange, 300); // TODO: config those 300?
}
};

waitForNetworkChange();

return deferred.promise;
}
2 changes: 2 additions & 0 deletions packages/bridge-ui/src/wagmi/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const setChain = (chainId: number) => {

log(`Network switched to ${L2Chain.name}`);
} else {
srcChain.set(null);
destChain.set(null);
isSwitchChainModalOpen.set(true);
}
};
Expand Down