From 2f70b52ecf999d32c2add71a09974a4aaf647c70 Mon Sep 17 00:00:00 2001 From: Fernando Boucquez Date: Sat, 7 Dec 2019 09:25:44 -0300 Subject: [PATCH 1/2] Fixed Github 367 - TS-111 --- src/infrastructure/BlockHttp.ts | 83 ++++++++++++++------------------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/src/infrastructure/BlockHttp.ts b/src/infrastructure/BlockHttp.ts index 76ab8832bc..3d2920cc3f 100644 --- a/src/infrastructure/BlockHttp.ts +++ b/src/infrastructure/BlockHttp.ts @@ -23,7 +23,7 @@ import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo'; import { NetworkType } from '../model/blockchain/NetworkType'; import {Transaction} from '../model/transaction/Transaction'; import {UInt64} from '../model/UInt64'; -import { BlockRoutesApi } from './api'; +import { BlockInfoDTO, BlockRoutesApi } from './api'; import {BlockRepository} from './BlockRepository'; import {Http} from './Http'; import {QueryParams} from './QueryParams'; @@ -72,30 +72,7 @@ export class BlockHttp extends Http implements BlockRepository { */ public getBlockByHeight(height: string): Observable { return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe( - map(({body}) => { - const blockDTO = body; - const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16); - return new BlockInfo( - blockDTO.meta.hash, - blockDTO.meta.generationHash, - UInt64.fromNumericString(blockDTO.meta.totalFee), - blockDTO.meta.numTransactions, - blockDTO.block.signature, - PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType), - networkType, - parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version - blockDTO.block.type, - UInt64.fromNumericString(blockDTO.block.height), - UInt64.fromNumericString(blockDTO.block.timestamp), - UInt64.fromNumericString(blockDTO.block.difficulty), - blockDTO.block.feeMultiplier, - blockDTO.block.previousBlockHash, - blockDTO.block.transactionsHash, - blockDTO.block.receiptsHash, - blockDTO.block.stateHash, - extractBeneficiary(blockDTO, networkType), - ); - }), + map(({body}) => this.toBlockInfo(body)), catchError((error) => throwError(this.errorHandling(error))), ); } @@ -129,33 +106,43 @@ export class BlockHttp extends Http implements BlockRepository { public getBlocksByHeightWithLimit(height: string, limit: LimitType = LimitType.N_25): Observable { return observableFrom( this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe( - map(({body}) => body.map((blockDTO) => { - const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16); - return new BlockInfo( - blockDTO.meta.hash, - blockDTO.meta.generationHash, - UInt64.fromNumericString(blockDTO.meta.totalFee), - blockDTO.meta.numTransactions, - blockDTO.block.signature, - PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType), - networkType, - parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version - blockDTO.block.type, - UInt64.fromNumericString(blockDTO.block.height), - UInt64.fromNumericString(blockDTO.block.timestamp), - UInt64.fromNumericString(blockDTO.block.difficulty), - blockDTO.block.feeMultiplier, - blockDTO.block.previousBlockHash, - blockDTO.block.transactionsHash, - blockDTO.block.receiptsHash, - blockDTO.block.stateHash, - extractBeneficiary(blockDTO, networkType), - ); - })), + map(({body}) => body.map((blockDTO) => this.toBlockInfo(blockDTO))), catchError((error) => throwError(this.errorHandling(error))), ); } + + /** + * This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object. + * + * @internal + * @param {BlockInfoDTO} blockDTO the dto object from rest. + * @returns {BlockInfo} a BlockInfo model + */ + private toBlockInfo(blockDTO: BlockInfoDTO): BlockInfo { + const networkType = blockDTO.block.network.valueOf(); + return new BlockInfo( + blockDTO.meta.hash, + blockDTO.meta.generationHash, + UInt64.fromNumericString(blockDTO.meta.totalFee), + blockDTO.meta.numTransactions, + blockDTO.block.signature, + PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType), + networkType, + blockDTO.block.version, + blockDTO.block.type, + UInt64.fromNumericString(blockDTO.block.height), + UInt64.fromNumericString(blockDTO.block.timestamp), + UInt64.fromNumericString(blockDTO.block.difficulty), + blockDTO.block.feeMultiplier, + blockDTO.block.previousBlockHash, + blockDTO.block.transactionsHash, + blockDTO.block.receiptsHash, + blockDTO.block.stateHash, + extractBeneficiary(blockDTO, networkType), + ); + } + /** * Get the merkle path for a given a transaction and block * Returns the merkle path for a [transaction](https://nemtech.github.io/concepts/transaction.html) From 198e8d60f04c94c63d3ea9f2ca73883666876d8d Mon Sep 17 00:00:00 2001 From: Fernando Boucquez Date: Sat, 7 Dec 2019 09:51:48 -0300 Subject: [PATCH 2/2] Improved rest dto mappings --- src/infrastructure/AccountHttp.ts | 84 +++++++-------- src/infrastructure/BlockHttp.ts | 40 +++---- src/infrastructure/RestrictionMosaicHttp.ts | 111 +++++++++----------- src/infrastructure/TransactionHttp.ts | 41 ++++---- 4 files changed, 130 insertions(+), 146 deletions(-) diff --git a/src/infrastructure/AccountHttp.ts b/src/infrastructure/AccountHttp.ts index d2c320504e..de956a48e7 100644 --- a/src/infrastructure/AccountHttp.ts +++ b/src/infrastructure/AccountHttp.ts @@ -61,32 +61,10 @@ export class AccountHttp extends Http implements AccountRepository { */ public getAccountInfo(address: Address): Observable { return observableFrom(this.accountRoutesApi.getAccountInfo(address.plain())).pipe( - map(({body}) => new AccountInfo( - Address.createFromEncoded(body.account.address), - UInt64.fromNumericString(body.account.addressHeight), - body.account.publicKey, - UInt64.fromNumericString(body.account.publicKeyHeight), - body.account.accountType.valueOf(), - body.account.linkedAccountKey, - body.account.activityBuckets.map((bucket) => { - return new ActivityBucket( - bucket.startHeight, - bucket.totalFeesPaid, - bucket.beneficiaryCount, - bucket.rawScore, - ); - }), - body.account.mosaics.map((mosaicDTO) => new Mosaic( - new MosaicId(mosaicDTO.id), - UInt64.fromNumericString(mosaicDTO.amount), - )), - UInt64.fromNumericString(body.account.importance), - UInt64.fromNumericString(body.account.importanceHeight), - )), + map(({body}) => this.toAccountInfo(body)), catchError((error) => throwError(this.errorHandling(error))), ); } - /** * Gets AccountsInfo for different accounts. * @param addresses List of Address @@ -98,35 +76,45 @@ export class AccountHttp extends Http implements AccountRepository { }; return observableFrom( this.accountRoutesApi.getAccountsInfo(accountIdsBody)).pipe( - map(({body}) => body.map((accountInfoDTO: AccountInfoDTO) => { - return new AccountInfo( - Address.createFromEncoded(accountInfoDTO.account.address), - UInt64.fromNumericString(accountInfoDTO.account.addressHeight), - accountInfoDTO.account.publicKey, - UInt64.fromNumericString(accountInfoDTO.account.publicKeyHeight), - accountInfoDTO.account.accountType.valueOf(), - accountInfoDTO.account.linkedAccountKey, - accountInfoDTO.account.activityBuckets.map((bucket) => { - return new ActivityBucket( - bucket.startHeight, - bucket.totalFeesPaid, - bucket.beneficiaryCount, - bucket.rawScore, - ); - }), - accountInfoDTO.account.mosaics.map((mosaicDTO) => new Mosaic( - new MosaicId(mosaicDTO.id), - UInt64.fromNumericString(mosaicDTO.amount), - )), - UInt64.fromNumericString(accountInfoDTO.account.importance), - UInt64.fromNumericString(accountInfoDTO.account.importanceHeight), - ); - - })), + map(({body}) => body.map(this.toAccountInfo)), catchError((error) => throwError(this.errorHandling(error))), ); } + + /** + * This method maps a AccountInfoDTO from rest to the SDK's AccountInfo model object. + * + * @internal + * @param {AccountInfoDTO} dto AccountInfoDTO the dto object from rest. + * @returns AccountInfo model + */ + private toAccountInfo(dto: AccountInfoDTO):AccountInfo { + return new AccountInfo( + Address.createFromEncoded(dto.account.address), + UInt64.fromNumericString(dto.account.addressHeight), + dto.account.publicKey, + UInt64.fromNumericString(dto.account.publicKeyHeight), + dto.account.accountType.valueOf(), + dto.account.linkedAccountKey, + dto.account.activityBuckets.map((bucket) => { + return new ActivityBucket( + bucket.startHeight, + bucket.totalFeesPaid, + bucket.beneficiaryCount, + bucket.rawScore, + ); + }), + dto.account.mosaics.map((mosaicDTO) => new Mosaic( + new MosaicId(mosaicDTO.id), + UInt64.fromNumericString(mosaicDTO.amount), + )), + UInt64.fromNumericString(dto.account.importance), + UInt64.fromNumericString(dto.account.importanceHeight), + ); + } + + /** * Gets an array of confirmed transactions for which an account is signer or receiver. * @param address - * Address can be created rawAddress or publicKey diff --git a/src/infrastructure/BlockHttp.ts b/src/infrastructure/BlockHttp.ts index 3d2920cc3f..62193b921e 100644 --- a/src/infrastructure/BlockHttp.ts +++ b/src/infrastructure/BlockHttp.ts @@ -116,30 +116,30 @@ export class BlockHttp extends Http implements BlockRepository { * This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object. * * @internal - * @param {BlockInfoDTO} blockDTO the dto object from rest. + * @param {BlockInfoDTO} dto the dto object from rest. * @returns {BlockInfo} a BlockInfo model */ - private toBlockInfo(blockDTO: BlockInfoDTO): BlockInfo { - const networkType = blockDTO.block.network.valueOf(); + private toBlockInfo(dto: BlockInfoDTO): BlockInfo { + const networkType = dto.block.network.valueOf(); return new BlockInfo( - blockDTO.meta.hash, - blockDTO.meta.generationHash, - UInt64.fromNumericString(blockDTO.meta.totalFee), - blockDTO.meta.numTransactions, - blockDTO.block.signature, - PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType), + dto.meta.hash, + dto.meta.generationHash, + UInt64.fromNumericString(dto.meta.totalFee), + dto.meta.numTransactions, + dto.block.signature, + PublicAccount.createFromPublicKey(dto.block.signerPublicKey, networkType), networkType, - blockDTO.block.version, - blockDTO.block.type, - UInt64.fromNumericString(blockDTO.block.height), - UInt64.fromNumericString(blockDTO.block.timestamp), - UInt64.fromNumericString(blockDTO.block.difficulty), - blockDTO.block.feeMultiplier, - blockDTO.block.previousBlockHash, - blockDTO.block.transactionsHash, - blockDTO.block.receiptsHash, - blockDTO.block.stateHash, - extractBeneficiary(blockDTO, networkType), + dto.block.version, + dto.block.type, + UInt64.fromNumericString(dto.block.height), + UInt64.fromNumericString(dto.block.timestamp), + UInt64.fromNumericString(dto.block.difficulty), + dto.block.feeMultiplier, + dto.block.previousBlockHash, + dto.block.transactionsHash, + dto.block.receiptsHash, + dto.block.stateHash, + extractBeneficiary(dto, networkType), ); } diff --git a/src/infrastructure/RestrictionMosaicHttp.ts b/src/infrastructure/RestrictionMosaicHttp.ts index 3ab891e78d..5dbb2e7695 100644 --- a/src/infrastructure/RestrictionMosaicHttp.ts +++ b/src/infrastructure/RestrictionMosaicHttp.ts @@ -25,6 +25,8 @@ import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRe import { RestrictionMosaicRoutesApi } from './api/restrictionMosaicRoutesApi'; import {Http} from './Http'; import { RestrictionMosaicRepository } from './RestrictionMosaicRespository'; +import { MosaicAddressRestrictionDTO } from "./model/mosaicAddressRestrictionDTO"; +import { MosaicGlobalRestrictionDTO } from "./model/mosaicGlobalRestrictionDTO"; /** * RestrictionMosaic http repository. @@ -58,20 +60,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo getMosaicAddressRestriction(mosaicId: MosaicId, address: Address): Observable { return observableFrom( this.restrictionMosaicRoutesApi.getMosaicAddressRestriction(mosaicId.toHex(), address.plain())).pipe( - map(({body}) => { - const payload = body.mosaicRestrictionEntry; - const restirctionItems = new Map(); - payload.restrictions.forEach((restriction) => { - restirctionItems.set(restriction.key, restriction.value); - }); - return new MosaicAddressRestriction( - payload.compositeHash, - payload.entryType.valueOf(), - new MosaicId(payload.mosaicId), - Address.createFromEncoded(payload.targetAddress), - restirctionItems, - ); - }), + map(({body}) => this.toMosaicAddressRestriction(body)), catchError((error) => throwError(this.errorHandling(error))), ); } @@ -89,23 +78,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo }; return observableFrom( this.restrictionMosaicRoutesApi.getMosaicAddressRestrictions(mosaicId.toHex(), accountIds)).pipe( - map(({body}) => body.map((payload) => { - const restirctionItems = new Map(); - payload.mosaicRestrictionEntry.restrictions.forEach((restriction) => { - restirctionItems.set(restriction.key, restriction.value); - }); - return new MosaicAddressRestriction( - payload.mosaicRestrictionEntry.compositeHash, - payload.mosaicRestrictionEntry.entryType.valueOf(), - new MosaicId(payload.mosaicRestrictionEntry.mosaicId), - Address.createFromEncoded(payload.mosaicRestrictionEntry.targetAddress), - restirctionItems, - ); - })), + map(({body}) => body.map(this.toMosaicAddressRestriction)), catchError((error) => throwError(this.errorHandling(error))), ); } + /** + * This method maps a MosaicAddressRestrictionDTO from rest to the SDK's MosaicAddressRestriction model object. + * + * @internal + * @param {MosaicAddressRestrictionDTO} dto the MosaicAddressRestrictionDTO object from rest. + * @returns {MosaicAddressRestriction} a MosaicAddressRestriction model + */ + private toMosaicAddressRestriction(dto: MosaicAddressRestrictionDTO): MosaicAddressRestriction { + const restrictionItems = new Map(); + dto.mosaicRestrictionEntry.restrictions.forEach((restriction) => { + restrictionItems.set(restriction.key, restriction.value); + }); + return new MosaicAddressRestriction( + dto.mosaicRestrictionEntry.compositeHash, + dto.mosaicRestrictionEntry.entryType.valueOf(), + new MosaicId(dto.mosaicRestrictionEntry.mosaicId), + Address.createFromEncoded(dto.mosaicRestrictionEntry.targetAddress), + restrictionItems, + ); + } + /** * Get mosaic global restriction. * @summary Get mosaic global restrictions for a given mosaic identifier. @@ -115,23 +113,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo getMosaicGlobalRestriction(mosaicId: MosaicId): Observable { return observableFrom( this.restrictionMosaicRoutesApi.getMosaicGlobalRestriction(mosaicId.toHex())).pipe( - map(({body}) => { - const payload = body.mosaicRestrictionEntry; - const restirctionItems = new Map(); - payload.restrictions.forEach((restriction) => - restirctionItems.set(restriction.key, - new MosaicGlobalRestrictionItem( - new MosaicId(restriction.restriction.referenceMosaicId), - restriction.restriction.restrictionValue, - restriction.restriction.restrictionType.valueOf(), - ))); - return new MosaicGlobalRestriction( - payload.compositeHash, - payload.entryType.valueOf(), - new MosaicId(payload.mosaicId), - restirctionItems, - ); - }), + map(({body}) => this.toMosaicGlobalRestriction(body)), catchError((error) => throwError(this.errorHandling(error))), ); } @@ -148,23 +130,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo }; return observableFrom( this.restrictionMosaicRoutesApi.getMosaicGlobalRestrictions(mosaicIdsBody)).pipe( - map(({body}) => body.map((payload) => { - const restirctionItems = new Map(); - payload.mosaicRestrictionEntry.restrictions.forEach((restriction) => - restirctionItems.set(restriction.key, - new MosaicGlobalRestrictionItem( - new MosaicId(restriction.restriction.referenceMosaicId), - restriction.restriction.restrictionValue, - restriction.restriction.restrictionType.valueOf(), - ))); - return new MosaicGlobalRestriction( - payload.mosaicRestrictionEntry.compositeHash, - payload.mosaicRestrictionEntry.entryType.valueOf(), - new MosaicId(payload.mosaicRestrictionEntry.mosaicId), - restirctionItems, - ); - })), + map(({body}) => body.map(this.toMosaicGlobalRestriction)), catchError((error) => throwError(this.errorHandling(error))), ); } + + /** + * This method maps a MosaicGlobalRestrictionDTO from rest to the SDK's MosaicGlobalRestriction model object. + * + * @internal + * @param {MosaicGlobalRestrictionDTO} dto the MosaicGlobalRestrictionDTO object from rest. + * @returns {MosaicGlobalRestriction} a MosaicGlobalRestriction model + */ + private toMosaicGlobalRestriction(dto: MosaicGlobalRestrictionDTO): MosaicGlobalRestriction { + const restirctionItems = new Map(); + dto.mosaicRestrictionEntry.restrictions.forEach((restriction) => + restirctionItems.set(restriction.key, + new MosaicGlobalRestrictionItem( + new MosaicId(restriction.restriction.referenceMosaicId), + restriction.restriction.restrictionValue, + restriction.restriction.restrictionType.valueOf(), + ))); + return new MosaicGlobalRestriction( + dto.mosaicRestrictionEntry.compositeHash, + dto.mosaicRestrictionEntry.entryType.valueOf(), + new MosaicId(dto.mosaicRestrictionEntry.mosaicId), + restirctionItems, + ); + } } diff --git a/src/infrastructure/TransactionHttp.ts b/src/infrastructure/TransactionHttp.ts index e58c8fe9be..d6a8b85906 100644 --- a/src/infrastructure/TransactionHttp.ts +++ b/src/infrastructure/TransactionHttp.ts @@ -30,8 +30,10 @@ import {TransactionInfo} from '../model/transaction/TransactionInfo'; import {TransactionStatus} from '../model/transaction/TransactionStatus'; import {TransactionType} from '../model/transaction/TransactionType'; import {UInt64} from '../model/UInt64'; -import { BlockInfoDTO, BlockRoutesApi, - TransactionRoutesApi } from './api'; +import { + BlockInfoDTO, BlockRoutesApi, + TransactionRoutesApi, TransactionStatusDTO +} from './api'; import {Http} from './Http'; import {CreateTransactionFromDTO} from './transaction/CreateTransactionFromDTO'; import {TransactionRepository} from './TransactionRepository'; @@ -102,13 +104,7 @@ export class TransactionHttp extends Http implements TransactionRepository { */ public getTransactionStatus(transactionHash: string): Observable { return observableFrom(this.transactionRoutesApi.getTransactionStatus(transactionHash)).pipe( - map(({body}) => new TransactionStatus( - body.status, - body.group, - body.hash, - body.deadline ? - Deadline.createFromDTO(UInt64.fromNumericString(body.deadline).toDTO()) : undefined, - body.height ? UInt64.fromNumericString(body.height) : undefined)), + map(({body}) => this.toTransactionStatus(body)), catchError((error) => throwError(this.errorHandling(error))), ); } @@ -124,19 +120,28 @@ export class TransactionHttp extends Http implements TransactionRepository { }; return observableFrom( this.transactionRoutesApi.getTransactionsStatuses(transactionHashesBody)).pipe( - map(({body}) => body.map((transactionStatusDTO) => { - return new TransactionStatus( - transactionStatusDTO.status, - transactionStatusDTO.group, - transactionStatusDTO.hash, - transactionStatusDTO.deadline ? - Deadline.createFromDTO(UInt64.fromNumericString(transactionStatusDTO.deadline).toDTO()) : undefined, - transactionStatusDTO.height ? UInt64.fromNumericString(transactionStatusDTO.height) : undefined); - })), + map(({body}) => body.map(this.toTransactionStatus)), catchError((error) => throwError(this.errorHandling(error))), ); } + /** + * This method maps a TransactionStatusDTO from rest to the SDK's TransactionStatus model object. + * + * @internal + * @param {TransactionStatusDTO} dto the TransactionStatusDTO object from rest. + * @returns {TransactionStatus} a TransactionStatus model + */ + private toTransactionStatus(dto: TransactionStatusDTO): TransactionStatus { + return new TransactionStatus( + dto.status, + dto.group, + dto.hash, + dto.deadline ? + Deadline.createFromDTO(UInt64.fromNumericString(dto.deadline).toDTO()) : undefined, + dto.height ? UInt64.fromNumericString(dto.height) : undefined); + } + /** * Send a signed transaction * @param signedTransaction - Signed transaction