Skip to content

Commit f2aa83e

Browse files
committed
Add wallet_switchEthereumChain support to snaps-jest
1 parent c0b30ff commit f2aa83e

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

packages/examples/packages/ethereum-provider/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "vdxQxaJ4dDVhuIu8cvKJkKFC9YeIvaRORuCdbtX5qLM=",
10+
"shasum": "u/BaDj7fzOZvB+bPRLbkBie65a5oxunLABteu1+qWns=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async function personalSign(message: string, from: string) {
118118
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
119119
*/
120120
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
121-
const { chainId = '0x1' } = request.params as BaseParams;
121+
const { chainId = '0x1' } = (request.params as BaseParams) ?? {};
122122
await switchChain(chainId);
123123

124124
switch (request.method) {

packages/snaps-jest/src/internals/simulation/methods/specifications.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const MOCK_HOOKS: MiddlewareHooks = {
2020
createInterface: jest.fn(),
2121
updateInterface: jest.fn(),
2222
getInterfaceState: jest.fn(),
23+
getIsLocked: jest.fn(),
24+
resolveInterface: jest.fn(),
2325
};
2426

2527
describe('resolve', () => {

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

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

55
import { getAccountsHandler } from './accounts';
66
import { getProviderStateHandler } from './provider-state';
7+
import { getSwitchEthereumChainHandler } from './switch-ethereum-chain';
78

89
export type InternalMethodsMiddlewareHooks = {
910
/**
@@ -19,6 +20,7 @@ const methodHandlers = {
1920
metamask_getProviderState: getProviderStateHandler,
2021
eth_requestAccounts: getAccountsHandler,
2122
eth_accounts: getAccountsHandler,
23+
wallet_switchEthereumChain: getSwitchEthereumChainHandler,
2224
/* eslint-enable @typescript-eslint/naming-convention */
2325
};
2426

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 { getSwitchEthereumChainHandler } from './switch-ethereum-chain';
4+
5+
describe('getSwitchEthereumChainHandler', () => {
6+
it('returns `null`', async () => {
7+
const end = jest.fn();
8+
const result: PendingJsonRpcResponse<Json> = {
9+
jsonrpc: '2.0' as const,
10+
id: 1,
11+
};
12+
13+
await getSwitchEthereumChainHandler(
14+
{
15+
jsonrpc: '2.0',
16+
id: 1,
17+
method: 'wallet_switchEthereumChain',
18+
params: [],
19+
},
20+
result,
21+
jest.fn(),
22+
end,
23+
);
24+
25+
expect(end).toHaveBeenCalled();
26+
expect(result.result).toBeNull();
27+
});
28+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 the `wallet_switchEthereumChain` method that always
13+
* returns `null`.
14+
*
15+
* @param _request - Incoming JSON-RPC request. This is ignored for this
16+
* specific 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+
*/
22+
export async function getSwitchEthereumChainHandler(
23+
_request: JsonRpcRequest,
24+
response: PendingJsonRpcResponse<Json>,
25+
_next: JsonRpcEngineNextCallback,
26+
end: JsonRpcEngineEndCallback,
27+
// hooks: GetAccountsHandlerHooks,
28+
) {
29+
response.result = null;
30+
return end();
31+
}

0 commit comments

Comments
 (0)