Skip to content

Commit

Permalink
test: does wl support network change? (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra authored Jan 19, 2022
1 parent f2e8f23 commit c964497
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/components/TxConfirmationView/NetworkMismatch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { getNetworkConfig } from '../../../helpers/config/markets-and-network-co
import messages from './messages';
import staticStyles from './style';
import { ChainId } from '@aave/contract-helpers';
import { useWeb3React } from '@web3-react/core';
import { providers } from 'ethers';

interface NetworkMismatchProps {
neededChainId: ChainId;
Expand Down Expand Up @@ -73,12 +75,13 @@ export default function NetworkMismatch({
}: NetworkMismatchProps) {
const intl = useIntl();
const { currentTheme } = useThemeContext();
const { library } = useWeb3React<providers.Web3Provider>();
const { handleNetworkChange } = useUserWalletDataContext();

const config = ADD_CONFIG[neededChainId];
const isAddable =
(global.window as any)?.ethereum?.isMetaMask &&
['browser'].includes(currentProviderName) &&
['browser', 'wallet-link'].includes(currentProviderName) &&
config;
const { publicJsonRPCWSUrl, publicJsonRPCUrl } = getNetworkConfig(neededChainId);

Expand Down Expand Up @@ -126,28 +129,32 @@ export default function NetworkMismatch({
<DefaultButton
title={intl.formatMessage(messages.changeNetwork)}
onClick={async () => {
try {
await (window as any).ethereum?.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: `0x${neededChainId.toString(16)}` }],
});
} catch (switchError) {
if (switchError.code === 4902) {
try {
await (window as any).ethereum?.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: `0x${neededChainId.toString(16)}`,
chainName: config.name,
nativeCurrency: config.nativeCurrency,
rpcUrls: [...publicJsonRPCUrl, publicJsonRPCWSUrl],
blockExplorerUrls: config.explorerUrls,
},
],
});
} catch (addError) {
// TODO: handle error somehow
if (library) {
try {
await library.provider.request!({
method: 'wallet_switchEthereumChain',
params: [{ chainId: `0x${neededChainId.toString(16)}` }],
});
} catch (switchError) {
console.log(switchError);
if (switchError.code === 4902) {
try {
await library.provider.request!({
method: 'wallet_addEthereumChain',
params: [
{
chainId: `0x${neededChainId.toString(16)}`,
chainName: config.name,
nativeCurrency: config.nativeCurrency,
rpcUrls: [...publicJsonRPCUrl, publicJsonRPCWSUrl],
blockExplorerUrls: config.explorerUrls,
},
],
});
} catch (addError) {
console.log(addError);
// TODO: handle error somehow
}
}
}
}
Expand Down

2 comments on commit c964497

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

Please sign in to comment.