Skip to content

Commit bc6133f

Browse files
jochem-brouwerholgerd77
authored andcommitted
client: some typesafety in rpc/modules
1 parent 72cc8eb commit bc6133f

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/client/lib/rpc/modules/admin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ export class Admin {
3535
* @param {*} [params] An empty array
3636
* @param {*} [cb] A function with an error object as the first argument and the result as the second
3737
*/
38-
async nodeInfo(params: any, cb: Function) {
38+
async nodeInfo(params: [], cb: Function) {
3939
const rlpxInfo = (this._client.server('rlpx') as RlpxServer).getRlpxInfo()
4040
const { enode, id, ip, listenAddr, ports } = rlpxInfo
4141
const { discovery, listener } = ports
4242
const clientName = getClientVersion()
4343

4444
// TODO version not present in reference..
4545
// const ethVersion = Math.max.apply(Math, this._ethProtocol.versions)
46-
const latestHeader = (this._chain as any)._headers.latest
47-
const difficulty = latestHeader?.difficulty ?? 0 // should be number
46+
const latestHeader = await this._chain.getLatestHeader()
47+
const difficulty = latestHeader.difficulty
4848
const genesis = bufferToHex(this._chain.genesis.hash)
49-
const head = bufferToHex(latestHeader?.mixHash ?? null)
49+
const head = bufferToHex(latestHeader.mixHash)
5050
const network = this._chain.networkId
5151

5252
const nodeInfo = {

packages/client/lib/rpc/modules/eth.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { Chain } from '../../blockchain'
22
import { middleware, validators } from '../validation'
33
import { toBuffer, stripHexPrefix, BN } from 'ethereumjs-util'
4+
import { EthereumClient } from '../..'
45

56
/**
67
* eth_* RPC module
78
* @memberof module:rpc/modules
89
*/
910
export class Eth {
1011
private _chain: Chain
11-
public ethVersion: any
12+
public ethVersion: number
1213

1314
/**
1415
* Create eth_* RPC module
1516
* @param {Node} Node to which the module binds
1617
*/
17-
constructor(node: any) {
18+
constructor(node: EthereumClient) {
1819
const service = node.services.find((s: any) => s.name === 'eth')
19-
this._chain = service.chain
20-
const ethProtocol = service.protocols.find((p: any) => p.name === 'eth')
21-
this.ethVersion = Math.max.apply(Math, ethProtocol.versions)
20+
this._chain = service!.chain
21+
const ethProtocol = service!.protocols.find((p: any) => p.name === 'eth')
22+
this.ethVersion = Math.max.apply(Math, ethProtocol!.versions)
2223

2324
this.blockNumber = middleware(this.blockNumber.bind(this), 0)
2425

packages/client/lib/rpc/modules/net.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EthereumService } from '../../service/ethereumservice'
22
import { middleware } from '../validation'
33
import { addHexPrefix } from 'ethereumjs-util'
4+
import { EthereumClient } from '../..'
45

56
/**
67
* net_* RPC module
@@ -15,8 +16,8 @@ export class Net {
1516
* Create net_* RPC module
1617
* @param {Node} Node to which the module binds
1718
*/
18-
constructor(node: any) {
19-
const service: EthereumService = node.services.find((s: any) => s.name === 'eth')
19+
constructor(node: EthereumClient) {
20+
const service: EthereumService = node.services.find((s: any) => s.name === 'eth')!
2021
this._chain = service.chain
2122
this._node = node
2223
this._peerPool = service.pool

packages/client/lib/rpc/modules/web3.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { middleware, validators } from '../validation'
22
import { addHexPrefix, keccak, toBuffer } from 'ethereumjs-util'
33
import { getClientVersion } from '../../util'
4+
import { EthereumClient } from '../..'
5+
import { Chain } from '../../blockchain'
46

57
/**
68
* web3_* RPC module
79
* @memberof module:rpc/modules
810
*/
911
export class Web3 {
10-
private _chain: any
12+
private _chain?: Chain
1113

1214
/**
1315
* Create web3_* RPC module
1416
* @param {Node} Node to which the module binds
1517
*/
16-
constructor(node: any) {
18+
constructor(node: EthereumClient) {
1719
const service = node.services.find((s: any) => s.name === 'eth')
18-
this._chain = service.chain
20+
this._chain = service?.chain
1921

2022
this.clientVersion = middleware(this.clientVersion.bind(this), 0, [])
2123

0 commit comments

Comments
 (0)