Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit 89cba78

Browse files
author
Noel
authored
refactor: isBlockNumber (#405)
1 parent 623b89f commit 89cba78

File tree

1 file changed

+6
-11
lines changed
  • packages/zilliqa-js-blockchain/src

1 file changed

+6
-11
lines changed

packages/zilliqa-js-blockchain/src/chain.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import {
3838

3939
import { toTxParams } from './util';
4040

41+
const isBlockNumber = (blockNum: number) =>
42+
Number.isFinite(blockNum) && Number.isInteger(blockNum) && blockNum >= 0;
43+
4144
export class Blockchain implements ZilliqaModule {
4245
signer: Wallet;
4346
provider: Provider;
@@ -427,9 +430,7 @@ export class Blockchain implements ZilliqaModule {
427430
getTransactionsForTxBlockEx(
428431
txBlock: number,
429432
): Promise<RPCResponse<any, string>> {
430-
const isBlockNumber =
431-
Number.isFinite(txBlock) && Number.isInteger(txBlock) && txBlock >= 0;
432-
if (!isBlockNumber) {
433+
if (!isBlockNumber(txBlock)) {
433434
throw new Error('invalid txBlock');
434435
}
435436
return this.provider.send(
@@ -452,9 +453,7 @@ export class Blockchain implements ZilliqaModule {
452453
// Returns the transactions in batches (or pages) of 2,500
453454
// This API behaves similar to GetTxBodiesForTxBlock
454455
getTxnBodiesForTxBlockEx(txBlock: number): Promise<RPCResponse<any, string>> {
455-
const isBlockNumber =
456-
Number.isFinite(txBlock) && Number.isInteger(txBlock) && txBlock >= 0;
457-
if (!isBlockNumber) {
456+
if (!isBlockNumber(txBlock)) {
458457
throw new Error('invalid txBlock');
459458
}
460459
return this.provider.send(
@@ -569,11 +568,7 @@ export class Blockchain implements ZilliqaModule {
569568
: contractAddress;
570569

571570
const isLatestStr = txBlock === 'latest';
572-
573-
const isBlockNumber =
574-
Number.isFinite(txBlock) && Number.isInteger(txBlock) && txBlock >= 0;
575-
576-
const isValid = isLatestStr || isBlockNumber;
571+
const isValid = isLatestStr || isBlockNumber(Number(txBlock));
577572
if (!isValid) {
578573
throw new Error('invalid txBlock');
579574
}

0 commit comments

Comments
 (0)