Skip to content

Commit 8a31998

Browse files
SteP-n-smicaelae
andauthored
fix: remove global network reference in bridge allowance handler (#6994)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> Updates logic of `getBridgeERC20Allowance` method to use `chainId` parameter to retrieve the `networkClient`. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > `getBridgeERC20Allowance` now finds the network client via `chainId` instead of the globally selected network, with new error handling and tests, and changelog updated. > > - **Bridge Controller**: > - Use `#getNetworkClientByChainId(chainId)` to resolve network client in `getBridgeERC20Allowance`, calling `NetworkController:findNetworkClientIdByChainId` then `NetworkController:getNetworkClientById`. > - Add error handling: throw when no network client for `chainId` or when no provider. > - **Tests**: > - Update `getBridgeERC20Allowance` tests to assert network client lookup by `chainId` and new error cases. > - **Changelog**: > - Note fix for removing global selected network reference in `getBridgeERC20Allowance`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 2239728. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Micaela Estabillo <micaela.estabillo@consensys.net> Co-authored-by: Micaela <100321200+micaelae@users.noreply.github.com>
1 parent 3656f13 commit 8a31998

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

packages/bridge-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Remove global selected network reference in `getBridgeERC20Allowance` handler ([#6994](https://github.com/MetaMask/core/pull/6994))
13+
1014
## [56.0.1]
1115

1216
### Changed

packages/bridge-controller/src/bridge-controller.test.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,28 +1306,71 @@ describe('BridgeController', function () {
13061306
allowance: jest.fn(() => '100000000000000000000'),
13071307
}));
13081308

1309-
messengerMock.call.mockReturnValue({
1310-
address: '0x123',
1311-
provider: jest.fn(),
1312-
} as never);
1309+
messengerMock.call
1310+
.mockReturnValueOnce('networkClientId-for-chain-0xa')
1311+
.mockReturnValueOnce({
1312+
// getNetworkClientById
1313+
address: '0x123',
1314+
provider: jest.fn(),
1315+
} as never);
13131316

13141317
const allowance = await bridgeController.getBridgeERC20Allowance(
13151318
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
13161319
'0xa',
13171320
);
13181321
expect(allowance).toBe('100000000000000000000');
1322+
expect(messengerMock.call).toHaveBeenCalledTimes(2);
1323+
expect(messengerMock.call).toHaveBeenNthCalledWith(
1324+
1,
1325+
'NetworkController:findNetworkClientIdByChainId',
1326+
'0xa',
1327+
);
1328+
expect(messengerMock.call).toHaveBeenNthCalledWith(
1329+
2,
1330+
'NetworkController:getNetworkClientById',
1331+
'networkClientId-for-chain-0xa',
1332+
);
1333+
});
1334+
1335+
it('should throw an error when no network client is found for chainId', async () => {
1336+
// Setup
1337+
const mockMessenger = {
1338+
call: jest.fn().mockImplementation((methodName) => {
1339+
if (methodName === 'NetworkController:findNetworkClientIdByChainId') {
1340+
return undefined; // No network client found
1341+
}
1342+
return undefined;
1343+
}),
1344+
registerActionHandler: jest.fn(),
1345+
publish: jest.fn(),
1346+
registerInitialEventPayload: jest.fn(),
1347+
} as unknown as jest.Mocked<BridgeControllerMessenger>;
1348+
1349+
const controller = new BridgeController({
1350+
messenger: mockMessenger,
1351+
clientId: BridgeClientId.EXTENSION,
1352+
clientVersion: '1.0.0',
1353+
getLayer1GasFee: jest.fn(),
1354+
fetchFn: mockFetchFn,
1355+
trackMetaMetricsFn,
1356+
});
1357+
1358+
// Test
1359+
await expect(
1360+
controller.getBridgeERC20Allowance('0xContractAddress', '0x1'),
1361+
).rejects.toThrow('No network client found for chainId: 0x1');
13191362
});
13201363

13211364
it('should throw an error when no provider is found', async () => {
13221365
// Setup
13231366
const mockMessenger = {
13241367
call: jest.fn().mockImplementation((methodName) => {
1368+
if (methodName === 'NetworkController:findNetworkClientIdByChainId') {
1369+
return 'networkClientId-for-chain-0x1';
1370+
}
13251371
if (methodName === 'NetworkController:getNetworkClientById') {
13261372
return { provider: null };
13271373
}
1328-
if (methodName === 'NetworkController:getState') {
1329-
return { selectedNetworkClientId: 'testNetworkClientId' };
1330-
}
13311374
return undefined;
13321375
}),
13331376
registerActionHandler: jest.fn(),

packages/bridge-controller/src/bridge-controller.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,21 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
779779
return networkClient;
780780
}
781781

782+
#getNetworkClientByChainId(chainId: Hex) {
783+
const networkClientId = this.messenger.call(
784+
'NetworkController:findNetworkClientIdByChainId',
785+
chainId,
786+
);
787+
if (!networkClientId) {
788+
throw new Error(`No network client found for chainId: ${chainId}`);
789+
}
790+
const networkClient = this.messenger.call(
791+
'NetworkController:getNetworkClientById',
792+
networkClientId,
793+
);
794+
return networkClient;
795+
}
796+
782797
readonly #getRequestParams = (): Omit<
783798
RequestParams,
784799
'token_symbol_source' | 'token_symbol_destination'
@@ -975,7 +990,8 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
975990
contractAddress: string,
976991
chainId: Hex,
977992
): Promise<string> => {
978-
const provider = this.#getSelectedNetworkClient()?.provider;
993+
const networkClient = this.#getNetworkClientByChainId(chainId);
994+
const provider = networkClient?.provider;
979995
if (!provider) {
980996
throw new Error('No provider found');
981997
}

0 commit comments

Comments
 (0)