Skip to content

Commit

Permalink
Switch network
Browse files Browse the repository at this point in the history
  • Loading branch information
Corantin committed Jul 5, 2023
1 parent ee0e4dd commit 41965f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 1 addition & 2 deletions packages/react-app/src/components/views/quest-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const ScrollLabelStyled = styled.div`
`;

export default function QuestList() {
const { walletAddress, walletConnected } = useWallet();
const { walletAddress } = useWallet();
const [quests, setQuests] = useState<QuestModel[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [newQuestLoading, setNewQuestLoading] = useState(false);
Expand Down Expand Up @@ -172,7 +172,6 @@ export default function QuestList() {
setHasMore(res.length >= QUESTS_PAGE_SIZE);
},
);
console.log(walletAddress, walletConnected);
}
};

Expand Down
36 changes: 20 additions & 16 deletions packages/react-app/src/contexts/wallet.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,28 @@ type Props = {
function WalletAugmented({ children }: Props) {
const wallet = useWallet();
const ethereum = wallet?.ethereum;
const [isWrongNetwork, setIsWrongNetwork] = useState<boolean>(false);
const isWrongChainError = (window as any).globalError?.name === 'ChainUnknownError';
const [isWrongNetwork, setIsWrongNetwork] = useState<boolean>(isWrongChainError);
const [activatingId, setActivating] = useState<string>();
const [isConnected, setIsConnected] = useState(false);
const [walletConnectOpened, openWalletConnect] = useState<boolean>(false);
const { chainId, networkId } = getNetwork();
let timeoutInstance: number | undefined;

useEffect(() => {
if (!isWrongChainError) {
const lastWalletConnected = localStorage.getItem('LAST_WALLET_CONNECTOR');
if (lastWalletConnected) {
handleConnect(lastWalletConnected);
}
}

const handleErr = (ev: ErrorEvent) => {
if (ev.error.name === 'ChainUnknownError') {
(window as any).globalError = ev.error;
}
};
window.addEventListener('error', handleErr);
const globalErrorName = (window as any).globalError?.name;
if (globalErrorName === 'ChainUnknownError') {
setIsWrongNetwork(true);
setIsConnected(false);
setActivating(undefined);
} else {
const lastWalletConnected = localStorage.getItem('LAST_WALLET_CONNECTOR');
if (lastWalletConnected) {
handleConnect(lastWalletConnected);
}
}

return () => {
window.removeEventListener('error', handleErr);
Expand Down Expand Up @@ -143,9 +140,16 @@ function WalletAugmented({ children }: Props) {
if (!newChainId) {
newChainId = chainId;
}
if (ethereum && EXPECTED_CHAIN_ID.includes(newChainId) && +ethereum.chainId !== newChainId) {

const ethConnection = ethereum ?? (window as any).ethereum;

if (
ethConnection &&
EXPECTED_CHAIN_ID.includes(newChainId) &&
+ethConnection.chainId !== newChainId
) {
try {
await ethereum.request({
await ethConnection.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: Web3.utils.toHex(newChainId) }],
});
Expand All @@ -154,7 +158,7 @@ function WalletAugmented({ children }: Props) {
if (error.code === 4902) {
const network = getNetwork();
try {
await ethereum.request({
await ethConnection.request({
method: 'wallet_addEthereumChain',
params: [
{
Expand Down Expand Up @@ -205,7 +209,7 @@ function WalletAugmented({ children }: Props) {
function WalletProvider({ children }: Props) {
const connectors = getUseWalletConnectors();
return (
<UseWalletProvider connectors={connectors} autoConnect>
<UseWalletProvider connectors={connectors}>
<WalletAugmented>{children}</WalletAugmented>
</UseWalletProvider>
);
Expand Down

0 comments on commit 41965f0

Please sign in to comment.