diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index b7e7a0d..f586760 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -549,16 +549,44 @@ describe('ain-js', function() { expect(txCount).not.toBeNull(); }); - it('getProposer', async function () { - const proposer = await ain.getProposer(1); - const hash = (await ain.getBlockByNumber(1)).hash || ""; - expect(await ain.getProposer(hash)).toBe(proposer); + it('getValidatorInfo', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.proposer).not.toBeNull(); + const info = await ain.getValidatorInfo(lastBlock.proposer); + expect(info).not.toBeNull(); + }); + + it('getValidatorsByNumber', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const validators = await ain.getValidatorsByNumber(lastBlock.number); + expect(validators).toStrictEqual(lastBlock.validators); + }); + + it('getValidatorsByHash', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const validators = await ain.getValidatorsByHash(lastBlock.hash); + expect(validators).toStrictEqual(lastBlock.validators); + }); + + it('getProposerByNumber', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.number).not.toBeNull(); + const proposer = await ain.getProposerByNumber(lastBlock.number); + expect(proposer).toBe(lastBlock.proposer); }); - it('getValidators', async function () { - const validators = await ain.getValidators(4); - const hash = (await ain.getBlockByNumber(4)).hash || ""; - expect(await ain.getValidators(hash)).toStrictEqual(validators); + it('getProposerByHash', async function () { + const lastBlock = await ain.getLastBlock(); + expect(lastBlock).not.toBeNull(); + expect(lastBlock.hash).not.toBeNull(); + const proposer = await ain.getProposerByHash(lastBlock.hash); + expect(proposer).toBe(lastBlock.proposer); }); // TODO(liayoo): add getTransactionResult method and test case for it. diff --git a/src/ain.ts b/src/ain.ts index 36f5bc8..49c4411 100755 --- a/src/ain.ts +++ b/src/ain.ts @@ -165,27 +165,48 @@ export default class Ain { } /** - * Fetches the forger's address of a block with a block hash or block number. - * @param {string | number} blockHashOrBlockNumber The block hash or block number. + * Fetches the information of the given validator address. + * @param {string} address The validator address. + * @returns {Promise} + */ + getValidatorInfo(address: string): Promise { + return this.provider.send('ain_getValidatorInfo', { address }); + } + + /** + * Fetches the validator list of a block with a block number. + * @param {number} blockNumber The block number. + * @returns {Promise} + */ + getValidatorsByNumber(blockNumber: number): Promise { + return this.provider.send('ain_getValidatorsByNumber', { number: blockNumber }); + } + + /** + * Fetches the validator list of a block with a block hash. + * @param {string} blockHash The block hash. + * @returns {Promise} + */ + getValidatorsByHash(blockHash: string): Promise { + return this.provider.send('ain_getValidatorsByHash', { hash: blockHash }); + } + + /** + * Fetches the block proproser's address of a block with a block number. + * @param {number} blockNumber The block number. * @returns {Promise} */ - getProposer(blockHashOrBlockNumber: string | number): Promise { - const byHash = typeof blockHashOrBlockNumber === 'string' - const rpcMethod = byHash ? 'ain_getProposerByHash' : 'ain_getProposerByNumber'; - return this.provider.send(rpcMethod, - {[byHash ? 'hash' : 'number']: blockHashOrBlockNumber}); + getProposerByNumber(blockNumber: number): Promise { + return this.provider.send('ain_getProposerByNumber', { number: blockNumber }); } /** - * Fetches the validator list of a block with a block hash or block number. - * @param {string | number} blockHashOrBlockNumber The block hash or block number. - * @returns {Promise} + * Fetches the block proproser's address of a block with a block hash. + * @param {string} blockHash The block hash. + * @returns {Promise} */ - getValidators(blockHashOrBlockNumber: string | number): Promise { - const byHash = typeof blockHashOrBlockNumber === 'string' - const rpcMethod = byHash ? 'ain_getValidatorsByHash' : 'ain_getValidatorsByNumber'; - return this.provider.send(rpcMethod, - {[byHash ? 'hash' : 'number']: blockHashOrBlockNumber}); + getProposerByHash(blockHash: string): Promise { + return this.provider.send('ain_getProposerByHash', { hash: blockHash }); } /**