Skip to content

Commit

Permalink
Add eth.getChainId method to 1.x (#3113)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gabmontes authored and nivida committed Oct 8, 2019
1 parent a790dd6 commit 5e8738e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Released with 1.0.0-beta.37 code base.
- localStorage support detection added (#3031)
- getNetworkType method extended with Görli testnet (#3095)
- supportsSubscriptions method added to providers (#3116)
- Add `eth.getChainId` method (#3113)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion docs/web3-eth-net.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ getNetworkType
Guesses the chain the node is connected by comparing the genesis hashes.

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

-------
Returns
Expand Down
28 changes: 28 additions & 0 deletions docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1467,3 +1467,31 @@ Example
------------------------------------------------------------------------------

.. _eth-chainId:

getChainId
==========

.. code-block:: javascript
web3.eth.getChainId([callback])
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>`_.

-------
Returns
-------

``Promise<Number>`` - Returns chain ID.

-------
Example
-------

.. code-block:: javascript
web3.eth.getChainId().then(console.log);
> 61
------------------------------------------------------------------------------
6 changes: 6 additions & 0 deletions packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ var Eth = function Eth() {
inputFormatter: [formatter.inputLogFormatter],
outputFormatter: formatter.outputLogFormatter
}),
new Method({
name: 'getChainId',
call: 'eth_chainId',
params: 0,
outputFormatter: utils.hexToNumber
}),

// subscriptions
new Subscriptions({
Expand Down
14 changes: 14 additions & 0 deletions test/eth.getChainId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var testMethod = require('./helpers/test.method.js');

var method = 'getChainId';
var methodCall = 'eth_chainId';

var tests = [{
result: '0x01',
formattedResult: 1,
call: methodCall
}];


testMethod.runTests('eth', method, tests);

2 changes: 1 addition & 1 deletion test/eth_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('eth', function() {
u.methodExists(eth, 'subscribe');
u.methodExists(eth, 'Contract');
u.methodExists(eth, 'Iban');

u.methodExists(eth, 'getChainId')

u.methodExists(eth, 'isMining');
u.methodExists(eth, 'getCoinbase');
Expand Down

0 comments on commit 5e8738e

Please sign in to comment.