Skip to content

Commit

Permalink
Added network clients
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Dec 5, 2022
1 parent 5f1f69d commit 9e31c95
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 89 deletions.
6 changes: 6 additions & 0 deletions packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@
"dependencies": {
"@metamask/base-controller": "workspace:~",
"@metamask/controller-utils": "workspace:~",
"@metamask/eth-json-rpc-infura": "^7.0.0",
"async-mutex": "^0.2.6",
"babel-runtime": "^6.26.0",
"eth-block-tracker": "^6.0.0",
"eth-json-rpc-infura": "^5.1.0",
"eth-json-rpc-middleware": "^9.0.1",
"eth-query": "^2.1.2",
"immer": "^9.0.6",
"json-rpc-engine": "^6.1.0",
"web3-provider-engine": "^16.0.3"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.1.0",
"@metamask/safe-event-emitter": "^2.0.0",
"@types/jest": "^26.0.22",
"deepmerge": "^4.2.2",
"jest": "^26.4.2",
"nock": "^13.2.9",
"sinon": "^9.2.4",
"ts-jest": "^26.5.2",
"typedoc": "^0.22.15",
Expand Down
137 changes: 116 additions & 21 deletions packages/network-controller/src/NetworkController.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
import * as sinon from 'sinon';
import Web3ProviderEngine from 'web3-provider-engine';
import { ControllerMessenger } from '@metamask/base-controller';
import { NetworkType, NetworksChainId } from '@metamask/controller-utils';
import SafeEventEmitter from '@metamask/safe-event-emitter';
import nock from 'nock';
import {
NetworkController,
NetworkControllerMessenger,
NetworkControllerOptions,
ProviderConfig,
} from './NetworkController';

const RPC_TARGET = 'http://foo';

type WithMockedBlockTrackerOptions = {
nextBlockNumber?: () => string;
};

const withMockedBlockTracker = async (
options: WithMockedBlockTrackerOptions = {},
) => {
const nextBlockNumber = options.nextBlockNumber
? options.nextBlockNumber
: () => '0x42';

const urlRegex = /https:\/\/.*/u;
const anyRegex = /.*/u;
nock(urlRegex)
.post(anyRegex, {
jsonrpc: '2.0',
id: anyRegex,
method: "eth_blockNumber",
params: [],
})
.reply((_, reqBody: any) => {
console.log(reqBody);
return [
200,
{ jsonrpc: '2.0', id: reqBody.id, result: nextBlockNumber() },
];
})
.persist();

nock(urlRegex)
.post(anyRegex, {
jsonrpc: '2.0',
id: anyRegex,
method: "eth_getBlockByNumber",
params: ["0x42", false],
})
.reply((_, reqBody: any) => {
console.log(reqBody);
return [
200,
{ jsonrpc: '2.0', id: reqBody.id, result: {} },
];
})
.persist();
};

const setupController = (
pType: NetworkType,
messenger: NetworkControllerMessenger,
Expand All @@ -28,7 +74,6 @@ const setupController = (
messenger,
};
const controller = new NetworkController(networkControllerOpts);
controller.providerConfig = {} as ProviderConfig;
return controller;
};

Expand All @@ -45,6 +90,8 @@ describe('NetworkController', () => {

afterEach(() => {
sinon.restore();
nock.restore();
nock.cleanAll();
});

it('should set default state', () => {
Expand All @@ -56,7 +103,9 @@ describe('NetworkController', () => {
expect(controller.state).toStrictEqual({
network: 'loading',
isCustomNetwork: false,
properties: { isEIP1559Compatible: false },
properties: {
isEIP1559Compatible: false
},
provider: {
type: 'mainnet',
chainId: '1',
Expand All @@ -70,23 +119,42 @@ describe('NetworkController', () => {
messenger,
};
const controller = new NetworkController(networkControllerOpts);
controller.providerConfig = {} as ProviderConfig;
expect(controller.provider instanceof Web3ProviderEngine).toBe(true);
const setupInfuraProvider = jest.spyOn(NetworkController.prototype as any, 'setupInfuraProvider');
setupInfuraProvider.mockImplementationOnce(() => { });

controller.setProviderType(controller.state.provider.type);
expect(setupInfuraProvider).toHaveBeenCalled();
});

(
['kovan', 'rinkeby', 'ropsten', 'mainnet', 'localhost'] as NetworkType[]
['kovan', 'rinkeby', 'ropsten', 'mainnet'] as NetworkType[]
).forEach((n) => {
it(`should create a provider instance for ${n} infura network`, () => {
const networkController = setupController(n, messenger);
expect(networkController.provider instanceof Web3ProviderEngine).toBe(
true,
);

const setupInfuraProvider = jest.spyOn(NetworkController.prototype as any, 'setupInfuraProvider');
setupInfuraProvider.mockImplementationOnce(() => { });
expect(networkController.state.isCustomNetwork).toBe(false);
networkController.setProviderType(n);
expect(setupInfuraProvider).toHaveBeenCalled();
});
});

it('should create a provider instance for optimism network', () => {
it(`should create a provider instance for localhost network`, () => {
const networkController = setupController('localhost', messenger);

const setupStandardProvider = jest.spyOn(
NetworkController.prototype as any,
'setupStandardProvider'
);
setupStandardProvider.mockImplementationOnce(() => { });

expect(networkController.state.isCustomNetwork).toBe(false);
networkController.setProviderType('localhost');
expect(setupStandardProvider).toHaveBeenCalled();
});

it.only('should create a provider instance for optimism network', () => {
const networkControllerOpts: NetworkControllerOptions = {
infuraProjectId: 'foo',
state: {
Expand All @@ -100,13 +168,21 @@ describe('NetworkController', () => {
},
messenger,
};

const controller = new NetworkController(networkControllerOpts);
controller.providerConfig = {} as ProviderConfig;
expect(controller.provider instanceof Web3ProviderEngine).toBe(true);

const setupStandardProvider = jest.spyOn(
NetworkController.prototype as any,
'setupStandardProvider'
);
setupStandardProvider.mockImplementationOnce(() => { });

controller.setProviderType(controller.state.provider.type);
expect(controller.state.isCustomNetwork).toBe(true);
expect(setupStandardProvider).toHaveBeenCalled();
});

it('should create a provider instance for rpc network', () => {
it.only('should create a provider instance for rpc network', () => {
const networkControllerOpts: NetworkControllerOptions = {
infuraProjectId: 'foo',
state: {
Expand All @@ -120,20 +196,33 @@ describe('NetworkController', () => {
messenger,
};
const controller = new NetworkController(networkControllerOpts);
controller.providerConfig = {} as ProviderConfig;
expect(controller.provider instanceof Web3ProviderEngine).toBe(true);

const setupStandardProvider = jest.spyOn(
NetworkController.prototype as any,
'setupStandardProvider'
);
setupStandardProvider.mockImplementationOnce(() => { });

controller.setProviderType(controller.state.provider.type);
expect(controller.state.isCustomNetwork).toBe(false);
expect(setupStandardProvider).toHaveBeenCalled();
});

it('should set new RPC target', () => {
const controller = new NetworkController({ messenger });
const controller = new NetworkController({
messenger,
infuraProjectId: 'potate',
});
controller.setRpcTarget(RPC_TARGET, NetworksChainId.rpc);
expect(controller.state.provider.rpcTarget).toBe(RPC_TARGET);
expect(controller.state.isCustomNetwork).toBe(false);
});

it('should set new provider type', () => {
const controller = new NetworkController({ messenger });
const controller = new NetworkController({
messenger,
infuraProjectId: 'potate',
});
controller.setProviderType('localhost');
expect(controller.state.provider.type).toBe('localhost');
expect(controller.state.isCustomNetwork).toBe(false);
Expand Down Expand Up @@ -180,7 +269,10 @@ describe('NetworkController', () => {
});

it('should throw when setting an unrecognized provider type', () => {
const controller = new NetworkController({ messenger });
const controller = new NetworkController({
messenger,
infuraProjectId: 'potate',
});
expect(() => controller.setProviderType('junk' as NetworkType)).toThrow(
"Unrecognized network type: 'junk'",
);
Expand All @@ -194,8 +286,11 @@ describe('NetworkController', () => {
network: 'loading',
},
});
controller.providerConfig = {} as ProviderConfig;
controller.setProviderType(controller.state.provider.type);
controller.lookupNetwork = sinon.stub();
if (controller.provider === undefined) {
throw new Error('provider is undefined');
}
controller.provider.emit('error', {});
expect((controller.lookupNetwork as any).called).toBe(true);
});
Expand All @@ -218,7 +313,7 @@ describe('NetworkController', () => {
};
messenger.subscribe(event, handleProviderChange);

controller.providerConfig = {} as ProviderConfig;
controller.setProviderType(controller.state.provider.type);
});
});
});
Loading

0 comments on commit 9e31c95

Please sign in to comment.