2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { getNetworkDisplayName } from './util';
|
2021-06-25 18:24:00 +02:00
|
|
|
import NetworkController, { NETWORK_EVENTS } from './network';
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('NetworkController', () => {
|
|
|
|
describe('controller', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let networkController;
|
2021-06-25 18:24:00 +02:00
|
|
|
let getLatestBlockStub;
|
|
|
|
let setProviderTypeAndWait;
|
2021-02-04 19:15:23 +01:00
|
|
|
const noop = () => undefined;
|
2020-02-11 17:51:13 +01:00
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
beforeEach(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController = new NetworkController();
|
2021-06-25 18:24:00 +02:00
|
|
|
getLatestBlockStub = sinon
|
|
|
|
.stub(networkController, 'getLatestBlock')
|
|
|
|
.callsFake(() => Promise.resolve({}));
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.setInfuraProjectId('foo');
|
2021-06-25 18:24:00 +02:00
|
|
|
setProviderTypeAndWait = () =>
|
|
|
|
new Promise((resolve) => {
|
|
|
|
networkController.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
networkController.setProviderType('mainnet');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
afterEach(() => {
|
2021-06-25 18:24:00 +02:00
|
|
|
getLatestBlockStub.reset();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-03-09 20:20:18 +01:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('#provider', () => {
|
|
|
|
it('provider should be updatable without reassignment', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
2022-07-31 20:26:40 +02:00
|
|
|
const providerProxy =
|
|
|
|
networkController.getProviderAndBlockTracker().provider;
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(providerProxy.test).toBeUndefined();
|
2021-02-04 19:15:23 +01:00
|
|
|
providerProxy.setTarget({ test: true });
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(providerProxy.test).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-07-25 20:25:34 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('#getNetworkState', () => {
|
|
|
|
it('should return "loading" when new', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const networkState = networkController.getNetworkState();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(networkState).toStrictEqual('loading');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('#setNetworkState', () => {
|
|
|
|
it('should update the network', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.setNetworkState('1');
|
|
|
|
const networkState = networkController.getNetworkState();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(networkState).toStrictEqual('1');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('#setProviderType', () => {
|
|
|
|
it('should update provider.type', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
networkController.setProviderType('mainnet');
|
|
|
|
const { type } = networkController.getProviderConfig();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(type).toStrictEqual('mainnet');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
it('should set the network to loading', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const spy = sinon.spy(networkController, 'setNetworkState');
|
|
|
|
networkController.setProviderType('mainnet');
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(spy.callCount).toStrictEqual(1);
|
|
|
|
expect(spy.calledOnceWithExactly('loading')).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2021-06-25 18:24:00 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('#getEIP1559Compatibility', () => {
|
|
|
|
it('should return false when baseFeePerGas is not in the block header', async () => {
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
2022-07-31 20:26:40 +02:00
|
|
|
const supportsEIP1559 =
|
|
|
|
await networkController.getEIP1559Compatibility();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(supportsEIP1559).toStrictEqual(false);
|
2021-06-25 18:24:00 +02:00
|
|
|
});
|
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
it('should return true when baseFeePerGas is in block header', async () => {
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
2022-07-31 20:26:40 +02:00
|
|
|
const supportsEIP1559 =
|
|
|
|
await networkController.getEIP1559Compatibility();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(supportsEIP1559).toStrictEqual(true);
|
2021-06-25 18:24:00 +02:00
|
|
|
});
|
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
it('should store EIP1559 support in state to reduce calls to getLatestBlock', async () => {
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
|
|
|
await networkController.getEIP1559Compatibility();
|
2022-07-31 20:26:40 +02:00
|
|
|
const supportsEIP1559 =
|
|
|
|
await networkController.getEIP1559Compatibility();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(getLatestBlockStub.calledOnce).toStrictEqual(true);
|
|
|
|
expect(supportsEIP1559).toStrictEqual(true);
|
2021-06-25 18:24:00 +02:00
|
|
|
});
|
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
it('should clear stored EIP1559 support when changing networks', async () => {
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
networkController.consoleThis = true;
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
|
|
|
await networkController.getEIP1559Compatibility();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
2021-12-10 18:25:58 +01:00
|
|
|
).toStrictEqual(true);
|
2021-06-25 18:24:00 +02:00
|
|
|
getLatestBlockStub.callsFake(() => Promise.resolve({}));
|
|
|
|
await setProviderTypeAndWait('mainnet');
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
2021-12-10 18:25:58 +01:00
|
|
|
).toBeUndefined();
|
2021-06-25 18:24:00 +02:00
|
|
|
await networkController.getEIP1559Compatibility();
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(
|
2021-06-25 18:24:00 +02:00
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
2021-12-10 18:25:58 +01:00
|
|
|
).toStrictEqual(false);
|
|
|
|
expect(getLatestBlockStub.calledTwice).toStrictEqual(true);
|
2021-06-25 18:24:00 +02:00
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2021-12-10 18:25:58 +01:00
|
|
|
describe('utils', () => {
|
|
|
|
it('getNetworkDisplayName should return the correct network name', () => {
|
2020-02-11 17:51:13 +01:00
|
|
|
const tests = [
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'mainnet',
|
2020-09-15 21:34:16 +02:00
|
|
|
expected: 'Ethereum Mainnet',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'goerli',
|
|
|
|
expected: 'Goerli',
|
|
|
|
},
|
2022-09-14 20:26:45 +02:00
|
|
|
{
|
|
|
|
input: 'sepolia',
|
|
|
|
expected: 'Sepolia',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
tests.forEach(({ input, expected }) =>
|
2021-12-10 18:25:58 +01:00
|
|
|
expect(getNetworkDisplayName(input)).toStrictEqual(expected),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|