Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Mock chain ID and network version calls in simulation #3017

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Json> = {
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');
});
});
Original file line number Diff line number Diff line change
@@ -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<Json>,
_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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 */
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Json> = {
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('1');
});
});
Original file line number Diff line number Diff line change
@@ -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<Json>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
) {
// For now this will return a mocked result, this should probably match
// whatever network the simulation is using.
response.result = '1';

return end();
}
Loading