Replies: 1 comment
-
I found this workaround for now: import { providers } from "ethers";
...
const { activeConnector } = useConnect();
const mint = async () => {
if (!activeConnector) {
throw new Error("Wallet not connected");
}
...
if ((await activeConnector.getChainId()) !== 4) {
if (activeConnector.switchChain) {
await activeConnector.switchChain(4);
} else {
throw new Error("Wrong network");
}
}
...
const tx = await myContract.connect(await activeConnector.getSigner()).mint();
const receipt = await tx.wait();
return receipt;
}; However, on Rainbow for iOS, this will switch to Rainbow to prompt for a network change but throws |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When calling a contract's write function, I will first check what network you are connected to and, if on the wrong network, prompt you to switch network (via
provider.send('wallet_switchEthereumChain')
). When switching between development and production environments, or moving between L1 and L2 apps, this helps prevent you from accidentally throwing gas into the void.As a UX measure, this happens ~synchronously alongside the transaction call so that one button click will ensure you're on the right network and fire off the transaction. The method tends to look something like this:
(See also https://twitter.com/frolic/status/1509574713739149313)
I've recently started using wagmi on a project and noticed that there are nice async methods like
connectAsync
andswitchNetworkAsync
, but using them inline like above results in errors like:Would it be possible for these async methods to return the latest provider instance post-connect or post-network-switch?
I also tried something like
I should note that in an ideal world, wallets would handle this for us and let us pass a chain ID along with the contract address and ensure that these calls only get executed on the correct chain. I'd be open to a similar approach in wagmi, but would want it to be compatible with my generated TypeChain types.
Edit: This is probably a better approach and accomplishes the same goals: #396
Related:
Beta Was this translation helpful? Give feedback.
All reactions