Skip to content

Commit

Permalink
Improve buildProviderConfig helper function (#1346)
Browse files Browse the repository at this point in the history
The network controller helper function `buildProviderConfig` now
ensures that the provider configuration returned is valid for Infura
networks. Previously it always returned valid RPC provider configs, but
if the config was an Infura type it might be missing some properties.

Relates to #1197
  • Loading branch information
Gudahtt authored May 8, 2023
1 parent 93d362a commit b91b9e5
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ describe('NetworkController', () => {
state: {
providerConfig: buildProviderConfig({
type: networkType,
chainId: '99999',
nickname: 'some nickname',
}),
},
infuraProjectId: 'infura-project-id',
Expand Down Expand Up @@ -560,7 +558,6 @@ describe('NetworkController', () => {
refreshNetworkTests({
expectedProviderConfig: buildProviderConfig({
type: networkType,
...BUILT_IN_NETWORKS[networkType],
}),
operation: async (controller) => {
await controller.setProviderType(networkType);
Expand Down Expand Up @@ -2341,7 +2338,6 @@ describe('NetworkController', () => {
state: {
providerConfig: buildProviderConfig({
type: networkType,
...BUILT_IN_NETWORKS[networkType],
}),
networkConfigurations: {
testNetworkConfiguration: {
Expand Down Expand Up @@ -2395,7 +2391,6 @@ describe('NetworkController', () => {
expect(controller.state.providerConfig).toStrictEqual(
buildProviderConfig({
type: networkType,
...BUILT_IN_NETWORKS[networkType],
}),
);
},
Expand Down Expand Up @@ -5041,16 +5036,23 @@ async function withController<ReturnValue>(
* @param config - An incomplete ProviderConfig object.
* @returns The complete ProviderConfig object.
*/
function buildProviderConfig(config: Partial<ProviderConfig> = {}) {
function buildProviderConfig(
config: Partial<ProviderConfig> = {},
): ProviderConfig {
if (config.type && config.type !== NetworkType.rpc) {
return {
...BUILT_IN_NETWORKS[config.type],
// This is redundant with the spread operation below, but this was
// required for TypeScript to understand that this property was set to an
// Infura type.
type: config.type,
...config,
};
}
return {
type: NetworkType.rpc,
chainId: '1337',
id: undefined,
nickname: undefined,
rpcUrl:
!config.type || config.type === NetworkType.rpc
? 'http://doesntmatter.com'
: undefined,
rpcUrl: 'http://doesntmatter.com',
...config,
};
}
Expand Down

0 comments on commit b91b9e5

Please sign in to comment.