Skip to content

Commit 58dd436

Browse files
committed
Added #352 aggregate bonded announcing
1 parent a2e0dd4 commit 58dd436

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

src/infrastructure/Listener.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,18 @@ export class Listener {
239239
* it emits a new Transaction in the event stream.
240240
*
241241
* @param address address we listen when a transaction is in confirmed state
242+
* @param transactionHash transactionHash for filtering multiple transactions
242243
* @return an observable stream of Transaction with state confirmed
243244
*/
244-
public confirmed(address: Address): Observable<Transaction> {
245+
public confirmed(address: Address, transactionHash?: string): Observable<Transaction> {
245246
this.subscribeTo(`confirmedAdded/${address.plain()}`);
246247
return this.messageSubject.asObservable().pipe(
247248
filter((_) => _.channelName === ListenerChannelName.confirmedAdded),
248249
filter((_) => _.message instanceof Transaction),
249250
map((_) => _.message as Transaction),
250-
filter((_) => this.transactionFromAddress(_, address)));
251+
filter((_) => this.transactionFromAddress(_, address)),
252+
filter((_) => transactionHash === undefined || _.transactionInfo!.hash === transactionHash),
253+
);
251254
}
252255

253256
/**
@@ -289,15 +292,18 @@ export class Listener {
289292
* it emits a new {@link AggregateTransaction} in the event stream.
290293
*
291294
* @param address address we listen when a transaction with missing signatures state
295+
* @param transactionHash transactionHash for filtering multiple transactions
292296
* @return an observable stream of AggregateTransaction with missing signatures state
293297
*/
294-
public aggregateBondedAdded(address: Address): Observable<AggregateTransaction> {
298+
public aggregateBondedAdded(address: Address, transactionHash?: string): Observable<AggregateTransaction> {
295299
this.subscribeTo(`partialAdded/${address.plain()}`);
296300
return this.messageSubject.asObservable().pipe(
297301
filter((_) => _.channelName === ListenerChannelName.aggregateBondedAdded),
298302
filter((_) => _.message instanceof AggregateTransaction),
299303
map((_) => _.message as AggregateTransaction),
300-
filter((_) => this.transactionFromAddress(_, address)));
304+
filter((_) => this.transactionFromAddress(_, address)),
305+
filter((_) => transactionHash === undefined || _.transactionInfo!.hash === transactionHash),
306+
);
301307
}
302308

303309
/**

src/model/transaction/SignedTransaction.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Address } from '../account/Address';
18+
import { PublicAccount } from '../account/PublicAccount';
1719
import {NetworkType} from '../blockchain/NetworkType';
1820

1921
/**
@@ -65,4 +67,12 @@ export class SignedTransaction {
6567
networkType: this.networkType,
6668
};
6769
}
70+
71+
/**
72+
* Return signer's address
73+
* @returns {Address}
74+
*/
75+
getSignerAddress(): Address {
76+
return PublicAccount.createFromPublicKey(this.signerPublicKey, this.networkType).address;
77+
}
6878
}

src/service/TransactionService.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
import {Observable} from 'rxjs';
1818
import { mergeMap, toArray} from 'rxjs/operators';
19+
import { flatMap } from 'rxjs/operators';
20+
import { Listener } from '../infrastructure/Listener';
1921
import { ReceiptHttp } from '../infrastructure/ReceiptHttp';
2022
import { TransactionHttp } from '../infrastructure/TransactionHttp';
2123
import { Address } from '../model/account/Address';
2224
import { MosaicId } from '../model/mosaic/MosaicId';
2325
import { NamespaceId } from '../model/namespace/NamespaceId';
2426
import { ResolutionType } from '../model/receipt/ResolutionType';
2527
import { Statement } from '../model/receipt/Statement';
28+
import { AggregateTransaction } from '../model/transaction/AggregateTransaction';
29+
import { SignedTransaction } from '../model/transaction/SignedTransaction';
2630
import { Transaction } from '../model/transaction/Transaction';
2731
import { ITransactionService } from './interfaces/ITransactionService';
2832

@@ -33,13 +37,15 @@ export class TransactionService implements ITransactionService {
3337

3438
private readonly transactionHttp: TransactionHttp;
3539
private readonly receiptHttp: ReceiptHttp;
40+
private readonly listener: Listener;
3641
/**
3742
* Constructor
3843
* @param url Base catapult-rest url
3944
*/
4045
constructor(url: string) {
4146
this.transactionHttp = new TransactionHttp(url);
4247
this.receiptHttp = new ReceiptHttp(url);
48+
this.listener = new Listener(url);
4349
}
4450

4551
/**
@@ -65,7 +71,7 @@ export class TransactionService implements ITransactionService {
6571
(resolution.unresolved as NamespaceId).equals(unresolved));
6672

6773
if (!resolutionStatement) {
68-
throw new Error('No resolution statement found');
74+
throw new Error(`No resolution statement found for unsolved value: ${unresolved.toHex()}`);
6975
}
7076
// source (0,0) is reserved for blocks, source (n, 0) is for txes, where n is 1-based index
7177
const resolutionEntry = resolutionStatement.resolutionEntries
@@ -74,7 +80,7 @@ export class TransactionService implements ITransactionService {
7480
entry.source.secondaryId === (aggregateTransactionIndex !== undefined ? transactionIndex + 1 : 0));
7581

7682
if (!resolutionEntry) {
77-
throw new Error('No resolution entry found');
83+
throw new Error(`No resolution entry found for unsolved value: ${unresolved.toHex()}`);
7884
}
7985

8086
return resolutionEntry.resolved;
@@ -91,4 +97,39 @@ export class TransactionService implements ITransactionService {
9197
toArray(),
9298
);
9399
}
100+
101+
/**
102+
* @param signedTransaction Signed transaction to be announced.
103+
* @returns {Observable<Transaction>}
104+
*/
105+
public announce(signedTransaction: SignedTransaction): Observable<Transaction> {
106+
return this.transactionHttp.announce(signedTransaction).pipe(
107+
flatMap(() => this.listener.confirmed(signedTransaction.getSignerAddress(), signedTransaction.hash)),
108+
);
109+
}
110+
111+
/**
112+
* Announce aggregate transaction
113+
* @param signedTransaction Signed aggregate bonded transaction.
114+
* @returns {Observable<AggregateTransaction>}
115+
*/
116+
public announceAggregateBonded(signedTransaction: SignedTransaction): Observable<AggregateTransaction> {
117+
return this.transactionHttp.announceAggregateBonded(signedTransaction).pipe(
118+
flatMap(() => this.listener.aggregateBondedAdded(signedTransaction.getSignerAddress(), signedTransaction.hash)),
119+
);
120+
}
121+
122+
/**
123+
* Announce aggregate bonded transaction with lock fund
124+
* @param signedHashLockTransaction Signed hash lock transaction.
125+
* @param signedAggregateTransaction Signed aggregate bonded transaction.
126+
* @returns {Observable<AggregateTransaction>}
127+
*/
128+
public announceHashLockAggregateBonded(signedHashLockTransaction: SignedTransaction,
129+
signedAggregateTransaction: SignedTransaction): Observable<AggregateTransaction> {
130+
return this.announce(signedHashLockTransaction).pipe(
131+
flatMap(() => this.announceAggregateBonded(signedAggregateTransaction)),
132+
);
133+
134+
}
94135
}

src/service/interfaces/ITransactionService.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import {Observable} from 'rxjs';
18+
import { AggregateTransaction } from '../../model/transaction/AggregateTransaction';
19+
import { SignedTransaction } from '../../model/transaction/SignedTransaction';
1820
import { Transaction } from '../../model/transaction/Transaction';
1921

2022
/**
@@ -24,7 +26,29 @@ export interface ITransactionService {
2426

2527
/**
2628
* @param transationHashes List of transaction hashes.
27-
* @returns Observable<Transaction[]>
29+
* @returns {Observable<Transaction[]>}
2830
*/
2931
resolveAliases(transationHashes: string[]): Observable<Transaction[]>;
32+
33+
/**
34+
* @param signedTransaction Signed transaction to be announced.
35+
* @returns {Observable<Transaction>}
36+
*/
37+
announce(signedTransaction: SignedTransaction): Observable<Transaction>;
38+
39+
/**
40+
* Announce aggregate transaction
41+
* @param signedTransaction Signed aggregate bonded transaction.
42+
* @returns {Observable<AggregateTransaction>}
43+
*/
44+
announceAggregateBonded(signedTransaction: SignedTransaction): Observable<AggregateTransaction>;
45+
46+
/**
47+
* Announce aggregate bonded transaction with lock fund
48+
* @param signedHashLockTransaction Signed hash lock transaction.
49+
* @param signedAggregateTransaction Signed aggregate bonded transaction.
50+
* @returns {Observable<AggregateTransaction>}
51+
*/
52+
announceHashLockAggregateBonded(signedHashLockTransaction: SignedTransaction,
53+
signedAggregateTransaction: SignedTransaction): Observable<AggregateTransaction>;
3054
}

0 commit comments

Comments
 (0)