@tchain/tcabci-read-js-client 1.2.1 → 1.3.1

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.
Files changed (3) hide show
  1. package/client.js +47 -13
  2. package/client_test.js +26 -12
  3. package/package.json +3 -2
package/client.js CHANGED
@@ -1,3 +1,4 @@
1
+ const ReconnectingWebSocket = require('reconnecting-websocket/dist/reconnecting-websocket-cjs')
1
2
  const {
2
3
  NOT_CONNECTED,
3
4
  ALREADY_CONNECTED,
@@ -18,8 +19,7 @@ class TCAbciClient {
18
19
  subscribed = false
19
20
  subscribedAddresses = []
20
21
  connected = false
21
- version = "v0.1.0"
22
- retrieverCount = 0
22
+ version = "v1.3.1"
23
23
  errorCb = null
24
24
  listenCb = null
25
25
  ws = null
@@ -38,7 +38,9 @@ class TCAbciClient {
38
38
  headers: {'Client': `tcabaci-read-js-client${this.version}`}
39
39
  })
40
40
  }
41
-
41
+ Socket() {
42
+ return this.ws
43
+ }
42
44
  SetError(cb) {
43
45
  this.errorCb = cb
44
46
  }
@@ -97,6 +99,30 @@ class TCAbciClient {
97
99
  throw BLOCK_NOT_FOUND
98
100
  })
99
101
  }
102
+ TxSummary({recipientAddrs, senderAddrs, typ}) {
103
+ return this.httpClient.post(
104
+ "/v1/tx_summary",
105
+ {
106
+ recipient_addrs: recipientAddrs,
107
+ sender_addrs: senderAddrs,
108
+ typ: typ,
109
+ })
110
+ .then(res => {
111
+ return {
112
+ last_block_height: res.data.data.last_block_height,
113
+ last_transaction: res.data.data.last_transaction,
114
+ total_count: res.data.total_count,
115
+ }
116
+ })
117
+ .catch(e => {
118
+ switch (e.response.status) {
119
+ case 400:
120
+ throw INVALID_ARGUMENTS
121
+ default:
122
+ throw e
123
+ }
124
+ })
125
+ }
100
126
  TxSearch({heightOperator, height, recipientAddrs, senderAddrs, hashes, typ, limit, offset, orderField, orderBy}) {
101
127
  return this.httpClient.post(
102
128
  "/v1/tx_search/p",
@@ -162,13 +188,25 @@ class TCAbciClient {
162
188
  }
163
189
  }
164
190
 
191
+ Reconnect(code = 1000) {
192
+ if (this.getConnected()) {
193
+ this.ws.reconnect(code)
194
+ }
195
+ }
196
+
165
197
  connect() {
166
198
  return new Promise((resolve, reject) => {
167
199
  if (this.getConnected()) {
168
200
  reject(ALREADY_CONNECTED)
169
201
  }
170
- const wsClient = this.wsClient()
171
- this.ws = new wsClient(this.readNodeWSAddress)
202
+
203
+ const options = {
204
+ WebSocket: this.wsClient(),
205
+ connectionTimeout: 1000,
206
+ maxRetries: 10,
207
+ }
208
+
209
+ this.ws = new ReconnectingWebSocket(this.readNodeWSAddress, [], options)
172
210
 
173
211
  this.ws.onerror = (event) => {
174
212
  this.setConnected(false)
@@ -179,7 +217,6 @@ class TCAbciClient {
179
217
  }
180
218
  this.ws.onopen = (event) => {
181
219
  this.setConnected(true)
182
- this.retrieverCount = 0
183
220
  resolve(event)
184
221
  }
185
222
  this.ws.onmessage = (message) => {
@@ -192,18 +229,15 @@ class TCAbciClient {
192
229
  }
193
230
  this.ws.onclose = (event) => {
194
231
  this.setConnected(false)
195
- if (event.code !== 1000 && this.retrieverCount <= 10) {
196
- this.retrieverCount++
197
- setTimeout(() => {
198
- this.connect().catch(() => {})
199
- }, 3000)
232
+ if (this.errorCb) {
233
+ this.errorCb(event.error)
200
234
  }
201
235
  }
202
236
  })
203
237
  }
204
238
 
205
239
  wsClient() {
206
- if (typeof window !== "undefined") {
240
+ if (typeof window !== "undefined" && typeof window.WebSocket !== "undefined") {
207
241
  return window.WebSocket
208
242
  }
209
243
  const { WebSocket } = require("ws")
@@ -233,4 +267,4 @@ class TCAbciClient {
233
267
  }
234
268
  }
235
269
 
236
- module.exports = TCAbciClient
270
+ module.exports = TCAbciClient
package/client_test.js CHANGED
@@ -42,19 +42,17 @@ describe("TCAbciClient TESTS", () => {
42
42
  unitJS.assert.equal(connected, true)
43
43
  unitJS.assert.equal(subscribed, false)
44
44
 
45
- client.Disconnect(1003)
45
+ client.Reconnect(1003)
46
+ const { connected: c2, subscribed: b2} = client.Status()
47
+ unitJS.assert.equal(c2, false)
48
+ unitJS.assert.equal(b2, false)
49
+
46
50
  setTimeout(() => {
47
- const { connected: c2, subscribed: b2} = client.Status()
48
- unitJS.assert.equal(c2, false)
49
- unitJS.assert.equal(b2, false)
50
-
51
- setTimeout(() => {
52
- const { connected, subscribed } = client.Status()
53
- unitJS.assert.equal(connected, true)
54
- unitJS.assert.equal(subscribed, false)
55
- done()
56
- }, 3100)
57
- }, 1000)
51
+ const { connected, subscribed } = client.Status()
52
+ unitJS.assert.equal(connected, true)
53
+ unitJS.assert.equal(subscribed, false)
54
+ done()
55
+ }, 3100)
58
56
  })
59
57
  .catch(err => {
60
58
  done(err)
@@ -130,6 +128,22 @@ describe("TCAbciClient TESTS", () => {
130
128
  })
131
129
  })
132
130
 
131
+ it('should return transaction summary result',(done) => {
132
+ const client = new tcAbciClient()
133
+ client.TxSummary({
134
+ recipientAddrs: ["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"],
135
+ })
136
+ .then(data => {
137
+ unitJS.value(data.last_block_height).isGreaterThan(0)
138
+ unitJS.value(data.total_count).isGreaterThan(0)
139
+ unitJS.value(data.last_transaction).isNotEmpty()
140
+ done()
141
+ })
142
+ .catch(err => {
143
+ done(err)
144
+ })
145
+ })
146
+
133
147
  it('should not broadcast transaction if type is incorrect',(done) => {
134
148
  const client = new tcAbciClient()
135
149
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "commonjs",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "axios": "^1.6.0",
28
- "ws": "^8.14.2"
28
+ "reconnecting-websocket": "^4.4.0",
29
+ "ws": "^8.16.0"
29
30
  }
30
31
  }