Skip to content

Commit

Permalink
Remove throwing when using setProviderType, throws inside of configur…
Browse files Browse the repository at this point in the history
…eProvider instead now
  • Loading branch information
BelfordZ committed Apr 5, 2023
1 parent 670da73 commit 7688b04
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
12 changes: 5 additions & 7 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ export class NetworkController extends BaseControllerV2<
this.setupInfuraProvider(type);
break;
case NetworkType.localhost:
this.setupStandardProvider(LOCALHOST_RPC_URL);
if (chainId === undefined) {
throw new Error('chainId must be passed in for custom rpcs');
}
this.setupStandardProvider(LOCALHOST_RPC_URL, toHex(chainId));
break;
case NetworkType.rpc:
if (chainId === undefined) {
Expand Down Expand Up @@ -334,7 +337,7 @@ export class NetworkController extends BaseControllerV2<
);
}

private setupStandardProvider(rpcTarget: string, chainId?: Hex) {
private setupStandardProvider(rpcTarget: string, chainId: Hex) {
const { provider, blockTracker } = createNetworkClient({
rpcUrl: rpcTarget,
chainId,
Expand Down Expand Up @@ -446,11 +449,6 @@ export class NetworkController extends BaseControllerV2<
* @param type - Human readable network name.
*/
setProviderType(type: NetworkType) {
if (type === NetworkType.rpc) {
throw new Error(
"Cannot use setProviderType to switch to a custom network (network type 'rpc'). Use setActiveNetwork instead.",
);
}
this.#setCurrentAsPreviousProvider();
// If testnet the ticker symbol should use a testnet prefix
const ticker =
Expand Down
6 changes: 3 additions & 3 deletions packages/network-controller/src/create-network-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export enum NetworkClientType {
}

type CustomNetworkConfiguration = {
chainId?: Hex;
chainId: Hex;
rpcUrl: string;
type: NetworkClientType.Custom;
};
Expand Down Expand Up @@ -156,7 +156,7 @@ function createNetworkAndChainIdMiddleware({
}

const createChainIdMiddleware = (
chainId?: string,
chainId: string,
): JsonRpcMiddleware<unknown, unknown> => {
return (req, res, next, end) => {
if (req.method === 'eth_chainId') {
Expand All @@ -182,7 +182,7 @@ function createCustomNetworkMiddleware({
rpcApiMiddleware,
}: {
blockTracker: PollingBlockTracker;
chainId?: string;
chainId: string;
rpcApiMiddleware: any;
}) {
// eslint-disable-next-line node/no-process-env
Expand Down
6 changes: 3 additions & 3 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('NetworkController', () => {

expect(createNetworkClientMock).toHaveBeenCalledWith({
rpcUrl: 'http://localhost:8545',
chainId: undefined,
chainId: '0x1046a',
type: NetworkClientType.Custom,
});
const { provider } = controller.getProviderAndBlockTracker();
Expand Down Expand Up @@ -1818,7 +1818,7 @@ describe('NetworkController', () => {
},
async ({ controller }) => {
expect(() => controller.setProviderType(NetworkType.rpc)).toThrow(
"Cannot use setProviderType to switch to a custom network (network type 'rpc'). Use setActiveNetwork instead.",
'rpcTarget must be passed in for custom rpcs',
);
},
);
Expand Down Expand Up @@ -1909,7 +1909,7 @@ describe('NetworkController', () => {

expect(createNetworkClientMock).toHaveBeenCalledWith({
rpcUrl: 'http://localhost:8545',
chainId: undefined,
chainId: '0x0',
type: NetworkClientType.Custom,
});
const { provider } = controller.getProviderAndBlockTracker();
Expand Down

0 comments on commit 7688b04

Please sign in to comment.