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

refactor: Simplify handleNetworkSwitch helper #6878

Merged
merged 1 commit into from
Jul 27, 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
7 changes: 2 additions & 5 deletions app/components/Nav/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Logger from '../../../util/Logger';
import { trackErrorAsAnalytics } from '../../../util/analyticsV2';
import { routingInstrumentation } from '../../../util/sentryUtils';
import Analytics from '../../../core/Analytics/Analytics';
import { connect, useSelector, useDispatch } from 'react-redux';
import { connect, useDispatch } from 'react-redux';
import {
CURRENT_APP_VERSION,
EXISTING_USER,
Expand Down Expand Up @@ -86,7 +86,6 @@ import EditAccountName from '../../Views/EditAccountName/EditAccountName';
import WC2Manager, {
isWC2Enabled,
} from '../../../../app/core/WalletConnect/WalletConnectV2';
import { selectFrequentRpcList } from '../../../selectors/preferencesController';

const clearStackNavigatorOptions = {
headerShown: false,
Expand Down Expand Up @@ -232,7 +231,6 @@ const App = ({ userLoggedIn }) => {
dispatch(setCurrentBottomNavRoute(route));
}
};
const frequentRpcList = useSelector(selectFrequentRpcList);

useEffect(() => {
if (prevNavigator.current || !navigator) return;
Expand Down Expand Up @@ -315,7 +313,6 @@ const App = ({ userLoggedIn }) => {
navigator.dispatch?.(CommonActions.navigate(params));
},
},
frequentRpcList,
dispatch,
});
if (!prevNavigator.current) {
Expand All @@ -336,7 +333,7 @@ const App = ({ userLoggedIn }) => {
}
prevNavigator.current = navigator;
}
}, [dispatch, handleDeeplink, frequentRpcList, navigator]);
}, [dispatch, handleDeeplink, navigator]);

useEffect(() => {
const initAnalytics = async () => {
Expand Down
12 changes: 7 additions & 5 deletions app/components/Views/SendFlow/AddressTo/AddressTo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { NetworkSwitchErrorType } from '../../../../constants/error';
import Routes from '../../../../constants/navigation/Routes';
import Engine from '../../../../core/Engine';
import { selectNetwork } from '../../../../selectors/networkController';
import { selectFrequentRpcList } from '../../../../selectors/preferencesController';
import { handleNetworkSwitch } from '../../../../util/networks';
import { AddressTo } from '../../../UI/AddressInputs';
import { createQRScannerNavDetails } from '../../QRScanner';
Expand All @@ -34,16 +33,19 @@ const SendFlowAddressTo = ({

const network = useSelector(selectNetwork);

const frequentRpcList = useSelector(selectFrequentRpcList);

const showAlertAction = (config: any) => dispatch(showAlert(config));

const onHandleNetworkSwitch = (chain_id: string) => {
try {
const { NetworkController, CurrencyRateController } = Engine.context;
const networkSwitch = handleNetworkSwitch(chain_id, frequentRpcList, {
const {
NetworkController,
CurrencyRateController,
PreferencesController,
} = Engine.context;
const networkSwitch = handleNetworkSwitch(chain_id, {
networkController: NetworkController,
currencyRateController: CurrencyRateController,
preferencesController: PreferencesController,
});

if (!networkSwitch) return;
Expand Down
17 changes: 8 additions & 9 deletions app/components/Views/SendFlow/SendTo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import {
selectTicker,
} from '../../../../selectors/networkController';
import {
selectFrequentRpcList,
selectIdentities,
selectSelectedAddress,
} from '../../../../selectors/preferencesController';
Expand Down Expand Up @@ -139,10 +138,6 @@ class SendFlow extends PureComponent {
* Indicates whether the current transaction is a deep link transaction
*/
isPaymentRequest: PropTypes.bool,
/**
* Frequent RPC list from PreferencesController
*/
frequentRpcList: PropTypes.array,
/**
* Boolean that indicates if the network supports buy
*/
Expand Down Expand Up @@ -253,11 +248,16 @@ class SendFlow extends PureComponent {

handleNetworkSwitch = (chainId) => {
try {
const { NetworkController, CurrencyRateController } = Engine.context;
const { showAlert, frequentRpcList } = this.props;
const network = handleNetworkSwitch(chainId, frequentRpcList, {
const {
NetworkController,
CurrencyRateController,
PreferencesController,
} = Engine.context;
const { showAlert } = this.props;
const network = handleNetworkSwitch(chainId, {
networkController: NetworkController,
currencyRateController: CurrencyRateController,
preferencesController: PreferencesController,
});

if (!network) return;
Expand Down Expand Up @@ -642,7 +642,6 @@ const mapStateToProps = (state) => ({
network: selectNetwork(state),
providerType: selectProviderType(state),
isPaymentRequest: state.transaction.paymentRequest,
frequentRpcList: selectFrequentRpcList(state),
isNativeTokenBuySupported: isNetworkBuyNativeTokenSupported(
selectChainId(state),
getRampNetworks(state),
Expand Down
12 changes: 6 additions & 6 deletions app/core/DeeplinkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ import { isNetworkBuySupported } from '../components/UI/Ramp/utils';
import { Minimizer } from './NativeModules';

class DeeplinkManager {
constructor({ navigation, frequentRpcList, dispatch }) {
constructor({ navigation, dispatch }) {
this.navigation = navigation;
this.pendingDeeplink = null;
this.frequentRpcList = frequentRpcList;
this.dispatch = dispatch;
}

Expand All @@ -48,10 +47,12 @@ class DeeplinkManager {
* @param switchToChainId - Corresponding chain id for new network
*/
_handleNetworkSwitch = (switchToChainId) => {
const { NetworkController, CurrencyRateController } = Engine.context;
const network = handleNetworkSwitch(switchToChainId, this.frequentRpcList, {
const { NetworkController, CurrencyRateController, PreferencesController } =
Engine.context;
const network = handleNetworkSwitch(switchToChainId, {
networkController: NetworkController,
currencyRateController: CurrencyRateController,
preferencesController: PreferencesController,
});

if (!network) return;
Expand Down Expand Up @@ -432,13 +433,12 @@ class DeeplinkManager {
let instance = null;

const SharedDeeplinkManager = {
init: ({ navigation, frequentRpcList, dispatch }) => {
init: ({ navigation, dispatch }) => {
if (instance) {
return;
}
instance = new DeeplinkManager({
navigation,
frequentRpcList,
dispatch,
});
},
Expand Down
23 changes: 14 additions & 9 deletions app/util/networks/handleNetworkSwitch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { getNetworkTypeById } from './index';
import type { NetworkController } from '@metamask/network-controller';
import type { PreferencesController } from '@metamask/preferences-controller';
import type { CurrencyRateController } from '@metamask/assets-controllers';

const handleNetworkSwitch = (
switchToChainId: string,
frequentRpcList: {
rpcUrl: string;
chainId: string;
ticker: string;
nickname: string;
}[],
{ networkController, currencyRateController }: any,
{
networkController,
currencyRateController,
preferencesController,
}: {
networkController: NetworkController;
currencyRateController: CurrencyRateController;
preferencesController: PreferencesController;
},
): string | undefined => {
// If not specified, use the current network
if (!switchToChainId) {
Expand All @@ -23,8 +28,8 @@ const handleNetworkSwitch = (
return;
}

const rpc = frequentRpcList.find(
({ chainId }: { chainId: string }) => chainId === switchToChainId,
const rpc = preferencesController.state.frequentRpcList.find(
({ chainId }) => chainId === switchToChainId,
);

if (rpc) {
Expand Down
20 changes: 11 additions & 9 deletions app/util/networks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jest.mock('./../../core/Engine', () => ({
},
},
},
PreferencesController: {
state: {},
},
},
}));

Expand Down Expand Up @@ -201,18 +204,17 @@ describe('NetworkUtils::handleNetworkSwitch', () => {
},
];

const { NetworkController, CurrencyRateController } = Engine.context as any;
const { NetworkController, CurrencyRateController, PreferencesController } =
Engine.context as any;

it('should change networks to the provided one', () => {
const network = mockRPCFrequentList[0];
const newNetwork = handleNetworkSwitch(
network.chainId,
mockRPCFrequentList,
{
networkController: NetworkController,
currencyRateController: CurrencyRateController,
},
);
PreferencesController.state.frequentRpcList = mockRPCFrequentList;
const newNetwork = handleNetworkSwitch(network.chainId, {
networkController: NetworkController,
currencyRateController: CurrencyRateController,
preferencesController: PreferencesController,
});
expect(newNetwork).toBe(network.nickname);
});
});
Expand Down
Loading