Skip to content

Commit

Permalink
Merge branch 'develop' into mm-security-code-scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
witmicko authored Feb 15, 2024
2 parents c477d72 + 9c59da5 commit 4333e8e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 13 deletions.
26 changes: 18 additions & 8 deletions app/scripts/controllers/detect-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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(
Expand Down
99 changes: 97 additions & 2 deletions app/scripts/controllers/detect-tokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/tests/send-eth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4333e8e

Please sign in to comment.