Skip to content

Commit 7db5995

Browse files
author
Greg S
committed
issue #100: fix BlockInfo fields & #53: add TransactionHttp.getTransactionEffectiveFee()
1 parent 02e4517 commit 7db5995

File tree

7 files changed

+91
-3
lines changed

7 files changed

+91
-3
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,15 @@ describe('TransactionHttp', () => {
874874
});
875875
});
876876
});
877+
878+
describe('getTransactionEffectiveFee', () => {
879+
it('should return effective paid fee given transactionHash', (done) => {
880+
transactionHttp.getTransactionEffectiveFee(transactionHash)
881+
.subscribe((effectiveFee) => {
882+
expect(effectiveFee).to.not.be.undefined;
883+
expect(effectiveFee).to.be.equal(0);
884+
done();
885+
});
886+
});
887+
});
877888
});

src/infrastructure/BlockchainHttp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
7070
new UInt64(blockDTO.block.height),
7171
new UInt64(blockDTO.block.timestamp),
7272
new UInt64(blockDTO.block.difficulty),
73+
blockDTO.block.feeMultiplier,
7374
blockDTO.block.previousBlockHash,
7475
blockDTO.block.blockTransactionsHash,
76+
blockDTO.block.blockReceiptsHash,
77+
blockDTO.block.stateHash,
78+
PublicAccount.createFromPublicKey(blockDTO.block.beneficiaryPublicKey, networkType),
7579
);
7680
}));
7781
}
@@ -116,8 +120,12 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
116120
new UInt64(blockDTO.block.height),
117121
new UInt64(blockDTO.block.timestamp),
118122
new UInt64(blockDTO.block.difficulty),
123+
blockDTO.block.feeMultiplier,
119124
blockDTO.block.previousBlockHash,
120125
blockDTO.block.blockTransactionsHash,
126+
blockDTO.block.blockReceiptsHash,
127+
blockDTO.block.stateHash,
128+
PublicAccount.createFromPublicKey(blockDTO.block.beneficiaryPublicKey, networkType),
121129
);
122130
});
123131
}));

src/infrastructure/Listener.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ export class Listener {
135135
new UInt64(message.block.height),
136136
new UInt64(message.block.timestamp),
137137
new UInt64(message.block.difficulty),
138+
message.block.feeMultiplier,
138139
message.block.previousBlockHash,
139140
message.block.blockTransactionsHash,
141+
message.block.blockReceiptsHash,
142+
message.block.stateHash,
143+
PublicAccount.createFromPublicKey(message.block.beneficiaryPublicKey, networkType),
140144
),
141145
});
142146
} else if (message.status) {

src/infrastructure/TransactionHttp.ts

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

17-
import {TransactionRoutesApi} from 'nem2-library';
17+
import {BlockchainRoutesApi, TransactionRoutesApi} from 'nem2-library';
1818
import * as requestPromise from 'request-promise-native';
1919
import {from as observableFrom, Observable, throwError as observableThrowError} from 'rxjs';
20-
import {catchError, map} from 'rxjs/operators';
20+
import {catchError, map, mergeMap} from 'rxjs/operators';
2121
import {PublicAccount} from '../model/account/PublicAccount';
2222
import {CosignatureSignedTransaction} from '../model/transaction/CosignatureSignedTransaction';
2323
import {Deadline} from '../model/transaction/Deadline';
@@ -44,13 +44,20 @@ export class TransactionHttp extends Http implements TransactionRepository {
4444
*/
4545
private transactionRoutesApi: TransactionRoutesApi;
4646

47+
/**
48+
* @internal
49+
* Nem2 Library blockchain routes api
50+
*/
51+
private blockchainRoutesApi: BlockchainRoutesApi;
52+
4753
/**
4854
* Constructor
4955
* @param url
5056
*/
5157
constructor(private readonly url: string) {
5258
super(url);
5359
this.transactionRoutesApi = new TransactionRoutesApi(this.apiClient);
60+
this.blockchainRoutesApi = new BlockchainRoutesApi(this.apiClient);
5461
}
5562

5663
/**
@@ -190,4 +197,23 @@ export class TransactionHttp extends Http implements TransactionRepository {
190197
return observableThrowError(err);
191198
}));
192199
}
200+
201+
/**
202+
* Gets a transaction's effective paid fee
203+
* @param transactionId - Transaction id or hash.
204+
* @returns Observable<number>
205+
*/
206+
public getTransactionEffectiveFee(transactionId: string): Observable<number> {
207+
return observableFrom(this.transactionRoutesApi.getTransaction(transactionId)).pipe(
208+
mergeMap((transactionDTO) => observableFrom(
209+
this.blockchainRoutesApi.getBlockByHeight(transactionDTO.meta.height)).pipe(
210+
map((blockDTO) => {
211+
// parse transaction for call to `size` overload
212+
const transaction = CreateTransactionFromDTO(transactionDTO);
213+
214+
// @see https://nemtech.github.io/concepts/transaction.html#fees
215+
// effective_fee = feeMultiplier x transaction::size
216+
return blockDTO.block.feeMultiplier * transaction.size;
217+
}))));
218+
}
193219
}

src/infrastructure/TransactionRepository.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export interface TransactionRepository {
5656
*/
5757
getTransactionsStatuses(transactionHashes: string[]): Observable<TransactionStatus[]>;
5858

59+
/**
60+
* Gets a transaction's effective paid fee
61+
* @param transactionId - Transaction id or hash.
62+
* @returns Observable<number>
63+
*/
64+
getTransactionEffectiveFee(transactionId: string): Observable<number>;
65+
5966
/**
6067
* Send a signed transaction
6168
* @param signedTransaction - Signed transaction

src/model/blockchain/BlockInfo.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ export class BlockInfo {
3636
* @param height
3737
* @param timestamp
3838
* @param difficulty
39+
* @param feeMultiplier
3940
* @param previousBlockHash
4041
* @param blockTransactionsHash
42+
* @param blockReceiptsHash
43+
* @param blockStateHash
44+
* @param beneficiaryPublicKey
4145
*/
4246
constructor(/**
4347
* The block hash.
@@ -90,14 +94,30 @@ export class BlockInfo {
9094
* The POI difficulty to harvest a block.
9195
*/
9296
public readonly difficulty: UInt64,
97+
/**
98+
* The feeMultiplier defined by the harvester.
99+
*/
100+
public readonly feeMultiplier: number,
93101
/**
94102
* The last block hash.
95103
*/
96104
public readonly previousBlockHash: string,
97105
/**
98106
* The block transaction hash.
99107
*/
100-
public readonly blockTransactionsHash: string,) {
108+
public readonly blockTransactionsHash: string,
109+
/**
110+
* The block receipt hash.
111+
*/
112+
public readonly blockReceiptsHash: string,
113+
/**
114+
* The state hash.
115+
*/
116+
public readonly stateHash: string,
117+
/**
118+
* The beneficiary public key.
119+
*/
120+
public readonly beneficiaryPublicKey: PublicAccount,) {
101121

102122
}
103123
}

test/model/blockchain/BlockInfo.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ describe('BlockInfo', () => {
2626
const blockDTO = {
2727
block: {
2828
blockTransactionsHash: '702090BA31CEF9E90C62BBDECC0CCCC0F88192B6625839382850357F70DD68A0',
29+
blockReceiptsHash: '702090BA31CEF9E90C62BBDECC0CCCC0F88192B6625839382850357F70DD68A0',
30+
stateHash: '702090BA31CEF9E90C62BBDECC0CCCC0F88192B6625839382850357F70DD68A0',
2931
difficulty: new UInt64([ 276447232, 23283 ]),
32+
feeMultiplier: 1,
3033
height: new UInt64([ 1, 0 ]),
3134
previousBlockHash: '0000000000000000000000000000000000000000000000000000000000000000',
3235
signature: '37351C8244AC166BE6664E3FA954E99A3239AC46E51E2B32CEA1C72DD0851100A7731868' +
3336
'E932E1A9BEF8A27D48E1FFEE401E933EB801824373E7537E51733E0F',
3437
signer: 'B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF',
38+
beneficiaryPublicKey: 'B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF',
3539
timestamp: new UInt64([ 0, 0 ]),
3640
type: 32768,
3741
version: 36867,
@@ -58,8 +62,12 @@ describe('BlockInfo', () => {
5862
blockDTO.block.height,
5963
blockDTO.block.timestamp,
6064
blockDTO.block.difficulty,
65+
blockDTO.block.feeMultiplier,
6166
blockDTO.block.previousBlockHash,
6267
blockDTO.block.blockTransactionsHash,
68+
blockDTO.block.blockReceiptsHash,
69+
blockDTO.block.stateHash,
70+
PublicAccount.createFromPublicKey(blockDTO.block.beneficiaryPublicKey, network),
6371
);
6472

6573
expect(blockInfo.hash).to.be.equal(blockDTO.meta.hash);
@@ -74,8 +82,12 @@ describe('BlockInfo', () => {
7482
deepEqual(blockInfo.height, blockDTO.block.height);
7583
deepEqual(blockInfo.timestamp, blockDTO.block.timestamp);
7684
deepEqual(blockInfo.difficulty, blockDTO.block.difficulty);
85+
expect(blockInfo.feeMultiplier).to.be.equal(blockDTO.block.feeMultiplier);
7786
expect(blockInfo.previousBlockHash).to.be.equal(blockDTO.block.previousBlockHash);
7887
expect(blockInfo.blockTransactionsHash).to.be.equal(blockDTO.block.blockTransactionsHash);
88+
expect(blockInfo.blockReceiptsHash).to.be.equal(blockDTO.block.blockReceiptsHash);
89+
expect(blockInfo.stateHash).to.be.equal(blockDTO.block.stateHash);
90+
expect(blockInfo.beneficiaryPublicKey.publicKey).to.be.equal(blockDTO.block.beneficiaryPublicKey);
7991

8092
});
8193
});

0 commit comments

Comments
 (0)