Skip to content

Commit 1dcbeda

Browse files
committed
Add wallet_switchEthereumChain support to snaps-jest
1 parent fba304e commit 1dcbeda

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-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": "qpgyjgYPOUeFqP4h6GBi4EE4sNtlcwJTBeVflPPkDAI=",
10+
"shasum": "hVuKuTyTaza8ezHZLGeC5Yq4E7LZPgO/spV7qwOEDlE=",
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
@@ -223,7 +223,7 @@ async function signTypedData(message: string, from: string) {
223223
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
224224
*/
225225
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
226-
const { chainId = '0x1' } = request.params as BaseParams;
226+
const { chainId = '0x1' } = (request.params as BaseParams) ?? {};
227227
await switchChain(chainId);
228228

229229
switch (request.method) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getAccountsHandler } from './accounts';
66
import { getChainIdHandler } from './chain-id';
77
import { getNetworkVersionHandler } from './net-version';
88
import { getProviderStateHandler } from './provider-state';
9+
import { getSwitchEthereumChainHandler } from './switch-ethereum-chain';
910

1011
export type InternalMethodsMiddlewareHooks = {
1112
/**
@@ -23,6 +24,7 @@ const methodHandlers = {
2324
eth_accounts: getAccountsHandler,
2425
eth_chainId: getChainIdHandler,
2526
net_version: getNetworkVersionHandler,
27+
wallet_switchEthereumChain: getSwitchEthereumChainHandler,
2628
/* eslint-enable @typescript-eslint/naming-convention */
2729
};
2830

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* @returns The response.
22+
*/
23+
export async function getSwitchEthereumChainHandler(
24+
_request: JsonRpcRequest,
25+
response: PendingJsonRpcResponse<Json>,
26+
_next: JsonRpcEngineNextCallback,
27+
end: JsonRpcEngineEndCallback,
28+
// hooks: GetAccountsHandlerHooks,
29+
) {
30+
response.result = null;
31+
return end();
32+
}

0 commit comments

Comments
 (0)