1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

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
This commit is contained in:
Dan J Miller 2023-04-19 18:15:26 -02:30 committed by GitHub
parent 5bb87978ac
commit 7ca9383785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 16 deletions

View File

@ -113,7 +113,7 @@ export default class SwapsController {
fetchTradesInfo = defaultFetchTradesInfo, fetchTradesInfo = defaultFetchTradesInfo,
getCurrentChainId, getCurrentChainId,
getEIP1559GasFeeEstimates, getEIP1559GasFeeEstimates,
onNetworkDidChange, onNetworkStateChange,
}) { }) {
this.store = new ObservableStore({ this.store = new ObservableStore({
swapsState: { ...initialState.swapsState }, swapsState: { ...initialState.swapsState },
@ -137,7 +137,7 @@ export default class SwapsController {
this.ethersProvider = new Web3Provider(provider); this.ethersProvider = new Web3Provider(provider);
this._currentNetworkId = networkController.store.getState().networkId; this._currentNetworkId = networkController.store.getState().networkId;
onNetworkDidChange(() => { onNetworkStateChange(() => {
const { networkId, networkStatus } = networkController.store.getState(); const { networkId, networkStatus } = networkController.store.getState();
if ( if (
networkStatus === NetworkStatus.Available && networkStatus === NetworkStatus.Available &&

View File

@ -160,7 +160,7 @@ describe('SwapsController', function () {
return new SwapsController({ return new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController: getMockNetworkController(), networkController: getMockNetworkController(),
onNetworkDidChange: sinon.stub(), onNetworkStateChange: sinon.stub(),
provider: _provider, provider: _provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
@ -208,11 +208,11 @@ describe('SwapsController', function () {
it('should replace ethers instance when network changes', function () { it('should replace ethers instance when network changes', function () {
const networkController = getMockNetworkController(); const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub(); const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({ const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController, networkController,
onNetworkDidChange, onNetworkStateChange,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
@ -220,7 +220,7 @@ describe('SwapsController', function () {
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
const currentEthersInstance = swapsController.ethersProvider; const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0]; const changeNetwork = onNetworkStateChange.getCall(0).args[0];
networkController.store.getState.returns({ networkController.store.getState.returns({
networkId: NETWORK_IDS.MAINNET, networkId: NETWORK_IDS.MAINNET,
@ -238,11 +238,11 @@ describe('SwapsController', function () {
it('should not replace ethers instance when network changes to loading', function () { it('should not replace ethers instance when network changes to loading', function () {
const networkController = getMockNetworkController(); const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub(); const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({ const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController, networkController,
onNetworkDidChange, onNetworkStateChange,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
@ -250,7 +250,7 @@ describe('SwapsController', function () {
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
const currentEthersInstance = swapsController.ethersProvider; const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0]; const changeNetwork = onNetworkStateChange.getCall(0).args[0];
networkController.store.getState.returns({ networkController.store.getState.returns({
networkId: null, networkId: null,
@ -268,11 +268,11 @@ describe('SwapsController', function () {
it('should not replace ethers instance when network changes to the same network', function () { it('should not replace ethers instance when network changes to the same network', function () {
const networkController = getMockNetworkController(); const networkController = getMockNetworkController();
const onNetworkDidChange = sinon.stub(); const onNetworkStateChange = sinon.stub();
const swapsController = new SwapsController({ const swapsController = new SwapsController({
getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT, getBufferedGasLimit: MOCK_GET_BUFFERED_GAS_LIMIT,
networkController, networkController,
onNetworkDidChange, onNetworkStateChange,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
getTokenRatesState: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
@ -280,7 +280,7 @@ describe('SwapsController', function () {
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
const currentEthersInstance = swapsController.ethersProvider; const currentEthersInstance = swapsController.ethersProvider;
const changeNetwork = onNetworkDidChange.getCall(0).args[0]; const changeNetwork = onNetworkStateChange.getCall(0).args[0];
networkController.store.getState.returns({ networkController.store.getState.returns({
networkId: NETWORK_IDS.GOERLI, networkId: NETWORK_IDS.GOERLI,

View File

@ -1195,10 +1195,8 @@ export default class MetamaskController extends EventEmitter {
this.txController.txGasUtil, this.txController.txGasUtil,
), ),
networkController: this.networkController, networkController: this.networkController,
onNetworkDidChange: networkControllerMessenger.subscribe.bind( onNetworkStateChange: (listener) =>
networkControllerMessenger, this.networkController.store.subscribe(listener),
NetworkControllerEventType.NetworkDidChange,
),
provider: this.provider, provider: this.provider,
getProviderConfig: () => this.networkController.store.getState().provider, getProviderConfig: () => this.networkController.store.getState().provider,
getTokenRatesState: () => this.tokenRatesController.state, getTokenRatesState: () => this.tokenRatesController.state,