@tchain/tcabci-read-js-client 2.6.2 → 2.6.4

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/callback.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { Transaction } from './transaction'
2
+
1
3
  export declare type SuccessCallback = (event: Event) => void
2
4
  export declare type ErrorCallback = (event: Event) => void
3
5
  export declare type CloseCallback = (event: Event) => void
4
- export declare type ListenCallback = (event: Event) => void
6
+ export declare type ListenCallback = (tx: Transaction) => void
package/client.js CHANGED
@@ -47,7 +47,7 @@ export default class TCaBCIClient {
47
47
  _connected = false
48
48
  _chainName = 'transferchain'
49
49
  _chainVersion = 'v1'
50
- _version = `v2.6.2`
50
+ _version = `v2.6.4`
51
51
  /**
52
52
  * @type {?SuccessCallback}
53
53
  */
@@ -383,20 +383,29 @@ export default class TCaBCIClient {
383
383
  }),
384
384
  })
385
385
  .then((res) => {
386
- const { transaction, error } = Transaction.FromObject(
387
- res.data.first_transaction,
388
- )
389
- if (error) return Promise.reject(error)
386
+ let firstTransaction, lastTransaction
390
387
 
391
- const { transaction: lastTransaction, error: errorTwo } =
392
- Transaction.FromObject(res.data.last_transaction)
393
- if (errorTwo) return Promise.reject(errorTwo)
388
+ if (res.data.first_transaction) {
389
+ const { transaction, error } = Transaction.FromObject(
390
+ res.data.first_transaction,
391
+ )
392
+ if (error) return Promise.reject(error)
393
+ firstTransaction = transaction
394
+ }
395
+
396
+ if (res.data.last_transaction) {
397
+ const { transaction, error: errorTwo } = Transaction.FromObject(
398
+ res.data.last_transaction,
399
+ )
400
+ if (errorTwo) return Promise.reject(errorTwo)
401
+ lastTransaction = transaction
402
+ }
394
403
 
395
404
  return {
396
405
  chain_name: res.data.chain_name,
397
406
  chain_version: res.data.chain_version,
398
407
  first_block_height: res.data.first_block_height,
399
- first_transaction: transaction,
408
+ first_transaction: firstTransaction,
400
409
  last_block_height: res.data.last_block_height,
401
410
  last_transaction: lastTransaction,
402
411
  total_count: res.total_count,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
package/transaction.d.ts CHANGED
@@ -69,7 +69,10 @@ export declare class Transaction {
69
69
  inserted_at,
70
70
  additionalData,
71
71
  cipherData,
72
+ chainName,
73
+ chainVersion,
72
74
  }?: Record<string, any>)
75
+ get Order(): number
73
76
  get ID(): string
74
77
  get Height(): number
75
78
  get Version(): number
@@ -83,10 +86,13 @@ export declare class Transaction {
83
86
  get Fee(): number
84
87
  get Hash(): string
85
88
  get InsertedAt(): Date
89
+ get ChainName(): string
90
+ get ChainVersion(): string
86
91
  static FromJSON(value: string): { transaction?: Transaction; error?: Error }
87
92
  static FromObject(obj: Record<string, any>): {
88
93
  transaction?: Transaction
89
94
  error?: Error
90
95
  }
91
96
  ToJSON(): string
97
+ ToObject(): Record<string, any>
92
98
  }
package/transaction.js CHANGED
@@ -121,6 +121,8 @@ export class Transaction {
121
121
  _fee
122
122
  _hash
123
123
  _inserted_at
124
+ _chainName
125
+ _chainVersion
124
126
 
125
127
  /**
126
128
  * @param {string} id
@@ -136,6 +138,8 @@ export class Transaction {
136
138
  * @param {Date} inserted_at
137
139
  * @param {?string} additionalData
138
140
  * @param {?string} cipherData
141
+ * @param {?string} chainName
142
+ * @param {?string} chainVersion
139
143
  * @throws Error
140
144
  */
141
145
  constructor({
@@ -152,6 +156,8 @@ export class Transaction {
152
156
  inserted_at,
153
157
  additionalData = null,
154
158
  cipherData = null,
159
+ chainName = null,
160
+ chainVersion = null,
155
161
  } = {}) {
156
162
  this._id = id
157
163
  this._height = height
@@ -166,6 +172,12 @@ export class Transaction {
166
172
  this._inserted_at = inserted_at
167
173
  if (additionalData) this._additionalData = additionalData
168
174
  if (cipherData) this._cipherData = cipherData
175
+ if (chainName) this._chainName = chainName
176
+ if (chainVersion) this._chainVersion = chainVersion
177
+ }
178
+
179
+ get Order() {
180
+ return this._order
169
181
  }
170
182
 
171
183
  get ID() {
@@ -220,25 +232,48 @@ export class Transaction {
220
232
  return this._inserted_at
221
233
  }
222
234
 
235
+ get ChainName() {
236
+ return this._chainName
237
+ }
238
+
239
+ get ChainVersion() {
240
+ return this._chainVersion
241
+ }
242
+
223
243
  static FromJSON(value) {
244
+ if (typeof value !== 'string')
245
+ return {
246
+ transaction: null,
247
+ error: new Error('invalid json payload'),
248
+ }
249
+
224
250
  try {
225
251
  const parsed = JSON.parse(value),
226
252
  tx = new Transaction()
227
253
 
228
- if (parsed.order) tx._order = parsed.order
254
+ if (typeof parsed.order !== 'undefined') tx._order = parsed.order
229
255
  tx._id = parsed.id
230
- if (parsed.identifier) tx._identifier = parsed.identifier
256
+ tx._typ = parsed.typ
257
+ if (typeof parsed.identifier !== 'undefined')
258
+ tx._identifier = parsed.identifier
231
259
  tx._height = parsed.height
232
260
  tx._version = parsed.version
233
261
  tx._sender_addr = parsed.sender_addr
234
262
  tx._recipient_addr = parsed.recipient_addr
235
263
  tx._data = parsed.data
236
- if (parsed.additional_data) tx._additionalData = parsed.additional_data
237
- if (parsed.cipher_data) tx._cipherData = parsed.cipher_data
264
+ if (typeof parsed.additional_data !== 'undefined')
265
+ tx._additionalData = parsed.additional_data
266
+ if (typeof parsed.cipher_data !== 'undefined')
267
+ tx._cipherData = parsed.cipher_data
238
268
  tx._sign = parsed.sign
239
269
  tx._fee = parsed.fee
240
270
  tx._hash = parsed.hash
241
- tx._inserted_at = new Date(parsed.inserted_at)
271
+ if (typeof parsed.inserted_at !== 'undefined')
272
+ tx._inserted_at = new Date(parsed.inserted_at)
273
+ if (typeof parsed.chain_name !== 'undefined')
274
+ this._chainName = parsed.chain_name
275
+ if (typeof parsed.chain_version !== 'undefined')
276
+ this._chainVersion = parsed.chain_version
242
277
 
243
278
  return { transaction: tx, error: null }
244
279
  } catch (e) {
@@ -247,23 +282,37 @@ export class Transaction {
247
282
  }
248
283
 
249
284
  static FromObject(obj) {
285
+ if (!obj)
286
+ return {
287
+ transaction: null,
288
+ error: new Error('invalid obj payload'),
289
+ }
290
+
250
291
  try {
251
292
  const tx = new Transaction()
252
293
 
253
- if (obj.order) tx._order = obj.order
294
+ if (typeof obj.order !== 'undefined') tx._order = obj.order
254
295
  tx._id = obj.id
255
- if (obj.identifier) tx._identifier = obj.identifier
296
+ tx._typ = obj.typ
297
+ if (typeof obj.identifier !== 'undefined') tx._identifier = obj.identifier
256
298
  tx._height = obj.height
257
299
  tx._version = obj.version
258
300
  tx._sender_addr = obj.sender_addr
259
301
  tx._recipient_addr = obj.recipient_addr
260
302
  tx._data = obj.data
261
- if (obj.additional_data) tx._additionalData = obj.additional_data
262
- if (obj.cipher_data) tx._cipherData = obj.cipher_data
303
+ if (typeof obj.additional_data !== 'undefined')
304
+ tx._additionalData = obj.additional_data
305
+ if (typeof obj.cipher_data !== 'undefined')
306
+ tx._cipherData = obj.cipher_data
263
307
  tx._sign = obj.sign
264
308
  tx._fee = obj.fee
265
309
  tx._hash = obj.hash
266
- tx._inserted_at = new Date(obj.inserted_at)
310
+ if (typeof obj.inserted_at !== 'undefined')
311
+ tx._inserted_at = new Date(obj.inserted_at)
312
+ if (typeof obj.chain_name !== 'undefined')
313
+ this._chainName = obj.chain_name
314
+ if (typeof obj.chain_version !== 'undefined')
315
+ this._chainVersion = obj.chain_version
267
316
 
268
317
  return { transaction: tx, error: null }
269
318
  } catch (e) {
@@ -276,7 +325,7 @@ export class Transaction {
276
325
  * @throws Error
277
326
  */
278
327
  ToJSON() {
279
- this._validate()
328
+ this.Validate()
280
329
 
281
330
  return JSON.stringify({
282
331
  id: this._id,
@@ -289,14 +338,48 @@ export class Transaction {
289
338
  sign: this._sign,
290
339
  fee: this._fee,
291
340
  hash: this._hash,
292
- inserted_at: this._inserted_at.toISOString(),
341
+ ...(this._inserted_at
342
+ ? { inserted_at: this._inserted_at.toISOString() }
343
+ : {}),
293
344
  ...(this._additionalData
294
345
  ? { additional_data: this._additionalData }
295
346
  : {}),
296
347
  ...(this._cipherData ? { cipher_data: this._cipherData } : {}),
348
+ ...(this._chainName ? { chain_name: this._chainName } : {}),
349
+ ...(this._chainVersion ? { chain_version: this._chainVersion } : {}),
297
350
  })
298
351
  }
299
352
 
353
+ /**
354
+ * @return {Object<string, *>}
355
+ * @throws Error
356
+ */
357
+ ToObject() {
358
+ this.Validate()
359
+
360
+ return {
361
+ id: this._id,
362
+ height: this._height,
363
+ version: this._version,
364
+ typ: this._typ,
365
+ sender_addr: this._sender_addr,
366
+ recipient_addr: this._recipient_addr,
367
+ data: this._data,
368
+ sign: this._sign,
369
+ fee: this._fee,
370
+ hash: this._hash,
371
+ ...(this._inserted_at
372
+ ? { inserted_at: this._inserted_at.toISOString() }
373
+ : {}),
374
+ ...(this._additionalData
375
+ ? { additional_data: this._additionalData }
376
+ : {}),
377
+ ...(this._cipherData ? { cipher_data: this._cipherData } : {}),
378
+ ...(this._chainName ? { chain_name: this._chainName } : {}),
379
+ ...(this._chainVersion ? { chain_version: this._chainVersion } : {}),
380
+ }
381
+ }
382
+
300
383
  Validate() {
301
384
  if (typeof this._id === 'undefined')
302
385
  throw new Error(INVALID_ARGUMENT_WITH_CS('id'))
@@ -312,23 +395,23 @@ export class Transaction {
312
395
  throw new Error(INVALID_ARGUMENT_WITH_CS('senderAddr'))
313
396
  if (typeof this._recipient_addr !== 'string')
314
397
  throw new Error(INVALID_ARGUMENT_WITH_CS('recipientAddr'))
315
- if (typeof this._data !== 'string')
398
+ if (typeof this._data === 'undefined')
316
399
  throw new Error(INVALID_ARGUMENT_WITH_CS('data'))
317
- if (
318
- typeof this._additionalData !== 'undefined' &&
319
- typeof this._additionalData !== 'string'
320
- )
321
- throw new Error(INVALID_ARGUMENT_WITH_CS('additionalData'))
322
- if (
323
- typeof this._cipherData !== 'undefined' &&
324
- typeof this._cipherData !== 'string'
325
- )
326
- throw new Error(INVALID_ARGUMENT_WITH_CS('cipherData'))
327
- if (typeof this._sign !== 'object')
400
+ if (typeof this._sign === 'undefined')
328
401
  throw new Error(INVALID_ARGUMENT_WITH_CS('sign'))
329
402
  if (typeof this._fee !== 'number')
330
403
  throw new Error(INVALID_ARGUMENT_WITH_CS('fee'))
331
- if (!(this._inserted_at instanceof Date))
404
+ if (this._inserted_at && !(this._inserted_at instanceof Date))
332
405
  throw new Error(INVALID_ARGUMENT_WITH_CS('insertedAt'))
406
+ if (
407
+ typeof this._chainName !== 'undefined' &&
408
+ typeof this._chainName !== 'string'
409
+ )
410
+ throw new Error(INVALID_ARGUMENT_WITH_CS('chainName'))
411
+ if (
412
+ typeof this._chainVersion !== 'undefined' &&
413
+ typeof this._chainVersion !== 'string'
414
+ )
415
+ throw new Error(INVALID_ARGUMENT_WITH_CS('chainVersion'))
333
416
  }
334
417
  }
@@ -13,7 +13,7 @@ export class Options {
13
13
  _protocols = []
14
14
  _customWS
15
15
  _endpoints = ['ws', 'longpoll']
16
- _version = `v2.6.2`
16
+ _version = `v2.6.4`
17
17
 
18
18
  _maxConnectionDelay = 10000
19
19
  _minReconnectionDelay = 1000 + Math.random() * 4000