From 7ca93837851bc4927ef17d6acb5d66dd85fa9345 Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Wed, 19 Apr 2023 18:15:26 -0230 Subject: [PATCH] Fix swaps controller: update provider after networkIdStore state update (#18670) * Fix swaps controller: update provider after networkIdStore state update * Listen for changes on networkController.store, instead of networkController.networkIdStore, in the swaps controller * Fix unit tests --- app/scripts/controllers/swaps.js | 4 ++-- app/scripts/controllers/swaps.test.js | 20 ++++++++++---------- app/scripts/metamask-controller.js | 6 ++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 5030f1cd3..b44e66a21 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -113,7 +113,7 @@ export default class SwapsController { fetchTradesInfo = defaultFetchTradesInfo, getCurrentChainId, getEIP1559GasFeeEstimates, - onNetworkDidChange, + onNetworkStateChange, }) { this.store = new ObservableStore({ swapsState: { ...initialState.swapsState }, @@ -137,7 +137,7 @@ export default class SwapsController { this.ethersProvider = new Web3Provider(provider); this._currentNetworkId = networkController.store.getState().networkId; - onNetworkDidChange(() => { + onNetworkStateChange(() => { const { networkId, networkStatus } = networkController.store.getState(); if ( networkStatus === NetworkStatus.Available && diff --git a/app/scripts/controllers/swaps.test.js b/app/scripts/controllers/swaps.test.js index f5a43e0dc..e3dbaafda 100644 --- a/app/scripts/controllers/swaps.test.js +++ b/app/scripts/controllers/swaps.test.js @@ -160,7 +160,7 @@ describe('SwapsController', function () { return new SwapsController({ getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, networkController: getMockNetworkController(), - onNetworkDidChange: sinon.stub(), + onNetworkStateChange: sinon.stub(), provider: _provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getTokenRatesState: MOCK_TOKEN_RATES_STORE, @@ -208,11 +208,11 @@ describe('SwapsController', function () { it('should replace ethers instance when network changes', function () { const networkController = getMockNetworkController(); - const onNetworkDidChange = sinon.stub(); + const onNetworkStateChange = sinon.stub(); const swapsController = new SwapsController({ getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, networkController, - onNetworkDidChange, + onNetworkStateChange, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getTokenRatesState: MOCK_TOKEN_RATES_STORE, @@ -220,7 +220,7 @@ describe('SwapsController', function () { getCurrentChainId: getCurrentChainIdStub, }); const currentEthersInstance = swapsController.ethersProvider; - const changeNetwork = onNetworkDidChange.getCall(0).args[0]; + const changeNetwork = onNetworkStateChange.getCall(0).args[0]; networkController.store.getState.returns({ networkId: NETWORK_IDS.MAINNET, @@ -238,11 +238,11 @@ describe('SwapsController', function () { it('should not replace ethers instance when network changes to loading', function () { const networkController = getMockNetworkController(); - const onNetworkDidChange = sinon.stub(); + const onNetworkStateChange = sinon.stub(); const swapsController = new SwapsController({ getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, networkController, - onNetworkDidChange, + onNetworkStateChange, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getTokenRatesState: MOCK_TOKEN_RATES_STORE, @@ -250,7 +250,7 @@ describe('SwapsController', function () { getCurrentChainId: getCurrentChainIdStub, }); const currentEthersInstance = swapsController.ethersProvider; - const changeNetwork = onNetworkDidChange.getCall(0).args[0]; + const changeNetwork = onNetworkStateChange.getCall(0).args[0]; networkController.store.getState.returns({ networkId: null, @@ -268,11 +268,11 @@ describe('SwapsController', function () { it('should not replace ethers instance when network changes to the same network', function () { const networkController = getMockNetworkController(); - const onNetworkDidChange = sinon.stub(); + const onNetworkStateChange = sinon.stub(); const swapsController = new SwapsController({ getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, networkController, - onNetworkDidChange, + onNetworkStateChange, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getTokenRatesState: MOCK_TOKEN_RATES_STORE, @@ -280,7 +280,7 @@ describe('SwapsController', function () { getCurrentChainId: getCurrentChainIdStub, }); const currentEthersInstance = swapsController.ethersProvider; - const changeNetwork = onNetworkDidChange.getCall(0).args[0]; + const changeNetwork = onNetworkStateChange.getCall(0).args[0]; networkController.store.getState.returns({ networkId: NETWORK_IDS.GOERLI, diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fa619c62f..f24e1777a 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1195,10 +1195,8 @@ export default class MetamaskController extends EventEmitter { this.txController.txGasUtil, ), networkController: this.networkController, - onNetworkDidChange: networkControllerMessenger.subscribe.bind( - networkControllerMessenger, - NetworkControllerEventType.NetworkDidChange, - ), + onNetworkStateChange: (listener) => + this.networkController.store.subscribe(listener), provider: this.provider, getProviderConfig: () => this.networkController.store.getState().provider, getTokenRatesState: () => this.tokenRatesController.state,