Skip to content

Commit

Permalink
fix: fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Sep 30, 2024
1 parent 5df8fb6 commit 59c503c
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 101 deletions.
11 changes: 0 additions & 11 deletions android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,4 @@
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1727268192852</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 0 additions & 11 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=false
show.console.view=false
show.executions.view=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import styleSheet from './ListItemMultiSelectButton.styles';
import { ListItemMultiSelectButtonProps } from './ListItemMultiSelectButton.types';
import {
BUTTON_TEST_ID,
BUTTON_TEXT_TEST_ID,
DEFAULT_LISTITEMMULTISELECT_GAP,
} from './ListItemMultiSelectButton.constants';
import ButtonIcon from '../../../component-library/components/Buttons/ButtonIcon';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ exports[`MultiRpcModal render matches snapshot 1`] = `
"paddingHorizontal": 16,
}
}
testID="button-text-select-test-id"
>
<Text
accessibilityRole="text"
Expand Down
3 changes: 1 addition & 2 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const NetworkSelector = () => {
isReadOnly: false,
});

const onRpcSelect = (clientId) => {
const onRpcSelect = (clientId: string) => {
const { NetworkController } = Engine.context;

NetworkController.setActiveNetwork(clientId);
Expand Down Expand Up @@ -426,7 +426,6 @@ const NetworkSelector = () => {
onLongPress={() => {
openModal(chainId, false, MAINNET, true);
}}
id={chainId}
/>
);
};
Expand Down
20 changes: 6 additions & 14 deletions app/store/migrations/054.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ describe(`migration #${version}`, () => {
const defaultStateToExpect = defaultPostMigrationState();
const expectedNetwork = {
...defaultStateToExpect.networkConfigurationsByChainId[
customNetwork.chainId
customNetwork.chainId as keyof typeof defaultStateToExpect.networkConfigurationsByChainId
],
};

expectedNetwork.defaultRpcEndpointIndex =
expectedNetwork.rpcEndpoints.push({
networkClientId: customNetwork.id,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
name: customNetwork.nickname,
url: customNetwork.rpcUrl,
type: 'custom',
Expand Down Expand Up @@ -244,14 +246,6 @@ describe(`migration #${version}`, () => {
customNetwork.rpcPrefs.blockExplorerUrl,
) - 1;

// const expectedState = {
// ...defaultStateToExpect,
// networkConfigurationsByChainId: {
// ...defaultStateToExpect.networkConfigurationsByChainId,
// [customNetwork.chainId]: expectedNetwork,
// },
// };

const expectedState = {
...defaultStateToExpect,
// Selected network shouldn't change
Expand All @@ -264,8 +258,6 @@ describe(`migration #${version}`, () => {
},
};

console.log('expectedState -----', expectedState);

const newState = (await migrate(oldState)) as EngineState;
expect(newState.engine.backgroundState.NetworkController).toStrictEqual(
expectedState,
Expand Down Expand Up @@ -309,7 +301,7 @@ describe(`migration #${version}`, () => {
},
};

const newState = await migrate(oldState);
const newState = (await migrate(oldState)) as EngineState;

const { networkConfigurationsByChainId } = newState.engine.backgroundState
.NetworkController as {
Expand Down Expand Up @@ -350,7 +342,7 @@ describe(`migration #${version}`, () => {
},
};

const newState = await migrate(oldState);
const newState = (await migrate(oldState)) as EngineState;

// Expect the configuration with an invalid URL to be omitted
expect(newState.engine.backgroundState.NetworkController).toStrictEqual(
Expand All @@ -371,7 +363,7 @@ describe(`migration #${version}`, () => {
},
};

const newState = await migrate(oldState);
const newState = (await migrate(oldState)) as EngineState;

// selectedNetworkClientId should fall back to mainnet
expect(newState.engine.backgroundState.NetworkController).toStrictEqual(
Expand Down
63 changes: 2 additions & 61 deletions app/store/migrations/054.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable import/no-namespace */
import { captureException } from '@sentry/react-native';
import {
TransactionControllerState,
TransactionMeta,
} from '@metamask/transaction-controller';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as URI from 'uri-js';
import { SelectedNetworkControllerState } from '@metamask/selected-network-controller';
import { hasProperty, isObject, RuntimeObject } from '@metamask/utils';
Expand All @@ -18,7 +20,6 @@ export const version = 54;
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function addBuiltInInfuraNetworks(networkConfigurations: any[]) {
console.log('Adding built-in Infura networks...');
return [
{
type: 'infura',
Expand Down Expand Up @@ -69,15 +70,10 @@ function isValidUrl(url: string) {
}

export default function migrate(state: unknown) {
console.log('Starting migration...');

if (!ensureValidState(state, 54)) {
console.log('State is not valid for migration 54, exiting...');
return state;
}

console.log('State is valid. Proceeding with migration...');

const networkControllerState =
state.engine?.backgroundState?.NetworkController;
const transactionControllerState = state.engine?.backgroundState
Expand All @@ -86,7 +82,6 @@ export default function migrate(state: unknown) {
?.SelectedNetworkController as SelectedNetworkControllerState;

if (!isObject(networkControllerState)) {
console.log('Error: Invalid NetworkController state.');
captureException(
new Error(
`FATAL ERROR: Migration ${version}: Invalid NetworkController state error: '${typeof networkControllerState}'`,
Expand All @@ -96,7 +91,6 @@ export default function migrate(state: unknown) {
}

if (!isObject(transactionControllerState)) {
console.log('Error: Invalid TransactionController state.');
captureException(
new Error(
`FATAL ERROR: Migration ${version}: Invalid TransactionController state error: '${typeof transactionControllerState}'`,
Expand All @@ -105,22 +99,17 @@ export default function migrate(state: unknown) {
return state;
}

console.log('Extracted NetworkController and TransactionController states.');

const networkState = networkControllerState;

// Get custom network configurations or default to an empty array
console.log('Fetching custom network configurations...');
let networkConfigurations = isObject(networkState.networkConfigurations)
? Object.values(networkState.networkConfigurations)
: [];

// Add built-in Infura networks
console.log('Adding built-in Infura networks...');
networkConfigurations = addBuiltInInfuraNetworks(networkConfigurations);

// Group the network configurations by chain id
console.log('Grouping network configurations by chain ID...');
const networkConfigurationArraysByChainId = networkConfigurations.reduce(
(acc: Record<string, RuntimeObject[]>, networkConfiguration) => {
if (
Expand All @@ -134,10 +123,7 @@ export default function migrate(state: unknown) {
{},
);

console.log('Network configurations grouped by chain ID.');

// Get transaction history in reverse chronological order to help with tie breaks
console.log('Fetching transaction history...');
const transactions: RuntimeObject[] = Array.isArray(
transactionControllerState.transactions,
)
Expand All @@ -151,15 +137,10 @@ export default function migrate(state: unknown) {
.sort((a, b) => b.time - a.time)
: [];

console.log('Transaction history fetched and sorted.');

// For each chain id, merge the array of network configurations
console.log('Merging network configurations by chain ID...');
const networkConfigurationsByChainId = Object.entries(
networkConfigurationArraysByChainId,
).reduce((acc: Record<string, unknown>, [chainId, networks]) => {
console.log(`Processing chain ID: ${chainId}`);

// Calculate the tie breaker network, whose values will be preferred
let tieBreaker: RuntimeObject | undefined;

Expand All @@ -170,9 +151,6 @@ export default function migrate(state: unknown) {

// Otherwise use the network that was most recently transacted on
if (!tieBreaker) {
console.log(
`No selected network found, using most recent transaction for chain ID: ${chainId}`,
);
transactions
.filter((tx) => tx.chainId === chainId)
.some(
Expand All @@ -186,14 +164,10 @@ export default function migrate(state: unknown) {
// If no transactions were found for the chain id, fall back
// to an arbitrary custom network that is not built in Infura
if (!tieBreaker) {
console.log(
`No transactions found for chain ID: ${chainId}, using a custom network if available.`,
);
tieBreaker = networks.find((network) => network.type !== 'infura');
}

// Calculate the unique set of valid rpc endpoints for this chain id
console.log(`Calculating valid RPC endpoints for chain ID: ${chainId}...`);
const rpcEndpoints = networks.reduce(
(endpoints: RuntimeObject[], network) => {
if (
Expand Down Expand Up @@ -248,9 +222,6 @@ export default function migrate(state: unknown) {

// If there were no valid unique endpoints, omit the network configuration
if (rpcEndpoints.length === 0) {
console.log(
`No valid RPC endpoints found for chain ID: ${chainId}, skipping...`,
);
return acc;
}

Expand All @@ -263,10 +234,6 @@ export default function migrate(state: unknown) {
0,
);

console.log(
`Default RPC endpoint index for chain ID ${chainId}: ${defaultRpcEndpointIndex}`,
);

// Calculate the unique array of non-empty block explorer urls
const blockExplorerUrls = [
...networks.reduce((urls, network) => {
Expand Down Expand Up @@ -295,10 +262,6 @@ export default function migrate(state: unknown) {
0,
);

console.log(
`Default block explorer URL index for chain ID ${chainId}: ${defaultBlockExplorerUrlIndex}`,
);

const name = networks.find((n) => n.nickname)?.nickname;
const nativeCurrency =
tieBreaker?.ticker ?? networks.find((n) => n.ticker)?.ticker;
Expand All @@ -314,14 +277,10 @@ export default function migrate(state: unknown) {
name,
nativeCurrency,
};
console.log(`Finished processing chain ID ${chainId}.`);
return acc;
}, {});

console.log('Finished merging network configurations by chain ID.');

// Given a network client id, returns the chain id it used to point to
console.log('Mapping network client IDs to chain IDs...');
const networkClientIdToChainId = (networkClientId: unknown) => {
const networkConfiguration = networkConfigurations.find(
(n) => isObject(n) && n.id === networkClientId,
Expand All @@ -334,7 +293,6 @@ export default function migrate(state: unknown) {

// Ensure that selectedNetworkClientId points to
// some endpoint of some network configuration.
console.log('Ensuring selected network client ID is valid...');
let selectedNetworkClientId = Object.values(networkConfigurationsByChainId)
.flatMap((n) =>
isObject(n) && Array.isArray(n.rpcEndpoints) ? n.rpcEndpoints : [],
Expand All @@ -345,9 +303,6 @@ export default function migrate(state: unknown) {

// If not valid, try to fallback to the default endpoint for the same chain
if (!selectedNetworkClientId) {
console.log(
`Selected network client ID is invalid, trying to fallback to default endpoint...`,
);
const chainId = networkClientIdToChainId(
networkState.selectedNetworkClientId,
);
Expand All @@ -366,10 +321,7 @@ export default function migrate(state: unknown) {
: 'mainnet';
}

console.log(`Selected network client ID: ${selectedNetworkClientId}`);

// Redirect domains in the selected network controller
console.log('Redirecting domains in the SelectedNetworkController...');
if (
hasProperty(state.engine.backgroundState, 'SelectedNetworkController') &&
isObject(state.engine.backgroundState.SelectedNetworkController) &&
Expand Down Expand Up @@ -404,28 +356,20 @@ export default function migrate(state: unknown) {

// Point the domain to the chain's default rpc endpoint
if (newNetworkClientId) {
console.log(
`Redirecting domain ${domain} to network client ID ${newNetworkClientId}`,
);
selectedNetworkController.domains[domain] = newNetworkClientId;
} else {
console.log(
`Removing domain ${domain} due to invalid network client ID.`,
);
delete selectedNetworkController.domains[domain];
}
}
}

console.log('Updating NetworkController state...');
state.engine.backgroundState.NetworkController = {
selectedNetworkClientId,
networkConfigurationsByChainId,
networksMetadata: networkState.networksMetadata ?? {},
};

// Set `showMultiRpcModal` based on whether there are any networks with multiple rpc endpoints
console.log('Checking for networks with multiple RPC endpoints...', state);
if (
hasProperty(state.engine.backgroundState, 'PreferencesController') &&
isObject(state.engine.backgroundState.PreferencesController) &&
Expand All @@ -435,7 +379,6 @@ export default function migrate(state: unknown) {
) &&
isObject(state.engine.backgroundState.PreferencesController.preferences)
) {
console.log('SALIM IS HERE ----------');
state.engine.backgroundState.PreferencesController.preferences.showMultiRpcModal =
Object.values(networkConfigurationsByChainId).some(
(networkConfiguration) =>
Expand All @@ -446,7 +389,6 @@ export default function migrate(state: unknown) {
}

// Migrate the user's drag + drop preference order for the network menu
console.log('Migrating NetworkOrderController...');
if (
hasProperty(state.engine.backgroundState, 'NetworkOrderController') &&
isObject(state.engine.backgroundState.NetworkOrderController) &&
Expand All @@ -463,7 +405,6 @@ export default function migrate(state: unknown) {
].map((networkId) => ({ networkId }));
}

console.log('Migration completed.');
// Return the modified state
return state;
}

0 comments on commit 59c503c

Please sign in to comment.