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

Migrate network configurations from PreferencesController to NetworkController #1064

Merged
merged 11 commits into from
Mar 22, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const setupControllers = () => {
});
const network = new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
});
const preferences = new PreferencesController();
const assetsContract = new AssetsContractController({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('TokenBalancesController', () => {
new NetworkController({
messenger,
infuraProjectId: 'potato',
trackMetaMetricsEvent: jest.fn(),
});
const preferences = new PreferencesController();
return { messenger, preferences };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('TokenRatesController', () => {
});

it('should update all rates', async () => {
new NetworkController({ messenger });
new NetworkController({ messenger, trackMetaMetricsEvent: jest.fn() });
const preferences = new PreferencesController();
const tokensController = new TokensController({
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
Expand Down
5 changes: 5 additions & 0 deletions packages/controller-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const IPFS_DEFAULT_GATEWAY_URL = 'https://cloudflare-ipfs.com/ipfs/';

// NETWORKS ID
export const GANACHE_CHAIN_ID = '1337';
/**
* The largest possible chain ID we can handle.
* Explanation: https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553
*/
export const MAX_SAFE_CHAIN_ID = 4503599627370476;
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved

// TOKEN STANDARDS
export const ERC721 = 'ERC721';
Expand Down
9 changes: 9 additions & 0 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BN } from 'ethereumjs-util';
import nock from 'nock';
import * as util from './util';
import { MAX_SAFE_CHAIN_ID } from './constants';

const VALID = '4e1fF7229BDdAf0A73DF183a88d9c3a04cc975e0';
const SOME_API = 'https://someapi.com';
Expand All @@ -11,6 +12,14 @@ describe('util', () => {
nock.cleanAll();
});

it('isSafeChainId', () => {
expect(util.isSafeChainId(MAX_SAFE_CHAIN_ID + 1)).toBe(false);
expect(util.isSafeChainId(MAX_SAFE_CHAIN_ID)).toBe(true);
mcmire marked this conversation as resolved.
Show resolved Hide resolved
expect(util.isSafeChainId(0)).toBe(false);
// @ts-expect-error - ensure that string args return false.
expect(util.isSafeChainId('test')).toBe(false);
});

it('bNToHex', () => {
expect(util.BNToHex(new BN('1337'))).toBe('0x539');
});
Expand Down
14 changes: 14 additions & 0 deletions packages/controller-utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ import { fromWei, toWei } from 'ethjs-unit';
import ensNamehash from 'eth-ens-namehash';
import deepEqual from 'fast-deep-equal';
import type { Json } from './types';
import { MAX_SAFE_CHAIN_ID } from './constants';

const TIMEOUT_ERROR = new Error('timeout');

/**
* Checks whether the given number primitive chain ID is safe.
* Because some cryptographic libraries we use expect the chain ID to be a
* number primitive, it must not exceed a certain size.
*
* @param chainId - The chain ID to check for safety.
* @returns Whether the given chain ID is safe.
*/
export function isSafeChainId(chainId: number): boolean {
return (
Number.isSafeInteger(chainId) && chainId > 0 && chainId <= MAX_SAFE_CHAIN_ID
);
}
/**
* Converts a BN object to a hex string with a '0x' prefix.
*
Expand Down
1 change: 1 addition & 0 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const setupNetworkController = (
const network = new NetworkController({
messenger: networkMessenger,
infuraProjectId: '123',
trackMetaMetricsEvent: jest.fn(),
});

return { network, networkMessenger };
Expand Down
2 changes: 2 additions & 0 deletions packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^",
"@metamask/swappable-obj-proxy": "^2.1.0",
"@metamask/utils": "^3.3.1",
"async-mutex": "^0.2.6",
"babel-runtime": "^6.26.0",
"eth-json-rpc-infura": "^5.1.0",
"eth-query": "^2.1.2",
"immer": "^9.0.6",
"uuid": "^8.3.2",
"web3-provider-engine": "^16.0.3"
},
"devDependencies": {
Expand Down
Loading