Skip to content

Commit

Permalink
Merge pull request #56 from axone-protocol/refactor/range
Browse files Browse the repository at this point in the history
refactor: change range enum
  • Loading branch information
yevhen-burkovskyi authored Jul 3, 2024
2 parents 3f700a1 + 637ee7c commit cf98e2e
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/core/enums/db-time-interval.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum DBTimeInterval {
TWO_MINUTES = '2 minutes',
TWO_HOUR = '2 hour',
SIX_HOUR = '6 hour',
DAY = '1 day',
Expand Down
3 changes: 0 additions & 3 deletions src/core/enums/range.enum.ts
Original file line number Diff line number Diff line change
@@ -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',
}

6 changes: 6 additions & 0 deletions src/core/schemas/range.schema.ts
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 2 additions & 2 deletions src/modules/governance/dto/gov-overview.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface GovOverviewDto {
totalProposals: number;
currentProposals: number;
totalProposals: string;
currentProposals: string;
votingPeriod: string;
depositRequired: string;
}
4 changes: 2 additions & 2 deletions src/modules/governance/services/governance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,7 +56,7 @@ export class GovernanceService implements OnModuleInit {
return count;
}
return acc;
}, 0);
}, 0).toString();
}

private votingPeriodToView(votingPeriod: string) {
Expand Down
6 changes: 0 additions & 6 deletions src/modules/supply/schemas/change-range.schema.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/modules/supply/schemas/charts-range.schema.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/modules/supply/schemas/historical-range.schema.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/modules/supply/services/supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}],
]);
}
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/modules/supply/supply.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -32,15 +30,15 @@ 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);
}

@Get(SupplyEndpoints.CHARTS)
async getCharts(
@Query(QueryParam.RANGE, new SchemaValidatePipe(ChartsRangeSchema))
@Query(QueryParam.RANGE, new SchemaValidatePipe(RangeSchema))
range: Range
) {
return this.service.getCharts(range);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/token/dtos/token-info.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface TokenInfoDto {
price: ItemWithChange;
marketCap: ItemWithChange;
volume: number;
apr: string;
apr: number;
}

export interface ItemWithChange {
Expand Down
7 changes: 0 additions & 7 deletions src/modules/token/schemas/historical-price-range.schema.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/modules/token/services/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
]);
}
Expand Down Expand Up @@ -109,7 +108,7 @@ export class TokenService implements OnModuleInit {
change: mcap!.change,
},
volume: res.volume_24h,
apr,
apr: isNaN(Number(apr)) ? 0 : Number(apr),
};

await this.cache.cacheTokenInfo(tokenInfoDto);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit cf98e2e

Please sign in to comment.