Skip to content

Commit

Permalink
eth_chainId method implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Furter committed Feb 18, 2019
1 parent 1f98597 commit bb3c21c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/web3-core-method/src/methods/network/ChainIdMethod.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* @file ListeningMethod.js
* @author Samuel Furter <samuel@ethereum.org>
* @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);
}
}
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit bb3c21c

Please sign in to comment.