Skip to content

Commit

Permalink
fix: fix rpc selector
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Sep 30, 2024
1 parent 407eff7 commit 9fb2094
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 46 deletions.
111 changes: 70 additions & 41 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
selectNetworkConfigurations,
selectProviderConfig,
ProviderConfig,
selectNetworkClientId,
} from '../../../selectors/networkController';
import { selectShowTestNetworks } from '../../../selectors/preferencesController';
import Networks, {
Expand Down Expand Up @@ -130,7 +129,6 @@ const NetworkSelector = () => {

const providerConfig: ProviderConfig = useSelector(selectProviderConfig);
const networkConfigurations = useSelector(selectNetworkConfigurations);
const selectedNetworkClientId = useSelector(selectNetworkClientId);

const route =
useRoute<RouteProp<Record<string, NetworkSelectorRouteParams>, string>>();
Expand Down Expand Up @@ -162,11 +160,39 @@ const NetworkSelector = () => {
isReadOnly: false,
});

const onRpcSelect = (clientId: string) => {
const { NetworkController } = Engine.context;
const onRpcSelect = useCallback(
async (clientId: string, chainId: `0x${string}`) => {
const { NetworkController } = Engine.context;

NetworkController.setActiveNetwork(clientId);
};
const existingNetwork = networkConfigurations[chainId];
if (!existingNetwork) {
console.error(`No existing network found for chainId: ${chainId}`);
return;
}

const indexOfRpc = existingNetwork.rpcEndpoints.findIndex(
({ networkClientId }) => clientId === networkClientId,
);

// Handle case where indexOfRpc is -1
if (indexOfRpc === -1) {
console.error(
`RPC endpoint with clientId: ${clientId} not found for chainId: ${chainId}`,
);
return;
}

// Proceed to update the network with the correct index
await NetworkController.updateNetwork(existingNetwork.chainId, {
...existingNetwork,
defaultRpcEndpointIndex: indexOfRpc,
});

// Set the active network
NetworkController.setActiveNetwork(clientId);
},
[networkConfigurations], // Dependencies
);

const [showMultiRpcSelectModal, setShowMultiRpcSelectModal] = useState<{
isVisible: boolean;
Expand Down Expand Up @@ -723,20 +749,19 @@ const NetworkSelector = () => {
};

const renderBottomSheetRpc = useCallback(() => {
let imageSource;
if (!showMultiRpcSelectModal.isVisible) return null;

if (showMultiRpcSelectModal.chainId === CHAIN_IDS.MAINNET) {
imageSource = images.ETHEREUM;
} else if (showMultiRpcSelectModal.chainId === CHAIN_IDS.LINEA_MAINNET) {
imageSource = images['LINEA-MAINNET'];
} else {
//@ts-expect-error - The utils/network file is still JS and this function expects a networkType, and should be optional
imageSource = getNetworkImageSource({
chainId: showMultiRpcSelectModal?.chainId?.toString(),
});
}
const chainId = showMultiRpcSelectModal.chainId;
const imageSource =
chainId === CHAIN_IDS.MAINNET
? images.ETHEREUM
: chainId === CHAIN_IDS.LINEA_MAINNET
? images['LINEA-MAINNET']
: //@ts-expect-error - The utils/network file is still JS and this function expects a networkType, and should be optional
getNetworkImageSource({ chainId: chainId?.toString() });

if (!showMultiRpcSelectModal.isVisible) return null;
const rpcEndpoints =
networkConfigurations[chainId as `0x${string}`]?.rpcEndpoints || [];

return (
<BottomSheet
Expand All @@ -747,7 +772,7 @@ const NetworkSelector = () => {
<BottomSheetHeader style={styles.baseHeader}>
<View style={styles.baseTitle}>
<Text variant={TextVariant.HeadingMD}>
{strings('app_settings.select_rpc_url')}{' '}
{strings('app_settings.select_rpc_url')}
</Text>
</View>

Expand All @@ -768,27 +793,32 @@ const NetworkSelector = () => {
</Text>
</Cell>
</BottomSheetHeader>

<View style={styles.rpcMenu}>
{networkConfigurations[providerConfig.chainId].rpcEndpoints?.map(
({ url, networkClientId }, index) => (
<ListItemSelect
key={index}
isSelected={networkClientId === selectedNetworkClientId}
isDisabled={false}
gap={8}
onPress={() => {
onRpcSelect(networkClientId);
closeRpcModal();
}}
>
<View style={styles.rpcText}>
<Text style={styles.textCentred}>
{hideKeyFromUrl(hideProtocolFromUrl(url))}
</Text>
</View>
</ListItemSelect>
),
)}
{rpcEndpoints.map(({ url, networkClientId }, index) => (
<ListItemSelect
key={index}
isSelected={
networkClientId ===
rpcEndpoints[
networkConfigurations[chainId as `0x${string}`]
.defaultRpcEndpointIndex
].networkClientId
}
isDisabled={false}
gap={8}
onPress={() => {
onRpcSelect(networkClientId, chainId as `0x${string}`);
closeRpcModal();
}}
>
<View style={styles.rpcText}>
<Text style={styles.textCentred}>
{hideKeyFromUrl(hideProtocolFromUrl(url))}
</Text>
</View>
</ListItemSelect>
))}
</View>
</BottomSheet>
);
Expand All @@ -798,8 +828,7 @@ const NetworkSelector = () => {
closeRpcModal,
styles,
networkConfigurations,
providerConfig.chainId,
selectedNetworkClientId,
onRpcSelect,
]);

const renderBottomSheetContent = () => (
Expand Down
5 changes: 0 additions & 5 deletions app/components/Views/Settings/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ describe('Settings', () => {
const contactsSettings = getByTestId(SettingsViewSelectorsIDs.CONTACTS);
expect(contactsSettings).toBeDefined();
});
it('should render network settings button', () => {
const { getByTestId } = renderWithProvider(<Settings />);
const networksSettings = getByTestId(SettingsViewSelectorsIDs.NETWORKS);
expect(networksSettings).toBeDefined();
});
it('should render feature request button', () => {
const { getByTestId } = renderWithProvider(<Settings />);
const onRampSettings = getByTestId(SettingsViewSelectorsIDs.ON_RAMP);
Expand Down

0 comments on commit 9fb2094

Please sign in to comment.