Skip to content

Commit 2004a33

Browse files
authored
Add eth_chainId (#791)
* Add eth_chainId to supported methods handler * add test
1 parent 87f4bca commit 2004a33

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

packages/cli/src/rpc/modules/eth.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PrefixedHexString } from '@ethereumjs/util'
22
import { bigIntToHex, hexToBytes, intToHex, toBytes } from '@ethereumjs/util'
3-
import { GET_LOGS_BLOCK_RANGE_LIMIT, NetworkId, NetworkIdByChain, getLogs } from 'portalnetwork'
3+
import { ChainId, GET_LOGS_BLOCK_RANGE_LIMIT, NetworkId, NetworkIdByChain, getLogs } from 'portalnetwork'
44

55
import { INTERNAL_ERROR, INVALID_PARAMS } from '../error-code.js'
66
import { jsonRpcLog } from '../types.js'
@@ -86,6 +86,7 @@ export class eth {
8686
[validators.transaction(['to'])],
8787
[validators.blockOption],
8888
])
89+
this.chainId = middleware(callWithStackTrace(this.chainId.bind(this), false), 0, [])
8990
}
9091

9192
async getBalance(params: [string, string]) {
@@ -130,7 +131,12 @@ export class eth {
130131
* @returns The chain ID.
131132
*/
132133
async chainId(_params = []) {
133-
return '0x01'
134+
switch (this._client.chainId) {
135+
case ChainId.MAINNET:
136+
return '0x1'
137+
case ChainId.SEPOLIA:
138+
return '0xaa36a7'
139+
}
134140
}
135141

136142
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { assert, afterAll, beforeAll, describe, it } from 'vitest'
2+
3+
import { startRpc } from '../util.js'
4+
const method = 'eth_chainId'
5+
describe(`${method} tests`, () => {
6+
describe(`${method} tests`, () => {
7+
let ul
8+
let rp
9+
beforeAll(async () => {
10+
const { ultralight, rpc } = await startRpc({
11+
networks: ['history'],
12+
rpcPort: 8547,
13+
port: 9002,
14+
})
15+
ul = ultralight
16+
rp = rpc
17+
})
18+
it('should return the correct chainId', async () => {
19+
const res = await rp.request(method, [])
20+
assert.equal(res.result, '0x1')
21+
})
22+
afterAll(() => {
23+
ul.kill()
24+
})
25+
})
26+
})

0 commit comments

Comments
 (0)