Skip to content

Commit

Permalink
fix: rpc tests
Browse files Browse the repository at this point in the history
by improving the block identifier parser
  • Loading branch information
janek26 committed Nov 29, 2022
1 parent b29c444 commit cd913ee
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable max-classes-per-file */
import { BN } from 'bn.js';

import type { BlockNumber } from '../types';
import { BigNumberish, isHex, toBN, toHex } from '../utils/number';

Expand Down Expand Up @@ -34,6 +36,7 @@ export function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string
// null appends nothing to the request url

export type BlockIdentifier = BlockNumber | BigNumberish;
export const validBlockTags = ['latest', 'pending'];

export class Block {
hash: BlockIdentifier = null;
Expand All @@ -42,22 +45,26 @@ export class Block {

tag: BlockIdentifier = null;

private setIdentifier: (_identifier: BlockIdentifier) => void;
private setIdentifier(__identifier: BlockIdentifier) {
if (typeof __identifier === 'string' && isHex(__identifier)) {
this.hash = __identifier;
} else if (BN.isBN(__identifier)) {
this.hash = toHex(__identifier);
} else if (typeof __identifier === 'number') {
this.number = __identifier;
} else if (typeof __identifier === 'string' && validBlockTags.includes(__identifier)) {
this.tag = __identifier;
} else {
// default
this.tag = 'pending';
}
}

constructor(_identifier: BlockIdentifier) {
this.setIdentifier = function (__identifier: BlockIdentifier) {
if (typeof __identifier === 'string' && isHex(__identifier)) {
this.hash = __identifier;
} else if (typeof __identifier === 'number') {
this.number = __identifier;
} else {
this.tag = __identifier;
}
};

this.setIdentifier(_identifier);
}

// TODO: fix any
get queryIdentifier(): any {
if (this.number !== null) {
return `blockNumber=${this.number}`;
Expand All @@ -70,6 +77,7 @@ export class Block {
return `blockNumber=${this.tag}`;
}

// TODO: fix any
get identifier(): any {
if (this.number !== null) {
return { block_number: this.number };
Expand Down

0 comments on commit cd913ee

Please sign in to comment.