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

Require Infura project ID #1276

Merged
merged 4 commits into from
Apr 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const setupControllers = () => {
allowedActions: [],
});
const network = new NetworkController({
infuraProjectId: 'infura-project-id',
messenger,
trackMetaMetricsEvent: jest.fn(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ describe('TokenRatesController', () => {
});

it('should update all rates', async () => {
new NetworkController({ messenger, trackMetaMetricsEvent: jest.fn() });
new NetworkController({
infuraProjectId: 'infura-project-id',
messenger,
trackMetaMetricsEvent: jest.fn(),
});
const preferences = new PreferencesController();
const tokensController = new TokensController({
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
Expand Down
7 changes: 5 additions & 2 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export type NetworkControllerMessenger = RestrictedControllerMessenger<
export type NetworkControllerOptions = {
messenger: NetworkControllerMessenger;
trackMetaMetricsEvent: () => void;
infuraProjectId?: string;
infuraProjectId: string;
Copy link
Contributor

@legobeat legobeat Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easy to get the impression that this does cement the provider.. Here or another place good to add a // TODO: or // FIXME: on that the intention is that the controller will be made provider-agnostic?

Copy link
Member Author

@Gudahtt Gudahtt Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should elaborate; the path to removing these would be to enhance the capabilities of our custom RPC configuration, such that it can be used to support our Infura networks as well.

We've already started down this path; the localhost e2e test network used to be baked-in, but now we use it as a custom network instead. And when we recently added Linea as a default network, it was added as a custom network.

That is, I'd expect us to delete the built-in networks completely rather than make them optional. Making them optional isn't need not be a step along the path to removing them.

It's long past time to track this work in an issue; I'll set a reminder to create one tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #1279

state?: Partial<NetworkState>;
};

Expand Down Expand Up @@ -213,7 +213,7 @@ export class NetworkController extends BaseControllerV2<
> {
#ethQuery?: EthQuery;

#infuraProjectId: string | undefined;
#infuraProjectId: string;

#trackMetaMetricsEvent: (event: MetaMetricsEventPayload) => void;

Expand Down Expand Up @@ -260,6 +260,9 @@ export class NetworkController extends BaseControllerV2<
messenger,
state: { ...defaultState, ...state },
});
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
throw new Error('Invalid Infura project ID');
}
this.#infuraProjectId = infuraProjectId;
this.#trackMetaMetricsEvent = trackMetaMetricsEvent;
this.messagingSystem.registerActionHandler(
Expand Down
34 changes: 30 additions & 4 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ describe('NetworkController', () => {
},
);
});

it('throws if the infura project ID is missing', async () => {
const messenger = buildMessenger();

expect(
() =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
}),
).toThrow('Invalid Infura project ID');
});

it('throws if the infura project ID is not a string', async () => {
const messenger = buildMessenger();

expect(
() =>
new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
infuraProjectId: 10,
}),
).toThrow('Invalid Infura project ID');
});
});

describe('initializeProvider', () => {
Expand Down Expand Up @@ -236,7 +265,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -274,7 +302,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -352,7 +379,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -3346,7 +3372,6 @@ describe('NetworkController', () => {
chainId: '0x9999999',
},
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -5724,6 +5749,7 @@ async function withController<ReturnValue>(
const controller = new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
infuraProjectId: 'infura-project-id',
...rest,
});
try {
Expand Down