From be470b8149f5bbde64b5943e4daf8ada3c9f26ba Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 14 Jan 2025 19:10:39 +0100 Subject: [PATCH 1/2] feat: Mock chain ID and network version calls in simulation --- .../internal-methods/chain-id.test.ts | 28 +++++++++++++++ .../middleware/internal-methods/chain-id.ts | 34 +++++++++++++++++++ .../middleware/internal-methods/middleware.ts | 4 +++ .../internal-methods/net-version.test.ts | 28 +++++++++++++++ .../internal-methods/net-version.ts | 34 +++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 packages/snaps-simulation/src/middleware/internal-methods/chain-id.test.ts create mode 100644 packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts create mode 100644 packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts create mode 100644 packages/snaps-simulation/src/middleware/internal-methods/net-version.ts diff --git a/packages/snaps-simulation/src/middleware/internal-methods/chain-id.test.ts b/packages/snaps-simulation/src/middleware/internal-methods/chain-id.test.ts new file mode 100644 index 0000000000..4862777297 --- /dev/null +++ b/packages/snaps-simulation/src/middleware/internal-methods/chain-id.test.ts @@ -0,0 +1,28 @@ +import type { Json, PendingJsonRpcResponse } from '@metamask/utils'; + +import { getChainIdHandler } from './chain-id'; + +describe('getChainIdHandler', () => { + it('returns the chain id', async () => { + const end = jest.fn(); + const result: PendingJsonRpcResponse = { + jsonrpc: '2.0' as const, + id: 1, + }; + + await getChainIdHandler( + { + jsonrpc: '2.0', + id: 1, + method: 'eth_chainId', + params: [], + }, + result, + jest.fn(), + end, + ); + + expect(end).toHaveBeenCalled(); + expect(result.result).toBe('0x01'); + }); +}); diff --git a/packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts b/packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts new file mode 100644 index 0000000000..877b4d90e6 --- /dev/null +++ b/packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts @@ -0,0 +1,34 @@ +import type { + JsonRpcEngineEndCallback, + JsonRpcEngineNextCallback, +} from '@metamask/json-rpc-engine'; +import type { + Json, + JsonRpcRequest, + PendingJsonRpcResponse, +} from '@metamask/utils'; + +/** + * A mock handler for eth_chainId that always returns a specific + * hardcoded result. + * + * @param _request - Incoming JSON-RPC request. Ignored for this specific + * handler. + * @param response - The outgoing JSON-RPC response, modified to return the + * result. + * @param _next - The `json-rpc-engine` middleware next handler. + * @param end - The `json-rpc-engine` middleware end handler. + * @returns The JSON-RPC response. + */ +export async function getChainIdHandler( + _request: JsonRpcRequest, + response: PendingJsonRpcResponse, + _next: JsonRpcEngineNextCallback, + end: JsonRpcEngineEndCallback, +) { + // For now this will return a mocked result, this should probably match + // whatever network the simulation is using. + response.result = '0x01'; + + return end(); +} diff --git a/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts b/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts index 72859120de..777ef497b9 100644 --- a/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts +++ b/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts @@ -3,6 +3,8 @@ import { logError } from '@metamask/snaps-utils'; import type { Json, JsonRpcParams } from '@metamask/utils'; import { getAccountsHandler } from './accounts'; +import { getChainIdHandler } from './chain-id'; +import { getNetworkVersionHandler } from './net-version'; import { getProviderStateHandler } from './provider-state'; export type InternalMethodsMiddlewareHooks = { @@ -19,6 +21,8 @@ const methodHandlers = { metamask_getProviderState: getProviderStateHandler, eth_requestAccounts: getAccountsHandler, eth_accounts: getAccountsHandler, + eth_chainId: getChainIdHandler, + net_version: getNetworkVersionHandler, /* eslint-enable @typescript-eslint/naming-convention */ }; diff --git a/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts b/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts new file mode 100644 index 0000000000..7a89fe8b4c --- /dev/null +++ b/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts @@ -0,0 +1,28 @@ +import type { Json, PendingJsonRpcResponse } from '@metamask/utils'; + +import { getNetworkVersionHandler } from './net-version'; + +describe('getNetworkVersionHandler', () => { + it('returns the network version', async () => { + const end = jest.fn(); + const result: PendingJsonRpcResponse = { + jsonrpc: '2.0' as const, + id: 1, + }; + + await getNetworkVersionHandler( + { + jsonrpc: '2.0', + id: 1, + method: 'net_version', + params: [], + }, + result, + jest.fn(), + end, + ); + + expect(end).toHaveBeenCalled(); + expect(result.result).toBe('0x01'); + }); +}); diff --git a/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts b/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts new file mode 100644 index 0000000000..fd8a56a1f2 --- /dev/null +++ b/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts @@ -0,0 +1,34 @@ +import type { + JsonRpcEngineEndCallback, + JsonRpcEngineNextCallback, +} from '@metamask/json-rpc-engine'; +import type { + Json, + JsonRpcRequest, + PendingJsonRpcResponse, +} from '@metamask/utils'; + +/** + * A mock handler for net_version that always returns a specific + * hardcoded result. + * + * @param _request - Incoming JSON-RPC request. Ignored for this specific + * handler. + * @param response - The outgoing JSON-RPC response, modified to return the + * result. + * @param _next - The `json-rpc-engine` middleware next handler. + * @param end - The `json-rpc-engine` middleware end handler. + * @returns The JSON-RPC response. + */ +export async function getNetworkVersionHandler( + _request: JsonRpcRequest, + response: PendingJsonRpcResponse, + _next: JsonRpcEngineNextCallback, + end: JsonRpcEngineEndCallback, +) { + // For now this will return a mocked result, this should probably match + // whatever network the simulation is using. + response.result = '0x01'; + + return end(); +} From 5528ba968e726bedd7ddf5c3bfe025db5f520822 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 14 Jan 2025 19:13:19 +0100 Subject: [PATCH 2/2] Fix return value for network version mocking --- .../packages/ethereum-provider/src/index.test.ts | 9 +-------- .../src/middleware/internal-methods/net-version.test.ts | 2 +- .../src/middleware/internal-methods/net-version.ts | 2 +- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/examples/packages/ethereum-provider/src/index.test.ts b/packages/examples/packages/ethereum-provider/src/index.test.ts index 44a706c97c..b2d025e7e8 100644 --- a/packages/examples/packages/ethereum-provider/src/index.test.ts +++ b/packages/examples/packages/ethereum-provider/src/index.test.ts @@ -45,14 +45,7 @@ describe('onRpcRequest', () => { const MOCK_VERSION = '1'; // Ethereum Mainnet it('returns the current network version', async () => { - const { request, mockJsonRpc } = await installSnap(); - - // To avoid relying on the network, we mock the response from the Ethereum - // provider. - mockJsonRpc({ - method: 'net_version', - result: MOCK_VERSION, - }); + const { request } = await installSnap(); const response = await request({ method: 'getVersion', diff --git a/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts b/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts index 7a89fe8b4c..0bd6d22c6f 100644 --- a/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts +++ b/packages/snaps-simulation/src/middleware/internal-methods/net-version.test.ts @@ -23,6 +23,6 @@ describe('getNetworkVersionHandler', () => { ); expect(end).toHaveBeenCalled(); - expect(result.result).toBe('0x01'); + expect(result.result).toBe('1'); }); }); diff --git a/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts b/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts index fd8a56a1f2..0c3a337d3c 100644 --- a/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts +++ b/packages/snaps-simulation/src/middleware/internal-methods/net-version.ts @@ -28,7 +28,7 @@ export async function getNetworkVersionHandler( ) { // For now this will return a mocked result, this should probably match // whatever network the simulation is using. - response.result = '0x01'; + response.result = '1'; return end(); }