From ec4f5209d997c4b2acb976e4fc3352dab8bbd3a6 Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Wed, 30 Nov 2022 12:42:45 -0800 Subject: [PATCH] Change naming of provider to providerConfig where applicable fixes #994 --- .../src/AssetsContractController.ts | 4 +- .../assets-controllers/src/NftController.ts | 4 +- .../src/NftDetectionController.ts | 6 +-- .../src/TokenDetectionController.test.ts | 12 ++--- .../src/TokenDetectionController.ts | 4 +- .../src/TokenListController.test.ts | 50 +++++++++---------- .../src/TokenListController.ts | 13 ++--- .../src/TokenRatesController.test.ts | 2 +- .../src/TokenRatesController.ts | 4 +- .../src/TokensController.test.ts | 6 +-- .../src/TokensController.ts | 4 +- .../src/ComposableController.test.ts | 4 +- .../src/GasFeeController.test.ts | 8 +-- .../src/GasFeeController.ts | 14 +++--- .../src/NetworkController.test.ts | 46 ++++++++--------- .../src/NetworkController.ts | 44 ++++++++-------- .../src/TransactionController.test.ts | 16 +++--- .../src/TransactionController.ts | 25 ++++++---- 18 files changed, 136 insertions(+), 130 deletions(-) diff --git a/packages/assets-controllers/src/AssetsContractController.ts b/packages/assets-controllers/src/AssetsContractController.ts index b3e3814ef7..9e23a4a7d5 100644 --- a/packages/assets-controllers/src/AssetsContractController.ts +++ b/packages/assets-controllers/src/AssetsContractController.ts @@ -114,9 +114,9 @@ export class AssetsContractController extends BaseController< }); onNetworkStateChange((networkState) => { - if (this.config.chainId !== networkState.provider.chainId) { + if (this.config.chainId !== networkState.providerConfig.chainId) { this.configure({ - chainId: networkState.provider.chainId, + chainId: networkState.providerConfig.chainId, }); } }); diff --git a/packages/assets-controllers/src/NftController.ts b/packages/assets-controllers/src/NftController.ts index 2b4913e804..e915e495b8 100644 --- a/packages/assets-controllers/src/NftController.ts +++ b/packages/assets-controllers/src/NftController.ts @@ -952,8 +952,8 @@ export class NftController extends BaseController { }, ); - onNetworkStateChange(({ provider }) => { - const { chainId } = provider; + onNetworkStateChange(({ providerConfig }) => { + const { chainId } = providerConfig; this.configure({ chainId }); }); } diff --git a/packages/assets-controllers/src/NftDetectionController.ts b/packages/assets-controllers/src/NftDetectionController.ts index 223afcecf3..38937cd388 100644 --- a/packages/assets-controllers/src/NftDetectionController.ts +++ b/packages/assets-controllers/src/NftDetectionController.ts @@ -267,10 +267,10 @@ export class NftDetectionController extends BaseController< } }); - onNetworkStateChange(({ provider }) => { + onNetworkStateChange(({ providerConfig }) => { this.configure({ - networkType: provider.type, - chainId: provider.chainId as NftDetectionConfig['chainId'], + networkType: providerConfig.type, + chainId: providerConfig.chainId as NftDetectionConfig['chainId'], }); }); this.getOpenSeaApiKey = getOpenSeaApiKey; diff --git a/packages/assets-controllers/src/TokenDetectionController.test.ts b/packages/assets-controllers/src/TokenDetectionController.test.ts index eaaccba598..17b3a42aae 100644 --- a/packages/assets-controllers/src/TokenDetectionController.test.ts +++ b/packages/assets-controllers/src/TokenDetectionController.test.ts @@ -4,7 +4,7 @@ import { BN } from 'ethereumjs-util'; import { NetworkController, NetworkControllerMessenger, - NetworkControllerProviderChangeEvent, + NetworkControllerProviderConfigChangeEvent, NetworkControllerStateChangeEvent, } from '@metamask/network-controller'; import { NetworksChainId, MAINNET } from '@metamask/controller-utils'; @@ -86,7 +86,7 @@ const sampleTokenB: Token = { type MainControllerMessenger = ControllerMessenger< GetTokenListState, | TokenListStateChange - | NetworkControllerProviderChangeEvent + | NetworkControllerProviderConfigChangeEvent | NetworkControllerStateChangeEvent >; @@ -100,7 +100,7 @@ const setupNetworkController = ( const networkMessenger = controllerMessenger.getRestricted({ name: 'NetworkController', allowedEvents: [ - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', 'NetworkController:stateChange', ], allowedActions: [], @@ -122,7 +122,7 @@ const setupTokenListController = ( allowedActions: [], allowedEvents: [ 'TokenListController:stateChange', - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ], }); @@ -468,7 +468,7 @@ describe('TokenDetectionController', () => { tokenDetection.stop(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await networkStateChangeListener!({ - provider: { chainId: polygonDecimalChainId }, + providerConfig: { chainId: polygonDecimalChainId }, }); expect(getBalancesInSingleCallMock.called).toBe(false); @@ -572,7 +572,7 @@ describe('TokenDetectionController', () => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await networkStateChangeListener!({ - provider: { chainId: NetworksChainId.mainnet }, + providerConfig: { chainId: NetworksChainId.mainnet }, }); expect(getBalancesInSingleCallMock.called).toBe(true); diff --git a/packages/assets-controllers/src/TokenDetectionController.ts b/packages/assets-controllers/src/TokenDetectionController.ts index 400ab4a950..b21a74789e 100644 --- a/packages/assets-controllers/src/TokenDetectionController.ts +++ b/packages/assets-controllers/src/TokenDetectionController.ts @@ -105,7 +105,7 @@ export class TokenDetectionController extends BaseController< state?: Partial, ) { const { - provider: { chainId: defaultChainId }, + providerConfig: { chainId: defaultChainId }, } = getNetworkState(); const { useTokenDetection: defaultUseTokenDetection } = getPreferencesState(); @@ -159,7 +159,7 @@ export class TokenDetectionController extends BaseController< } }); - onNetworkStateChange(({ provider: { chainId } }) => { + onNetworkStateChange(({ providerConfig: { chainId } }) => { const { chainId: currentChainId } = this.config; const isDetectionEnabledForNetwork = isTokenDetectionSupportedForNetwork(chainId); diff --git a/packages/assets-controllers/src/TokenListController.test.ts b/packages/assets-controllers/src/TokenListController.test.ts index b3689cbd17..b88207edd8 100644 --- a/packages/assets-controllers/src/TokenListController.test.ts +++ b/packages/assets-controllers/src/TokenListController.test.ts @@ -3,7 +3,7 @@ import nock from 'nock'; import { ControllerMessenger } from '@metamask/base-controller'; import { NetworkController, - NetworkControllerProviderChangeEvent, + NetworkControllerProviderConfigChangeEvent, } from '@metamask/network-controller'; import { NetworksChainId } from '@metamask/controller-utils'; import { @@ -473,7 +473,7 @@ const expiredCacheExistingState: TokenListState = { type MainControllerMessenger = ControllerMessenger< GetTokenListState, - TokenListStateChange | NetworkControllerProviderChangeEvent + TokenListStateChange | NetworkControllerProviderConfigChangeEvent >; const getControllerMessenger = (): MainControllerMessenger => { @@ -485,7 +485,7 @@ const setupNetworkController = ( ) => { const networkMessenger = controllerMessenger.getRestricted({ name: 'NetworkController', - allowedEvents: ['NetworkController:providerChange'], + allowedEvents: ['NetworkController:providerConfigChange'], allowedActions: [], }); @@ -505,7 +505,7 @@ const getRestrictedMessenger = ( allowedActions: [], allowedEvents: [ 'TokenListController:stateChange', - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ], }); @@ -536,7 +536,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -586,7 +586,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -607,7 +607,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -627,7 +627,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -645,7 +645,7 @@ describe('TokenListController', () => { chainId: NetworksChainId.mainnet, onNetworkStateChange: (callback) => controllerMessenger.subscribe( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', callback, ), preventPollingOnNetworkRestart: false, @@ -663,7 +663,7 @@ describe('TokenListController', () => { expect(controller.state.tokenList).toStrictEqual({}); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -692,7 +692,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -723,7 +723,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -758,7 +758,7 @@ describe('TokenListController', () => { expect(tokenListMock.calledThrice).toBe(true); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -784,7 +784,7 @@ describe('TokenListController', () => { expect(tokenListMock.called).toBe(true); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -812,7 +812,7 @@ describe('TokenListController', () => { controller.destroy(); tokenListMock.restore(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -852,7 +852,7 @@ describe('TokenListController', () => { ); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); } finally { controller.destroy(); @@ -892,7 +892,7 @@ describe('TokenListController', () => { ); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -919,7 +919,7 @@ describe('TokenListController', () => { ); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -966,7 +966,7 @@ describe('TokenListController', () => { ).toStrictEqual(sampleWithDuplicateSymbolsTokensChainsCache); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -994,7 +994,7 @@ describe('TokenListController', () => { ).toStrictEqual(sampleWith3OrMoreOccurrences); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -1026,7 +1026,7 @@ describe('TokenListController', () => { ); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -1061,7 +1061,7 @@ describe('TokenListController', () => { ); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -1127,7 +1127,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -1149,7 +1149,7 @@ describe('TokenListController', () => { controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); }); @@ -1206,7 +1206,7 @@ describe('TokenListController', () => { messenger.clearEventSubscriptions('TokenListController:stateChange'); controller.destroy(); controllerMessenger.clearEventSubscriptions( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ); resolve(); }); diff --git a/packages/assets-controllers/src/TokenListController.ts b/packages/assets-controllers/src/TokenListController.ts index 675bb23a22..c6d57c5691 100644 --- a/packages/assets-controllers/src/TokenListController.ts +++ b/packages/assets-controllers/src/TokenListController.ts @@ -7,7 +7,7 @@ import { } from '@metamask/base-controller'; import { safelyExecute } from '@metamask/controller-utils'; import { - NetworkControllerProviderChangeEvent, + NetworkControllerProviderConfigChangeEvent, NetworkState, ProviderConfig, } from '@metamask/network-controller'; @@ -62,9 +62,10 @@ export type GetTokenListState = { type TokenListMessenger = RestrictedControllerMessenger< typeof name, GetTokenListState, - TokenListStateChange | NetworkControllerProviderChangeEvent, + TokenListStateChange | NetworkControllerProviderConfigChangeEvent, never, - TokenListStateChange['type'] | NetworkControllerProviderChangeEvent['type'] + | TokenListStateChange['type'] + | NetworkControllerProviderConfigChangeEvent['type'] >; const metadata = { @@ -146,9 +147,9 @@ export class TokenListController extends BaseControllerV2< // this check for "provider" is for testing purposes, since in the extension this callback will receive // an object typed as NetworkState but within repo we can only simulate as if the callback receives an // object typed as ProviderConfig - if ('provider' in networkStateOrProviderConfig) { + if ('providerConfig' in networkStateOrProviderConfig) { await this.#onNetworkStateChangeCallback( - networkStateOrProviderConfig.provider, + networkStateOrProviderConfig.providerConfig, ); } else { await this.#onNetworkStateChangeCallback( @@ -158,7 +159,7 @@ export class TokenListController extends BaseControllerV2< }); } else { this.messagingSystem.subscribe( - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', async (providerConfig) => { await this.#onNetworkStateChangeCallback(providerConfig); }, diff --git a/packages/assets-controllers/src/TokenRatesController.test.ts b/packages/assets-controllers/src/TokenRatesController.test.ts index cc4f3d12b7..577efc9f5e 100644 --- a/packages/assets-controllers/src/TokenRatesController.test.ts +++ b/packages/assets-controllers/src/TokenRatesController.test.ts @@ -453,7 +453,7 @@ describe('TokenRatesController', () => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await networkChangeListener!({ - provider: { chainId: '4' }, + providerConfig: { chainId: '4' }, }); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/packages/assets-controllers/src/TokenRatesController.ts b/packages/assets-controllers/src/TokenRatesController.ts index 68eb27e8dd..33048873c0 100644 --- a/packages/assets-controllers/src/TokenRatesController.ts +++ b/packages/assets-controllers/src/TokenRatesController.ts @@ -212,8 +212,8 @@ export class TokenRatesController extends BaseController< this.configure({ nativeCurrency: currencyRateState.nativeCurrency }); }); - onNetworkStateChange(({ provider }) => { - const { chainId } = provider; + onNetworkStateChange(({ providerConfig }) => { + const { chainId } = providerConfig; this.update({ contractExchangeRates: {} }); this.configure({ chainId }); }); diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 41f662eab5..5f885d8490 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -40,7 +40,7 @@ describe('TokensController', () => { name: 'NetworkController', allowedEvents: [ 'NetworkController:stateChange', - 'NetworkController:providerChange', + 'NetworkController:providerConfigChange', ], allowedActions: [], }); @@ -55,7 +55,7 @@ describe('TokensController', () => { onNetworkStateChange: (listener) => messenger.subscribe('NetworkController:stateChange', (state, patch) => { const touchesProviderConfig = patch.find( - (p) => p.path[0] === 'provider', + (p) => p.path[0] === 'providerConfig', ); if (touchesProviderConfig) { listener(state); @@ -346,7 +346,7 @@ describe('TokensController', () => { preferences.update({ selectedAddress: address }); expect(preferences.state.selectedAddress).toStrictEqual(address); network.setProviderType(networkType); - expect(network.state.provider.type).toStrictEqual(networkType); + expect(network.state.providerConfig.type).toStrictEqual(networkType); }); describe('ignoredTokens', () => { diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index f26cd4f739..5c49ef469f 100644 --- a/packages/assets-controllers/src/TokensController.ts +++ b/packages/assets-controllers/src/TokensController.ts @@ -238,10 +238,10 @@ export class TokensController extends BaseController< }); }); - onNetworkStateChange(({ provider }) => { + onNetworkStateChange(({ providerConfig }) => { const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state; const { selectedAddress } = this.config; - const { chainId } = provider; + const { chainId } = providerConfig; this.abortController.abort(); this.abortController = new WhatwgAbortController(); this.configure({ chainId }); diff --git a/packages/composable-controller/src/ComposableController.test.ts b/packages/composable-controller/src/ComposableController.test.ts index 08accbc5cf..310a742e39 100644 --- a/packages/composable-controller/src/ComposableController.test.ts +++ b/packages/composable-controller/src/ComposableController.test.ts @@ -224,7 +224,7 @@ describe('ComposableController', () => { network: 'loading', isCustomNetwork: false, properties: { isEIP1559Compatible: false }, - provider: { type: 'mainnet', chainId: NetworksChainId.mainnet }, + providerConfig: { type: 'mainnet', chainId: NetworksChainId.mainnet }, }, PreferencesController: { featureFlags: {}, @@ -284,7 +284,7 @@ describe('ComposableController', () => { network: 'loading', isCustomNetwork: false, properties: { isEIP1559Compatible: false }, - provider: { type: 'mainnet', chainId: NetworksChainId.mainnet }, + providerConfig: { type: 'mainnet', chainId: NetworksChainId.mainnet }, selectedAddress: '', useTokenDetection: true, useNftDetection: false, diff --git a/packages/gas-fee-controller/src/GasFeeController.test.ts b/packages/gas-fee-controller/src/GasFeeController.test.ts index 1e0b178d6d..46f2efefbf 100644 --- a/packages/gas-fee-controller/src/GasFeeController.test.ts +++ b/packages/gas-fee-controller/src/GasFeeController.test.ts @@ -4,7 +4,7 @@ import { NetworkController, NetworkControllerGetEthQueryAction, NetworkControllerGetProviderConfigAction, - NetworkControllerProviderChangeEvent, + NetworkControllerProviderConfigChangeEvent, } from '@metamask/network-controller'; import { GAS_ESTIMATE_TYPES, @@ -32,7 +32,7 @@ type MainControllerMessenger = ControllerMessenger< | GetGasFeeState | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction, - GasFeeStateChange | NetworkControllerProviderChangeEvent + GasFeeStateChange | NetworkControllerProviderConfigChangeEvent >; const getControllerMessenger = (): MainControllerMessenger => { @@ -44,7 +44,7 @@ const setupNetworkController = ( ) => { const networkMessenger = controllerMessenger.getRestricted({ name: 'NetworkController', - allowedEvents: ['NetworkController:providerChange'], + allowedEvents: ['NetworkController:providerConfigChange'], allowedActions: [ 'NetworkController:getProviderConfig', 'NetworkController:getEthQuery', @@ -68,7 +68,7 @@ const getRestrictedMessenger = ( 'NetworkController:getProviderConfig', 'NetworkController:getEthQuery', ], - allowedEvents: ['NetworkController:providerChange'], + allowedEvents: ['NetworkController:providerConfigChange'], }); return messenger; diff --git a/packages/gas-fee-controller/src/GasFeeController.ts b/packages/gas-fee-controller/src/GasFeeController.ts index e1cd5b5186..4d00ef895b 100644 --- a/packages/gas-fee-controller/src/GasFeeController.ts +++ b/packages/gas-fee-controller/src/GasFeeController.ts @@ -10,7 +10,7 @@ import { safelyExecute } from '@metamask/controller-utils'; import type { NetworkControllerGetEthQueryAction, NetworkControllerGetProviderConfigAction, - NetworkControllerProviderChangeEvent, + NetworkControllerProviderConfigChangeEvent, NetworkController, NetworkState, } from '@metamask/network-controller'; @@ -216,10 +216,10 @@ type GasFeeMessenger = RestrictedControllerMessenger< | GetGasFeeState | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction, - GasFeeStateChange | NetworkControllerProviderChangeEvent, + GasFeeStateChange | NetworkControllerProviderConfigChangeEvent, | NetworkControllerGetProviderConfigAction['type'] | NetworkControllerGetEthQueryAction['type'], - NetworkControllerProviderChangeEvent['type'] + NetworkControllerProviderConfigChangeEvent['type'] >; const defaultState: GasFeeState = { @@ -353,14 +353,14 @@ export class GasFeeController extends BaseControllerV2< ); this.messagingSystem.subscribe( - 'NetworkController:providerChange', - async (provider) => { + 'NetworkController:providerConfigChange', + async (newProviderConfig) => { this.ethQuery = this.messagingSystem.call( 'NetworkController:getEthQuery', ); - if (this.currentChainId !== provider.chainId) { - this.currentChainId = provider.chainId; + if (this.currentChainId !== newProviderConfig.chainId) { + this.currentChainId = newProviderConfig.chainId; await this.resetPolling(); } }, diff --git a/packages/network-controller/src/NetworkController.test.ts b/packages/network-controller/src/NetworkController.test.ts index a49fbfa1bf..d831218949 100644 --- a/packages/network-controller/src/NetworkController.test.ts +++ b/packages/network-controller/src/NetworkController.test.ts @@ -19,7 +19,7 @@ const setupController = ( infuraProjectId: 'foo', state: { network: '0', - provider: { + providerConfig: { type: pType, chainId: NetworksChainId[pType], }, @@ -38,7 +38,7 @@ describe('NetworkController', () => { beforeEach(() => { messenger = new ControllerMessenger().getRestricted({ name: 'NetworkController', - allowedEvents: ['NetworkController:providerChange'], + allowedEvents: ['NetworkController:providerConfigChange'], allowedActions: [], }); }); @@ -57,7 +57,7 @@ describe('NetworkController', () => { network: 'loading', isCustomNetwork: false, properties: { isEIP1559Compatible: false }, - provider: { + providerConfig: { type: 'mainnet', chainId: '1', }, @@ -91,7 +91,7 @@ describe('NetworkController', () => { infuraProjectId: 'foo', state: { network: '0', - provider: { + providerConfig: { rpcTarget: RPC_TARGET, type: 'rpc', chainId: '10', @@ -111,7 +111,7 @@ describe('NetworkController', () => { infuraProjectId: 'foo', state: { network: '0', - provider: { + providerConfig: { rpcTarget: RPC_TARGET, type: 'rpc', chainId: NetworksChainId.mainnet, @@ -128,14 +128,14 @@ describe('NetworkController', () => { it('should set new RPC target', () => { const controller = new NetworkController({ messenger }); controller.setRpcTarget(RPC_TARGET, NetworksChainId.rpc); - expect(controller.state.provider.rpcTarget).toBe(RPC_TARGET); + expect(controller.state.providerConfig.rpcTarget).toBe(RPC_TARGET); expect(controller.state.isCustomNetwork).toBe(false); }); it('should set new provider type', () => { const controller = new NetworkController({ messenger }); controller.setProviderType('localhost'); - expect(controller.state.provider.type).toBe('localhost'); + expect(controller.state.providerConfig.type).toBe('localhost'); expect(controller.state.isCustomNetwork).toBe(false); }); @@ -145,11 +145,11 @@ describe('NetworkController', () => { infuraProjectId: '123', }); controller.setProviderType('goerli' as NetworkType); - expect(controller.state.provider.type).toBe('goerli'); - expect(controller.state.provider.ticker).toBe('GoerliETH'); + expect(controller.state.providerConfig.type).toBe('goerli'); + expect(controller.state.providerConfig.ticker).toBe('GoerliETH'); expect(controller.state.isCustomNetwork).toBe(false); - expect(controller.state.provider.rpcTarget).toBeUndefined(); - expect(controller.state.provider.nickname).toBeUndefined(); + expect(controller.state.providerConfig.rpcTarget).toBeUndefined(); + expect(controller.state.providerConfig.nickname).toBeUndefined(); }); it('should set mainnet provider type', () => { @@ -158,11 +158,11 @@ describe('NetworkController', () => { infuraProjectId: '123', }); controller.setProviderType('mainnet' as NetworkType); - expect(controller.state.provider.type).toBe('mainnet'); - expect(controller.state.provider.ticker).toBe('ETH'); + expect(controller.state.providerConfig.type).toBe('mainnet'); + expect(controller.state.providerConfig.ticker).toBe('ETH'); expect(controller.state.isCustomNetwork).toBe(false); - expect(controller.state.provider.rpcTarget).toBeUndefined(); - expect(controller.state.provider.nickname).toBeUndefined(); + expect(controller.state.providerConfig.rpcTarget).toBeUndefined(); + expect(controller.state.providerConfig.nickname).toBeUndefined(); }); it('should set rpcTarget and nickname props to undefined when set a provider type', () => { @@ -172,11 +172,11 @@ describe('NetworkController', () => { }); controller.setRpcTarget(RPC_TARGET, NetworksChainId.rpc); controller.setProviderType('mainnet' as NetworkType); - expect(controller.state.provider.type).toBe('mainnet'); - expect(controller.state.provider.ticker).toBe('ETH'); + expect(controller.state.providerConfig.type).toBe('mainnet'); + expect(controller.state.providerConfig.ticker).toBe('ETH'); expect(controller.state.isCustomNetwork).toBe(false); - expect(controller.state.provider.rpcTarget).toBeUndefined(); - expect(controller.state.provider.nickname).toBeUndefined(); + expect(controller.state.providerConfig.rpcTarget).toBeUndefined(); + expect(controller.state.providerConfig.nickname).toBeUndefined(); }); it('should throw when setting an unrecognized provider type', () => { @@ -207,16 +207,16 @@ describe('NetworkController', () => { infuraProjectId: '341eacb578dd44a1a049cbc5f6fd4035', messenger, }; - const event = 'NetworkController:providerChange'; + const event = 'NetworkController:providerConfigChange'; const controller = new NetworkController(testConfig); await new Promise((resolve) => { - const handleProviderChange = () => { + const handleProviderConfigChange = () => { expect(controller.state.network !== 'loading').toBe(true); - messenger.unsubscribe(event, handleProviderChange); + messenger.unsubscribe(event, handleProviderConfigChange); resolve(''); }; - messenger.subscribe(event, handleProviderChange); + messenger.subscribe(event, handleProviderConfigChange); controller.providerConfig = {} as ProviderConfig; }); diff --git a/packages/network-controller/src/NetworkController.ts b/packages/network-controller/src/NetworkController.ts index a0f9aaccef..730094891c 100644 --- a/packages/network-controller/src/NetworkController.ts +++ b/packages/network-controller/src/NetworkController.ts @@ -53,7 +53,7 @@ export type NetworkProperties = { export type NetworkState = { network: string; isCustomNetwork: boolean; - provider: ProviderConfig; + providerConfig: ProviderConfig; properties: NetworkProperties; }; @@ -68,8 +68,8 @@ export type NetworkControllerStateChangeEvent = { payload: [NetworkState, Patch[]]; }; -export type NetworkControllerProviderChangeEvent = { - type: `NetworkController:providerChange`; +export type NetworkControllerProviderConfigChangeEvent = { + type: `NetworkController:providerConfigChange`; payload: [ProviderConfig]; }; @@ -86,7 +86,8 @@ export type NetworkControllerGetEthQueryAction = { export type NetworkControllerMessenger = RestrictedControllerMessenger< typeof name, NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction, - NetworkControllerStateChangeEvent | NetworkControllerProviderChangeEvent, + | NetworkControllerStateChangeEvent + | NetworkControllerProviderConfigChangeEvent, string, string >; @@ -100,7 +101,7 @@ export type NetworkControllerOptions = { const defaultState: NetworkState = { network: 'loading', isCustomNetwork: false, - provider: { type: MAINNET, chainId: NetworksChainId.mainnet }, + providerConfig: { type: MAINNET, chainId: NetworksChainId.mainnet }, properties: { isEIP1559Compatible: false }, }; @@ -136,7 +137,7 @@ export class NetworkController extends BaseControllerV2< persist: true, anonymous: false, }, - provider: { + providerConfig: { persist: true, anonymous: false, }, @@ -148,7 +149,7 @@ export class NetworkController extends BaseControllerV2< this.messagingSystem.registerActionHandler( `${this.name}:getProviderConfig`, () => { - return this.state.provider; + return this.state.providerConfig; }, ); @@ -197,7 +198,7 @@ export class NetworkController extends BaseControllerV2< state.network = 'loading'; state.properties = {}; }); - const { rpcTarget, type, chainId, ticker } = this.state.provider; + const { rpcTarget, type, chainId, ticker } = this.state.providerConfig; this.initializeProvider(type, rpcTarget, chainId, ticker); this.lookupNetwork(); } @@ -286,7 +287,8 @@ export class NetworkController extends BaseControllerV2< */ set providerConfig(providerConfig: ProviderConfig) { this.internalProviderConfig = providerConfig; - const { type, rpcTarget, chainId, ticker, nickname } = this.state.provider; + const { type, rpcTarget, chainId, ticker, nickname } = + this.state.providerConfig; this.initializeProvider(type, rpcTarget, chainId, ticker, nickname); this.registerProvider(); this.lookupNetwork(); @@ -320,8 +322,8 @@ export class NetworkController extends BaseControllerV2< }); this.messagingSystem.publish( - `NetworkController:providerChange`, - this.state.provider, + `NetworkController:providerConfigChange`, + this.state.providerConfig, ); } finally { releaseLock(); @@ -344,11 +346,11 @@ export class NetworkController extends BaseControllerV2< : 'ETH'; this.update((state) => { - state.provider.type = type; - state.provider.ticker = ticker; - state.provider.chainId = NetworksChainId[type]; - state.provider.rpcTarget = undefined; - state.provider.nickname = undefined; + state.providerConfig.type = type; + state.providerConfig.ticker = ticker; + state.providerConfig.chainId = NetworksChainId[type]; + state.providerConfig.rpcTarget = undefined; + state.providerConfig.nickname = undefined; }); this.refreshNetwork(); } @@ -368,11 +370,11 @@ export class NetworkController extends BaseControllerV2< nickname?: string, ) { this.update((state) => { - state.provider.type = RPC; - state.provider.rpcTarget = rpcTarget; - state.provider.chainId = chainId; - state.provider.ticker = ticker; - state.provider.nickname = nickname; + state.providerConfig.type = RPC; + state.providerConfig.rpcTarget = rpcTarget; + state.providerConfig.chainId = chainId; + state.providerConfig.ticker = ticker; + state.providerConfig.nickname = nickname; }); this.refreshNetwork(); } diff --git a/packages/transaction-controller/src/TransactionController.test.ts b/packages/transaction-controller/src/TransactionController.test.ts index 3901ceb506..2cd18b8ac6 100644 --- a/packages/transaction-controller/src/TransactionController.test.ts +++ b/packages/transaction-controller/src/TransactionController.test.ts @@ -134,7 +134,7 @@ const MOCK_NETWORK = { network: '3', isCustomNetwork: false, properties: { isEIP1559Compatible: false }, - provider: { + providerConfig: { type: 'ropsten' as NetworkType, chainId: NetworksChainId.ropsten, }, @@ -147,7 +147,7 @@ const MOCK_NETWORK_CUSTOM = { network: '10', isCustomNetwork: true, properties: { isEIP1559Compatible: false }, - provider: { + providerConfig: { type: 'rpc' as NetworkType, chainId: '10', }, @@ -157,7 +157,7 @@ const MOCK_NETWORK_CUSTOM = { const MOCK_NETWORK_WITHOUT_CHAIN_ID = { getProvider: () => PROVIDER, isCustomNetwork: false, - state: { network: '3', provider: { type: 'ropsten' as NetworkType } }, + state: { network: '3', providerConfig: { type: 'ropsten' as NetworkType } }, subscribe: () => undefined, }; const MOCK_MAINNET_NETWORK = { @@ -166,7 +166,7 @@ const MOCK_MAINNET_NETWORK = { network: '1', isCustomNetwork: false, properties: { isEIP1559Compatible: false }, - provider: { + providerConfig: { type: 'mainnet' as NetworkType, chainId: NetworksChainId.mainnet, }, @@ -179,7 +179,7 @@ const MOCK_CUSTOM_NETWORK = { network: '80001', isCustomNetwork: true, properties: { isEIP1559Compatible: false }, - provider: { + providerConfig: { type: 'rpc' as NetworkType, chainId: '80001', }, @@ -507,7 +507,7 @@ describe('TransactionController', () => { ); expect(controller.state.transactions[0].chainId).toBe( - MOCK_NETWORK.state.provider.chainId, + MOCK_NETWORK.state.providerConfig.chainId, ); expect(controller.state.transactions[0].status).toBe( @@ -546,7 +546,7 @@ describe('TransactionController', () => { ); expect(controller.state.transactions[0].chainId).toBe( - MOCK_MAINNET_NETWORK.state.provider.chainId, + MOCK_MAINNET_NETWORK.state.providerConfig.chainId, ); expect(controller.state.transactions[0].status).toBe( @@ -585,7 +585,7 @@ describe('TransactionController', () => { ); expect(controller.state.transactions[0].chainId).toBe( - MOCK_NETWORK_CUSTOM.state.provider.chainId, + MOCK_NETWORK_CUSTOM.state.providerConfig.chainId, ); expect(controller.state.transactions[0].status).toBe( diff --git a/packages/transaction-controller/src/TransactionController.ts b/packages/transaction-controller/src/TransactionController.ts index 30aa451a50..b31107bcd2 100644 --- a/packages/transaction-controller/src/TransactionController.ts +++ b/packages/transaction-controller/src/TransactionController.ts @@ -508,7 +508,7 @@ export class TransactionController extends BaseController< origin?: string, deviceConfirmedOn?: WalletDevice, ): Promise { - const { provider, network } = this.getNetworkState(); + const { providerConfig, network } = this.getNetworkState(); const { transactions } = this.state; transaction = normalizeTransaction(transaction); validateTransaction(transaction); @@ -516,7 +516,7 @@ export class TransactionController extends BaseController< const transactionMeta: TransactionMeta = { id: random(), networkID: network, - chainId: provider.chainId, + chainId: providerConfig.chainId, origin, status: TransactionStatus.unapproved as TransactionStatus.unapproved, time: Date.now(), @@ -593,7 +593,7 @@ export class TransactionController extends BaseController< getCommonConfiguration(): Common { const { network: networkId, - provider: { type: chain, chainId, nickname: name }, + providerConfig: { type: chain, chainId, nickname: name }, } = this.getNetworkState(); if (chain !== RPC) { @@ -620,8 +620,8 @@ export class TransactionController extends BaseController< async approveTransaction(transactionID: string) { const { transactions } = this.state; const releaseLock = await this.mutex.acquire(); - const { provider } = this.getNetworkState(); - const { chainId: currentChainId } = provider; + const { providerConfig } = this.getNetworkState(); + const { chainId: currentChainId } = providerConfig; const index = transactions.findIndex(({ id }) => transactionID === id); const transactionMeta = transactions[index]; const { nonce } = transactionMeta.transaction; @@ -1035,8 +1035,9 @@ export class TransactionController extends BaseController< */ async queryTransactionStatuses() { const { transactions } = this.state; - const { provider, network: currentNetworkID } = this.getNetworkState(); - const { chainId: currentChainId } = provider; + const { providerConfig, network: currentNetworkID } = + this.getNetworkState(); + const { chainId: currentChainId } = providerConfig; let gotUpdates = false; await safelyExecute(() => Promise.all( @@ -1095,8 +1096,9 @@ export class TransactionController extends BaseController< this.update({ transactions: [] }); return; } - const { provider, network: currentNetworkID } = this.getNetworkState(); - const { chainId: currentChainId } = provider; + const { providerConfig, network: currentNetworkID } = + this.getNetworkState(); + const { chainId: currentChainId } = providerConfig; const newTransactions = this.state.transactions.filter( ({ networkID, chainId }) => { // Using fallback to networkID only when there is no chainId present. Should be removed when networkID is completely removed. @@ -1125,8 +1127,9 @@ export class TransactionController extends BaseController< address: string, opt?: FetchAllOptions, ): Promise { - const { provider, network: currentNetworkID } = this.getNetworkState(); - const { chainId: currentChainId, type: networkType } = provider; + const { providerConfig, network: currentNetworkID } = + this.getNetworkState(); + const { chainId: currentChainId, type: networkType } = providerConfig; const { transactions } = this.state; const supportedNetworkIds = ['1', '3', '4', '42'];