Skip to content

Commit a5b2c60

Browse files
committed
Update Ethereum provider example
1 parent 4abe68a commit a5b2c60

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
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": "zLYiM3hZxvbSmXsvp2rA8G5wYvR8EjWBkUxiICBQRsE=",
10+
"shasum": "TVujszFS0k34sSwk9X4zz7uyecPsZM80xhtDpN1xdUg=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ import {
55
import type { Hex } from '@metamask/utils';
66
import { assert, stringToBytes, bytesToHex } from '@metamask/utils';
77

8-
import type { PersonalSignParams } from './types';
8+
import type { BaseParams, PersonalSignParams } from './types';
9+
10+
/**
11+
* Set the active Ethereum chain for the Snap.
12+
*
13+
* @param chainId - The chain ID to switch to.
14+
*/
15+
async function switchChain(chainId: Hex) {
16+
await ethereum.request({
17+
method: 'wallet_switchEthereumChain',
18+
params: [{ chainId }],
19+
});
20+
}
921

1022
/**
1123
* Get the current gas price using the `ethereum` global. This is essentially
@@ -106,6 +118,9 @@ async function personalSign(message: string, from: string) {
106118
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
107119
*/
108120
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
121+
const { chainId = '0x1' } = request.params as BaseParams;
122+
await switchChain(chainId);
123+
109124
switch (request.method) {
110125
case 'getGasPrice':
111126
return await getGasPrice();
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
export type PersonalSignParams = {
1+
import type { Hex } from '@metamask/utils';
2+
3+
export type BaseParams = {
4+
chainId?: Hex;
5+
};
6+
7+
export type PersonalSignParams = BaseParams & {
28
message: string;
39
};

0 commit comments

Comments
 (0)