Skip to content

Commit

Permalink
GasFeeController: providerConfig -> selectedNetworkClientId (#4356)
Browse files Browse the repository at this point in the history
The `providerConfig` state property is being removed from
NetworkController. Currently this property is used in GasFeeController
to get the currently selected chain, but `selectedNetworkClientId` can
be used instead. This commit makes that transition so that we can fully
drop `providerConfig`.
  • Loading branch information
mcmire authored Jun 4, 2024
1 parent 66acffc commit 95abd17
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 31 deletions.
103 changes: 76 additions & 27 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ControllerMessenger } from '@metamask/base-controller';
import {
ChainId,
convertHexToDecimal,
NetworkType,
toHex,
} from '@metamask/controller-utils';
import EthQuery from '@metamask/eth-query';
Expand Down Expand Up @@ -64,10 +63,12 @@ const setupNetworkController = async ({
unrestrictedMessenger,
state,
clock,
initializeProvider = true,
}: {
unrestrictedMessenger: MainControllerMessenger;
state: Partial<NetworkState>;
clock: sinon.SinonFakeTimers;
initializeProvider?: boolean;
}) => {
const restrictedMessenger = unrestrictedMessenger.getRestricted({
name: 'NetworkController',
Expand All @@ -81,12 +82,15 @@ const setupNetworkController = async ({
infuraProjectId: '123',
trackMetaMetricsEvent: jest.fn(),
});
// Call this without awaiting to simulate what the extension or mobile app
// might do
networkController.initializeProvider();
// Ensure that the request for eth_getBlockByNumber made by the PollingBlockTracker
// inside the NetworkController goes through
await clock.nextAsync();

if (initializeProvider) {
// Call this without awaiting to simulate what the extension or mobile app
// might do
networkController.initializeProvider();
// Ensure that the request for eth_getBlockByNumber made by the PollingBlockTracker
// inside the NetworkController goes through
await clock.nextAsync();
}

return networkController;
};
Expand Down Expand Up @@ -228,6 +232,8 @@ describe('GasFeeController', () => {
* @param options.interval - The polling interval.
* @param options.state - The initial GasFeeController state
* @param options.infuraAPIKey - The Infura API key.
* @param options.initializeNetworkProvider - Whether to instruct the
* NetworkController to initialize its provider.
*/
async function setupGasFeeController({
getIsEIP1559Compatible = jest.fn().mockResolvedValue(true),
Expand All @@ -241,6 +247,7 @@ describe('GasFeeController', () => {
networkControllerState = {},
state,
interval,
initializeNetworkProvider = true,
}: {
getChainId?: jest.Mock<Hex>;
onNetworkDidChange?: jest.Mock<void>;
Expand All @@ -251,12 +258,14 @@ describe('GasFeeController', () => {
state?: GasFeeState;
interval?: number;
infuraAPIKey?: string;
initializeNetworkProvider?: boolean;
} = {}) {
const controllerMessenger = getControllerMessenger();
networkController = await setupNetworkController({
unrestrictedMessenger: controllerMessenger,
state: networkControllerState,
clock,
initializeProvider: initializeNetworkProvider,
});
const messenger = getRestrictedMessenger(controllerMessenger);
gasFeeController = new GasFeeController({
Expand Down Expand Up @@ -323,14 +332,24 @@ describe('GasFeeController', () => {
.fn()
.mockReturnValue(true),
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
networkConfigurations: {
'AAAA-BBBB-CCCC-DDDD': {
id: 'AAAA-BBBB-CCCC-DDDD',
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
},
},
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
},
clientId: '99999',
// Currently initializing the provider overwrites the
// `selectedNetworkClientId` we specify above based on whatever
// `providerConfig` is. So we prevent the provider from being
// initialized to make this test pass. Once `providerConfig` is
// removed, then we don't need this anymore and
// `selectedNetworkClientId` should no longer be overwritten.
initializeNetworkProvider: false,
});

await gasFeeController.getGasFeeEstimatesAndStartPolling(undefined);
Expand Down Expand Up @@ -377,14 +396,24 @@ describe('GasFeeController', () => {
.fn()
.mockReturnValue(true),
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
networkConfigurations: {
'AAAA-BBBB-CCCC-DDDD': {
id: 'AAAA-BBBB-CCCC-DDDD',
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
},
},
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
},
clientId: '99999',
// Currently initializing the provider overwrites the
// `selectedNetworkClientId` we specify above based on whatever
// `providerConfig` is. So we prevent the provider from being
// initialized to make this test pass. Once `providerConfig` is
// removed, then we don't need this anymore and
// `selectedNetworkClientId` should no longer be overwritten.
initializeNetworkProvider: false,
});

await gasFeeController.getGasFeeEstimatesAndStartPolling(
Expand Down Expand Up @@ -716,14 +745,24 @@ describe('GasFeeController', () => {
await setupGasFeeController({
...defaultConstructorOptions,
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
networkConfigurations: {
'AAAA-BBBB-CCCC-DDDD': {
id: 'AAAA-BBBB-CCCC-DDDD',
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
},
},
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
},
clientId: '99999',
// Currently initializing the provider overwrites the
// `selectedNetworkClientId` we specify above based on whatever
// `providerConfig` is. So we prevent the provider from being
// initialized to make this test pass. Once `providerConfig` is
// removed, then we don't need this anymore and
// `selectedNetworkClientId` should no longer be overwritten.
initializeNetworkProvider: false,
});

await gasFeeController.fetchGasFeeEstimates();
Expand Down Expand Up @@ -860,14 +899,24 @@ describe('GasFeeController', () => {
await setupGasFeeController({
...defaultConstructorOptions,
networkControllerState: {
providerConfig: {
type: NetworkType.rpc,
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
networkConfigurations: {
'AAAA-BBBB-CCCC-DDDD': {
id: 'AAAA-BBBB-CCCC-DDDD',
chainId: toHex(1337),
rpcUrl: 'http://some/url',
ticker: 'TEST',
},
},
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
},
clientId: '99999',
// Currently initializing the provider overwrites the
// `selectedNetworkClientId` we specify above based on whatever
// `providerConfig` is. So we prevent the provider from being
// initialized to make this test pass. Once `providerConfig` is
// removed, then we don't need this anymore and
// `selectedNetworkClientId` should no longer be overwritten.
initializeNetworkProvider: false,
});

await gasFeeController.fetchGasFeeEstimates();
Expand Down
17 changes: 13 additions & 4 deletions packages/gas-fee-controller/src/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,13 @@ export class GasFeeController extends StaticIntervalPollingController<
await this.#onNetworkControllerDidChange(networkControllerState);
});
} else {
this.currentChainId = this.messagingSystem.call(
const { selectedNetworkClientId } = this.messagingSystem.call(
'NetworkController:getState',
).providerConfig.chainId;
);
this.currentChainId = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
selectedNetworkClientId,
).configuration.chainId;
this.messagingSystem.subscribe(
'NetworkController:networkDidChange',
async (networkControllerState) => {
Expand Down Expand Up @@ -586,8 +590,13 @@ export class GasFeeController extends StaticIntervalPollingController<
);
}

async #onNetworkControllerDidChange(networkControllerState: NetworkState) {
const newChainId = networkControllerState.providerConfig.chainId;
async #onNetworkControllerDidChange({
selectedNetworkClientId,
}: NetworkState) {
const newChainId = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
selectedNetworkClientId,
).configuration.chainId;

if (newChainId !== this.currentChainId) {
this.ethQuery = new EthQuery(this.#getProvider());
Expand Down

0 comments on commit 95abd17

Please sign in to comment.