From 7ae8a4fa1aa36f96c9b34e691f424b56de7e33da Mon Sep 17 00:00:00 2001 From: "yevhen.burkovskyi" Date: Wed, 3 Jul 2024 09:01:29 +0300 Subject: [PATCH 1/2] refactor: change range enum --- src/core/enums/db-time-interval.enum.ts | 1 - src/core/enums/range.enum.ts | 3 --- src/core/schemas/range.schema.ts | 6 ++++++ src/modules/governance/dto/gov-overview.dto.ts | 4 ++-- src/modules/governance/services/governance.service.ts | 4 ++-- src/modules/supply/schemas/change-range.schema.ts | 6 ------ src/modules/supply/schemas/charts-range.schema.ts | 6 ------ src/modules/supply/schemas/historical-range.schema.ts | 6 ------ src/modules/supply/services/supply.service.ts | 5 ++--- src/modules/supply/supply.controller.ts | 10 ++++------ src/modules/token/dtos/token-info.dto.ts | 2 +- .../token/schemas/historical-price-range.schema.ts | 7 ------- src/modules/token/services/token.service.ts | 3 +-- src/modules/token/token.controller.ts | 4 ++-- 14 files changed, 20 insertions(+), 47 deletions(-) create mode 100644 src/core/schemas/range.schema.ts delete mode 100644 src/modules/supply/schemas/change-range.schema.ts delete mode 100644 src/modules/supply/schemas/charts-range.schema.ts delete mode 100644 src/modules/supply/schemas/historical-range.schema.ts delete mode 100644 src/modules/token/schemas/historical-price-range.schema.ts diff --git a/src/core/enums/db-time-interval.enum.ts b/src/core/enums/db-time-interval.enum.ts index 4aab151..affbf0f 100644 --- a/src/core/enums/db-time-interval.enum.ts +++ b/src/core/enums/db-time-interval.enum.ts @@ -1,5 +1,4 @@ export enum DBTimeInterval { - TWO_MINUTES = '2 minutes', TWO_HOUR = '2 hour', SIX_HOUR = '6 hour', DAY = '1 day', diff --git a/src/core/enums/range.enum.ts b/src/core/enums/range.enum.ts index 6ed6d15..a40ba2b 100644 --- a/src/core/enums/range.enum.ts +++ b/src/core/enums/range.enum.ts @@ -1,11 +1,8 @@ export enum Range { ALL = 'all', - FIVE_MIN = 'fiveMin', - HOUR = 'hour', DAY = 'day', WEEK = 'week', MONTH = 'month', - THREE_MONTH = 'threeMonth', YEAR = 'year', } \ No newline at end of file diff --git a/src/core/schemas/range.schema.ts b/src/core/schemas/range.schema.ts new file mode 100644 index 0000000..c27deb4 --- /dev/null +++ b/src/core/schemas/range.schema.ts @@ -0,0 +1,6 @@ +import { Range } from '@core/enums/range.enum'; +import * as Joi from 'joi'; + +export const RangeSchema = Joi.string() + .valid(...Object.values(Range)) + .required(); \ No newline at end of file diff --git a/src/modules/governance/dto/gov-overview.dto.ts b/src/modules/governance/dto/gov-overview.dto.ts index 3399327..b14f104 100644 --- a/src/modules/governance/dto/gov-overview.dto.ts +++ b/src/modules/governance/dto/gov-overview.dto.ts @@ -1,6 +1,6 @@ export interface GovOverviewDto { - totalProposals: number; - currentProposals: number; + totalProposals: string; + currentProposals: string; votingPeriod: string; depositRequired: string; } \ No newline at end of file diff --git a/src/modules/governance/services/governance.service.ts b/src/modules/governance/services/governance.service.ts index ed0617b..4adad89 100644 --- a/src/modules/governance/services/governance.service.ts +++ b/src/modules/governance/services/governance.service.ts @@ -28,7 +28,7 @@ export class GovernanceService implements OnModuleInit { const govProposals = await this.okp4Service.getProposals(); const govOverview: GovOverviewDto = { - totalProposals: Number.parseInt(govProposals.pagination.total), + totalProposals: govProposals.pagination.total, currentProposals: this.currentProposals(govProposals.proposals), votingPeriod: this.votingPeriodToView( govResponse.voting_params.voting_period @@ -56,7 +56,7 @@ export class GovernanceService implements OnModuleInit { return count; } return acc; - }, 0); + }, 0).toString(); } private votingPeriodToView(votingPeriod: string) { diff --git a/src/modules/supply/schemas/change-range.schema.ts b/src/modules/supply/schemas/change-range.schema.ts deleted file mode 100644 index d49da3e..0000000 --- a/src/modules/supply/schemas/change-range.schema.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Range } from '@core/enums/range.enum'; -import * as Joi from 'joi'; - -export const ChangeRangeSchema = Joi.string() - .valid(Range.FIVE_MIN, Range.HOUR, Range.DAY, Range.WEEK, Range.MONTH) - .required(); \ No newline at end of file diff --git a/src/modules/supply/schemas/charts-range.schema.ts b/src/modules/supply/schemas/charts-range.schema.ts deleted file mode 100644 index b02de3a..0000000 --- a/src/modules/supply/schemas/charts-range.schema.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Range } from "@core/enums/range.enum"; -import * as Joi from "joi"; - -export const ChartsRangeSchema = Joi.string() - .valid(Range.FIVE_MIN, Range.HOUR, Range.DAY, Range.WEEK, Range.MONTH) - .required(); diff --git a/src/modules/supply/schemas/historical-range.schema.ts b/src/modules/supply/schemas/historical-range.schema.ts deleted file mode 100644 index defd67f..0000000 --- a/src/modules/supply/schemas/historical-range.schema.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Range } from '@core/enums/range.enum'; -import * as Joi from 'joi'; - -export const HistoricalRangeSchema = Joi.string() - .valid(Range.ALL, Range.DAY, Range.WEEK, Range.MONTH, Range.HOUR) - .required(); \ No newline at end of file diff --git a/src/modules/supply/services/supply.service.ts b/src/modules/supply/services/supply.service.ts index a856e1d..9f903ec 100644 --- a/src/modules/supply/services/supply.service.ts +++ b/src/modules/supply/services/supply.service.ts @@ -26,10 +26,10 @@ export class SupplyService implements OnModuleInit { private readonly cache: SupplyCache, ) { this.rangeTimeIntervalMap = new Map([ - [Range.HOUR, { interval: DBTimeInterval.TWO_MINUTES, count: 30 }], [Range.DAY, { interval: DBTimeInterval.TWO_HOUR, count: 12 }], [Range.WEEK, { interval: DBTimeInterval.SIX_HOUR, count: 28 }], [Range.MONTH, { interval: DBTimeInterval.DAY, count: 30 }], + [Range.YEAR, { interval: DBTimeInterval.MONTH, count: 12 }], [Range.ALL, { interval: DBTimeInterval.MONTH}], ]); } @@ -181,11 +181,10 @@ export class SupplyService implements OnModuleInit { private calculatePastDateByRange(range: Range): Date { let date = new Date(); switch (range) { - case Range.FIVE_MIN: date = new Date(date.setMinutes(date.getMinutes() - 5)); break; - case Range.HOUR: date = new Date(date.setHours(date.getHours() - 1)); break; case Range.DAY: date = new Date(date.setDate(date.getDate() - 1)); break; case Range.WEEK: date = new Date(date.setDate(date.getDate() - 7)); break; case Range.MONTH: date = new Date(date.setMonth(date.getMonth() - 1)); break; + case Range.YEAR: date = new Date(date.setFullYear(date.getFullYear() - 1)); break; } return date; diff --git a/src/modules/supply/supply.controller.ts b/src/modules/supply/supply.controller.ts index e93a869..e9f81e6 100644 --- a/src/modules/supply/supply.controller.ts +++ b/src/modules/supply/supply.controller.ts @@ -4,11 +4,9 @@ import { SupplyEndpoints } from "./enums/supply-endpoints.enum"; import { SupplyCache } from "./services/supply.cache"; import { QueryParam } from "./enums/query-param.enum"; import { SchemaValidatePipe } from "@core/pipes/schema-validate.pipe"; -import { HistoricalRangeSchema } from "./schemas/historical-range.schema"; import { SupplyService } from "./services/supply.service"; -import { ChangeRangeSchema } from "./schemas/change-range.schema"; import { Range } from "@core/enums/range.enum"; -import { ChartsRangeSchema } from "./schemas/charts-range.schema"; +import { RangeSchema } from "@core/schemas/range.schema"; @Controller(Routes.SUPPLY) export class SupplyController { @@ -19,7 +17,7 @@ export class SupplyController { @Get(SupplyEndpoints.HISTORICAL) async getHistoricalSupply( - @Query(QueryParam.RANGE, new SchemaValidatePipe(HistoricalRangeSchema)) + @Query(QueryParam.RANGE, new SchemaValidatePipe(RangeSchema)) range: Range ) { return this.cache.getSupplyHistorical(range); @@ -32,7 +30,7 @@ export class SupplyController { @Get(SupplyEndpoints.CHANGE) async getSupplyChange( - @Query(QueryParam.RANGE, new SchemaValidatePipe(ChangeRangeSchema)) + @Query(QueryParam.RANGE, new SchemaValidatePipe(RangeSchema)) range: Range ) { return this.service.getSupplyChange(range); @@ -40,7 +38,7 @@ export class SupplyController { @Get(SupplyEndpoints.CHARTS) async getCharts( - @Query(QueryParam.RANGE, new SchemaValidatePipe(ChartsRangeSchema)) + @Query(QueryParam.RANGE, new SchemaValidatePipe(RangeSchema)) range: Range ) { return this.service.getCharts(range); diff --git a/src/modules/token/dtos/token-info.dto.ts b/src/modules/token/dtos/token-info.dto.ts index 5348297..bcfdcab 100644 --- a/src/modules/token/dtos/token-info.dto.ts +++ b/src/modules/token/dtos/token-info.dto.ts @@ -2,7 +2,7 @@ export interface TokenInfoDto { price: ItemWithChange; marketCap: ItemWithChange; volume: number; - apr: string; + apr: number; } export interface ItemWithChange { diff --git a/src/modules/token/schemas/historical-price-range.schema.ts b/src/modules/token/schemas/historical-price-range.schema.ts deleted file mode 100644 index 690ef0d..0000000 --- a/src/modules/token/schemas/historical-price-range.schema.ts +++ /dev/null @@ -1,7 +0,0 @@ -import * as Joi from 'joi'; - -import { Range } from '@core/enums/range.enum'; - -export const HistoricalPriceRange = Joi.string() - .valid(Range.ALL, Range.DAY, Range.MONTH, Range.THREE_MONTH, Range.WEEK, Range.YEAR) - .required(); \ No newline at end of file diff --git a/src/modules/token/services/token.service.ts b/src/modules/token/services/token.service.ts index efb255e..3a93dd2 100644 --- a/src/modules/token/services/token.service.ts +++ b/src/modules/token/services/token.service.ts @@ -33,7 +33,6 @@ export class TokenService implements OnModuleInit { [Range.DAY, { interval: DBTimeInterval.TWO_HOUR, count: 12 }], [Range.WEEK, { interval: DBTimeInterval.SIX_HOUR, count: 28 }], [Range.MONTH, { interval: DBTimeInterval.DAY, count: 30 }], - [Range.THREE_MONTH, { interval: DBTimeInterval.THREE_DAY, count: 30 }], [Range.YEAR, { interval: DBTimeInterval.MONTH, count: 12 }], ]); } @@ -109,7 +108,7 @@ export class TokenService implements OnModuleInit { change: mcap!.change, }, volume: res.volume_24h, - apr, + apr: Number(apr), }; await this.cache.cacheTokenInfo(tokenInfoDto); diff --git a/src/modules/token/token.controller.ts b/src/modules/token/token.controller.ts index dd47e0d..6c78c0f 100644 --- a/src/modules/token/token.controller.ts +++ b/src/modules/token/token.controller.ts @@ -4,8 +4,8 @@ import { TokenCache } from "./services/token.cache"; import { TokenEndpoint } from "./enums/token-endpoint.enum"; import { QueryParam } from "@core/enums/query-param.enum"; import { SchemaValidatePipe } from "@core/pipes/schema-validate.pipe"; -import { HistoricalPriceRange } from "./schemas/historical-price-range.schema"; import { Range } from "@core/enums/range.enum"; +import { RangeSchema } from "@core/schemas/range.schema"; @Controller(Routes.TOKEN) export class TokenController { @@ -20,7 +20,7 @@ export class TokenController { @Get(TokenEndpoint.HISTORICAL) async getHistoricalPrice( - @Query(QueryParam.RANGE, new SchemaValidatePipe(HistoricalPriceRange)) + @Query(QueryParam.RANGE, new SchemaValidatePipe(RangeSchema)) range: Range, ) { return this.cache.getTokenHistoricalPrice(range); From 637ee7cdc910f3d8c908049e0eca7e707ab9edfc Mon Sep 17 00:00:00 2001 From: "yevhen.burkovskyi" Date: Wed, 3 Jul 2024 09:43:40 +0300 Subject: [PATCH 2/2] refactor: apr is nan verification --- src/modules/token/services/token.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/token/services/token.service.ts b/src/modules/token/services/token.service.ts index 3a93dd2..5accbdb 100644 --- a/src/modules/token/services/token.service.ts +++ b/src/modules/token/services/token.service.ts @@ -108,7 +108,7 @@ export class TokenService implements OnModuleInit { change: mcap!.change, }, volume: res.volume_24h, - apr: Number(apr), + apr: isNaN(Number(apr)) ? 0 : Number(apr), }; await this.cache.cacheTokenInfo(tokenInfoDto);