From bb3c21cab2208eddd9bcc56339e07b7e89b5f7ed Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Mon, 18 Feb 2019 17:40:08 +0100 Subject: [PATCH] eth_chainId method implemented --- .../src/methods/network/ChainIdMethod.js | 48 +++++++++++++++++++ .../src/methods/network/ChainIdMethodTest.js | 37 ++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 packages/web3-core-method/src/methods/network/ChainIdMethod.js create mode 100644 packages/web3-core-method/tests/src/methods/network/ChainIdMethodTest.js diff --git a/packages/web3-core-method/src/methods/network/ChainIdMethod.js b/packages/web3-core-method/src/methods/network/ChainIdMethod.js new file mode 100644 index 00000000000..d2a972defbe --- /dev/null +++ b/packages/web3-core-method/src/methods/network/ChainIdMethod.js @@ -0,0 +1,48 @@ +/* + This file is part of web3.js. + + web3.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + web3.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with web3.js. If not, see . +*/ +/** + * @file ListeningMethod.js + * @author Samuel Furter + * @date 2018 + */ + +import AbstractCallMethod from '../../../lib/methods/AbstractCallMethod'; + +export default class ChainIdMethod extends AbstractCallMethod { + /** + * @param {Utils} utils + * @param {Object} formatters + * + * @constructor + */ + constructor(utils, formatters) { + super('eth_chainId', 0, utils, formatters); + } + + /** + * This method will be executed after the RPC request. + * + * @method afterExecution + * + * @param {Object} response + * + * @returns {Number} + */ + afterExecution(response) { + return this.utils.hexToNumber(response); + } +} diff --git a/packages/web3-core-method/tests/src/methods/network/ChainIdMethodTest.js b/packages/web3-core-method/tests/src/methods/network/ChainIdMethodTest.js new file mode 100644 index 00000000000..8787b47c36b --- /dev/null +++ b/packages/web3-core-method/tests/src/methods/network/ChainIdMethodTest.js @@ -0,0 +1,37 @@ +import * as Utils from 'web3-utils'; +import AbstractCallMethod from '../../../../lib/methods/AbstractCallMethod'; +import ChainIdMethod from '../../../../src/methods/network/ChainIdMethod'; + +// Mocks +jest.mock('Utils'); + +/** + * PeerCountMethod test + */ +describe('PeerCountMethodTest', () => { + let method; + + beforeEach(() => { + method = new ChainIdMethod(Utils, null); + }); + + it('constructor check', () => { + expect(method).toBeInstanceOf(AbstractCallMethod); + + expect(method.rpcMethod).toEqual('eth_chainId'); + + expect(method.parametersAmount).toEqual(0); + + expect(method.utils).toEqual(Utils); + + expect(method.formatters).toEqual(null); + }); + + it('afterExecution should map the response', () => { + Utils.hexToNumber.mockReturnValueOnce(61); + + expect(method.afterExecution('0x0')).toEqual(61); + + expect(Utils.hexToNumber).toHaveBeenCalledWith('0x0'); + }); +});