Skip to content

Commit

Permalink
Add wallet_switchEthereumChain support to snaps-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Aug 21, 2024
1 parent c0b30ff commit f2aa83e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "vdxQxaJ4dDVhuIu8cvKJkKFC9YeIvaRORuCdbtX5qLM=",
"shasum": "u/BaDj7fzOZvB+bPRLbkBie65a5oxunLABteu1+qWns=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethereum-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async function personalSign(message: string, from: string) {
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
const { chainId = '0x1' } = request.params as BaseParams;
const { chainId = '0x1' } = (request.params as BaseParams) ?? {};
await switchChain(chainId);

switch (request.method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const MOCK_HOOKS: MiddlewareHooks = {
createInterface: jest.fn(),
updateInterface: jest.fn(),
getInterfaceState: jest.fn(),
getIsLocked: jest.fn(),
resolveInterface: jest.fn(),
};

describe('resolve', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Json, JsonRpcParams } from '@metamask/utils';

import { getAccountsHandler } from './accounts';
import { getProviderStateHandler } from './provider-state';
import { getSwitchEthereumChainHandler } from './switch-ethereum-chain';

export type InternalMethodsMiddlewareHooks = {
/**
Expand All @@ -19,6 +20,7 @@ const methodHandlers = {
metamask_getProviderState: getProviderStateHandler,
eth_requestAccounts: getAccountsHandler,
eth_accounts: getAccountsHandler,
wallet_switchEthereumChain: getSwitchEthereumChainHandler,
/* 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 { getSwitchEthereumChainHandler } from './switch-ethereum-chain';

describe('getSwitchEthereumChainHandler', () => {
it('returns `null`', async () => {
const end = jest.fn();
const result: PendingJsonRpcResponse<Json> = {
jsonrpc: '2.0' as const,
id: 1,
};

await getSwitchEthereumChainHandler(
{
jsonrpc: '2.0',
id: 1,
method: 'wallet_switchEthereumChain',
params: [],
},
result,
jest.fn(),
end,
);

expect(end).toHaveBeenCalled();
expect(result.result).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {
JsonRpcEngineEndCallback,
JsonRpcEngineNextCallback,
} from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcRequest,
PendingJsonRpcResponse,
} from '@metamask/utils';

/**
* A mock handler for the `wallet_switchEthereumChain` method that always
* returns `null`.
*
* @param _request - Incoming JSON-RPC request. This is 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.
*/
export async function getSwitchEthereumChainHandler(
_request: JsonRpcRequest,
response: PendingJsonRpcResponse<Json>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
// hooks: GetAccountsHandlerHooks,
) {
response.result = null;
return end();
}

0 comments on commit f2aa83e

Please sign in to comment.