@tchain/tcabci-read-js-client 2.1.4 → 2.2.0

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/README.md CHANGED
@@ -15,11 +15,11 @@ $ npm i @tchain/tcabci-read-js-client
15
15
  **Subscribe, Listen and Unsubscribe Example**
16
16
 
17
17
  ```js
18
- import TCAbciClient from '@tchain/tcabci-read-js-client'
18
+ import TCaBCIClient from '@tchain/tcabci-read-js-client'
19
19
 
20
- const client = new tcAbciClient()
20
+ const client = new TCaBCIClient()
21
21
  // OR
22
- const client = new tcAbciClient([
22
+ const client = new TCaBCIClient([
23
23
  'https://read-node-01.transferchain.io',
24
24
  'wss://read-node-01.transferchain.io/ws',
25
25
  ])
@@ -32,6 +32,14 @@ client.SetListenCallback(() => {
32
32
  // If a transaction has been sent to your addresses, the callback you set here will be called.
33
33
  })
34
34
 
35
+ client.SetErrorCallback(() => {
36
+ // If network errors will be called.
37
+ })
38
+
39
+ client.SetCloseCallback(() => {
40
+ // If server side close will be called.
41
+ })
42
+
35
43
  client.Unsubscribe()
36
44
  client.Stop()
37
45
  ```
package/client.js CHANGED
@@ -20,12 +20,47 @@ import {
20
20
  import Message from './message.js'
21
21
  import { toJSON } from './util.js'
22
22
 
23
- export default class TCAbciClient {
23
+
24
+ /**
25
+ * @callback successCallback
26
+ * @param {Event} event
27
+ * @return void
28
+ */
29
+ /**
30
+ * @callback errorCallback
31
+ * @param {Event} event
32
+ * @return void
33
+ */
34
+ /**
35
+ * @callback closeCallback
36
+ * @param {Event} event
37
+ * @return void
38
+ */
39
+ /**
40
+ * @callback listenCallback
41
+ * @param {*} message
42
+ * @return void
43
+ */
44
+ export default class TCaBCIClient {
24
45
  subscribed = false
25
46
  subscribedAddresses = []
26
47
  connected = false
27
- version = 'v2.1.4'
48
+ version = 'v2.2.0'
49
+ /**
50
+ * @type {?successCallback}
51
+ */
52
+ successCb = null
53
+ /**
54
+ * @type {?errorCallback}
55
+ */
28
56
  errorCb = null
57
+ /**
58
+ * @type {?closeCallback}
59
+ */
60
+ closeCb = null
61
+ /**
62
+ * @type {?listenCallback}
63
+ */
29
64
  listenCb = null
30
65
  wsLibrary = null
31
66
  ws = null
@@ -75,12 +110,44 @@ export default class TCAbciClient {
75
110
  return this.ws
76
111
  }
77
112
 
113
+ /**
114
+ * @param {successCallback} cb
115
+ * @constructor
116
+ */
117
+ SetSuccessCallback(cb) {
118
+ this.successCb = cb
119
+
120
+ return this
121
+ }
122
+
123
+ /**
124
+ * @param {errorCallback} cb
125
+ * @constructor
126
+ */
78
127
  SetErrorCallback(cb) {
79
128
  this.errorCb = cb
129
+
130
+ return this
80
131
  }
81
132
 
133
+ /**
134
+ * @param {closeCallback} cb
135
+ * @constructor
136
+ */
137
+ SetCloseCallback(cb) {
138
+ this.closeCb = cb
139
+
140
+ return this
141
+ }
142
+
143
+ /**
144
+ * @param {listenCallback} cb
145
+ * @constructor
146
+ */
82
147
  SetListenCallback(cb) {
83
148
  this.listenCb = cb
149
+
150
+ return this
84
151
  }
85
152
 
86
153
  async Start() {
@@ -94,6 +161,8 @@ export default class TCAbciClient {
94
161
  this.Disconnect(1000)
95
162
  this.setConnected(false)
96
163
  this.setSubscribed(false)
164
+
165
+ return this
97
166
  }
98
167
 
99
168
  Subscribe(addresses = [], txTypes = []) {
@@ -332,30 +401,55 @@ export default class TCAbciClient {
332
401
  if (this.getConnected()) {
333
402
  this.ws.reconnect(code)
334
403
  }
404
+
405
+ return this
406
+ }
407
+
408
+ callSuccessCallback(event) {
409
+ if (this.successCb) this.successCb(event)
410
+ }
411
+
412
+ callErrorCallback(event) {
413
+ if (this.errorCb) this.errorCb(event)
414
+ }
415
+
416
+ callCloseCallback(event) {
417
+ if (this.closeCb) this.closeCb(event)
418
+ }
419
+
420
+ callListenCallback(message) {
421
+ if (this.listenCb) this.listenCb(message)
335
422
  }
336
423
 
424
+
425
+ /**
426
+ * @return {Promise<Event>}
427
+ */
337
428
  async connect() {
338
- return new Promise((resolve, reject) => {
339
- if (this.getConnected()) {
340
- reject(ALREADY_CONNECTED)
341
- }
429
+ if (this.getConnected()) {
430
+ return Promise.reject(ALREADY_CONNECTED)
431
+ }
342
432
 
343
- const options = {
344
- WebSocket: this.wsLibrary,
345
- connectionTimeout: 1000,
346
- maxRetries: 10,
347
- }
433
+ const options = {
434
+ WebSocket: this.wsLibrary,
435
+ connectionTimeout: 1000,
436
+ maxRetries: 10,
437
+ }
348
438
 
349
- this.ws = new ReconnectingWebSocket(this.readNodeWSAddress, [], options)
439
+ this.ws = new ReconnectingWebSocket(this.readNodeWSAddress, [], options)
350
440
 
441
+ return new Promise((resolve, reject) => {
351
442
  this.ws.onerror = (event) => {
352
443
  this.setConnected(false)
353
- if (this.errorCb) this.errorCb(event.error ?? event)
444
+ this.setSubscribed(false)
445
+ this.callErrorCallback(event)
354
446
 
355
- reject(event.error ?? event)
447
+ reject(event)
356
448
  }
357
449
  this.ws.onopen = (event) => {
358
450
  this.setConnected(true)
451
+ this.callSuccessCallback(event)
452
+
359
453
  resolve(event)
360
454
  }
361
455
  this.ws.onmessage = (message) => {
@@ -363,14 +457,14 @@ export default class TCAbciClient {
363
457
  return { status: message.data }
364
458
  }
365
459
 
366
- if (this.listenCb) this.listenCb(toJSON(message.data))
460
+ this.callListenCallback(toJSON(message.data))
367
461
  }
368
462
  this.ws.onclose = (event) => {
369
463
  this.setConnected(false)
464
+ this.setSubscribed(false)
465
+ this.callCloseCallback(event)
370
466
 
371
- if (this.errorCb) this.errorCb(event.error ?? event)
372
-
373
- reject(event.error ?? event)
467
+ resolve(event)
374
468
  }
375
469
  })
376
470
  }
package/client_test.js CHANGED
@@ -1,4 +1,4 @@
1
- import TCAbciClient from './client.js'
1
+ import TCaBCIClient from './client.js'
2
2
  import { WebSocket } from 'ws'
3
3
  import unitJS from 'unit.js'
4
4
  import {
@@ -19,9 +19,9 @@ import {
19
19
  //
20
20
  // const { randomString } = require('./util')
21
21
 
22
- describe('TCAbciClient TESTS', () => {
23
- it('should start with valid parameters', (done) => {
24
- const client = new TCAbciClient([], WebSocket)
22
+ describe('TCaBCIClient', () => {
23
+ it('start with valid parameters', (done) => {
24
+ const client = new TCaBCIClient([], WebSocket)
25
25
 
26
26
  client
27
27
  .Start()
@@ -37,8 +37,8 @@ describe('TCAbciClient TESTS', () => {
37
37
  })
38
38
  })
39
39
 
40
- it('should start with valid parameters and read node addresses', (done) => {
41
- const client = new TCAbciClient([READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS], WebSocket)
40
+ it('start with valid parameters and read node addresses', (done) => {
41
+ const client = new TCaBCIClient([READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS], WebSocket)
42
42
 
43
43
  client
44
44
  .Start()
@@ -54,8 +54,8 @@ describe('TCAbciClient TESTS', () => {
54
54
  })
55
55
  })
56
56
 
57
- it('should reconnect with start and valid parameters', (done) => {
58
- const client = new TCAbciClient([], WebSocket)
57
+ it('reconnect with start and valid parameters', (done) => {
58
+ const client = new TCaBCIClient([], WebSocket)
59
59
 
60
60
  client
61
61
  .Start()
@@ -71,21 +71,21 @@ describe('TCAbciClient TESTS', () => {
71
71
  unitJS.assert.equal(c2, false)
72
72
  unitJS.assert.equal(b2, false)
73
73
 
74
- setTimeout(() => {
74
+ setTimeout((client, done) => {
75
75
  const { connected, subscribed } = client.Status()
76
76
 
77
77
  unitJS.assert.equal(connected, true)
78
78
  unitJS.assert.equal(subscribed, false)
79
79
  done()
80
- }, 3100)
80
+ }, 3100, client, done)
81
81
  })
82
82
  .catch((err) => {
83
83
  done(err)
84
84
  })
85
85
  })
86
86
 
87
- it('should subscribe with valid parameters', (done) => {
88
- const client = new TCAbciClient([], WebSocket)
87
+ it('subscribe with valid parameters', (done) => {
88
+ const client = new TCaBCIClient([], WebSocket)
89
89
 
90
90
  client
91
91
  .Start()
@@ -96,10 +96,12 @@ describe('TCAbciClient TESTS', () => {
96
96
  ],
97
97
  [TX_TYPE.TX_TYPE_STORAGE],
98
98
  )
99
+
99
100
  const { connected, subscribed } = client.Status()
100
101
 
101
102
  unitJS.assert.equal(connected, true)
102
103
  unitJS.assert.equal(subscribed, true)
104
+
103
105
  done()
104
106
  })
105
107
  .catch((err) => {
@@ -108,7 +110,7 @@ describe('TCAbciClient TESTS', () => {
108
110
  })
109
111
 
110
112
  it('should error subscribe with invalid tx type parameter', (done) => {
111
- const client = new TCAbciClient([], WebSocket)
113
+ const client = new TCaBCIClient([], WebSocket)
112
114
 
113
115
  client
114
116
  .Start()
@@ -126,8 +128,8 @@ describe('TCAbciClient TESTS', () => {
126
128
  })
127
129
  })
128
130
 
129
- it('should unsubscribe with valid parameters', (done) => {
130
- const client = new TCAbciClient([], WebSocket)
131
+ it('unsubscribe with valid parameters', (done) => {
132
+ const client = new TCaBCIClient([], WebSocket)
131
133
 
132
134
  client
133
135
  .Start()
@@ -155,8 +157,8 @@ describe('TCAbciClient TESTS', () => {
155
157
  })
156
158
  })
157
159
 
158
- it('should return last block', (done) => {
159
- const client = new TCAbciClient([], WebSocket)
160
+ it('last block', (done) => {
161
+ const client = new TCaBCIClient([], WebSocket)
160
162
 
161
163
  client
162
164
  .LastBlock()
@@ -170,8 +172,8 @@ describe('TCAbciClient TESTS', () => {
170
172
  })
171
173
  })
172
174
 
173
- it('should return transaction search result', (done) => {
174
- const client = new TCAbciClient([], WebSocket)
175
+ it('transaction search result', (done) => {
176
+ const client = new TCaBCIClient([], WebSocket)
175
177
 
176
178
  client
177
179
  .TxSearch({
@@ -194,8 +196,8 @@ describe('TCAbciClient TESTS', () => {
194
196
  })
195
197
  })
196
198
  //
197
- it('should return transaction summary result', (done) => {
198
- const client = new TCAbciClient([], WebSocket)
199
+ it('transaction summary result', (done) => {
200
+ const client = new TCaBCIClient([], WebSocket)
199
201
 
200
202
  client
201
203
  .TxSummary({
@@ -209,6 +211,7 @@ describe('TCAbciClient TESTS', () => {
209
211
  unitJS.value(data.last_block_height).isGreaterThan(0)
210
212
  unitJS.value(data.total_count).isGreaterThan(0)
211
213
  unitJS.value(data.last_transaction).isNotEmpty()
214
+
212
215
  done()
213
216
  })
214
217
  .catch((err) => {
@@ -216,8 +219,8 @@ describe('TCAbciClient TESTS', () => {
216
219
  })
217
220
  })
218
221
 
219
- it('should not broadcast transaction if type is incorrect', (done) => {
220
- const client = new TCAbciClient([], WebSocket)
222
+ it('should error not broadcast transaction if type is incorrect', (done) => {
223
+ const client = new TCaBCIClient([], WebSocket)
221
224
 
222
225
  try {
223
226
  client.Broadcast({
@@ -234,8 +237,8 @@ describe('TCAbciClient TESTS', () => {
234
237
  }
235
238
  })
236
239
 
237
- it('should not broadcast transaction', (done) => {
238
- const client = new TCAbciClient([], WebSocket)
240
+ it('should error not broadcast transaction', (done) => {
241
+ const client = new TCaBCIClient([], WebSocket)
239
242
 
240
243
  client
241
244
  .Broadcast({
@@ -261,7 +264,7 @@ describe('TCAbciClient TESTS', () => {
261
264
  })
262
265
 
263
266
  it('should error in bulk tx if addresses count is zero', (done) => {
264
- const client = new TCAbciClient([], WebSocket)
267
+ const client = new TCaBCIClient([], WebSocket)
265
268
 
266
269
  client
267
270
  .Bulk([])
@@ -277,13 +280,13 @@ describe('TCAbciClient TESTS', () => {
277
280
  it('should error in bulk tx if addresses count is greater than 251', (done) => {
278
281
  const addresses = []
279
282
 
280
- for (let i = 0; i < 252; i++) {
283
+ for (let i = 0; i < 51; i++) {
281
284
  addresses.push(
282
285
  '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
283
286
  )
284
287
  }
285
288
 
286
- const client = new TCAbciClient(null, WebSocket)
289
+ const client = new TCaBCIClient(null, WebSocket)
287
290
 
288
291
  client
289
292
  .Bulk(addresses)
package/constants.js CHANGED
@@ -18,6 +18,7 @@ export const MESSAGE_TYPE = {
18
18
  TX_TYPE_TRANSFER_CANCEL: 'transfer_Cancel',
19
19
  TX_TYPE_TRANSFER_SENT: 'transfer_sent',
20
20
  TX_TYPE_TRANSFER_RECEIVE_DELETE: 'transfer_receive_delete',
21
+ TX_TYPE_TRANSFER_INFO: 'transfer_info',
21
22
  TX_TYPE_STORAGE: 'storage',
22
23
  TX_TYPE_STORAGE_DELETE: 'storage_delete',
23
24
  TX_TYPE_BACKUP: 'backup',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "2.1.4",
3
+ "version": "2.2.0",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
@@ -23,15 +23,16 @@
23
23
  },
24
24
  "homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
25
25
  "devDependencies": {
26
- "mocha": "^10.7.3",
27
- "nyc": "^17.1.0",
28
- "unit.js": "^2.1.1",
29
- "eslint": "^9.11.1",
26
+ "eslint": "^9.13.0",
30
27
  "eslint-config-prettier": "^9.1.0",
31
- "eslint-plugin-import": "^2.30.0",
28
+ "eslint-plugin-import": "^2.31.0",
32
29
  "eslint-plugin-node": "^11.1.0",
33
30
  "eslint-plugin-promise": "^7.1.0",
31
+ "express": "^4.21.1",
32
+ "mocha": "^10.7.3",
33
+ "nyc": "^17.1.0",
34
34
  "prettier": "^3.3.3",
35
+ "unit.js": "^2.1.1",
35
36
  "ws": "^8.18.0"
36
37
  },
37
38
  "dependencies": {