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

Add getEthQuery action to NetworkController (#19420)

The core version of NetworkControler features a controller action,
`NetworkController:getEthQuery`, which, as its name implies, can be used
to get the EthQuery instance that has been created specially to talk to
the currently selected network. As a result there is a corresponding
unit test for this action.

To make the test suites between this version of NetworkController and
core easier to compare, this commit adds the action along with its test.
This commit is contained in:
Elliot Winkler 2023-06-08 18:00:33 -06:00 committed by GitHub
parent 25b7016f06
commit c84b85f953
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 4 deletions

View File

@ -2587,6 +2587,48 @@ describe('NetworkController', () => {
});
});
describe('NetworkController:getEthQuery action', () => {
it('returns a EthQuery object that can be used to make requests to the currently selected network', async () => {
await withController(async ({ controller, messenger }) => {
await setFakeProvider(controller, {
stubs: [
{
request: {
method: 'test_method',
params: [],
},
response: {
result: 'test response',
},
},
],
});
const ethQuery = messenger.call('NetworkController:getEthQuery');
assert(ethQuery, 'ethQuery is not set');
const promisifiedSendAsync = promisify(ethQuery.sendAsync).bind(
ethQuery,
);
const result = await promisifiedSendAsync({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
expect(result).toBe('test response');
});
});
it('returns undefined if the provider has not been set yet', async () => {
await withController(({ messenger }) => {
const ethQuery = messenger.call('NetworkController:getEthQuery');
expect(ethQuery).toBeUndefined();
});
});
});
describe('rollbackToPreviousProvider', () => {
for (const { networkType } of INFURA_NETWORKS) {
describe(`if the previous provider configuration had a type of "${networkType}"`, () => {
@ -6328,7 +6370,10 @@ function buildMessenger() {
function buildNetworkControllerMessenger(messenger = buildMessenger()) {
return messenger.getRestricted({
name: 'NetworkController',
allowedActions: ['NetworkController:getProviderConfig'],
allowedActions: [
'NetworkController:getProviderConfig',
'NetworkController:getEthQuery',
],
allowedEvents: [
'NetworkController:networkDidChange',
'NetworkController:networkWillChange',

View File

@ -135,7 +135,14 @@ export type NetworkControllerGetProviderConfigAction = {
handler: () => ProviderConfiguration;
};
export type NetworkControllerAction = NetworkControllerGetProviderConfigAction;
export type NetworkControllerGetEthQueryAction = {
type: `NetworkController:getEthQuery`;
handler: () => EthQuery | undefined;
};
export type NetworkControllerAction =
| NetworkControllerGetProviderConfigAction
| NetworkControllerGetEthQueryAction;
/**
* The messenger that the NetworkController uses to publish events.
@ -443,6 +450,8 @@ export class NetworkController extends EventEmitter {
#blockTrackerProxy: SwappableProxy<PollingBlockTracker> | null;
#ethQuery: EthQuery | undefined;
#infuraProjectId: NetworkControllerOptions['infuraProjectId'];
#trackMetaMetricsEvent: NetworkControllerOptions['trackMetaMetricsEvent'];
@ -486,9 +495,13 @@ export class NetworkController extends EventEmitter {
}
this.#infuraProjectId = infuraProjectId;
this.#trackMetaMetricsEvent = trackMetaMetricsEvent;
this.#messenger.registerActionHandler(`${name}:getProviderConfig`, () => {
return this.store.getState().providerConfig;
});
this.#messenger.registerActionHandler(`${name}:getEthQuery`, () => {
return this.#ethQuery;
});
}
/**
@ -918,6 +931,18 @@ export class NetworkController extends EventEmitter {
}
}
/**
* Creates a new instance of EthQuery that wraps the current provider and
* saves it for future usage.
*/
#registerProvider() {
const { provider } = this.getProviderAndBlockTracker();
if (provider) {
this.#ethQuery = new EthQuery(provider);
}
}
/**
* Creates a network client (a stack of middleware along with a provider and
* block tracker) to talk to an Infura-supported network.
@ -941,7 +966,7 @@ export class NetworkController extends EventEmitter {
infuraProjectId,
type: NetworkClientType.Infura,
});
this.#setProviderAndBlockTracker({ provider, blockTracker });
this.#updateProvider(provider, blockTracker);
}
/**
@ -958,7 +983,27 @@ export class NetworkController extends EventEmitter {
rpcUrl,
type: NetworkClientType.Custom,
});
this.#setProviderAndBlockTracker({ provider, blockTracker });
this.#updateProvider(provider, blockTracker);
}
/**
* Given a provider and a block tracker, updates any proxies pointing to
* these objects that have been previously set, or initializes any proxies
* that have not been previously set, then creates an instance of EthQuery
* that wraps the provider.
*
* @param provider - The provider.
* @param blockTracker - The block tracker.
*/
#updateProvider(
provider: SafeEventEmitterProvider,
blockTracker: PollingBlockTracker,
) {
this.#setProviderAndBlockTracker({
provider,
blockTracker,
});
this.#registerProvider();
}
/**