File tree Expand file tree Collapse file tree 6 files changed +129
-8
lines changed
examples/packages/ethereum-provider/src
snaps-simulation/src/middleware/internal-methods Expand file tree Collapse file tree 6 files changed +129
-8
lines changed Original file line number Diff line number Diff line change @@ -45,14 +45,7 @@ describe('onRpcRequest', () => {
4545 const MOCK_VERSION = '1' ; // Ethereum Mainnet
4646
4747 it ( 'returns the current network version' , async ( ) => {
48- const { request, mockJsonRpc } = await installSnap ( ) ;
49-
50- // To avoid relying on the network, we mock the response from the Ethereum
51- // provider.
52- mockJsonRpc ( {
53- method : 'net_version' ,
54- result : MOCK_VERSION ,
55- } ) ;
48+ const { request } = await installSnap ( ) ;
5649
5750 const response = await request ( {
5851 method : 'getVersion' ,
Original file line number Diff line number Diff line change 1+ import type { Json , PendingJsonRpcResponse } from '@metamask/utils' ;
2+
3+ import { getChainIdHandler } from './chain-id' ;
4+
5+ describe ( 'getChainIdHandler' , ( ) => {
6+ it ( 'returns the chain id' , async ( ) => {
7+ const end = jest . fn ( ) ;
8+ const result : PendingJsonRpcResponse < Json > = {
9+ jsonrpc : '2.0' as const ,
10+ id : 1 ,
11+ } ;
12+
13+ await getChainIdHandler (
14+ {
15+ jsonrpc : '2.0' ,
16+ id : 1 ,
17+ method : 'eth_chainId' ,
18+ params : [ ] ,
19+ } ,
20+ result ,
21+ jest . fn ( ) ,
22+ end ,
23+ ) ;
24+
25+ expect ( end ) . toHaveBeenCalled ( ) ;
26+ expect ( result . result ) . toBe ( '0x01' ) ;
27+ } ) ;
28+ } ) ;
Original file line number Diff line number Diff line change 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 eth_chainId that always returns a specific
13+ * hardcoded result.
14+ *
15+ * @param _request - Incoming JSON-RPC request. Ignored for this specific
16+ * 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 JSON-RPC response.
22+ */
23+ export async function getChainIdHandler (
24+ _request : JsonRpcRequest ,
25+ response : PendingJsonRpcResponse < Json > ,
26+ _next : JsonRpcEngineNextCallback ,
27+ end : JsonRpcEngineEndCallback ,
28+ ) {
29+ // For now this will return a mocked result, this should probably match
30+ // whatever network the simulation is using.
31+ response . result = '0x01' ;
32+
33+ return end ( ) ;
34+ }
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ import { logError } from '@metamask/snaps-utils';
33import type { Json , JsonRpcParams } from '@metamask/utils' ;
44
55import { getAccountsHandler } from './accounts' ;
6+ import { getChainIdHandler } from './chain-id' ;
7+ import { getNetworkVersionHandler } from './net-version' ;
68import { getProviderStateHandler } from './provider-state' ;
79
810export type InternalMethodsMiddlewareHooks = {
@@ -19,6 +21,8 @@ const methodHandlers = {
1921 metamask_getProviderState : getProviderStateHandler ,
2022 eth_requestAccounts : getAccountsHandler ,
2123 eth_accounts : getAccountsHandler ,
24+ eth_chainId : getChainIdHandler ,
25+ net_version : getNetworkVersionHandler ,
2226 /* eslint-enable @typescript-eslint/naming-convention */
2327} ;
2428
Original file line number Diff line number Diff line change 1+ import type { Json , PendingJsonRpcResponse } from '@metamask/utils' ;
2+
3+ import { getNetworkVersionHandler } from './net-version' ;
4+
5+ describe ( 'getNetworkVersionHandler' , ( ) => {
6+ it ( 'returns the network version' , async ( ) => {
7+ const end = jest . fn ( ) ;
8+ const result : PendingJsonRpcResponse < Json > = {
9+ jsonrpc : '2.0' as const ,
10+ id : 1 ,
11+ } ;
12+
13+ await getNetworkVersionHandler (
14+ {
15+ jsonrpc : '2.0' ,
16+ id : 1 ,
17+ method : 'net_version' ,
18+ params : [ ] ,
19+ } ,
20+ result ,
21+ jest . fn ( ) ,
22+ end ,
23+ ) ;
24+
25+ expect ( end ) . toHaveBeenCalled ( ) ;
26+ expect ( result . result ) . toBe ( '1' ) ;
27+ } ) ;
28+ } ) ;
Original file line number Diff line number Diff line change 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 net_version that always returns a specific
13+ * hardcoded result.
14+ *
15+ * @param _request - Incoming JSON-RPC request. Ignored for this specific
16+ * 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 JSON-RPC response.
22+ */
23+ export async function getNetworkVersionHandler (
24+ _request : JsonRpcRequest ,
25+ response : PendingJsonRpcResponse < Json > ,
26+ _next : JsonRpcEngineNextCallback ,
27+ end : JsonRpcEngineEndCallback ,
28+ ) {
29+ // For now this will return a mocked result, this should probably match
30+ // whatever network the simulation is using.
31+ response . result = '1' ;
32+
33+ return end ( ) ;
34+ }
You can’t perform that action at this time.
0 commit comments