Skip to content

Commit 409941f

Browse files
committed
Migrated Receipt and added receit tests
1 parent 3588370 commit 409941f

22 files changed

+1487
-52
lines changed

e2e/infrastructure/BlockHttp.spec.ts

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,78 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {expect} from 'chai';
17+
import {assert, expect} from 'chai';
1818
import {BlockHttp} from '../../src/infrastructure/BlockHttp';
19+
import { Listener, TransactionHttp } from '../../src/infrastructure/infrastructure';
1920
import {QueryParams} from '../../src/infrastructure/QueryParams';
21+
import { Account } from '../../src/model/account/Account';
22+
import { NetworkType } from '../../src/model/blockchain/NetworkType';
23+
import { NetworkCurrencyMosaic } from '../../src/model/mosaic/NetworkCurrencyMosaic';
24+
import { Deadline } from '../../src/model/transaction/Deadline';
25+
import { PlainMessage } from '../../src/model/transaction/PlainMessage';
26+
import { Transaction } from '../../src/model/transaction/Transaction';
27+
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
2028

2129
describe('BlockHttp', () => {
30+
let account: Account;
31+
let account2: Account;
2232
let blockHttp: BlockHttp;
33+
let transactionHttp: TransactionHttp;
2334
let blockReceiptHash = '';
2435
let blockTransactionHash = '';
36+
let config;
37+
let chainHeight;
2538
before((done) => {
2639
const path = require('path');
2740
require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
2841
if (err) {
2942
throw err;
3043
}
3144
const json = JSON.parse(data);
45+
config = json;
46+
account = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST);
47+
account2 = Account.createFromPrivateKey(json.testAccount2.privateKey, NetworkType.MIJIN_TEST);
3248
blockHttp = new BlockHttp(json.apiUrl);
49+
transactionHttp = new TransactionHttp(json.apiUrl);
3350
done();
3451
});
3552
});
53+
54+
describe('Setup test data', () => {
55+
let listener: Listener;
56+
before (() => {
57+
listener = new Listener(config.apiUrl);
58+
return listener.open();
59+
});
60+
after(() => {
61+
return listener.close();
62+
});
63+
64+
it('standalone', (done) => {
65+
const transferTransaction = TransferTransaction.create(
66+
Deadline.create(),
67+
account2.address,
68+
[NetworkCurrencyMosaic.createAbsolute(1)],
69+
PlainMessage.create('test-message'),
70+
NetworkType.MIJIN_TEST,
71+
);
72+
const signedTransaction = transferTransaction.signWith(account);
73+
74+
listener.confirmed(account.address).subscribe((transaction: Transaction) => {
75+
76+
chainHeight = transaction.transactionInfo!.height.lower;
77+
console.log(`Test chain height: ${chainHeight}`);
78+
done();
79+
});
80+
listener.status(account.address).subscribe((error) => {
81+
console.log('Error:', error);
82+
assert(false);
83+
done();
84+
});
85+
transactionHttp.announce(signedTransaction);
86+
});
87+
});
88+
3689
describe('getBlockByHeight', () => {
3790
it('should return block info given height', (done) => {
3891
blockHttp.getBlockByHeight(1)
@@ -74,7 +127,7 @@ describe('BlockHttp', () => {
74127

75128
describe('getBlocksByHeightWithLimit', () => {
76129
it('should return block info given height and limit', (done) => {
77-
blockHttp.getBlocksByHeightWithLimit(1, 50)
130+
blockHttp.getBlocksByHeightWithLimit(chainHeight, 50)
78131
.subscribe((blocksInfo) => {
79132
expect(blocksInfo.length).to.be.greaterThan(0);
80133
done();
@@ -83,7 +136,7 @@ describe('BlockHttp', () => {
83136
});
84137
describe('getMerkleReceipts', () => {
85138
it('should return Merkle Receipts', (done) => {
86-
blockHttp.getMerkleReceipts(1, blockReceiptHash)
139+
blockHttp.getMerkleReceipts(chainHeight, blockReceiptHash)
87140
.subscribe((merkleReceipts) => {
88141
expect(merkleReceipts.type).not.to.be.null;
89142
expect(merkleReceipts.payload).not.to.be.null;
@@ -93,7 +146,7 @@ describe('BlockHttp', () => {
93146
});
94147
describe('getMerkleTransaction', () => {
95148
it('should return Merkle Transaction', (done) => {
96-
blockHttp.getMerkleTransaction(1, blockTransactionHash)
149+
blockHttp.getMerkleTransaction(chainHeight, blockTransactionHash)
97150
.subscribe((merkleTransactionss) => {
98151
expect(merkleTransactionss.type).not.to.be.null;
99152
expect(merkleTransactionss.payload).not.to.be.null;
@@ -102,14 +155,15 @@ describe('BlockHttp', () => {
102155
});
103156
});
104157

105-
// describe('getBlockReceipts', () => {
106-
// it('should return block receipts', (done) => {
107-
// blockHttp.(1, '')
108-
// .subscribe((merkleTransactionss) => {
109-
// expect(merkleTransactionss.type).not.to.be.null;
110-
// expect(merkleTransactionss.payload).not.to.be.null;
111-
// done();
112-
// });
113-
// });
114-
// });
158+
describe('getBlockReceipts', () => {
159+
it('should return block receipts', (done) => {
160+
blockHttp.getBlockReceipts(chainHeight)
161+
.subscribe((statement) => {
162+
console.log(statement);
163+
expect(statement.transactionStatements).not.to.be.null;
164+
expect(statement.transactionStatements.length).to.be.greaterThan(0);
165+
done();
166+
});
167+
});
168+
});
115169
});

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ describe('TransactionHttp', () => {
518518
NetworkType.MIJIN_TEST,
519519
);
520520
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
521-
[accountLinkTransaction.toAggregate(harvestingAccount.publicAccount)],
521+
[accountLinkTransaction.toAggregate(account.publicAccount)],
522522
NetworkType.MIJIN_TEST,
523523
[],
524524
);

e2e/service/MosaicService.spec.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
// import { map, mergeMap, toArray } from 'rxjs/operators';
2-
// import { AccountHttp } from '../../src/infrastructure/AccountHttp';
3-
// import { MosaicHttp } from '../../src/infrastructure/MosaicHttp';
4-
// import { Address } from '../../src/model/account/Address';
5-
// import { MosaicService } from '../../src/service/MosaicService';
1+
import { map, mergeMap, toArray } from 'rxjs/operators';
2+
import { AccountHttp } from '../../src/infrastructure/AccountHttp';
3+
import { MosaicHttp } from '../../src/infrastructure/MosaicHttp';
4+
import { Address } from '../../src/model/account/Address';
5+
import { MosaicService } from '../../src/service/MosaicService';
66

7-
// describe('MosaicService', () => {
8-
// let accountAddress: Address;
9-
// let accountHttp: AccountHttp;
10-
// let mosaicHttp: MosaicHttp;
7+
describe('MosaicService', () => {
8+
let accountAddress: Address;
9+
let accountHttp: AccountHttp;
10+
let mosaicHttp: MosaicHttp;
1111

12-
// before((done) => {
13-
// const path = require('path');
14-
// require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
15-
// if (err) {
16-
// throw err;
17-
// }
18-
// const json = JSON.parse(data);
19-
// accountAddress = Address.createFromRawAddress(json.testAccount.address);
20-
// accountHttp = new AccountHttp(json.apiUrl);
21-
// mosaicHttp = new MosaicHttp(json.apiUrl);
22-
// done();
23-
// });
24-
// });
25-
// it('should return the mosaic list skipping the expired mosaics', () => {
26-
// const mosaicService = new MosaicService(accountHttp, mosaicHttp);
12+
before((done) => {
13+
const path = require('path');
14+
require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
15+
if (err) {
16+
throw err;
17+
}
18+
const json = JSON.parse(data);
19+
accountAddress = Address.createFromRawAddress(json.testAccount.address);
20+
accountHttp = new AccountHttp(json.apiUrl);
21+
mosaicHttp = new MosaicHttp(json.apiUrl);
22+
done();
23+
});
24+
});
25+
it('should return the mosaic list skipping the expired mosaics', () => {
26+
const mosaicService = new MosaicService(accountHttp, mosaicHttp);
2727

28-
// const address = accountAddress;
28+
const address = accountAddress;
2929

30-
// return mosaicService.mosaicsAmountViewFromAddress(address).pipe(
31-
// mergeMap((_) => _),
32-
// map((mosaic) => console.log('You have', mosaic.relativeAmount(), mosaic.fullName())),
33-
// toArray(),
34-
// ).toPromise();
35-
// });
36-
// });
30+
return mosaicService.mosaicsAmountViewFromAddress(address).pipe(
31+
mergeMap((_) => _),
32+
map((mosaic) => console.log('You have', mosaic.relativeAmount(), mosaic.fullName())),
33+
toArray(),
34+
).toPromise();
35+
});
36+
});

src/infrastructure/BlockHttp.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
import {BlockRoutesApi} from 'nem2-library';
1818
import {from as observableFrom, Observable} from 'rxjs';
19-
import {map} from 'rxjs/operators';
19+
import {map, mergeMap} from 'rxjs/operators';
2020
import {PublicAccount} from '../model/account/PublicAccount';
2121
import {BlockInfo} from '../model/blockchain/BlockInfo';
2222
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
2323
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
2424
import { MerkleProofInfoPayload } from '../model/blockchain/MerkleProofInfoPayload';
25+
import { Statement } from '../model/receipt/Statement';
2526
import {Transaction} from '../model/transaction/Transaction';
2627
import {UInt64} from '../model/UInt64';
2728
import {BlockRepository} from './BlockRepository';
2829
import {Http} from './Http';
2930
import {QueryParams} from './QueryParams';
31+
import { CreateStatementFromDTO } from './receipt/CreateReceiptFromDTO';
3032
import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/CreateTransactionFromDTO';
33+
import { NetworkHttp } from './NetworkHttp';
3134

3235
/**
3336
* Blockchain http repository.
@@ -45,8 +48,9 @@ export class BlockHttp extends Http implements BlockRepository {
4548
* Constructor
4649
* @param url
4750
*/
48-
constructor(url: string) {
49-
super(url);
51+
constructor(url: string, networkHttp?: NetworkHttp) {
52+
networkHttp = networkHttp == null ? new NetworkHttp(url) : networkHttp;
53+
super(url, networkHttp);
5054
this.blockRoutesApi = new BlockRoutesApi(this.apiClient);
5155
}
5256

@@ -173,4 +177,22 @@ export class BlockHttp extends Http implements BlockRepository {
173177
);
174178
}));
175179
}
180+
181+
/**
182+
* Gets an array receipts for a block height.
183+
* @param height - Block height from which will be the first block in the array
184+
* @param queryParams - (Optional) Query params
185+
* @returns Observable<Statement>
186+
*/
187+
public getBlockReceipts(height: number): Observable<Statement> {
188+
return this.getNetworkTypeObservable().pipe(
189+
mergeMap((networkType) => observableFrom(
190+
this.blockRoutesApi.getBlockReceipts(height)).pipe(
191+
map((receiptDTO) => {
192+
return CreateStatementFromDTO(receiptDTO, networkType);
193+
}),
194+
),
195+
),
196+
);
197+
}
176198
}

src/infrastructure/BlockRepository.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import {Observable} from 'rxjs';
1818
import {BlockInfo} from '../model/blockchain/BlockInfo';
1919
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
20+
import { Statement } from '../model/receipt/Statement';
2021
import {Transaction} from '../model/transaction/Transaction';
2122
import {QueryParams} from './QueryParams';
2223

@@ -76,6 +77,11 @@ export interface BlockRepository {
7677
*/
7778
getMerkleTransaction(height: number, hash: string): Observable<MerkleProofInfo>;
7879

79-
// TODO:
80-
// getBlockReceipts(height: number): Observable<any>;
80+
/**
81+
* Get receipts from a block
82+
* Returns the receipts linked to a block.
83+
* @param {Number} height The height of the block.
84+
* @return Observable<Statement>
85+
*/
86+
getBlockReceipts(height: number): Observable<Statement>;
8187
}

0 commit comments

Comments
 (0)