Skip to content

Commit

Permalink
Merge pull request #168 from UniqueNetwork/release/v1.1.2
Browse files Browse the repository at this point in the history
Release/v1.1.2
  • Loading branch information
sswebcoder committed Nov 24, 2022
2 parents 36f9572 + 7700720 commit 1e576f1
Show file tree
Hide file tree
Showing 36 changed files with 957 additions and 593 deletions.
5 changes: 1 addition & 4 deletions apps/crawler/src/services/token/nesting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ export class TokenNestingService {
}
}

private async removeTokenFromParents(
collection_id: number,
token_id: number,
) {
public async removeTokenFromParents(collection_id: number, token_id: number) {
const parents = await this.getParentsByChildren(collection_id, token_id);

for (const parent of parents) {
Expand Down
3 changes: 2 additions & 1 deletion apps/crawler/src/services/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class TokenService {
blockHash: string,
): Promise<TokenData | null> {
let tokenDecoded = await this.sdkService.getToken(collectionId, tokenId);

if (!tokenDecoded) {
tokenDecoded = await this.sdkService.getToken(
collectionId,
Expand Down Expand Up @@ -175,6 +174,8 @@ export class TokenService {
}

async burn(collectionId: number, tokenId: number) {
await this.nestingService.removeTokenFromParents(collectionId, tokenId);

return this.tokensRepository.update(
{
collection_id: collectionId,
Expand Down
4 changes: 3 additions & 1 deletion apps/web-api/src/account/account.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Args,
ArgsType,
Field,
Info,
InputType,
ObjectType,
Query,
Expand Down Expand Up @@ -89,8 +90,9 @@ export class AccountResolver {
@Query(() => AccountDataResponse)
public async accounts(
@Args() args: QueryArgs,
@Info() info,
): Promise<IDataListResponse<AccountDTO>> {
return this.service.find(args);
return this.service.find(args, info);
}

@Query(() => StatisticDataResponse)
Expand Down
10 changes: 3 additions & 7 deletions apps/web-api/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../utils/gql-query-args';
import { AccountDTO } from './account.dto';
import { SentryWrapper } from '../utils/sentry.decorator';
import { GraphQLResolveInfo } from 'graphql';

@Injectable()
export class AccountService extends BaseService<Account, AccountDTO> {
Expand All @@ -21,16 +22,11 @@ export class AccountService extends BaseService<Account, AccountDTO> {
@SentryWrapper({ data: [], count: 0 })
public async find(
queryArgs: IGQLQueryArgs<AccountDTO>,
queryInfo: GraphQLResolveInfo,
): Promise<IDataListResponse<Account>> {
const qb = this.repo.createQueryBuilder();
qb.select('Account.account_id', 'account_id');
qb.addSelect('Account.available_balance', 'available_balance');
qb.addSelect('Account.free_balance', 'free_balance');
qb.addSelect('Account.locked_balance', 'locked_balance');
qb.addSelect('Account.timestamp', 'timestamp');
qb.addSelect('Account.block_height', 'block_height');
qb.addSelect('Account.account_id_normalized', 'account_id_normalized');

this.applySelect(qb, queryArgs, this.getQueryFields(queryInfo));
this.applyLimitOffset(qb, queryArgs);
this.applyWhereCondition(qb, queryArgs);
this.applyOrderCondition(qb, queryArgs);
Expand Down
5 changes: 3 additions & 2 deletions apps/web-api/src/block/block.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, Query, Resolver } from '@nestjs/graphql';
import { Args, Info, Query, Resolver } from '@nestjs/graphql';
import { IDataListResponse } from '../utils/gql-query-args';
import { BlockDto } from './block.dto';
import { BlockService } from './block.service';
Expand All @@ -11,7 +11,8 @@ export class BlockResolver {
@Query(() => BlockDataResponse)
public async block(
@Args() args: BlockQueryArgs,
@Info() info,
): Promise<IDataListResponse<BlockDto>> {
return this.service.find(args);
return this.service.find(args, info);
}
}
19 changes: 3 additions & 16 deletions apps/web-api/src/block/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseService } from '../utils/base.service';
import { IDataListResponse, IGQLQueryArgs } from '../utils/gql-query-args';
import { BlockDto } from './block.dto';
import { SentryWrapper } from '../utils/sentry.decorator';
import { GraphQLResolveInfo } from 'graphql';

@Injectable()
export class BlockService extends BaseService<Block, BlockDto> {
Expand All @@ -16,25 +17,11 @@ export class BlockService extends BaseService<Block, BlockDto> {
@SentryWrapper({ data: [], count: 0 })
public async find(
queryArgs: IGQLQueryArgs<BlockDto>,
queryInfo: GraphQLResolveInfo,
): Promise<IDataListResponse<Block>> {
const qb = this.repo.createQueryBuilder();

qb.select([
'block_number',
'timestamp',
'block_hash',
'parent_hash',
'extrinsics_root',
'state_root',
'spec_name',
'spec_version',
'total_events',
'num_transfers',
'new_accounts',
'timestamp',
'total_extrinsics',
]);

this.applySelect(qb, queryArgs, this.getQueryFields(queryInfo));
this.applyLimitOffset(qb, queryArgs);
this.applyWhereCondition(qb, queryArgs);
this.applyOrderCondition(qb, queryArgs);
Expand Down
15 changes: 12 additions & 3 deletions apps/web-api/src/collection/collection.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
import {
Args,
Info,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import { forwardRef, Inject } from '@nestjs/common';
import {
DateRangeArgs,
Expand All @@ -25,16 +32,18 @@ export class CollectionResolver {
@Query(() => CollectionDataResponse)
public async collections(
@Args() args: QueryArgs,
@Info() info,
): Promise<IDataListResponse<CollectionDTO>> {
return this.service.find(args);
return this.service.find(args, info);
}

@ResolveField()
async tokens(
@Parent() { collection_id }: CollectionEntity,
@Args({ nullable: true, defaultValue: {} }) args: TokenQueryArgs,
@Info() info,
) {
return this.tokenService.getByCollectionId(collection_id, args);
return this.tokenService.getByCollectionId(collection_id, args, info);
}

@Query(() => StatisticDataResponse)
Expand Down
137 changes: 57 additions & 80 deletions apps/web-api/src/collection/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { isEmpty } from 'lodash';
import { BaseService } from '../utils/base.service';
import { OperatorMethods } from '../utils/base.service.types';
import { IRelations, OperatorMethods } from '../utils/base.service.types';
import {
IDataListResponse,
IDateRange,
Expand All @@ -16,12 +16,26 @@ import { CollectionDTO } from './collection.dto';
import { TokenDTO } from '../tokens/token.dto';
import { TokenService } from '../tokens/token.service';
import { SentryWrapper } from '../utils/sentry.decorator';
import { GraphQLResolveInfo } from 'graphql';

const STATISTICS_RELATION_ALIAS = 'Statistics';

const relationsFields = {
tokens_count: 'Statistics',
actions_count: 'Statistics',
holders_count: 'Statistics',
transfers_count: 'Statistics',
tokens_count: STATISTICS_RELATION_ALIAS,
actions_count: STATISTICS_RELATION_ALIAS,
holders_count: STATISTICS_RELATION_ALIAS,
transfers_count: STATISTICS_RELATION_ALIAS,
};

const aliasFields = {
type: 'mode',
};

const customQueryFields = {
tokens_count: `COALESCE("${STATISTICS_RELATION_ALIAS}".tokens_count, 0::bigint)`,
actions_acount: `'COALESCE("${STATISTICS_RELATION_ALIAS}".actions_count, 0::bigint)`,
holders_count: `COALESCE("${STATISTICS_RELATION_ALIAS}".holders_count, 0::bigint)`,
transfers_count: `COALESCE("${STATISTICS_RELATION_ALIAS}".transfers_count, 0::bigint)`,
};

@Injectable()
Expand All @@ -30,59 +44,67 @@ export class CollectionService extends BaseService<Collections, CollectionDTO> {
@InjectRepository(Collections) private repo: Repository<Collections>,
@Inject(forwardRef(() => TokenService)) private tokenService: TokenService,
) {
super({ relationsFields, relations: ['tokens'] });
super({
relationsFields,
aliasFields,
customQueryFields,
relations: ['tokens'],
});
}

@SentryWrapper({ data: [], count: 0 })
public async find(
queryArgs: IGQLQueryArgs<CollectionDTO>,
queryInfo: GraphQLResolveInfo,
): Promise<IDataListResponse<CollectionDTO>> {
const qb = this.repo.createQueryBuilder();
this.applyFilters(qb, queryArgs);

this.applyArgs(qb, queryArgs, queryInfo);

return this.getDataAndCount(qb, queryArgs);
}

public async findOne(
queryArgs: IGQLQueryArgs<CollectionDTO>,
public getCollectionById(
id: number,
queryInfo: GraphQLResolveInfo,
): Promise<CollectionDTO> {
const qb = this.repo.createQueryBuilder();
qb.where(`collection_id = :id`, { id });

this.applyFilters(qb, queryArgs);
const queryFields = this.getQueryFields(queryInfo, { skip: ['__*'] });

return qb.getRawOne();
}
this.applySelect(qb, {}, queryFields);

public getCollectionById(id: number): Promise<CollectionDTO> {
return this.findOne({
where: { collection_id: { _eq: id } },
});
return qb.getRawOne();
}

public async statistic({
public statistic({
fromDate,
toDate,
}: IDateRange): Promise<IStatsResponse[]> {
const qb = await this.repo.createQueryBuilder();
const qb = this.repo.createQueryBuilder();
qb.select(`date_trunc('hour', TO_TIMESTAMP(date_of_creation))`, 'date');
qb.addSelect('count(*)', 'count');
qb.groupBy('date');

if (fromDate) {
qb.where(`"date_of_creation" >= ${this.formatDate(fromDate)}`);
}

if (toDate) {
qb.andWhere(`"date_of_creation" <= ${this.formatDate(fromDate)}`);
}

return qb.getRawMany();
}

private applyFilters(
private applyArgs(
qb: SelectQueryBuilder<Collections>,
queryArgs: IGQLQueryArgs<CollectionDTO>,
queryInfo: GraphQLResolveInfo,
): void {
this.select(qb);
this.select(qb, queryArgs, queryInfo);

this.applyLimitOffset(qb, queryArgs);
this.applyOrderCondition(qb, queryArgs);
this.applyWhereCondition(
Expand All @@ -108,65 +130,20 @@ export class CollectionService extends BaseService<Collections, CollectionDTO> {
}
}

private select(qb: SelectQueryBuilder<Collections>): void {
qb.select('Collections.collection_id', 'collection_id');
qb.addSelect('Collections.owner', 'owner');
qb.addSelect('Collections.owner_normalized', 'owner_normalized');
qb.addSelect('Collections.name', 'name');
qb.addSelect('Collections.description', 'description');
qb.addSelect('Collections.offchain_schema', 'offchain_schema');
qb.addSelect('Collections.token_limit', 'token_limit');
qb.addSelect('Collections.token_prefix', 'token_prefix');
qb.addSelect('Collections.collection_cover', 'collection_cover');
qb.addSelect('Collections.mode', 'type');
qb.addSelect('Collections.mint_mode', 'mint_mode');
qb.addSelect('Collections.nesting_enabled', 'nesting_enabled');
qb.addSelect('Collections.attributes_schema', 'attributes_schema');
qb.addSelect(
'Collections.limits_account_ownership',
'limits_account_ownership',
);
qb.addSelect(
'Collections.limits_sponsore_data_size',
'limits_sponsore_data_size',
);
qb.addSelect(
'Collections.limits_sponsore_data_rate',
'limits_sponsore_data_rate',
);
qb.addSelect('Collections.owner_can_transfer', 'owner_can_transfer');
qb.addSelect('Collections.owner_can_destroy', 'owner_can_destroy');
qb.addSelect('Collections.schema_version', 'schema_version');
qb.addSelect('Collections.sponsorship', 'sponsorship');
qb.addSelect('Collections.const_chain_schema', 'const_chain_schema');
qb.addSelect('Collections.burned', 'burned')
qb.addSelect('Collections.properties', 'properties');
qb.addSelect('Collections.permissions', 'permissions');
qb.addSelect(
'Collections.token_property_permissions',
'token_property_permissions',
);
qb.addSelect(
`COALESCE("Statistics".tokens_count, 0::bigint)`,
'tokens_count',
);
qb.addSelect(
`COALESCE("Statistics".holders_count, 0::bigint)`,
'holders_count',
);
qb.addSelect(
`COALESCE("Statistics".actions_count, 0::bigint)`,
'actions_count',
);
qb.addSelect(
`COALESCE("Statistics".transfers_count, 0::bigint)`,
'transfers_count',
);
qb.addSelect('Collections.date_of_creation', 'date_of_creation');
qb.leftJoin(
'collections_stats',
'Statistics',
'"Collections".collection_id = "Statistics".collection_id',
);
private select(
qb: SelectQueryBuilder<Collections>,
queryArgs: IGQLQueryArgs<CollectionDTO>,
queryInfo: GraphQLResolveInfo,
): void {
const queryFields = this.getQueryFields(queryInfo);

const relations = {
[STATISTICS_RELATION_ALIAS]: {
table: 'collections_stats',
on: `"Collections".collection_id = "${STATISTICS_RELATION_ALIAS}".collection_id`,
},
} as IRelations;

this.applySelect(qb, queryArgs, queryFields, relations);
}
}
14 changes: 13 additions & 1 deletion apps/web-api/src/event/event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Event } from '@entities/Event';
import { Field, Float, ObjectType } from '@nestjs/graphql';
import { Field, Float, Int, ObjectType } from '@nestjs/graphql';

@ObjectType('event')
export class EventDTO implements Partial<Event> {
@Field(() => String, { nullable: true })
block_index?: string;

@Field(() => String)
method?: string;

@Field(() => String)
section?: string;

@Field(() => String)
block_number?: string;

Expand All @@ -14,4 +20,10 @@ export class EventDTO implements Partial<Event> {

@Field(() => Float)
fee?: string;

@Field(() => Int, { nullable: true })
collection_id?: number;

@Field(() => Int, { nullable: true })
token_id?: number;
}
Loading

0 comments on commit 1e576f1

Please sign in to comment.