Skip to content

Commit 1ab6bc9

Browse files
feat: Mock chain ID and network version calls in simulation (#3017)
Mock calls to `eth_chainId` and `net_version` in the simulation framework, so users don't have to mock them manually. This should also prevent some flakiness in our tests.
1 parent 030ebee commit 1ab6bc9

File tree

6 files changed

+129
-8
lines changed

6 files changed

+129
-8
lines changed

packages/examples/packages/ethereum-provider/src/index.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@ describe('onRpcRequest', () => {
4545
const MOCK_VERSION = '1'; // Ethereum Mainnet
4646

4747
it('returns the current network version', async () => {
48-
const { request, mockJsonRpc } = await installSnap();
49-
50-
// To avoid relying on the network, we mock the response from the Ethereum
51-
// provider.
52-
mockJsonRpc({
53-
method: 'net_version',
54-
result: MOCK_VERSION,
55-
});
48+
const { request } = await installSnap();
5649

5750
const response = await request({
5851
method: 'getVersion',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Json, PendingJsonRpcResponse } from '@metamask/utils';
2+
3+
import { getChainIdHandler } from './chain-id';
4+
5+
describe('getChainIdHandler', () => {
6+
it('returns the chain id', async () => {
7+
const end = jest.fn();
8+
const result: PendingJsonRpcResponse<Json> = {
9+
jsonrpc: '2.0' as const,
10+
id: 1,
11+
};
12+
13+
await getChainIdHandler(
14+
{
15+
jsonrpc: '2.0',
16+
id: 1,
17+
method: 'eth_chainId',
18+
params: [],
19+
},
20+
result,
21+
jest.fn(),
22+
end,
23+
);
24+
25+
expect(end).toHaveBeenCalled();
26+
expect(result.result).toBe('0x01');
27+
});
28+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type {
2+
JsonRpcEngineEndCallback,
3+
JsonRpcEngineNextCallback,
4+
} from '@metamask/json-rpc-engine';
5+
import type {
6+
Json,
7+
JsonRpcRequest,
8+
PendingJsonRpcResponse,
9+
} from '@metamask/utils';
10+
11+
/**
12+
* A mock handler for eth_chainId that always returns a specific
13+
* hardcoded result.
14+
*
15+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
16+
* handler.
17+
* @param response - The outgoing JSON-RPC response, modified to return the
18+
* result.
19+
* @param _next - The `json-rpc-engine` middleware next handler.
20+
* @param end - The `json-rpc-engine` middleware end handler.
21+
* @returns The JSON-RPC response.
22+
*/
23+
export async function getChainIdHandler(
24+
_request: JsonRpcRequest,
25+
response: PendingJsonRpcResponse<Json>,
26+
_next: JsonRpcEngineNextCallback,
27+
end: JsonRpcEngineEndCallback,
28+
) {
29+
// For now this will return a mocked result, this should probably match
30+
// whatever network the simulation is using.
31+
response.result = '0x01';
32+
33+
return end();
34+
}

packages/snaps-simulation/src/middleware/internal-methods/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { logError } from '@metamask/snaps-utils';
33
import type { Json, JsonRpcParams } from '@metamask/utils';
44

55
import { getAccountsHandler } from './accounts';
6+
import { getChainIdHandler } from './chain-id';
7+
import { getNetworkVersionHandler } from './net-version';
68
import { getProviderStateHandler } from './provider-state';
79

810
export type InternalMethodsMiddlewareHooks = {
@@ -19,6 +21,8 @@ const methodHandlers = {
1921
metamask_getProviderState: getProviderStateHandler,
2022
eth_requestAccounts: getAccountsHandler,
2123
eth_accounts: getAccountsHandler,
24+
eth_chainId: getChainIdHandler,
25+
net_version: getNetworkVersionHandler,
2226
/* eslint-enable @typescript-eslint/naming-convention */
2327
};
2428

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Json, PendingJsonRpcResponse } from '@metamask/utils';
2+
3+
import { getNetworkVersionHandler } from './net-version';
4+
5+
describe('getNetworkVersionHandler', () => {
6+
it('returns the network version', async () => {
7+
const end = jest.fn();
8+
const result: PendingJsonRpcResponse<Json> = {
9+
jsonrpc: '2.0' as const,
10+
id: 1,
11+
};
12+
13+
await getNetworkVersionHandler(
14+
{
15+
jsonrpc: '2.0',
16+
id: 1,
17+
method: 'net_version',
18+
params: [],
19+
},
20+
result,
21+
jest.fn(),
22+
end,
23+
);
24+
25+
expect(end).toHaveBeenCalled();
26+
expect(result.result).toBe('1');
27+
});
28+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type {
2+
JsonRpcEngineEndCallback,
3+
JsonRpcEngineNextCallback,
4+
} from '@metamask/json-rpc-engine';
5+
import type {
6+
Json,
7+
JsonRpcRequest,
8+
PendingJsonRpcResponse,
9+
} from '@metamask/utils';
10+
11+
/**
12+
* A mock handler for net_version that always returns a specific
13+
* hardcoded result.
14+
*
15+
* @param _request - Incoming JSON-RPC request. Ignored for this specific
16+
* handler.
17+
* @param response - The outgoing JSON-RPC response, modified to return the
18+
* result.
19+
* @param _next - The `json-rpc-engine` middleware next handler.
20+
* @param end - The `json-rpc-engine` middleware end handler.
21+
* @returns The JSON-RPC response.
22+
*/
23+
export async function getNetworkVersionHandler(
24+
_request: JsonRpcRequest,
25+
response: PendingJsonRpcResponse<Json>,
26+
_next: JsonRpcEngineNextCallback,
27+
end: JsonRpcEngineEndCallback,
28+
) {
29+
// For now this will return a mocked result, this should probably match
30+
// whatever network the simulation is using.
31+
response.result = '1';
32+
33+
return end();
34+
}

0 commit comments

Comments
 (0)