Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eth.getChainId method to 1.x #3113

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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