1
+ import { Chain } from '../../blockchain'
1
2
import { middleware , validators } from '../validation'
2
- import { toBuffer , bufferToHex } from 'ethereumjs-util'
3
+ import { toBuffer , stripHexPrefix , BN } from 'ethereumjs-util'
3
4
4
5
/**
5
6
* eth_* RPC module
6
7
* @memberof module:rpc/modules
7
8
*/
8
9
export class Eth {
9
- private _chain : any
10
+ private _chain : Chain
10
11
public ethVersion : any
11
12
12
13
/**
@@ -50,8 +51,7 @@ export class Eth {
50
51
async blockNumber ( _params = [ ] , cb : ( err : Error | null , val ?: string ) => void ) {
51
52
try {
52
53
const latestHeader = await this . _chain . getLatestHeader ( )
53
- const latestBlockNumber = bufferToHex ( latestHeader . number )
54
- cb ( null , `${ latestBlockNumber } ` )
54
+ cb ( null , `0x${ latestHeader . number . toString ( 16 ) } ` )
55
55
} catch ( err ) {
56
56
cb ( err )
57
57
}
@@ -65,16 +65,15 @@ export class Eth {
65
65
* as the second argument
66
66
* @return {Promise }
67
67
*/
68
- async getBlockByNumber ( params : any [ ] | boolean [ ] , cb : ( err : Error | null , val ?: any ) => void ) {
68
+ async getBlockByNumber ( params : [ string , boolean ] , cb : ( err : Error | null , val ?: any ) => void ) {
69
69
// eslint-disable-next-line prefer-const
70
70
let [ blockNumber , includeTransactions ] = params
71
-
72
- blockNumber = Number . parseInt ( blockNumber , 16 )
71
+ const blockNumberBN = new BN ( stripHexPrefix ( blockNumber ) , 16 )
73
72
try {
74
- const block = await this . _chain . getBlock ( blockNumber )
75
- const json = block . toJSON ( true )
73
+ const block = await this . _chain . getBlock ( blockNumberBN )
74
+ const json = block . toJSON ( )
76
75
if ( ! includeTransactions ) {
77
- json . transactions = json . transactions . map ( ( tx : any ) => tx . hash )
76
+ json . transactions = json . transactions ! . map ( ( tx : any ) => tx . hash )
78
77
}
79
78
cb ( null , json )
80
79
} catch ( err ) {
@@ -95,11 +94,10 @@ export class Eth {
95
94
96
95
try {
97
96
const block = await this . _chain . getBlock ( toBuffer ( blockHash ) )
98
-
99
- const json = block . toJSON ( true )
97
+ const json = block . toJSON ( )
100
98
101
99
if ( ! includeTransactions ) {
102
- json . transactions = json . transactions . map ( ( tx : any ) => tx . hash )
100
+ json . transactions = json . transactions ! . map ( ( tx : any ) => tx . hash )
103
101
}
104
102
cb ( null , json )
105
103
} catch ( err ) {
@@ -115,16 +113,16 @@ export class Eth {
115
113
* @return {Promise }
116
114
*/
117
115
async getBlockTransactionCountByHash (
118
- params : string [ ] ,
116
+ params : [ string ] ,
119
117
cb : ( err : Error | null , val ?: any ) => void
120
118
) {
121
119
const [ blockHash ] = params
122
120
123
121
try {
124
122
const block = await this . _chain . getBlock ( toBuffer ( blockHash ) )
125
123
126
- const json = block . toJSON ( true )
127
- cb ( null , `0x${ json . transactions . length . toString ( 16 ) } ` )
124
+ const json = block . toJSON ( )
125
+ cb ( null , `0x${ json . transactions ! . length . toString ( 16 ) } ` )
128
126
} catch ( err ) {
129
127
cb ( err )
130
128
}
0 commit comments