@@ -18,17 +18,19 @@ import { Observable, of, OperatorFunction, Subject } from 'rxjs';
18
18
import { filter , flatMap , map , share } from 'rxjs/operators' ;
19
19
import * as WebSocket from 'ws' ;
20
20
import { Address } from '../model/account/Address' ;
21
- import { BlockInfo } from '../model/blockchain/BlockInfo' ;
22
21
import { NamespaceName } from '../model/namespace/NamespaceName' ;
23
22
import { AggregateTransaction } from '../model/transaction/AggregateTransaction' ;
24
23
import { CosignatureSignedTransaction } from '../model/transaction/CosignatureSignedTransaction' ;
25
24
import { Deadline } from '../model/transaction/Deadline' ;
26
25
import { Transaction } from '../model/transaction/Transaction' ;
27
26
import { TransactionStatusError } from '../model/transaction/TransactionStatusError' ;
28
- import { BlockHttp } from './BlockHttp' ;
29
27
import { IListener } from './IListener' ;
30
28
import { NamespaceRepository } from './NamespaceRepository' ;
31
29
import { CreateTransactionFromDTO } from './transaction/CreateTransactionFromDTO' ;
30
+ import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client/dist/model/blockInfoDTO' ;
31
+ import { NewBlock } from '../model/blockchain/NewBlock' ;
32
+ import { PublicAccount } from '../model/account/PublicAccount' ;
33
+ import { UInt64 } from '../model/UInt64' ;
32
34
33
35
export enum ListenerChannelName {
34
36
block = 'block' ,
@@ -44,14 +46,13 @@ export enum ListenerChannelName {
44
46
45
47
interface ListenerMessage {
46
48
readonly channelName : ListenerChannelName ;
47
- readonly message : Transaction | string | BlockInfo | TransactionStatusError | CosignatureSignedTransaction ;
49
+ readonly message : Transaction | string | NewBlock | TransactionStatusError | CosignatureSignedTransaction ;
48
50
}
49
51
50
52
/**
51
53
* Listener service
52
54
*/
53
55
export class Listener implements IListener {
54
- public readonly url : string ;
55
56
/**
56
57
* @internal
57
58
* WebSocket connector
@@ -70,14 +71,15 @@ export class Listener implements IListener {
70
71
71
72
/**
72
73
* Constructor
73
- * @param config - Listener configuration
74
- * @param websocketInjected - (Optional) WebSocket injected when using listeners in client
74
+ * @param url - Listener websocket server url. default: rest-gateway's url with ''/ws'' suffix. (e.g. http://localhost:3000/ws).
75
+ * @param namespaceRepository - NamespaceRepository interface for resolving alias.
76
+ * @param websocketInjected - (Optional) WebSocket injected when using listeners in client.
75
77
*/
76
78
constructor (
77
79
/**
78
- * Listener configuration.
80
+ * Listener websocket server url. default: rest-gateway's url with ''/ws'' suffix. (e.g. http://localhost:3000/ws)
79
81
*/
80
- private config : string ,
82
+ public readonly url : string ,
81
83
/**
82
84
* Namespace repository for resolving account alias
83
85
*/
@@ -87,8 +89,7 @@ export class Listener implements IListener {
87
89
*/
88
90
private websocketInjected ?: any ,
89
91
) {
90
- this . config = config . replace ( / \/ $ / , '' ) ;
91
- this . url = `${ this . config } /ws` ;
92
+ this . url = url . replace ( / \/ $ / , '' ) ;
92
93
this . messageSubject = new Subject ( ) ;
93
94
}
94
95
@@ -139,7 +140,7 @@ export class Listener implements IListener {
139
140
} else if ( message . block ) {
140
141
this . messageSubject . next ( {
141
142
channelName : ListenerChannelName . block ,
142
- message : BlockHttp . toBlockInfo ( message ) ,
143
+ message : this . toNewBlock ( message ) ,
143
144
} ) ;
144
145
} else if ( message . code ) {
145
146
this . messageSubject . next ( {
@@ -192,13 +193,13 @@ export class Listener implements IListener {
192
193
*
193
194
* @return an observable stream of BlockInfo
194
195
*/
195
- public newBlock ( ) : Observable < BlockInfo > {
196
+ public newBlock ( ) : Observable < NewBlock > {
196
197
this . subscribeTo ( 'block' ) ;
197
198
return this . messageSubject . asObservable ( ) . pipe (
198
199
share ( ) ,
199
200
filter ( ( _ ) => _ . channelName === ListenerChannelName . block ) ,
200
- filter ( ( _ ) => _ . message instanceof BlockInfo ) ,
201
- map ( ( _ ) => _ . message as BlockInfo ) ,
201
+ filter ( ( _ ) => _ . message instanceof NewBlock ) ,
202
+ map ( ( _ ) => _ . message as NewBlock ) ,
202
203
) ;
203
204
}
204
205
@@ -406,4 +407,36 @@ export class Listener implements IListener {
406
407
} ;
407
408
this . webSocket . send ( JSON . stringify ( subscriptionMessage ) ) ;
408
409
}
410
+
411
+ /**
412
+ * This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object.
413
+ *
414
+ * @internal
415
+ * @param {BlockInfoDTO } dto the dto object from rest.
416
+ * @returns {NewBlock } a BlockInfo model
417
+ */
418
+ private toNewBlock ( dto : BlockInfoDTO ) : NewBlock {
419
+ const networkType = dto . block . network . valueOf ( ) ;
420
+ return new NewBlock (
421
+ dto . meta . hash ,
422
+ dto . meta . generationHash ,
423
+ dto . block . signature ,
424
+ PublicAccount . createFromPublicKey ( dto . block . signerPublicKey , networkType ) ,
425
+ networkType ,
426
+ dto . block . version ,
427
+ dto . block . type ,
428
+ UInt64 . fromNumericString ( dto . block . height ) ,
429
+ UInt64 . fromNumericString ( dto . block . timestamp ) ,
430
+ UInt64 . fromNumericString ( dto . block . difficulty ) ,
431
+ dto . block . feeMultiplier ,
432
+ dto . block . previousBlockHash ,
433
+ dto . block . transactionsHash ,
434
+ dto . block . receiptsHash ,
435
+ dto . block . stateHash ,
436
+ dto . block . proofGamma ,
437
+ dto . block . proofScalar ,
438
+ dto . block . proofVerificationHash ,
439
+ dto . block . beneficiaryPublicKey ? PublicAccount . createFromPublicKey ( dto . block . beneficiaryPublicKey , networkType ) : undefined ,
440
+ ) ;
441
+ }
409
442
}
0 commit comments