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 Jan 15, 2025
1 parent fba304e commit 1dcbeda
Show file tree
Hide file tree
Showing 5 changed files with 64 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": "qpgyjgYPOUeFqP4h6GBi4EE4sNtlcwJTBeVflPPkDAI=",
"shasum": "hVuKuTyTaza8ezHZLGeC5Yq4E7LZPgO/spV7qwOEDlE=",
"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 @@ -223,7 +223,7 @@ async function signTypedData(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 @@ -6,6 +6,7 @@ import { getAccountsHandler } from './accounts';
import { getChainIdHandler } from './chain-id';
import { getNetworkVersionHandler } from './net-version';
import { getProviderStateHandler } from './provider-state';
import { getSwitchEthereumChainHandler } from './switch-ethereum-chain';

export type InternalMethodsMiddlewareHooks = {
/**
Expand All @@ -23,6 +24,7 @@ const methodHandlers = {
eth_accounts: getAccountsHandler,
eth_chainId: getChainIdHandler,
net_version: getNetworkVersionHandler,
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,32 @@
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.
* @returns The response.
*/
export async function getSwitchEthereumChainHandler(
_request: JsonRpcRequest,
response: PendingJsonRpcResponse<Json>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
// hooks: GetAccountsHandlerHooks,
) {
response.result = null;
return end();
}

0 comments on commit 1dcbeda

Please sign in to comment.