@tchain/tcabci-read-js-client 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.js +65 -65
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -15,27 +15,27 @@ const {toJSON} = require("./util")
|
|
|
15
15
|
const axios = require("axios")
|
|
16
16
|
|
|
17
17
|
class TCAbciClient {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
subscribed = false
|
|
19
|
+
subscribedAddresses = []
|
|
20
|
+
connected = false
|
|
21
|
+
version = "v0.1.0"
|
|
22
|
+
retrieverCount = 0
|
|
23
23
|
errorCb = null
|
|
24
24
|
listenCb = null
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
ws = null
|
|
26
|
+
httpClient = null
|
|
27
|
+
readNodeAddress = READ_NODE_ADDRESS
|
|
28
|
+
readNodeWSAddress = READ_NODE_WS_ADDRESS
|
|
29
29
|
|
|
30
30
|
constructor(readNodeAddresses = []) {
|
|
31
31
|
if (readNodeAddresses.length === 2) {
|
|
32
|
-
this
|
|
33
|
-
this
|
|
32
|
+
this.readNodeAddress = readNodeAddresses[0]
|
|
33
|
+
this.readNodeWSAddress = readNodeAddresses[1]
|
|
34
34
|
}
|
|
35
|
-
this
|
|
36
|
-
baseURL: this
|
|
35
|
+
this.httpClient = axios.create({
|
|
36
|
+
baseURL: this.readNodeAddress,
|
|
37
37
|
timeout: 10000,
|
|
38
|
-
headers: {'Client': `tcabaci-read-js-client${this
|
|
38
|
+
headers: {'Client': `tcabaci-read-js-client${this.version}`}
|
|
39
39
|
})
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -46,21 +46,21 @@ class TCAbciClient {
|
|
|
46
46
|
this.listenCb = cb
|
|
47
47
|
}
|
|
48
48
|
Start() {
|
|
49
|
-
return this
|
|
49
|
+
return this.connect()
|
|
50
50
|
}
|
|
51
51
|
Stop() {
|
|
52
|
-
if (!this
|
|
52
|
+
if (!this.getConnected()) {
|
|
53
53
|
throw NOT_CONNECTED
|
|
54
54
|
}
|
|
55
|
-
this
|
|
56
|
-
this
|
|
57
|
-
this
|
|
55
|
+
this.ws.close()
|
|
56
|
+
this.setConnected(false)
|
|
57
|
+
this.setSubscribed(false)
|
|
58
58
|
}
|
|
59
59
|
Subscribe(addresses) {
|
|
60
60
|
if (!Array.isArray(addresses)) {
|
|
61
61
|
throw INVALID_ARGUMENTS
|
|
62
62
|
}
|
|
63
|
-
if (!this
|
|
63
|
+
if (!this.getConnected()) {
|
|
64
64
|
throw NOT_CONNECTED
|
|
65
65
|
}
|
|
66
66
|
let addrs = []
|
|
@@ -68,37 +68,37 @@ class TCAbciClient {
|
|
|
68
68
|
throw ADDRESSES_IS_EMPTY
|
|
69
69
|
}
|
|
70
70
|
addrs = addresses
|
|
71
|
-
if (this
|
|
71
|
+
if (this.getSubscribeAddresses().length > 0) {
|
|
72
72
|
let newAddress = []
|
|
73
73
|
for (let i = 0; i < addresses.length; i++) {
|
|
74
|
-
if (this
|
|
74
|
+
if (this.getSubscribeAddresses().indexOf(addresses[i]) === -1 ){
|
|
75
75
|
newAddress.push(addresses[i])
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
addrs = newAddress
|
|
79
79
|
}
|
|
80
80
|
const message = new Message(true, MESSAGE_TYPE.SUBSCRIBE, addrs)
|
|
81
|
-
this
|
|
82
|
-
this
|
|
83
|
-
this
|
|
81
|
+
this.ws.send(message.ToJSONString())
|
|
82
|
+
this.setSubscribeAddresses(addrs, true)
|
|
83
|
+
this.setSubscribed(true)
|
|
84
84
|
}
|
|
85
85
|
Unsubscribe() {
|
|
86
|
-
if (!this
|
|
86
|
+
if (!this.getSubscribed()) {
|
|
87
87
|
throw NOT_SUBSCRIBED
|
|
88
88
|
}
|
|
89
|
-
this
|
|
90
|
-
this
|
|
91
|
-
this
|
|
89
|
+
this.ws.send(new Message(true, MESSAGE_TYPE.UNSUBSCRIBE, this.getSubscribeAddresses()).ToJSONString())
|
|
90
|
+
this.setSubscribed(false)
|
|
91
|
+
this.setSubscribeAddresses([])
|
|
92
92
|
}
|
|
93
93
|
LastBlock() {
|
|
94
|
-
return this
|
|
94
|
+
return this.httpClient.get("/v1/blocks?limit=1&offset=0")
|
|
95
95
|
.then(res => { return { blocks: res.data.data, total_count: res.data.total_count } } )
|
|
96
96
|
.catch(e => {
|
|
97
97
|
throw BLOCK_NOT_FOUND
|
|
98
98
|
})
|
|
99
99
|
}
|
|
100
100
|
TxSearch({heightOperator, height, recipientAddrs, senderAddrs, hashes, typ, limit, offset, orderField, orderBy}) {
|
|
101
|
-
return this
|
|
101
|
+
return this.httpClient.post(
|
|
102
102
|
"/v1/tx_search/p",
|
|
103
103
|
{
|
|
104
104
|
height: `${heightOperator} ${height}`,
|
|
@@ -123,8 +123,8 @@ class TCAbciClient {
|
|
|
123
123
|
}
|
|
124
124
|
Status() {
|
|
125
125
|
return {
|
|
126
|
-
connected: this
|
|
127
|
-
subscribed: this
|
|
126
|
+
connected: this.connected,
|
|
127
|
+
subscribed: this.subscribed,
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
Broadcast({id, version, type, data, sender_addr, recipient_addr, sign, fee}) {
|
|
@@ -132,7 +132,7 @@ class TCAbciClient {
|
|
|
132
132
|
throw TRANSACTION_TYPE_NOT_VALID
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
return this
|
|
135
|
+
return this.httpClient.post(
|
|
136
136
|
"/v1/broadcast",
|
|
137
137
|
{
|
|
138
138
|
id,
|
|
@@ -149,32 +149,32 @@ class TCAbciClient {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
Disconnect(code = 1000) {
|
|
152
|
-
if (this
|
|
153
|
-
this
|
|
152
|
+
if (this.getConnected()) {
|
|
153
|
+
this.ws.close(code)
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
|
|
157
|
+
connect() {
|
|
158
158
|
return new Promise((resolve, reject) => {
|
|
159
|
-
if (this
|
|
159
|
+
if (this.getConnected()) {
|
|
160
160
|
reject(ALREADY_CONNECTED)
|
|
161
161
|
}
|
|
162
|
-
const wsClient = this
|
|
163
|
-
this
|
|
162
|
+
const wsClient = this.wsClient()
|
|
163
|
+
this.ws = new wsClient(this.readNodeWSAddress)
|
|
164
164
|
|
|
165
|
-
this
|
|
166
|
-
this
|
|
165
|
+
this.ws.onerror = (event) => {
|
|
166
|
+
this.setConnected(false)
|
|
167
167
|
if (this.errorCb) {
|
|
168
168
|
this.errorCb(event.error)
|
|
169
169
|
}
|
|
170
170
|
reject(event)
|
|
171
171
|
}
|
|
172
|
-
this
|
|
173
|
-
this
|
|
174
|
-
this
|
|
172
|
+
this.ws.onopen = (event) => {
|
|
173
|
+
this.setConnected(true)
|
|
174
|
+
this.retrieverCount = 0
|
|
175
175
|
resolve(event)
|
|
176
176
|
}
|
|
177
|
-
this
|
|
177
|
+
this.ws.onmessage = (message) => {
|
|
178
178
|
if (message.data === "OK" && message.data.length < 10) {
|
|
179
179
|
return { status: message.data }
|
|
180
180
|
}
|
|
@@ -182,46 +182,46 @@ class TCAbciClient {
|
|
|
182
182
|
this.listenCb(toJSON(message.data))
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
|
-
this
|
|
186
|
-
this
|
|
187
|
-
if (event.code !== 1000 && this
|
|
188
|
-
this
|
|
185
|
+
this.ws.onclose = (event) => {
|
|
186
|
+
this.setConnected(false)
|
|
187
|
+
if (event.code !== 1000 && this.retrieverCount <= 10) {
|
|
188
|
+
this.retrieverCount++
|
|
189
189
|
setTimeout(() => {
|
|
190
|
-
this
|
|
190
|
+
this.connect().catch(() => {})
|
|
191
191
|
}, 3000)
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
})
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
wsClient() {
|
|
198
198
|
if (typeof window !== "undefined") {
|
|
199
199
|
return window.WebSocket
|
|
200
200
|
}
|
|
201
201
|
const { WebSocket } = require("ws")
|
|
202
202
|
return WebSocket
|
|
203
203
|
}
|
|
204
|
-
|
|
205
|
-
return this
|
|
204
|
+
getConnected() {
|
|
205
|
+
return this.connected
|
|
206
206
|
}
|
|
207
|
-
|
|
208
|
-
this
|
|
207
|
+
setConnected(value) {
|
|
208
|
+
this.connected = value
|
|
209
209
|
}
|
|
210
|
-
|
|
211
|
-
return this
|
|
210
|
+
getSubscribed() {
|
|
211
|
+
return this.subscribed
|
|
212
212
|
}
|
|
213
|
-
|
|
214
|
-
this
|
|
213
|
+
setSubscribed(value) {
|
|
214
|
+
this.subscribed = value
|
|
215
215
|
}
|
|
216
|
-
|
|
217
|
-
return this
|
|
216
|
+
getSubscribeAddresses() {
|
|
217
|
+
return this.subscribedAddresses
|
|
218
218
|
}
|
|
219
|
-
|
|
219
|
+
setSubscribeAddresses(addresses, push = false) {
|
|
220
220
|
if (push) {
|
|
221
|
-
this
|
|
221
|
+
this.subscribedAddresses.push(...addresses)
|
|
222
222
|
return
|
|
223
223
|
}
|
|
224
|
-
this
|
|
224
|
+
this.subscribedAddresses = addresses
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
|