@tchain/tcabci-read-js-client 2.2.4 → 2.3.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 +16 -0
- package/client.js +163 -20
- package/client_test.js +30 -20
- package/errors_test.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,6 +4,11 @@ TransferChain Fastest Read Network WebSocket Client
|
|
|
4
4
|
Read Node Address: [https://read-node-01.transferchain.io](https://read-node-01.transferchain.io)
|
|
5
5
|
Read Node WebSocket Address: [wss://read-node-01.transferchain.io/ws](wss://read-node-01.transferchain.io/ws)
|
|
6
6
|
|
|
7
|
+
## Networks
|
|
8
|
+
|
|
9
|
+
- transferchain - v1
|
|
10
|
+
- medusa - v2 (new blocks)
|
|
11
|
+
|
|
7
12
|
## Installation
|
|
8
13
|
|
|
9
14
|
```shell
|
|
@@ -23,6 +28,17 @@ const client = new TCaBCIClient([
|
|
|
23
28
|
'https://read-node-01.transferchain.io',
|
|
24
29
|
'wss://read-node-01.transferchain.io/ws',
|
|
25
30
|
])
|
|
31
|
+
// OR
|
|
32
|
+
// With addresses and WebSocket library and network name, version
|
|
33
|
+
const client = new TCaBCIClient(
|
|
34
|
+
[
|
|
35
|
+
'https://read-node-01.transferchain.io',
|
|
36
|
+
'wss://read-node-01.transferchain.io/ws',
|
|
37
|
+
],
|
|
38
|
+
WebSocket,
|
|
39
|
+
'transferchain',
|
|
40
|
+
'v1',
|
|
41
|
+
)
|
|
26
42
|
|
|
27
43
|
client.Start()
|
|
28
44
|
|
package/client.js
CHANGED
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
import Message from './message.js'
|
|
21
21
|
import { toJSON } from './util.js'
|
|
22
22
|
|
|
23
|
-
|
|
24
23
|
/**
|
|
25
24
|
* @callback successCallback
|
|
26
25
|
* @param {Event} event
|
|
@@ -45,7 +44,9 @@ export default class TCaBCIClient {
|
|
|
45
44
|
subscribed = false
|
|
46
45
|
subscribedAddresses = []
|
|
47
46
|
connected = false
|
|
48
|
-
|
|
47
|
+
chainName = 'transferchain'
|
|
48
|
+
chainVersion = 'v1'
|
|
49
|
+
version = 'v2.3.0'
|
|
49
50
|
/**
|
|
50
51
|
* @type {?successCallback}
|
|
51
52
|
*/
|
|
@@ -70,8 +71,15 @@ export default class TCaBCIClient {
|
|
|
70
71
|
/**
|
|
71
72
|
* @param {Array<String>} readNodeAddresses
|
|
72
73
|
* @param {WebSocket} wsLibrary
|
|
74
|
+
* @param {?string} chainName
|
|
75
|
+
* @param {?string} chainVersion
|
|
73
76
|
*/
|
|
74
|
-
constructor(
|
|
77
|
+
constructor(
|
|
78
|
+
readNodeAddresses = [],
|
|
79
|
+
wsLibrary,
|
|
80
|
+
chainName = null,
|
|
81
|
+
chainVersion = null,
|
|
82
|
+
) {
|
|
75
83
|
if (!wsLibrary) throw INVALID_ARGUMENTS
|
|
76
84
|
this.wsLibrary = wsLibrary
|
|
77
85
|
|
|
@@ -82,6 +90,9 @@ export default class TCaBCIClient {
|
|
|
82
90
|
if (!readNodeAddresses[0].startsWith('http')) throw INVALID_ARGUMENTS
|
|
83
91
|
if (!readNodeAddresses[1].startsWith('ws')) throw INVALID_ARGUMENTS
|
|
84
92
|
}
|
|
93
|
+
|
|
94
|
+
if (chainName) this.chainName = chainName
|
|
95
|
+
if (chainVersion) this.chainVersion = chainVersion
|
|
85
96
|
}
|
|
86
97
|
|
|
87
98
|
/**
|
|
@@ -224,12 +235,17 @@ export default class TCaBCIClient {
|
|
|
224
235
|
}
|
|
225
236
|
|
|
226
237
|
/**
|
|
238
|
+
* @param {?string} chainName
|
|
239
|
+
* @param {?string} chainVersion
|
|
227
240
|
* @return {Promise<any>}
|
|
228
241
|
*/
|
|
229
|
-
LastBlock() {
|
|
230
|
-
return this.httpClient(
|
|
231
|
-
|
|
232
|
-
|
|
242
|
+
LastBlock(chainName = null, chainVersion = null) {
|
|
243
|
+
return this.httpClient(
|
|
244
|
+
`/v1/blocks?chain_name=${chainName ?? this.chainName}&chain_version=${chainVersion ?? this.chainVersion}&limit=1&offset=0`,
|
|
245
|
+
{
|
|
246
|
+
method: 'GET',
|
|
247
|
+
},
|
|
248
|
+
)
|
|
233
249
|
.then((res) => {
|
|
234
250
|
return { blocks: res.data, total_count: res.total_count }
|
|
235
251
|
})
|
|
@@ -252,10 +268,19 @@ export default class TCaBCIClient {
|
|
|
252
268
|
* @param {?Array<string>} recipientAddrs
|
|
253
269
|
* @param {?Array<string>} senderAddrs
|
|
254
270
|
* @param {?string} typ
|
|
255
|
-
* @param {?Array<string>}
|
|
271
|
+
* @param {?Array<string>} types
|
|
272
|
+
* @param {?string} chainName
|
|
273
|
+
* @param {?string} chainVersion
|
|
256
274
|
* @return {Promise<any>}
|
|
257
275
|
*/
|
|
258
|
-
TxSummary({
|
|
276
|
+
TxSummary({
|
|
277
|
+
recipientAddrs,
|
|
278
|
+
senderAddrs,
|
|
279
|
+
typ,
|
|
280
|
+
types = null,
|
|
281
|
+
chainName = null,
|
|
282
|
+
chainVersion = null,
|
|
283
|
+
}) {
|
|
259
284
|
if (!recipientAddrs && !senderAddrs) {
|
|
260
285
|
return Promise.reject(INVALID_ARGUMENTS)
|
|
261
286
|
}
|
|
@@ -263,6 +288,8 @@ export default class TCaBCIClient {
|
|
|
263
288
|
return this.httpClient('/v1/tx_summary', {
|
|
264
289
|
method: 'POST',
|
|
265
290
|
body: JSON.stringify({
|
|
291
|
+
chain_name: chainName ?? this.chainName,
|
|
292
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
266
293
|
recipient_addrs: recipientAddrs,
|
|
267
294
|
sender_addrs: senderAddrs,
|
|
268
295
|
...(types ? { types: types } : { typ: typ }),
|
|
@@ -270,6 +297,8 @@ export default class TCaBCIClient {
|
|
|
270
297
|
})
|
|
271
298
|
.then((res) => {
|
|
272
299
|
return {
|
|
300
|
+
chain_name: res.data.chain_name,
|
|
301
|
+
chain_version: res.data.chain_version,
|
|
273
302
|
first_block_height: res.data.first_block_height,
|
|
274
303
|
first_transaction: res.data.first_transaction,
|
|
275
304
|
last_block_height: res.data.last_block_height,
|
|
@@ -294,6 +323,8 @@ export default class TCaBCIClient {
|
|
|
294
323
|
* @param {number} offset
|
|
295
324
|
* @param {string} orderField
|
|
296
325
|
* @param {string} orderBy
|
|
326
|
+
* @param {?string} chainName
|
|
327
|
+
* @param {?string} chainVersion
|
|
297
328
|
* @return {Promise<any>}
|
|
298
329
|
*/
|
|
299
330
|
TxSearch({
|
|
@@ -310,10 +341,14 @@ export default class TCaBCIClient {
|
|
|
310
341
|
offset,
|
|
311
342
|
orderField,
|
|
312
343
|
orderBy,
|
|
344
|
+
chainName = null,
|
|
345
|
+
chainVersion = null,
|
|
313
346
|
}) {
|
|
314
347
|
return this.httpClient('/v1/tx_search/p', {
|
|
315
348
|
method: 'POST',
|
|
316
349
|
body: JSON.stringify({
|
|
350
|
+
chain_name: chainName ?? this.chainName,
|
|
351
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
317
352
|
height: `${heightOperator} ${height}`,
|
|
318
353
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
319
354
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
@@ -338,6 +373,8 @@ export default class TCaBCIClient {
|
|
|
338
373
|
|
|
339
374
|
Status() {
|
|
340
375
|
return {
|
|
376
|
+
chain_name: this.chainName,
|
|
377
|
+
chain_version: this.chainVersion,
|
|
341
378
|
connected: this.connected,
|
|
342
379
|
subscribed: this.subscribed,
|
|
343
380
|
}
|
|
@@ -354,7 +391,7 @@ export default class TCaBCIClient {
|
|
|
354
391
|
* @param {number} fee
|
|
355
392
|
* @return {Promise<any>}
|
|
356
393
|
*/
|
|
357
|
-
|
|
394
|
+
BroadcastCommit({
|
|
358
395
|
id,
|
|
359
396
|
version,
|
|
360
397
|
type,
|
|
@@ -364,13 +401,45 @@ export default class TCaBCIClient {
|
|
|
364
401
|
sign,
|
|
365
402
|
fee,
|
|
366
403
|
}) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
404
|
+
return this.broadcast(
|
|
405
|
+
{
|
|
406
|
+
id,
|
|
407
|
+
version,
|
|
408
|
+
type,
|
|
409
|
+
data,
|
|
410
|
+
sender_addr,
|
|
411
|
+
recipient_addr,
|
|
412
|
+
sign,
|
|
413
|
+
fee,
|
|
414
|
+
},
|
|
415
|
+
false,
|
|
416
|
+
true,
|
|
417
|
+
)
|
|
418
|
+
}
|
|
370
419
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
420
|
+
/**
|
|
421
|
+
* @param {string} id
|
|
422
|
+
* @param {number} version
|
|
423
|
+
* @param {string} type
|
|
424
|
+
* @param {string} data
|
|
425
|
+
* @param {string} sender_addr
|
|
426
|
+
* @param {string} recipient_addr
|
|
427
|
+
* @param {string} sign
|
|
428
|
+
* @param {number} fee
|
|
429
|
+
* @return {Promise<any>}
|
|
430
|
+
*/
|
|
431
|
+
BroadcastSync({
|
|
432
|
+
id,
|
|
433
|
+
version,
|
|
434
|
+
type,
|
|
435
|
+
data,
|
|
436
|
+
sender_addr,
|
|
437
|
+
recipient_addr,
|
|
438
|
+
sign,
|
|
439
|
+
fee,
|
|
440
|
+
}) {
|
|
441
|
+
return this.broadcast(
|
|
442
|
+
{
|
|
374
443
|
id,
|
|
375
444
|
version,
|
|
376
445
|
type,
|
|
@@ -379,8 +448,74 @@ export default class TCaBCIClient {
|
|
|
379
448
|
recipient_addr,
|
|
380
449
|
sign,
|
|
381
450
|
fee,
|
|
382
|
-
}
|
|
383
|
-
|
|
451
|
+
},
|
|
452
|
+
true,
|
|
453
|
+
false,
|
|
454
|
+
)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* @param {string} id
|
|
459
|
+
* @param {number} version
|
|
460
|
+
* @param {string} type
|
|
461
|
+
* @param {string} data
|
|
462
|
+
* @param {string} sender_addr
|
|
463
|
+
* @param {string} recipient_addr
|
|
464
|
+
* @param {string} sign
|
|
465
|
+
* @param {number} fee
|
|
466
|
+
* @return {Promise<any>}
|
|
467
|
+
*/
|
|
468
|
+
Broadcast({
|
|
469
|
+
id,
|
|
470
|
+
version,
|
|
471
|
+
type,
|
|
472
|
+
data,
|
|
473
|
+
sender_addr,
|
|
474
|
+
recipient_addr,
|
|
475
|
+
sign,
|
|
476
|
+
fee,
|
|
477
|
+
}) {
|
|
478
|
+
return this.broadcast(
|
|
479
|
+
{
|
|
480
|
+
id,
|
|
481
|
+
version,
|
|
482
|
+
type,
|
|
483
|
+
data,
|
|
484
|
+
sender_addr,
|
|
485
|
+
recipient_addr,
|
|
486
|
+
sign,
|
|
487
|
+
fee,
|
|
488
|
+
},
|
|
489
|
+
false,
|
|
490
|
+
false,
|
|
491
|
+
)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
broadcast(
|
|
495
|
+
{ id, version, type, data, sender_addr, recipient_addr, sign, fee },
|
|
496
|
+
sync = false,
|
|
497
|
+
commit = false,
|
|
498
|
+
) {
|
|
499
|
+
if (Object.values(TX_TYPE).indexOf(type) < 0) {
|
|
500
|
+
throw TRANSACTION_TYPE_NOT_VALID
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return this.httpClient(
|
|
504
|
+
commit ? '/v1/tx/commit' : sync ? '/v1/tx/sync' : '/v1/tx',
|
|
505
|
+
{
|
|
506
|
+
method: 'POST',
|
|
507
|
+
body: JSON.stringify({
|
|
508
|
+
id,
|
|
509
|
+
version,
|
|
510
|
+
type,
|
|
511
|
+
data,
|
|
512
|
+
sender_addr,
|
|
513
|
+
recipient_addr,
|
|
514
|
+
sign,
|
|
515
|
+
fee,
|
|
516
|
+
}),
|
|
517
|
+
},
|
|
518
|
+
)
|
|
384
519
|
.then((res) => {
|
|
385
520
|
return { data: res.data }
|
|
386
521
|
})
|
|
@@ -390,13 +525,22 @@ export default class TCaBCIClient {
|
|
|
390
525
|
/**
|
|
391
526
|
* @param {Array<string>} addresses
|
|
392
527
|
* @param {?number} maxHeight
|
|
528
|
+
* @param {?string} chainName
|
|
529
|
+
* @param {?string} chainVersion
|
|
393
530
|
* @return {Promise<*>}
|
|
394
531
|
* @constructor
|
|
395
532
|
*/
|
|
396
|
-
Bulk(
|
|
533
|
+
Bulk(
|
|
534
|
+
addresses = [],
|
|
535
|
+
maxHeight = null,
|
|
536
|
+
chainName = null,
|
|
537
|
+
chainVersion = null,
|
|
538
|
+
) {
|
|
397
539
|
return this.httpClient('/v1/bulk_tx', {
|
|
398
540
|
method: 'POST',
|
|
399
541
|
body: JSON.stringify({
|
|
542
|
+
chain_name: chainName ?? this.chainName,
|
|
543
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
400
544
|
addresses: addresses,
|
|
401
545
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
402
546
|
}),
|
|
@@ -433,7 +577,6 @@ export default class TCaBCIClient {
|
|
|
433
577
|
if (this.listenCb) this.listenCb(message)
|
|
434
578
|
}
|
|
435
579
|
|
|
436
|
-
|
|
437
580
|
/**
|
|
438
581
|
* @return {Promise<Event>}
|
|
439
582
|
*/
|
package/client_test.js
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
|
|
22
22
|
describe('TCaBCIClient', () => {
|
|
23
23
|
it('start with valid parameters', (done) => {
|
|
24
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
24
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
25
25
|
|
|
26
26
|
client
|
|
27
27
|
.Start()
|
|
@@ -38,7 +38,12 @@ describe('TCaBCIClient', () => {
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
it('start with valid parameters and read node addresses', (done) => {
|
|
41
|
-
const client = new TCaBCIClient(
|
|
41
|
+
const client = new TCaBCIClient(
|
|
42
|
+
[READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS],
|
|
43
|
+
WebSocket,
|
|
44
|
+
'transferchain',
|
|
45
|
+
'v1',
|
|
46
|
+
)
|
|
42
47
|
|
|
43
48
|
client
|
|
44
49
|
.Start()
|
|
@@ -55,7 +60,7 @@ describe('TCaBCIClient', () => {
|
|
|
55
60
|
})
|
|
56
61
|
|
|
57
62
|
it('reconnect with start and valid parameters', (done) => {
|
|
58
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
63
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
59
64
|
|
|
60
65
|
client
|
|
61
66
|
.Start()
|
|
@@ -71,13 +76,18 @@ describe('TCaBCIClient', () => {
|
|
|
71
76
|
unitJS.assert.equal(c2, false)
|
|
72
77
|
unitJS.assert.equal(b2, false)
|
|
73
78
|
|
|
74
|
-
setTimeout(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
setTimeout(
|
|
80
|
+
(client, done) => {
|
|
81
|
+
const { connected, subscribed } = client.Status()
|
|
82
|
+
|
|
83
|
+
unitJS.assert.equal(connected, true)
|
|
84
|
+
unitJS.assert.equal(subscribed, false)
|
|
85
|
+
done()
|
|
86
|
+
},
|
|
87
|
+
3100,
|
|
88
|
+
client,
|
|
89
|
+
done,
|
|
90
|
+
)
|
|
81
91
|
})
|
|
82
92
|
.catch((err) => {
|
|
83
93
|
done(err)
|
|
@@ -85,7 +95,7 @@ describe('TCaBCIClient', () => {
|
|
|
85
95
|
})
|
|
86
96
|
|
|
87
97
|
it('subscribe with valid parameters', (done) => {
|
|
88
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
98
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
89
99
|
|
|
90
100
|
client
|
|
91
101
|
.Start()
|
|
@@ -110,7 +120,7 @@ describe('TCaBCIClient', () => {
|
|
|
110
120
|
})
|
|
111
121
|
|
|
112
122
|
it('should error subscribe with invalid tx type parameter', (done) => {
|
|
113
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
123
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
114
124
|
|
|
115
125
|
client
|
|
116
126
|
.Start()
|
|
@@ -129,7 +139,7 @@ describe('TCaBCIClient', () => {
|
|
|
129
139
|
})
|
|
130
140
|
|
|
131
141
|
it('unsubscribe with valid parameters', (done) => {
|
|
132
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
142
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
133
143
|
|
|
134
144
|
client
|
|
135
145
|
.Start()
|
|
@@ -158,7 +168,7 @@ describe('TCaBCIClient', () => {
|
|
|
158
168
|
})
|
|
159
169
|
|
|
160
170
|
it('last block', (done) => {
|
|
161
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
171
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
162
172
|
|
|
163
173
|
client
|
|
164
174
|
.LastBlock()
|
|
@@ -173,7 +183,7 @@ describe('TCaBCIClient', () => {
|
|
|
173
183
|
})
|
|
174
184
|
|
|
175
185
|
it('transaction search result', (done) => {
|
|
176
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
186
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
177
187
|
|
|
178
188
|
client
|
|
179
189
|
.TxSearch({
|
|
@@ -197,7 +207,7 @@ describe('TCaBCIClient', () => {
|
|
|
197
207
|
})
|
|
198
208
|
//
|
|
199
209
|
it('transaction summary result', (done) => {
|
|
200
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
210
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
201
211
|
|
|
202
212
|
client
|
|
203
213
|
.TxSummary({
|
|
@@ -220,7 +230,7 @@ describe('TCaBCIClient', () => {
|
|
|
220
230
|
})
|
|
221
231
|
|
|
222
232
|
it('should error not broadcast transaction if type is incorrect', (done) => {
|
|
223
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
233
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
224
234
|
|
|
225
235
|
try {
|
|
226
236
|
client.Broadcast({
|
|
@@ -238,7 +248,7 @@ describe('TCaBCIClient', () => {
|
|
|
238
248
|
})
|
|
239
249
|
|
|
240
250
|
it('should error not broadcast transaction', (done) => {
|
|
241
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
251
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
242
252
|
|
|
243
253
|
client
|
|
244
254
|
.Broadcast({
|
|
@@ -264,7 +274,7 @@ describe('TCaBCIClient', () => {
|
|
|
264
274
|
})
|
|
265
275
|
|
|
266
276
|
it('should error in bulk tx if addresses count is zero', (done) => {
|
|
267
|
-
const client = new TCaBCIClient([], WebSocket)
|
|
277
|
+
const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
|
|
268
278
|
|
|
269
279
|
client
|
|
270
280
|
.Bulk([])
|
|
@@ -286,7 +296,7 @@ describe('TCaBCIClient', () => {
|
|
|
286
296
|
)
|
|
287
297
|
}
|
|
288
298
|
|
|
289
|
-
const client = new TCaBCIClient(null, WebSocket)
|
|
299
|
+
const client = new TCaBCIClient(null, WebSocket, 'transferchain', 'v1')
|
|
290
300
|
|
|
291
301
|
client
|
|
292
302
|
.Bulk(addresses)
|
package/errors_test.js
CHANGED