@tchain/tcabci-read-js-client 2.2.2 → 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 +175 -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,26 +235,52 @@ 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
|
})
|
|
236
252
|
.catch((e) => this.handleRestError(e, BLOCK_NOT_FOUND))
|
|
237
253
|
}
|
|
238
254
|
|
|
255
|
+
Tx(id) {
|
|
256
|
+
if (!id || typeof id !== 'string') {
|
|
257
|
+
return Promise.reject(INVALID_ARGUMENTS)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return this.httpClient(`/v1/tx/${id}`, { method: 'GET' })
|
|
261
|
+
.then((res) => {
|
|
262
|
+
return { tx: res.data }
|
|
263
|
+
})
|
|
264
|
+
.catch((e) => this.handleRestError(e))
|
|
265
|
+
}
|
|
266
|
+
|
|
239
267
|
/**
|
|
240
268
|
* @param {?Array<string>} recipientAddrs
|
|
241
269
|
* @param {?Array<string>} senderAddrs
|
|
242
270
|
* @param {?string} typ
|
|
243
|
-
* @param {?Array<string>}
|
|
271
|
+
* @param {?Array<string>} types
|
|
272
|
+
* @param {?string} chainName
|
|
273
|
+
* @param {?string} chainVersion
|
|
244
274
|
* @return {Promise<any>}
|
|
245
275
|
*/
|
|
246
|
-
TxSummary({
|
|
276
|
+
TxSummary({
|
|
277
|
+
recipientAddrs,
|
|
278
|
+
senderAddrs,
|
|
279
|
+
typ,
|
|
280
|
+
types = null,
|
|
281
|
+
chainName = null,
|
|
282
|
+
chainVersion = null,
|
|
283
|
+
}) {
|
|
247
284
|
if (!recipientAddrs && !senderAddrs) {
|
|
248
285
|
return Promise.reject(INVALID_ARGUMENTS)
|
|
249
286
|
}
|
|
@@ -251,6 +288,8 @@ export default class TCaBCIClient {
|
|
|
251
288
|
return this.httpClient('/v1/tx_summary', {
|
|
252
289
|
method: 'POST',
|
|
253
290
|
body: JSON.stringify({
|
|
291
|
+
chain_name: chainName ?? this.chainName,
|
|
292
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
254
293
|
recipient_addrs: recipientAddrs,
|
|
255
294
|
sender_addrs: senderAddrs,
|
|
256
295
|
...(types ? { types: types } : { typ: typ }),
|
|
@@ -258,6 +297,8 @@ export default class TCaBCIClient {
|
|
|
258
297
|
})
|
|
259
298
|
.then((res) => {
|
|
260
299
|
return {
|
|
300
|
+
chain_name: res.data.chain_name,
|
|
301
|
+
chain_version: res.data.chain_version,
|
|
261
302
|
first_block_height: res.data.first_block_height,
|
|
262
303
|
first_transaction: res.data.first_transaction,
|
|
263
304
|
last_block_height: res.data.last_block_height,
|
|
@@ -282,6 +323,8 @@ export default class TCaBCIClient {
|
|
|
282
323
|
* @param {number} offset
|
|
283
324
|
* @param {string} orderField
|
|
284
325
|
* @param {string} orderBy
|
|
326
|
+
* @param {?string} chainName
|
|
327
|
+
* @param {?string} chainVersion
|
|
285
328
|
* @return {Promise<any>}
|
|
286
329
|
*/
|
|
287
330
|
TxSearch({
|
|
@@ -298,10 +341,14 @@ export default class TCaBCIClient {
|
|
|
298
341
|
offset,
|
|
299
342
|
orderField,
|
|
300
343
|
orderBy,
|
|
344
|
+
chainName = null,
|
|
345
|
+
chainVersion = null,
|
|
301
346
|
}) {
|
|
302
347
|
return this.httpClient('/v1/tx_search/p', {
|
|
303
348
|
method: 'POST',
|
|
304
349
|
body: JSON.stringify({
|
|
350
|
+
chain_name: chainName ?? this.chainName,
|
|
351
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
305
352
|
height: `${heightOperator} ${height}`,
|
|
306
353
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
307
354
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
@@ -326,6 +373,8 @@ export default class TCaBCIClient {
|
|
|
326
373
|
|
|
327
374
|
Status() {
|
|
328
375
|
return {
|
|
376
|
+
chain_name: this.chainName,
|
|
377
|
+
chain_version: this.chainVersion,
|
|
329
378
|
connected: this.connected,
|
|
330
379
|
subscribed: this.subscribed,
|
|
331
380
|
}
|
|
@@ -342,7 +391,7 @@ export default class TCaBCIClient {
|
|
|
342
391
|
* @param {number} fee
|
|
343
392
|
* @return {Promise<any>}
|
|
344
393
|
*/
|
|
345
|
-
|
|
394
|
+
BroadcastCommit({
|
|
346
395
|
id,
|
|
347
396
|
version,
|
|
348
397
|
type,
|
|
@@ -352,13 +401,45 @@ export default class TCaBCIClient {
|
|
|
352
401
|
sign,
|
|
353
402
|
fee,
|
|
354
403
|
}) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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
|
+
}
|
|
358
419
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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
|
+
{
|
|
362
443
|
id,
|
|
363
444
|
version,
|
|
364
445
|
type,
|
|
@@ -367,8 +448,74 @@ export default class TCaBCIClient {
|
|
|
367
448
|
recipient_addr,
|
|
368
449
|
sign,
|
|
369
450
|
fee,
|
|
370
|
-
}
|
|
371
|
-
|
|
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
|
+
)
|
|
372
519
|
.then((res) => {
|
|
373
520
|
return { data: res.data }
|
|
374
521
|
})
|
|
@@ -378,13 +525,22 @@ export default class TCaBCIClient {
|
|
|
378
525
|
/**
|
|
379
526
|
* @param {Array<string>} addresses
|
|
380
527
|
* @param {?number} maxHeight
|
|
528
|
+
* @param {?string} chainName
|
|
529
|
+
* @param {?string} chainVersion
|
|
381
530
|
* @return {Promise<*>}
|
|
382
531
|
* @constructor
|
|
383
532
|
*/
|
|
384
|
-
Bulk(
|
|
533
|
+
Bulk(
|
|
534
|
+
addresses = [],
|
|
535
|
+
maxHeight = null,
|
|
536
|
+
chainName = null,
|
|
537
|
+
chainVersion = null,
|
|
538
|
+
) {
|
|
385
539
|
return this.httpClient('/v1/bulk_tx', {
|
|
386
540
|
method: 'POST',
|
|
387
541
|
body: JSON.stringify({
|
|
542
|
+
chain_name: chainName ?? this.chainName,
|
|
543
|
+
chain_version: chainVersion ?? this.chainVersion,
|
|
388
544
|
addresses: addresses,
|
|
389
545
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
390
546
|
}),
|
|
@@ -421,7 +577,6 @@ export default class TCaBCIClient {
|
|
|
421
577
|
if (this.listenCb) this.listenCb(message)
|
|
422
578
|
}
|
|
423
579
|
|
|
424
|
-
|
|
425
580
|
/**
|
|
426
581
|
* @return {Promise<Event>}
|
|
427
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