diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index 203541df338f..57f36a3b634b 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -112,10 +112,9 @@ export default class DetectTokensController extends StaticIntervalPollingControl } async _executePoll(networkClientId, options) { - const networkClient = this.getNetworkClientById(networkClientId); await this.detectNewTokens({ ...options, - chainId: networkClient.configuration.chainId, + networkClientId, }); } @@ -125,11 +124,24 @@ export default class DetectTokensController extends StaticIntervalPollingControl * @param options * @param options.selectedAddress - the selectedAddress against which to detect for token balances * @param options.chainId - the chainId against which to detect for token balances + * @param options.networkClientId */ - async detectNewTokens({ selectedAddress, chainId } = {}) { + async detectNewTokens({ selectedAddress, chainId, networkClientId } = {}) { const addressAgainstWhichToDetect = selectedAddress ?? this.selectedAddress; - const chainIdAgainstWhichToDetect = - chainId ?? this.getChainIdFromNetworkStore(); + let chainIdAgainstWhichToDetect; + let networkClientIdAgainstWhichToDetect; + + if (networkClientId) { + networkClientIdAgainstWhichToDetect = networkClientId; + const networkClient = this.getNetworkClientById(networkClientId); + chainIdAgainstWhichToDetect = networkClient.configuration.chainId; + } else { + chainIdAgainstWhichToDetect = + chainId ?? this.getChainIdFromNetworkStore(); + networkClientIdAgainstWhichToDetect = + this.network.findNetworkClientIdByChainId(chainIdAgainstWhichToDetect); + } + if (!this.isActive) { return; } @@ -184,9 +196,7 @@ export default class DetectTokensController extends StaticIntervalPollingControl result = await this.assetsContractController.getBalancesInSingleCall( addressAgainstWhichToDetect, tokensSlice, - this.network.findNetworkClientIdByChainId( - chainIdAgainstWhichToDetect, - ), + networkClientIdAgainstWhichToDetect, ); } catch (error) { warn( diff --git a/app/scripts/controllers/detect-tokens.test.js b/app/scripts/controllers/detect-tokens.test.js index 600afd56a5f0..c0ba312b38b2 100644 --- a/app/scripts/controllers/detect-tokens.test.js +++ b/app/scripts/controllers/detect-tokens.test.js @@ -488,6 +488,101 @@ describe('DetectTokensController', function () { ]); }); + it('gets balances in single call using the networkClientId when provided', async function () { + sandbox.useFakeTimers(); + await network.setProviderType(NETWORK_TYPES.MAINNET); + const controller = new DetectTokensController({ + messenger: getRestrictedMessenger(), + preferences, + network, + tokenList: tokenListController, + tokensController, + assetsContractController, + trackMetaMetricsEvent: noop, + getCurrentSelectedAccount: + accountsController.getSelectedAccount.bind(accountsController), + getNetworkClientById: () => ({ + configuration: { + chainId: '0x1', + }, + provider: {}, + blockTracker: {}, + destroy: () => { + // noop + }, + }), + }); + controller.isOpen = true; + controller.isUnlocked = true; + + const stub = sandbox.stub( + assetsContractController, + 'getBalancesInSingleCall', + ); + + await controller.detectNewTokens({ + networkClientId: 'mainnet', + }); + + sandbox.assert.calledWith( + stub, + '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', + [ + '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f', + '0x514910771af9ca656af840dff83e8264ecf986ca', + '0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c', + ], + 'mainnet', + ); + sandbox.assert.calledWith( + stub, + '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', + [], + 'mainnet', + ); + }); + + it('uses the resolved chainId from networkClientId over the passed in chainId arg', async function () { + sandbox.useFakeTimers(); + await network.setProviderType(NETWORK_TYPES.MAINNET); + const controller = new DetectTokensController({ + messenger: getRestrictedMessenger(), + preferences, + network, + tokenList: tokenListController, + tokensController, + assetsContractController, + trackMetaMetricsEvent: noop, + getCurrentSelectedAccount: + accountsController.getSelectedAccount.bind(accountsController), + getNetworkClientById: () => ({ + configuration: { + chainId: '0x1', + }, + provider: {}, + blockTracker: {}, + destroy: () => { + // noop + }, + }), + }); + controller.isOpen = true; + controller.isUnlocked = true; + + const stub = sandbox.stub( + assetsContractController, + 'getBalancesInSingleCall', + ); + + await controller.detectNewTokens({ + // non supported chainId will cause this method to exit early if used instead of the networkClientId + chainId: '0xdeadbeef', + networkClientId: 'mainnet', + }); + + sandbox.assert.called(stub); + }); + it('should check and add tokens while on supported networks', async function () { sandbox.useFakeTimers(); await network.setProviderType(NETWORK_TYPES.MAINNET); @@ -712,8 +807,8 @@ describe('DetectTokensController', function () { await Promise.all([jest.advanceTimersByTime(1000), flushPromises()]); expect(detectNewTokensSpy).toHaveBeenCalledTimes(2); expect(detectNewTokensSpy.mock.calls).toStrictEqual([ - [{ chainId: '0x1' }], - [{ chainId: '0x1' }], + [{ networkClientId: 'mainnet' }], + [{ networkClientId: 'mainnet' }], ]); detectNewTokensSpy.mockRestore(); diff --git a/package.json b/package.json index 607c8854efed..1225e3867e2f 100644 --- a/package.json +++ b/package.json @@ -217,8 +217,7 @@ "nonce-tracker@npm:^3.0.0": "patch:nonce-tracker@npm%3A3.0.0#~/.yarn/patches/nonce-tracker-npm-3.0.0-c5e9a93f9d.patch", "@trezor/connect-web": "9.0.11", "lavamoat-core@npm:^15.1.1": "patch:lavamoat-core@npm%3A15.1.1#~/.yarn/patches/lavamoat-core-npm-15.1.1-51fbe39988.patch", - "tar-stream@npm:^3.1.6": "patch:tar-stream@npm%3A3.1.6#~/.yarn/patches/tar-stream-npm-3.1.6-ce3ac17e49.patch", - "socks": "^2.7.3" + "tar-stream@npm:^3.1.6": "patch:tar-stream@npm%3A3.1.6#~/.yarn/patches/tar-stream-npm-3.1.6-ce3ac17e49.patch" }, "dependencies": { "@babel/runtime": "^7.23.2", diff --git a/test/e2e/tests/send-eth.spec.js b/test/e2e/tests/send-eth.spec.js index 2ffc72a197a5..8d8d86bd873d 100644 --- a/test/e2e/tests/send-eth.spec.js +++ b/test/e2e/tests/send-eth.spec.js @@ -175,6 +175,9 @@ describe('Send ETH', function () { if (process.env.MULTICHAIN) { await driver.clickElement({ text: 'Continue', tag: 'button' }); } else { + // We need to wait for the text "Max Fee: 0.000xxxx ETH" before clicking Next + await driver.findElement({ text: '0.000', tag: 'span' }); + await driver.findClickableElement({ text: 'Next', tag: 'button', diff --git a/yarn.lock b/yarn.lock index cd02dac6e9b7..d69c0d00a4d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -30969,7 +30969,7 @@ __metadata: languageName: node linkType: hard -"socks@npm:^2.7.3": +"socks@npm:^2.6.1, socks@npm:^2.6.2, socks@npm:^2.7.1": version: 2.8.0 resolution: "socks@npm:2.8.0" dependencies: