Skip to content

Commit 8dc8622

Browse files
feat: Add getChainId method to Ethereum provider example (#3407)
`net_version` seems to be mocked in the extension E2E environment, so we can't use it to test the switch functionality. --------- Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
1 parent bc050af commit 8dc8622

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
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": "7i20r0cCFwMESzBcTMgRNOt82AjwHneiM6LtD7vXCGw=",
10+
"shasum": "caZ/yWFuxoz8zSE78Anp+6/K1zhJ+CPEwuC63ujX86Q=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ describe('onRpcRequest', () => {
5555
});
5656
});
5757

58+
describe('getChainId', () => {
59+
const MOCK_CHAIN_ID = '0x01'; // Ethereum Mainnet
60+
61+
it('returns the current network version', async () => {
62+
const { request } = await installSnap();
63+
64+
const response = await request({
65+
method: 'getChainId',
66+
});
67+
68+
expect(response).toRespondWith(MOCK_CHAIN_ID);
69+
});
70+
});
71+
5872
describe('getAccounts', () => {
5973
it('returns the addresses granted access to by the user', async () => {
6074
const { request } = await installSnap();

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ async function getVersion() {
6464
return version;
6565
}
6666

67+
/**
68+
* Get the current chain ID using the `ethereum` global. This is essentially
69+
* the same as the `window.ethereum` global, but does not have access to all
70+
* methods.
71+
*
72+
* Note that using the `ethereum` global requires the
73+
* `endowment:ethereum-provider` permission.
74+
*
75+
* @returns The current chain ID as a string.
76+
* @see https://docs.metamask.io/snaps/reference/permissions/#endowmentethereum-provider
77+
*/
78+
async function getChainId() {
79+
const chainId = await ethereum.request<string>({
80+
method: 'eth_chainId',
81+
});
82+
83+
assert(chainId, 'Ethereum provider did not return a chain ID.');
84+
85+
return chainId;
86+
}
87+
6788
/**
6889
* Get the Ethereum accounts that the snap has access to using the `ethereum`
6990
* global. This is essentially the same as the `window.ethereum` global, but
@@ -208,10 +229,11 @@ async function signTypedData(message: string, from: string) {
208229

209230
/**
210231
* Handle incoming JSON-RPC requests from the dapp, sent through the
211-
* `wallet_invokeSnap` method. This handler handles five methods:
232+
* `wallet_invokeSnap` method. This handler handles six methods:
212233
*
213234
* - `getGasPrice`: Get the current Ethereum gas price as a hexadecimal string.
214235
* - `getVersion`: Get the current Ethereum network version as a string.
236+
* - `getChainId`: Get the current Ethereum chain ID as a string.
215237
* - `getAccounts`: Get the Ethereum accounts that the snap has access to.
216238
* - `personalSign`: Sign a message using an Ethereum account.
217239
* - `signTypedData` Sign a struct using an Ethereum account.
@@ -233,6 +255,9 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
233255
case 'getVersion':
234256
return await getVersion();
235257

258+
case 'getChainId':
259+
return await getChainId();
260+
236261
case 'getAccounts':
237262
return await getAccounts();
238263

packages/test-snaps/src/features/snaps/ethereum-provider/EthereumProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const EthereumProvider: FunctionComponent = () => {
2828
}).catch(logError);
2929
};
3030

31-
const handleGetVersion = () => handleSubmit('getVersion');
31+
const handleGetChainId = () => handleSubmit('getChainId');
3232
const handleGetAccounts = () => handleSubmit('getAccounts');
3333

3434
return (
@@ -46,9 +46,9 @@ export const EthereumProvider: FunctionComponent = () => {
4646
id="sendEthprovider"
4747
className="mb-3"
4848
disabled={isLoading}
49-
onClick={handleGetVersion}
49+
onClick={handleGetChainId}
5050
>
51-
Get Version
51+
Get Chain ID
5252
</Button>
5353
<Button
5454
variant="primary"

0 commit comments

Comments
 (0)