88 stringToBytes ,
99 bytesToHex ,
1010 hexToNumber ,
11+ numberToHex ,
1112} from '@metamask/utils' ;
1213
1314import type {
@@ -29,60 +30,47 @@ async function switchChain(chainId: Hex) {
2930}
3031
3132/**
32- * Get the current gas price using the `ethereum` global. This is essentially
33+ * Get the current chain ID using the `ethereum` global. This is essentially
3334 * the same as the `window.ethereum` global, but does not have access to all
3435 * methods.
3536 *
3637 * Note that using the `ethereum` global requires the
3738 * `endowment:ethereum-provider` permission.
3839 *
39- * @returns The current gas price as a hexadecimal string.
40+ * @returns The current chain ID as a string.
4041 * @see https://docs.metamask.io/snaps/reference/permissions/#endowmentethereum-provider
4142 */
42- async function getGasPrice ( ) {
43- const gasPrice = await ethereum . request < Hex > ( { method : 'eth_gasPrice' } ) ;
44- assert ( gasPrice , 'Ethereum provider did not return a gas price.' ) ;
45-
46- return gasPrice ;
47- }
43+ async function getChainId ( ) {
44+ const chainId = await ethereum . request < string > ( {
45+ method : 'eth_chainId' ,
46+ } ) ;
4847
49- /**
50- * Get the current network version using the `ethereum` global. This is
51- * essentially the same as the `window.ethereum` global, but does not have
52- * access to all methods.
53- *
54- * Note that using the `ethereum` global requires the
55- * `endowment:ethereum-provider` permission.
56- *
57- * @returns The current network version as a string.
58- * @see https://docs.metamask.io/snaps/reference/permissions/#endowmentethereum-provider
59- */
60- async function getVersion ( ) {
61- const version = await ethereum . request < string > ( { method : 'net_version' } ) ;
62- assert ( version , 'Ethereum provider did not return a version.' ) ;
48+ assert ( chainId , 'Ethereum provider did not return a chain ID.' ) ;
6349
64- return version ;
50+ return chainId ;
6551}
6652
6753/**
68- * Get the current chain ID using the `ethereum` global. This is essentially
54+ * Get a block by number using the `ethereum` global. This is essentially
6955 * the same as the `window.ethereum` global, but does not have access to all
7056 * methods.
7157 *
7258 * Note that using the `ethereum` global requires the
7359 * `endowment:ethereum-provider` permission.
7460 *
75- * @returns The current chain ID as a string.
61+ * @param blockNumber - The block number.
62+ * @returns Information about the requested block.
7663 * @see https://docs.metamask.io/snaps/reference/permissions/#endowmentethereum-provider
7764 */
78- async function getChainId ( ) {
79- const chainId = await ethereum . request < string > ( {
80- method : 'eth_chainId' ,
65+ async function getBlock ( blockNumber : number ) {
66+ const block = await ethereum . request < string > ( {
67+ method : 'eth_getBlockByNumber' ,
68+ params : [ numberToHex ( blockNumber ) , false ] ,
8169 } ) ;
8270
83- assert ( chainId , 'Ethereum provider did not return a chain ID .' ) ;
71+ assert ( block , 'Ethereum provider did not return a block .' ) ;
8472
85- return chainId ;
73+ return block ;
8674}
8775
8876/**
@@ -249,18 +237,15 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
249237 await switchChain ( chainId ) ;
250238
251239 switch ( request . method ) {
252- case 'getGasPrice' :
253- return await getGasPrice ( ) ;
254-
255- case 'getVersion' :
256- return await getVersion ( ) ;
257-
258240 case 'getChainId' :
259241 return await getChainId ( ) ;
260242
261243 case 'getAccounts' :
262244 return await getAccounts ( ) ;
263245
246+ case 'getGenesisBlock' :
247+ return await getBlock ( 0 ) ;
248+
264249 case 'personalSign' : {
265250 const params = request . params as PersonalSignParams ;
266251 const accounts = await getAccounts ( ) ;
0 commit comments