Skip to content

Commit 7ec4682

Browse files
committed
Update bp public methods to async/await
1 parent 2208f50 commit 7ec4682

File tree

4 files changed

+56
-65
lines changed

4 files changed

+56
-65
lines changed

src/__tests__/integrate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import CQL from '../index'
1414
// console.log(status)
1515
// let list = await cql.bp.getBlockList(1, 10)
1616
// console.log(list)
17-
let txList = await cql.bp.getTransactionList(1, 30)
18-
console.log(txList)
1917
// let block = await cql.bp.getBlockByHeight(52170)
2018
// console.log(block)
2119
// let blockh = await cql.bp.getBlockByHash('5c075d620dd63cba04b1e887ddb805840e03b6732f81e03d058a82a15ab739f9')
2220
// console.log(blockh)
21+
// let txList = await cql.bp.getTransactionList(1, 30)
22+
// console.log(txList)
2323
// let txList = await cql.bp.getTransactionListOfBlock(52170, 0, 10)
2424
// console.log(txList)
25-
// let txH = await cql.bp.getTransactionByHash('fooo')
26-
// console.log(txH)
25+
let txH = await cql.bp.getTransactionByHash('fooo')
26+
console.log(txH)
2727
})()

src/client/listeners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class Listeners {
2121
}
2222

2323
this.debug(
24-
'Found matching listener for request id [%s], forwarding message method of [%s]',
24+
'Found matching listener for request id [%s]',
2525
message.id,
2626
message.method
2727
)

src/client/socket.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class WebSocketClient implements CqlWebSocket {
5353
resolve()
5454
}
5555
} catch (error) {
56-
reject(error)
56+
reject(JSON.stringify(error, null, 2))
5757
}
5858
})
5959

@@ -109,11 +109,13 @@ export default class WebSocketClient implements CqlWebSocket {
109109
): Promise<any> {
110110
this.debugRpc('Send RPC message', req)
111111

112-
this.send(req, (res) => {
113-
this.debugRpc('Got RPC response', res)
114-
return new Promise((resolve, reject) => {
112+
// wait until got response
113+
return new Promise((resolve, reject) => {
114+
this.send(req, (res) => {
115+
this.debugRpc('Got RPC response', res)
116+
115117
if (res.error) {
116-
reject(res.error)
118+
reject(JSON.stringify(res.error, null, 2))
117119
} else {
118120
resolve(res.result)
119121
}
@@ -125,6 +127,7 @@ export default class WebSocketClient implements CqlWebSocket {
125127
event: MessageEvent
126128
) => {
127129
let payload: any
130+
this.debug('Got socket %s', event.type)
128131

129132
try {
130133
payload = JSON.parse(event.data) as { [key: string]: any }

src/libs/bp.ts

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,73 +38,65 @@ export default class Bp implements BpInterface {
3838
this.debug = debugFactory('cql:bp')
3939
}
4040

41-
public getProtocolVersion(): Promise<string> {
41+
public async getProtocolVersion(): Promise<string> {
4242
let req: RPCObject = constructRPCObj(BpMethodType.GET_PROTOCOL_VERSION)
4343
this.debug('Send getProtocolVersion request', req)
44-
return new Promise(resolve => {
45-
this.client.send(req, res => {
46-
this.debug('Got getProtocolVersion response', res)
47-
resolve(res.result)
48-
})
49-
})
44+
45+
let result = await this.client.sendRpc(req)
46+
this.debug('Got getProtocolVersion response', result)
47+
48+
return result
5049
}
5150

52-
public getRunningStatus(): Promise<object> {
51+
public async getRunningStatus(): Promise<object> {
5352
let req: RPCObject = constructRPCObj(BpMethodType.GET_RUNNING_STATUS)
5453
this.debug('Send getRunningStatus request', req)
55-
return new Promise(resolve => {
56-
this.client.send(req, res => {
57-
this.debug('Got getRunningStatus response', res)
58-
resolve(res.result)
59-
})
60-
})
54+
55+
let result = await this.client.sendRpc(req)
56+
this.debug('Got getRunningStatus response', result)
57+
58+
return result
6159
}
6260

63-
public getBlockList(
61+
public async getBlockList(
6462
page: number,
6563
size: number,
6664
since: number = 0
6765
): Promise<object> {
6866
let params = [since, page, size]
6967
let req: RPCObject = constructRPCObj(BpMethodType.GET_BLOCK_LIST, params)
70-
7168
this.debug('Send getBlockList request', req)
72-
return new Promise((resolve) => {
73-
this.client.send(req, (res) => {
74-
this.debug('Got getBlockList response', res)
75-
resolve(res.result)
76-
})
77-
})
69+
70+
let result = await this.client.sendRpc(req)
71+
this.debug('Got getBlockList response', result)
72+
73+
return result
7874
}
7975

80-
public getBlockByHeight(
76+
public async getBlockByHeight(
8177
height: number
8278
): Promise<object> {
8379
let params = [height]
8480
let req: RPCObject = constructRPCObj(BpMethodType.GET_BLOCK_BY_HEIGHT, params)
85-
8681
this.debug('Send getBlockByHeight request', req)
87-
return new Promise((resolve) => {
88-
this.client.send(req, (res) => {
89-
this.debug('Got getBlockByHeight response', res)
90-
resolve(res.result)
91-
})
92-
})
82+
83+
let result = await this.client.sendRpc(req)
84+
this.debug('Got getBlockByHeight response', result)
85+
86+
return result
9387
}
9488

95-
public getBlockByHash(
89+
public async getBlockByHash(
9690
hash: string
9791
): Promise<object> {
9892
let params = [hash]
9993
let req: RPCObject = constructRPCObj(BpMethodType.GET_BLOCK_BY_HASH, params)
100-
10194
this.debug('Send getBlockByHash request', req)
102-
return new Promise((resolve) => {
103-
this.client.send(req, (res) => {
104-
this.debug('Got getBlockByHash response', res)
105-
resolve(res.result)
106-
})
107-
})
95+
96+
let result = await this.client.sendRpc(req)
97+
this.debug('Got getBlockByHash response', result)
98+
99+
return result
108100
}
109101

110102
public async getTransactionList(
@@ -114,43 +106,39 @@ export default class Bp implements BpInterface {
114106
): Promise<object> {
115107
let params = [since, page, size]
116108
let req: RPCObject = constructRPCObj(BpMethodType.GET_TRANSACTION_LIST, params)
117-
118109
this.debug('Send getTransactionList request', req)
110+
119111
let result = await this.client.sendRpc(req)
120112
this.debug('Got getTransactionList response', result)
121113

122114
return result
123115
}
124116

125-
public getTransactionListOfBlock(
117+
public async getTransactionListOfBlock(
126118
height: number,
127119
from: number,
128120
to: number
129121
): Promise<Array<object>> {
130122
let params = [height, from, to]
131123
let req: RPCObject = constructRPCObj(BpMethodType.GET_TRANSACTION_LIST_OF_BLOCK, params)
132-
133124
this.debug('Send getTransactionListOfBlock request', req)
134-
return new Promise((resolve) => {
135-
this.client.send(req, (res) => {
136-
this.debug('Got getTransactionListOfBlock response', res)
137-
resolve(res.result)
138-
})
139-
})
125+
126+
let result = await this.client.sendRpc(req)
127+
this.debug('Got getTransactionListOfBlock response', result)
128+
129+
return result
140130
}
141131

142-
public getTransactionByHash(
132+
public async getTransactionByHash(
143133
hash: string
144134
): Promise<object> {
145135
let params = [hash]
146136
let req: RPCObject = constructRPCObj(BpMethodType.GET_TRANSACTION_BY_HASH, params)
147-
148137
this.debug('Send getTransactionByHash request', req)
149-
return new Promise((resolve) => {
150-
this.client.send(req, (res) => {
151-
this.debug('Got getTransactionByHash response', res)
152-
resolve(res.result)
153-
})
154-
})
138+
139+
let result = await this.client.sendRpc(req)
140+
this.debug('Got getTransactionByHash response', result)
141+
142+
return result
155143
}
156144
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy