Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 36 additions & 48 deletions src/infrastructure/AccountHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,10 @@ export class AccountHttp extends Http implements AccountRepository {
*/
public getAccountInfo(address: Address): Observable<AccountInfo> {
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
Expand All @@ -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
Expand Down
83 changes: 35 additions & 48 deletions src/infrastructure/BlockHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -72,30 +72,7 @@ export class BlockHttp extends Http implements BlockRepository {
*/
public getBlockByHeight(height: string): Observable<BlockInfo> {
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))),
);
}
Expand Down Expand Up @@ -129,33 +106,43 @@ export class BlockHttp extends Http implements BlockRepository {
public getBlocksByHeightWithLimit(height: string, limit: LimitType = LimitType.N_25): Observable<BlockInfo[]> {
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} dto the dto object from rest.
* @returns {BlockInfo} a BlockInfo model
*/
private toBlockInfo(dto: BlockInfoDTO): BlockInfo {
const networkType = dto.block.network.valueOf();
return new BlockInfo(
dto.meta.hash,
dto.meta.generationHash,
UInt64.fromNumericString(dto.meta.totalFee),
dto.meta.numTransactions,
dto.block.signature,
PublicAccount.createFromPublicKey(dto.block.signerPublicKey, networkType),
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),
);
}

/**
* 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)
Expand Down
111 changes: 51 additions & 60 deletions src/infrastructure/RestrictionMosaicHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -58,20 +60,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
getMosaicAddressRestriction(mosaicId: MosaicId, address: Address): Observable<MosaicAddressRestriction> {
return observableFrom(
this.restrictionMosaicRoutesApi.getMosaicAddressRestriction(mosaicId.toHex(), address.plain())).pipe(
map(({body}) => {
const payload = body.mosaicRestrictionEntry;
const restirctionItems = new Map<string, string>();
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))),
);
}
Expand All @@ -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<string, string>();
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<string, string>();
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.
Expand All @@ -115,23 +113,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
getMosaicGlobalRestriction(mosaicId: MosaicId): Observable<MosaicGlobalRestriction> {
return observableFrom(
this.restrictionMosaicRoutesApi.getMosaicGlobalRestriction(mosaicId.toHex())).pipe(
map(({body}) => {
const payload = body.mosaicRestrictionEntry;
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
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))),
);
}
Expand All @@ -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<string, MosaicGlobalRestrictionItem>();
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<string, MosaicGlobalRestrictionItem>();
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,
);
}
}
Loading