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
25 changes: 25 additions & 0 deletions e2e/infrastructure/AccountHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import { NamespaceRegistrationTransaction } from '../../src/model/transaction/Na
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
import { UInt64 } from '../../src/model/UInt64';
import { IntegrationTestHelper } from './IntegrationTestHelper';
import { AccountPaginationStreamer } from '../../src/infrastructure/paginationStreamer/AccountPaginationStreamer';
import { toArray, take } from 'rxjs/operators';
import { deepEqual } from 'assert';
import { Order } from '../../src/infrastructure/infrastructure';
import { AccountOrderBy } from '../../src/infrastructure/searchCriteria/AccountOrderBy';

describe('AccountHttp', () => {
const helper = new IntegrationTestHelper();
Expand Down Expand Up @@ -178,6 +183,26 @@ describe('AccountHttp', () => {
});
});

describe('searchAccount', () => {
it('should return account info', async () => {
const info = await accountRepository.search({}).toPromise();
expect(info.data.length).to.be.greaterThan(0);
});
});

describe('searchAccount with streamer', () => {
it('should return account info', async () => {
const streamer = new AccountPaginationStreamer(accountRepository);
const infoStreamer = await streamer
.search({ pageSize: 20, order: Order.Asc, orderBy: AccountOrderBy.Id })
.pipe(take(20), toArray())
.toPromise();
const info = await accountRepository.search({ pageSize: 20, order: Order.Asc, orderBy: AccountOrderBy.Id }).toPromise();
expect(infoStreamer.length).to.be.greaterThan(0);
deepEqual(infoStreamer[0], info.data[0]);
});
});

describe('transactions', () => {
it('should not return accounts when account does not exist', () => {
return accountRepository
Expand Down
39 changes: 24 additions & 15 deletions e2e/infrastructure/NamespaceHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { Deadline } from '../../src/model/transaction/Deadline';
import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction';
import { UInt64 } from '../../src/model/UInt64';
import { IntegrationTestHelper } from './IntegrationTestHelper';
import { NamespacePaginationStreamer } from '../../src/infrastructure/paginationStreamer/NamespacePaginationStreamer';
import { take, toArray } from 'rxjs/operators';
import { Order } from '../../src/infrastructure/infrastructure';

describe('NamespaceHttp', () => {
let defaultNamespaceId: NamespaceId;
Expand Down Expand Up @@ -90,23 +93,9 @@ describe('NamespaceHttp', () => {
});
});

describe('getNamespacesFromAccount', () => {
it('should return namespace data given publicKeyNemesis', async () => {
const namespaces = await namespaceRepository.getNamespacesFromAccount(account.address).toPromise();
deepEqual(namespaces[0].ownerAddress, account.address);
});
});

describe('getNamespacesFromAccounts', () => {
it('should return namespaces data given publicKeyNemesis', async () => {
const namespaces = await namespaceRepository.getNamespacesFromAccounts([account.address]).toPromise();
deepEqual(namespaces[0].ownerAddress, account.address);
});
});

describe('getNamespacesName', () => {
it('should return namespace name given array of namespaceIds', async () => {
const namespaceNames = await namespaceRepository.getNamespacesName([defaultNamespaceId]).toPromise();
const namespaceNames = await namespaceRepository.getNamespacesNames([defaultNamespaceId]).toPromise();
expect(namespaceNames[0].name).to.be.equal('currency');
});
});
Expand All @@ -124,4 +113,24 @@ describe('NamespaceHttp', () => {
expect(address.plain()).to.be.equal(account.address.plain());
});
});

describe('searchNamespace', () => {
it('should return namespace info', async () => {
const info = await namespaceRepository.search({ ownerAddress: account.address }).toPromise();
expect(info.data.length).to.be.greaterThan(0);
});
});

describe('searchNamespace with streamer', () => {
it('should return namespace info', async () => {
const streamer = new NamespacePaginationStreamer(namespaceRepository);
const infoStreamer = await streamer
.search({ ownerAddress: account.address, pageSize: 20, order: Order.Desc })
.pipe(take(20), toArray())
.toPromise();
const info = await namespaceRepository.search({ pageSize: 20, order: Order.Desc }).toPromise();
expect(infoStreamer.length).to.be.greaterThan(0);
deepEqual(infoStreamer[0], info.data[0]);
});
});
});
2 changes: 1 addition & 1 deletion e2e/service/AccountService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('AccountService', () => {

describe('call accountNamespacesWithName', () => {
it('accountNamespacesWithName', async () => {
const info = await accountService.accountNamespacesWithName([account.address]).toPromise();
const info = await accountService.accountNamespacesWithName(account.address).toPromise();
expect(info).to.not.be.undefined;
expect(info.find((i) => i.id.equals(namespaceId))).to.not.be.undefined;
expect(info.find((i) => i.id.equals(namespaceId))?.namespaceName).to.be.equal(name);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"ripemd160": "^2.0.2",
"rxjs": "^6.5.3",
"rxjs-compat": "^6.5.3",
"symbol-openapi-typescript-fetch-client": "0.9.4",
"symbol-openapi-typescript-fetch-client": "0.9.5-SNAPSHOT.202007141039",
"tweetnacl": "^1.0.3",
"utf8": "^3.0.0",
"ws": "^7.2.3"
Expand Down
22 changes: 22 additions & 0 deletions src/infrastructure/AccountHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import { Http } from './Http';
import { SupplementalPublicKeys } from '../model/account/SupplementalPublicKeys';
import { AccountLinkPublicKey } from '../model/account/AccountLinkPublicKey';
import { AccountLinkVotingKey } from '../model/account/AccountLinkVotingKey';
import { AccountSearchCriteria } from './searchCriteria/AccountSearchCriteria';
import { DtoMapping } from '../core/utils/DtoMapping';
import { Page } from './Page';

/**
* Account http repository.
Expand Down Expand Up @@ -71,6 +74,25 @@ export class AccountHttp extends Http implements AccountRepository {
return this.call(this.accountRoutesApi.getAccountsInfo(accountIds), (body) => body.map(this.toAccountInfo));
}

/**
* Gets an array of accounts.
* @param criteria - Account search criteria
* @returns Observable<AccountInfo[]>
*/
public search(criteria: AccountSearchCriteria): Observable<Page<AccountInfo>> {
return this.call(
this.accountRoutesApi.searchAccounts(
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
DtoMapping.mapEnum(criteria.order),
DtoMapping.mapEnum(criteria.orderBy),
criteria.mosaicId?.toHex(),
),
(body) => super.toPage(body.pagination, body.data, this.toAccountInfo),
);
}

/**
* This method maps a AccountInfoDTO from rest to the SDK's AccountInfo model object.
*
Expand Down
4 changes: 3 additions & 1 deletion src/infrastructure/AccountRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import { Observable } from 'rxjs';
import { AccountInfo } from '../model/account/AccountInfo';
import { Address } from '../model/account/Address';
import { Searcher } from './paginationStreamer/Searcher';
import { AccountSearchCriteria } from './searchCriteria/AccountSearchCriteria';

/**
* Account interface repository.
*
* @since 1.0
*/
export interface AccountRepository {
export interface AccountRepository extends Searcher<AccountInfo, AccountSearchCriteria> {
/**
* Gets an AccountInfo for an account.
* @param address Address
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/BlockHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class BlockHttp extends Http implements BlockRepository {
}

/**
* Gets an array of bocks.
* Gets an array of blocks.
* @param criteria - Block search criteria
* @returns Observable<BlockInfo[]>
*/
Expand Down
74 changes: 35 additions & 39 deletions src/infrastructure/NamespaceHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import { NetworkType } from '../model/network/NetworkType';
import { UInt64 } from '../model/UInt64';
import { Http } from './Http';
import { NamespaceRepository } from './NamespaceRepository';
import { QueryParams } from './QueryParams';
import { NamespaceSearchCriteria } from './searchCriteria/NamespaceSearchCriteria';
import { Page } from './Page';
import { DtoMapping } from '../core/utils/DtoMapping';

/**
* Namespace http repository.
Expand Down Expand Up @@ -119,44 +121,12 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
return this.call(this.namespaceRoutesApi.getNamespace(namespaceId.toHex()), (body) => this.toNamespaceInfo(body));
}

/**
* Gets array of NamespaceInfo for an account
* @param address - Address
* @param queryParams - (Optional) Query params
* @returns Observable<NamespaceInfo[]>
*/
public getNamespacesFromAccount(address: Address, queryParams?: QueryParams): Observable<NamespaceInfo[]> {
return this.call(
this.namespaceRoutesApi.getNamespacesFromAccount(
address.plain(),
this.queryParams(queryParams).pageSize,
this.queryParams(queryParams).id,
),
(body) => body.namespaces.map((namespaceInfoDTO) => this.toNamespaceInfo(namespaceInfoDTO)),
);
}

/**
* Gets array of NamespaceInfo for different account
* @param addresses - Array of Address
* @param queryParams - (Optional) Query params
* @returns Observable<NamespaceInfo[]>
*/
public getNamespacesFromAccounts(addresses: Address[]): Observable<NamespaceInfo[]> {
const publicKeysBody = {
addresses: addresses.map((address) => address.plain()),
};
return this.call(this.namespaceRoutesApi.getNamespacesFromAccounts(publicKeysBody), (body) =>
body.namespaces.map((namespaceInfoDTO) => this.toNamespaceInfo(namespaceInfoDTO)),
);
}

/**
* Gets array of NamespaceName for different namespaceIds
* @param namespaceIds - Array of namespace ids
* @returns Observable<NamespaceName[]>
*/
public getNamespacesName(namespaceIds: NamespaceId[]): Observable<NamespaceName[]> {
public getNamespacesNames(namespaceIds: NamespaceId[]): Observable<NamespaceName[]> {
const namespaceIdsBody = {
namespaceIds: namespaceIds.map((id) => id.toHex()),
};
Expand All @@ -171,6 +141,27 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
);
}

/**
* Gets an array of namespaces.
* @param criteria - Namespace search criteria
* @returns Observable<NamespaceInfo[]>
*/
public search(criteria: NamespaceSearchCriteria): Observable<Page<NamespaceInfo>> {
return this.call(
this.namespaceRoutesApi.searchNamespaces(
criteria.ownerAddress?.plain(),
criteria.registrationType?.valueOf(),
criteria.level0?.toHex(),
criteria.aliasType?.valueOf(),
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
DtoMapping.mapEnum(criteria.order),
),
(body) => super.toPage(body.pagination, body.data, this.toNamespaceInfo),
);
}

/**
* Gets the MosaicId from a MosaicAlias
* @param namespaceId - the namespaceId of the namespace
Expand Down Expand Up @@ -239,16 +230,22 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
dto.meta.id,
dto.namespace.registrationType as number,
dto.namespace.depth,
this.extractLevels(dto.namespace),
NamespaceHttp.extractLevels(dto.namespace),
NamespaceId.createFromEncoded(dto.namespace.parentId),
Address.createFromEncoded(dto.namespace.ownerAddress),
UInt64.fromNumericString(dto.namespace.startHeight),
UInt64.fromNumericString(dto.namespace.endHeight),
this.extractAlias(dto.namespace),
NamespaceHttp.extractAlias(dto.namespace),
);
}

private extractLevels(namespace: NamespaceDTO): NamespaceId[] {
/**
* Extract the namespace levels
*
* @internal
* @param namespace
*/
private static extractLevels(namespace: NamespaceDTO): NamespaceId[] {
const result: NamespaceId[] = [];
if (namespace.level0) {
result.push(NamespaceId.createFromEncoded(namespace.level0));
Expand All @@ -266,10 +263,9 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
* Extract the alias from a namespace
*
* @internal
* @access private
* @param namespace
*/
private extractAlias(namespace: NamespaceDTO): Alias {
private static extractAlias(namespace: NamespaceDTO): Alias {
if (namespace.alias && namespace.alias.type.valueOf() === AliasType.Mosaic) {
return new MosaicAlias(new MosaicId(namespace.alias.mosaicId!));
} else if (namespace.alias && namespace.alias.type.valueOf() === AliasType.Address) {
Expand Down
23 changes: 4 additions & 19 deletions src/infrastructure/NamespaceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import { MosaicNames } from '../model/mosaic/MosaicNames';
import { NamespaceId } from '../model/namespace/NamespaceId';
import { NamespaceInfo } from '../model/namespace/NamespaceInfo';
import { NamespaceName } from '../model/namespace/NamespaceName';
import { QueryParams } from './QueryParams';
import { Searcher } from './paginationStreamer/Searcher';
import { NamespaceSearchCriteria } from './searchCriteria/NamespaceSearchCriteria';

/**
* Namespace interface repository.
*
* @since 1.0
*/
export interface NamespaceRepository {
export interface NamespaceRepository extends Searcher<NamespaceInfo, NamespaceSearchCriteria> {
/**
* Get readable names for a set of accountIds.
* Returns friendly names for accounts.
Expand All @@ -53,28 +54,12 @@ export interface NamespaceRepository {
*/
getNamespace(namespaceId: NamespaceId): Observable<NamespaceInfo>;

/**
* Gets array of NamespaceInfo for an account
* @param address - Address
* @param queryParams - (Optional) Query params
* @returns Observable<NamespaceInfo[]>
*/
getNamespacesFromAccount(address: Address, queryParams?: QueryParams): Observable<NamespaceInfo[]>;

/**
* Gets array of NamespaceInfo for different account
* @param addresses - Array of Address
* @param queryParams - (Optional) Query params
* @returns Observable<NamespaceInfo[]>
*/
getNamespacesFromAccounts(addresses: Address[], queryParams?: QueryParams): Observable<NamespaceInfo[]>;

/**
* Gets array of NamespaceName for different namespaceIds
* @param namespaceIds - Array of namespace ids
* @returns Observable<NamespaceName[]>
*/
getNamespacesName(namespaceIds: NamespaceId[]): Observable<NamespaceName[]>;
getNamespacesNames(namespaceIds: NamespaceId[]): Observable<NamespaceName[]>;

/**
* Gets the MosaicId from a MosaicAlias
Expand Down
34 changes: 34 additions & 0 deletions src/infrastructure/paginationStreamer/AccountPaginationStreamer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { PaginationStreamer } from './PaginationStreamer';
import { Searcher } from './Searcher';
import { AccountInfo } from '../../model/account/AccountInfo';
import { AccountSearchCriteria } from '../searchCriteria/AccountSearchCriteria';

/**
* A helper object that streams {@link AccountInfo} using the search.
*/
export class AccountPaginationStreamer extends PaginationStreamer<AccountInfo, AccountSearchCriteria> {
/**
* Constructor
*
* @param searcher the account repository that will perform the searches
*/
constructor(searcher: Searcher<AccountInfo, AccountSearchCriteria>) {
super(searcher);
}
}
Loading