Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 5e8738e

Browse files
gabmontesnivida
authored andcommitted
Add eth.getChainId method to 1.x (#3113)
* Add eth.getChainId method This method was added to the branch 2.x in d98cef2. This change is to support the method in the 1.x line. * Updated changelog * Add eth.getChainId to the docs
1 parent a790dd6 commit 5e8738e

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Released with 1.0.0-beta.37 code base.
5555
- localStorage support detection added (#3031)
5656
- getNetworkType method extended with Görli testnet (#3095)
5757
- supportsSubscriptions method added to providers (#3116)
58+
- Add `eth.getChainId` method (#3113)
5859

5960
### Fixed
6061

docs/web3-eth-net.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ getNetworkType
2525
2626
Guesses the chain the node is connected by comparing the genesis hashes.
2727

28-
.. note:: This is not a 100% accurate guess as any private network could use testnet and mainnet genesis blocks and network IDs.
28+
.. note:: It's recommended to use the :ref:`web3.eth.getChainId <eth-chainId>` method to detect the currently connected chain.
2929

3030
-------
3131
Returns

docs/web3-eth.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,31 @@ Example
14671467
14681468
14691469
------------------------------------------------------------------------------
1470+
1471+
.. _eth-chainId:
1472+
1473+
getChainId
1474+
==========
1475+
1476+
.. code-block:: javascript
1477+
1478+
web3.eth.getChainId([callback])
1479+
1480+
Returns the chain ID of the current connected node as described in the `EIP-695 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-695.md>`_.
1481+
1482+
-------
1483+
Returns
1484+
-------
1485+
1486+
``Promise<Number>`` - Returns chain ID.
1487+
1488+
-------
1489+
Example
1490+
-------
1491+
1492+
.. code-block:: javascript
1493+
1494+
web3.eth.getChainId().then(console.log);
1495+
> 61
1496+
1497+
------------------------------------------------------------------------------

packages/web3-eth/src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ var Eth = function Eth() {
368368
inputFormatter: [formatter.inputLogFormatter],
369369
outputFormatter: formatter.outputLogFormatter
370370
}),
371+
new Method({
372+
name: 'getChainId',
373+
call: 'eth_chainId',
374+
params: 0,
375+
outputFormatter: utils.hexToNumber
376+
}),
371377

372378
// subscriptions
373379
new Subscriptions({

test/eth.getChainId.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var testMethod = require('./helpers/test.method.js');
2+
3+
var method = 'getChainId';
4+
var methodCall = 'eth_chainId';
5+
6+
var tests = [{
7+
result: '0x01',
8+
formattedResult: 1,
9+
call: methodCall
10+
}];
11+
12+
13+
testMethod.runTests('eth', method, tests);
14+

test/eth_methods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('eth', function() {
2222
u.methodExists(eth, 'subscribe');
2323
u.methodExists(eth, 'Contract');
2424
u.methodExists(eth, 'Iban');
25-
25+
u.methodExists(eth, 'getChainId')
2626

2727
u.methodExists(eth, 'isMining');
2828
u.methodExists(eth, 'getCoinbase');

0 commit comments

Comments
 (0)