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 Vortex automatically tries to switch to polygon network #436

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
2 changes: 1 addition & 1 deletion src/components/Nabla/useSwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const useSwapForm = () => {

const network = getCaseSensitiveNetwork(initialValues.network);
if (network !== selectedNetwork) {
setSelectedNetwork(network);
setSelectedNetwork(network, true);
}

const initialFromToken = getInputTokenDetails(network, initialValues.from as InputTokenType);
Expand Down
2 changes: 1 addition & 1 deletion src/components/NetworkSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const NetworkSelector = ({ disabled }: { disabled?: boolean }) => {
useClickOutside(dropdownRef, () => setIsOpen(false));

const handleNetworkSelect = (network: Networks) => {
setSelectedNetwork(network);
setSelectedNetwork(network, true);
setIsOpen(false);
};

Expand Down
27 changes: 17 additions & 10 deletions src/contexts/network.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext } from 'preact';
import { useContext, useState, useEffect, useCallback } from 'preact/hooks';
import { useSwitchChain } from 'wagmi';
import { useAccount, useSwitchChain } from 'wagmi';
import { useLocalStorage, LocalStorageKeys } from '../hooks/useLocalStorage';
import { WALLETCONNECT_ASSETHUB_ID } from '../constants/constants';
import { useOfframpActions } from '../stores/offrampStore';
Expand All @@ -10,15 +10,15 @@ import { useSep24Actions } from '../stores/sep24Store';
interface NetworkContextType {
walletConnectPolkadotSelectedNetworkId: string;
selectedNetwork: Networks;
setSelectedNetwork: (network: Networks) => void;
setSelectedNetwork: (network: Networks, resetState?: boolean) => Promise<void>;
networkSelectorDisabled: boolean;
setNetworkSelectorDisabled: (disabled: boolean) => void;
}

const NetworkContext = createContext<NetworkContextType>({
walletConnectPolkadotSelectedNetworkId: WALLETCONNECT_ASSETHUB_ID,
selectedNetwork: Networks.AssetHub,
setSelectedNetwork: () => null,
setSelectedNetwork: async () => undefined,
networkSelectorDisabled: false,
setNetworkSelectorDisabled: () => null,
});
Expand All @@ -38,21 +38,28 @@ export const NetworkProvider = ({ children }: NetworkProviderProps) => {

const { resetOfframpState } = useOfframpActions();
const { cleanup: cleanupSep24Variables } = useSep24Actions();
const { switchChain } = useSwitchChain();
const { switchChainAsync } = useSwitchChain();
const { chain: connectedEvmChain } = useAccount();

const setSelectedNetwork = useCallback(
(network: Networks) => {
resetOfframpState();
cleanupSep24Variables();
async (network: Networks, resetState = false) => {
if (resetState) {
resetOfframpState();
cleanupSep24Variables();
}
setSelectedNetworkState(network);
setSelectedNetworkLocalStorage(network);

// Will only switch chain on the EVM conneted wallet case.
// Will only switch chain on the EVM connected wallet case.
if (isNetworkEVM(network)) {
switchChain({ chainId: getNetworkId(network) });
// Only switch chain if the network is different from the current one
// see https://github.com/wevm/wagmi/issues/3417
if (!connectedEvmChain || connectedEvmChain.id !== getNetworkId(network)) {
await switchChainAsync({ chainId: getNetworkId(network) });
}
}
},
[switchChain, setSelectedNetworkLocalStorage, resetOfframpState, cleanupSep24Variables],
[connectedEvmChain, switchChainAsync, setSelectedNetworkLocalStorage, resetOfframpState, cleanupSep24Variables],
);

// Only run on first render
Expand Down
15 changes: 3 additions & 12 deletions src/hooks/offramp/useSubmitOfframp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useCallback } from 'preact/compat';
import { polygon } from 'wagmi/chains';
import { useSwitchChain } from 'wagmi';

import { getNetworkId, isNetworkEVM } from '../../helpers/networks';
import { useVortexAccount } from '../useVortexAccount';
import { useNetwork } from '../../contexts/network';
import { useEventsContext } from '../../contexts/events';
Expand All @@ -22,8 +19,7 @@ import { useSep24Actions } from '../../stores/sep24Store';
import { showToast, ToastMessage } from '../../helpers/notifications';

export const useSubmitOfframp = () => {
const { selectedNetwork } = useNetwork();
const { switchChainAsync, switchChain } = useSwitchChain();
const { selectedNetwork, setSelectedNetwork } = useNetwork();
const { trackEvent } = useEventsContext();
const { address } = useVortexAccount();
const { checkAndWaitForSignature, forceRefreshAndWaitForSignature } = useSiweContext();
Expand All @@ -48,7 +44,6 @@ export const useSubmitOfframp = () => {
}

(async () => {
switchChain({ chainId: polygon.id });
setOfframpStarted(true);

trackEvent({
Expand All @@ -60,10 +55,7 @@ export const useSubmitOfframp = () => {
});

try {
// For substrate, we only have AssetHub only now. Thus no need to change.
if (isNetworkEVM(selectedNetwork)) {
await switchChainAsync({ chainId: getNetworkId(selectedNetwork) });
}
await setSelectedNetwork(selectedNetwork);

setOfframpStarted(true);

Expand Down Expand Up @@ -142,7 +134,6 @@ export const useSubmitOfframp = () => {
offrampStarted,
offrampState,
setOfframpInitiating,
switchChain,
setOfframpStarted,
trackEvent,
selectedNetwork,
Expand All @@ -154,7 +145,7 @@ export const useSubmitOfframp = () => {
setInitialResponseSEP24,
setUrlIntervalSEP24,
cleanupSEP24,
switchChainAsync,
setSelectedNetwork,
],
);
};
Loading