Skip to content

Commit 0d5cebc

Browse files
committed
fix: fix PR comments
1 parent 166ed74 commit 0d5cebc

File tree

5 files changed

+28
-145
lines changed

5 files changed

+28
-145
lines changed

packages/assets-controllers/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Updated `NftController` and `NftDetectionController` to eliminate the dependency on the current chain.
13+
- All functions that previously accepted networkClientId as an optional parameter now require it as a mandatory parameter
14+
1015
## [56.0.0]
1116

1217
### Changed

packages/network-controller/src/NetworkController.ts

+23-28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
BUILT_IN_NETWORKS,
1818
BuiltInNetworkName,
1919
} from '@metamask/controller-utils';
20+
import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';
2021
import EthQuery from '@metamask/eth-query';
2122
import { errorCodes } from '@metamask/rpc-errors';
2223
import { createEventEmitterProxy } from '@metamask/swappable-obj-proxy';
@@ -512,11 +513,6 @@ export type NetworkControllerGetNetworkClientByIdAction = {
512513
handler: NetworkController['getNetworkClientById'];
513514
};
514515

515-
export type NetworkControllerGetNetworkClientIdByChainIdAction = {
516-
type: `NetworkController:getNetworkClientIdByChainId`;
517-
handler: NetworkController['getNetworkClientIdByChainId'];
518-
};
519-
520516
export type NetworkControllerGetSelectedNetworkClientAction = {
521517
type: `NetworkController:getSelectedNetworkClient`;
522518
handler: NetworkController['getSelectedNetworkClient'];
@@ -592,8 +588,7 @@ export type NetworkControllerActions =
592588
| NetworkControllerGetNetworkConfigurationByNetworkClientId
593589
| NetworkControllerAddNetworkAction
594590
| NetworkControllerRemoveNetworkAction
595-
| NetworkControllerUpdateNetworkAction
596-
| NetworkControllerGetNetworkClientIdByChainIdAction;
591+
| NetworkControllerUpdateNetworkAction;
597592

598593
export type NetworkControllerMessenger = RestrictedMessenger<
599594
typeof controllerName,
@@ -627,16 +622,23 @@ export type NetworkControllerOptions = {
627622
*/
628623
log?: Logger;
629624
/**
630-
* A function that can be used to customize the options passed to a
631-
* RPC service constructed for an RPC endpoint. The object that the function
632-
* should return is the same as {@link RpcServiceOptions}, except that
633-
* `failoverService` and `endpointUrl` are not accepted (as they are filled in
634-
* automatically).
625+
* A function that can be used to customize a RPC service constructed for an
626+
* RPC endpoint. The function takes the URL of the endpoint and should return
627+
* an object with type {@link RpcServiceOptions}, minus `failoverService`
628+
* and `endpointUrl` (as they are filled in automatically).
635629
*/
636630
getRpcServiceOptions: (
637631
rpcEndpointUrl: string,
638632
) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;
639-
633+
/**
634+
* A function that can be used to customize a block tracker constructed for an
635+
* RPC endpoint. The function takes the URL of the endpoint and should return
636+
* an object of type {@link PollingBlockTrackerOptions}, minus `provider` (as
637+
* it is filled in automatically).
638+
*/
639+
getBlockTrackerOptions?: (
640+
rpcEndpointUrl: string,
641+
) => Omit<PollingBlockTrackerOptions, 'provider'>;
640642
/**
641643
* An array of Hex Chain IDs representing the additional networks to be included as default.
642644
*/
@@ -1086,6 +1088,8 @@ export class NetworkController extends BaseController<
10861088

10871089
readonly #getRpcServiceOptions: NetworkControllerOptions['getRpcServiceOptions'];
10881090

1091+
readonly #getBlockTrackerOptions: NetworkControllerOptions['getBlockTrackerOptions'];
1092+
10891093
#networkConfigurationsByNetworkClientId: Map<
10901094
NetworkClientId,
10911095
NetworkConfiguration
@@ -1103,6 +1107,7 @@ export class NetworkController extends BaseController<
11031107
infuraProjectId,
11041108
log,
11051109
getRpcServiceOptions,
1110+
getBlockTrackerOptions,
11061111
additionalDefaultNetworks,
11071112
} = options;
11081113
const initialState = {
@@ -1137,6 +1142,7 @@ export class NetworkController extends BaseController<
11371142
this.#infuraProjectId = infuraProjectId;
11381143
this.#log = log;
11391144
this.#getRpcServiceOptions = getRpcServiceOptions;
1145+
this.#getBlockTrackerOptions = getBlockTrackerOptions;
11401146

11411147
this.#previouslySelectedNetworkClientId =
11421148
this.state.selectedNetworkClientId;
@@ -1161,11 +1167,6 @@ export class NetworkController extends BaseController<
11611167
this.getNetworkClientById.bind(this),
11621168
);
11631169

1164-
this.messagingSystem.registerActionHandler(
1165-
`${this.name}:getNetworkClientIdByChainId`,
1166-
this.getNetworkClientIdByChainId.bind(this),
1167-
);
1168-
11691170
this.messagingSystem.registerActionHandler(
11701171
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
11711172
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -1838,16 +1839,6 @@ export class NetworkController extends BaseController<
18381839
return this.state.networkConfigurationsByChainId[chainId];
18391840
}
18401841

1841-
getNetworkClientIdByChainId(chainId: Hex): NetworkClientId | undefined {
1842-
const networkConfiguration = this.getNetworkConfigurationByChainId(chainId);
1843-
if (networkConfiguration) {
1844-
return networkConfiguration.rpcEndpoints[
1845-
networkConfiguration.defaultRpcEndpointIndex
1846-
]?.networkClientId;
1847-
}
1848-
return undefined;
1849-
}
1850-
18511842
/**
18521843
* Returns the network configuration that contains an RPC endpoint with the
18531844
* given network client ID.
@@ -2659,6 +2650,7 @@ export class NetworkController extends BaseController<
26592650
ticker: networkFields.nativeCurrency,
26602651
},
26612652
getRpcServiceOptions: this.#getRpcServiceOptions,
2653+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
26622654
messenger: this.messagingSystem,
26632655
});
26642656
} else {
@@ -2673,6 +2665,7 @@ export class NetworkController extends BaseController<
26732665
ticker: networkFields.nativeCurrency,
26742666
},
26752667
getRpcServiceOptions: this.#getRpcServiceOptions,
2668+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
26762669
messenger: this.messagingSystem,
26772670
});
26782671
}
@@ -2833,6 +2826,7 @@ export class NetworkController extends BaseController<
28332826
ticker: networkConfiguration.nativeCurrency,
28342827
},
28352828
getRpcServiceOptions: this.#getRpcServiceOptions,
2829+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
28362830
messenger: this.messagingSystem,
28372831
}),
28382832
] as const;
@@ -2848,6 +2842,7 @@ export class NetworkController extends BaseController<
28482842
ticker: networkConfiguration.nativeCurrency,
28492843
},
28502844
getRpcServiceOptions: this.#getRpcServiceOptions,
2845+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
28512846
messenger: this.messagingSystem,
28522847
}),
28532848
] as const;

packages/network-controller/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export type {
3939
NetworkControllerRpcEndpointUnavailableEvent,
4040
NetworkControllerRpcEndpointDegradedEvent,
4141
NetworkControllerRpcEndpointRequestRetriedEvent,
42-
NetworkControllerGetNetworkClientIdByChainIdAction,
4342
} from './NetworkController';
4443
export {
4544
getDefaultNetworkControllerState,

packages/network-controller/tests/NetworkController.test.ts

-66
Original file line numberDiff line numberDiff line change
@@ -1206,72 +1206,6 @@ describe('NetworkController', () => {
12061206
});
12071207
});
12081208

1209-
describe('getNetworkClientIdByChainId', () => {
1210-
it('returns undefined if no networkConfiguration found for the given chainId', async () => {
1211-
const infuraProjectId = 'some-infura-project-id';
1212-
1213-
await withController(
1214-
{
1215-
state: {
1216-
networkConfigurationsByChainId: {
1217-
'0x1337': buildCustomNetworkConfiguration({
1218-
chainId: '0x1337',
1219-
nativeCurrency: 'TOKEN1',
1220-
rpcEndpoints: [
1221-
buildCustomRpcEndpoint({
1222-
failoverUrls: ['https://first.failover.endpoint'],
1223-
networkClientId: 'AAAA-AAAA-AAAA-AAAA',
1224-
url: 'https://test.network/1',
1225-
}),
1226-
],
1227-
}),
1228-
},
1229-
selectedNetworkClientId: 'AAAA-AAAA-AAAA-AAAA',
1230-
},
1231-
infuraProjectId,
1232-
},
1233-
async ({ controller }) => {
1234-
const networkClientId =
1235-
controller.getNetworkClientIdByChainId('0x1111');
1236-
1237-
expect(networkClientId).toBeUndefined();
1238-
},
1239-
);
1240-
});
1241-
1242-
it('returns the correct network client ID for the given chainId', async () => {
1243-
const infuraProjectId = 'some-infura-project-id';
1244-
1245-
await withController(
1246-
{
1247-
state: {
1248-
networkConfigurationsByChainId: {
1249-
'0x1337': buildCustomNetworkConfiguration({
1250-
chainId: '0x1337',
1251-
nativeCurrency: 'TOKEN1',
1252-
rpcEndpoints: [
1253-
buildCustomRpcEndpoint({
1254-
failoverUrls: ['https://first.failover.endpoint'],
1255-
networkClientId: 'AAAA-AAAA-AAAA-AAAA',
1256-
url: 'https://test.network/1',
1257-
}),
1258-
],
1259-
}),
1260-
},
1261-
selectedNetworkClientId: 'AAAA-AAAA-AAAA-AAAA',
1262-
},
1263-
infuraProjectId,
1264-
},
1265-
async ({ controller }) => {
1266-
const networkClientId =
1267-
controller.getNetworkClientIdByChainId('0x1337');
1268-
1269-
expect(networkClientId).toBe('AAAA-AAAA-AAAA-AAAA');
1270-
},
1271-
);
1272-
});
1273-
});
1274-
12751209
describe('getNetworkClientRegistry', () => {
12761210
describe('if no network configurations were specified at initialization', () => {
12771211
it('returns network clients for default RPC endpoints, keyed by network client ID', async () => {

packages/network-controller/tests/helpers.ts

-50
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
NetworksTicker,
77
toHex,
88
} from '@metamask/controller-utils';
9-
import type { Hex } from '@metamask/utils';
109
import { v4 as uuidV4 } from 'uuid';
1110

1211
import { FakeBlockTracker } from '../../../tests/fake-block-tracker';
@@ -193,55 +192,6 @@ export function buildMockGetNetworkClientById(
193192
return getNetworkClientById;
194193
}
195194

196-
/**
197-
* Builds a mock version of the `getNetworkClientIdByChainId` method on
198-
* NetworkController.
199-
*
200-
* @param mockNetworkClientConfigurationsByNetworkClientId - Allows for defining
201-
* the network client configuration — and thus the network client itself — that
202-
* belongs to a particular network client ID.
203-
* @returns The mock version of `getNetworkClientIdByChainId`.
204-
*/
205-
export function buildMockGetNetworkClientByChainId(
206-
mockNetworkClientConfigurationsByNetworkClientId: Record<
207-
Hex,
208-
NetworkClientConfiguration
209-
> = {},
210-
): NetworkController['getNetworkClientIdByChainId'] {
211-
const defaultMockNetworkClientConfigurationsByNetworkClientId = Object.values(
212-
InfuraNetworkType,
213-
).reduce((obj, infuraNetworkType) => {
214-
return {
215-
...obj,
216-
[infuraNetworkType]:
217-
buildInfuraNetworkClientConfiguration(infuraNetworkType),
218-
};
219-
}, {});
220-
const mergedMockNetworkClientConfigurationsByNetworkClientId: Record<
221-
NetworkClientId,
222-
NetworkClientConfiguration
223-
> = {
224-
...defaultMockNetworkClientConfigurationsByNetworkClientId,
225-
...mockNetworkClientConfigurationsByNetworkClientId,
226-
};
227-
228-
function getNetworkClientIdByChainId(chainId: Hex): NetworkClientId;
229-
// eslint-disable-next-line jsdoc/require-jsdoc
230-
function getNetworkClientIdByChainId(chainId: Hex): NetworkClientId {
231-
const networkClientConfigForChainId = Object.entries(
232-
mergedMockNetworkClientConfigurationsByNetworkClientId,
233-
).find(([_, value]) => value.chainId === chainId);
234-
if (!networkClientConfigForChainId) {
235-
throw new Error(
236-
`Unknown chainId '${chainId}'. Please add it to mockNetworkClientConfigurationsByNetworkClientId.`,
237-
);
238-
}
239-
return networkClientConfigForChainId[0];
240-
}
241-
242-
return getNetworkClientIdByChainId;
243-
}
244-
245195
/**
246196
* Builds a configuration object for an Infura network client based on the name
247197
* of an Infura network.

0 commit comments

Comments
 (0)