Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added token info endpoint #8

Merged
merged 2 commits into from
May 9, 2024
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
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import tseslint from "typescript-eslint";


export default [
{languageOptions: { globals: globals.browser }},
{ languageOptions: { globals: globals.browser } },
{ rules: { indent: ["error", 2, { SwitchCase: 1 }] } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"scripts": {
"build": "nest build",
"start": "nest start",
"dev": "npm run prisma:generate && dotenvx run --env-file=.env.local -- nest start --watch --debug",
"prisma:generate": "npx prisma generate",
"dev": "npm run prisma:init && dotenvx run --env-file=.env.local -- nest start --watch --debug",
"prisma:init": "npx prisma generate && npx dotenvx run --env-file=.env.local -- prisma migrate deploy",
"upgrade": "ncu -u && npm i"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE "historical_mcap" (
"time" TIMESTAMP(3) NOT NULL,
"mcap" DOUBLE PRECISION NOT NULL,
"change" DOUBLE PRECISION NOT NULL,

CONSTRAINT "historical_mcap_pkey" PRIMARY KEY ("time")
);

CREATE UNIQUE INDEX "historical_mcap_time_key" ON "historical_mcap"("time");
7 changes: 7 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ model HistoricalSupply {
supply String
change Float
@@map("historical_supply")
}

model HistoricalMcap {
time DateTime @id @unique
mcap Float
change Float
@@map("historical_mcap")
}
2 changes: 1 addition & 1 deletion src/core/config/config.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export interface RedisConfig {
}

export interface CacheConfig {
userStackingTtl: string;
userStackingTtl: number;
}
2 changes: 1 addition & 1 deletion src/core/config/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const ConfigSchema = Joi.object({
TOKEN_DENOM: Joi.string().required(),
REDIS_HOST: Joi.string().required(),
REDIS_PORT: Joi.string().required(),
USER_STACKING_TTL: Joi.string().required(),
USER_STACKING_TTL: Joi.number().required(),
}).required();
2 changes: 1 addition & 1 deletion src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export const config: ConfigDto = {
port: process.env.REDIS_PORT!
},
cache: {
userStackingTtl: process.env.USER_STACKING_TTL!,
userStackingTtl: +process.env.USER_STACKING_TTL!,
yevhen-burkovskyi marked this conversation as resolved.
Show resolved Hide resolved
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export enum QueryParam {
RANGE = 'range',
}
RANGE = 'range',
}
2 changes: 1 addition & 1 deletion src/core/enums/routes.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum Routes {
PRICE = 'price',
SUPPLY = 'supply',
STACKING = 'stacking',
TOKEN = 'token',
}
72 changes: 36 additions & 36 deletions src/core/lib/okp4/okp4.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,48 @@ export class Okp4Service {
constructor(private readonly httpService: HttpService) {}


private constructUrl(endpoint: string, params?: string): string {
return `${this.BASE_URL}/${endpoint}${params ? `?${params}` : ''}`;
}
private constructUrl(endpoint: string, params?: string): string {
return `${this.BASE_URL}/${endpoint}${params ? `?${params}` : ''}`;
}

async getSupplyByDenom(denom: string): Promise<SupplyByDenomResponse> {
return this.errorHandleWrapper(
this.httpService.get.bind(
null,
this.constructUrl(
Endpoints.SUPPLY_BY_DENOM,
createUrlParams({ denom }),
),
),
);
}
async getSupplyByDenom(denom: string): Promise<SupplyByDenomResponse> {
return this.errorHandleWrapper(
this.httpService.get.bind(
null,
this.constructUrl(
Endpoints.SUPPLY_BY_DENOM,
createUrlParams({ denom }),
),
),
);
}

async getDelegations(addr: string): Promise<WithPaginationResponse<GetDelegationsResponse>> {
return this.errorHandleWrapper(
this.httpService.get.bind(
null,
this.constructUrl(`${Endpoints.STACKING_DELEGATIONS}/${addr}`,)
),
async getDelegations(addr: string): Promise<WithPaginationResponse<GetDelegationsResponse>> {
return this.errorHandleWrapper(
this.httpService.get.bind(
null,
this.constructUrl(`${Endpoints.STACKING_DELEGATIONS}/${addr}`,)
),
);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async errorHandleWrapper<T>(fn: any): Promise<T> {
try {
const response: GSFResponse<T> = await fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async errorHandleWrapper<T>(fn: any): Promise<T> {
try {
const response: GSFResponse<T> = await fn();

if (this.isFailedResponse(response)) {
throw new BadRequestException(response.message);
}
if (this.isFailedResponse(response)) {
throw new BadRequestException(response.message);
}

return response as T;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
throw new BadRequestException(e.message);
}
return response as T;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
throw new BadRequestException(e.message);
}
}

private isFailedResponse<T>(response: GSFResponse<T>): response is FailedResponse {
return (response as FailedResponse).message !== undefined;
}
private isFailedResponse<T>(response: GSFResponse<T>): response is FailedResponse {
return (response as FailedResponse).message !== undefined;
}
}
2 changes: 2 additions & 0 deletions src/core/lib/osmosis/enums/endpoints.enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export enum Endpoints {
HISTORICAL_PRICE = 'tokens/v2/historical/:symbol/chart',
TOKEN_BY_SYMBOL = 'tokens/v2/:symbol',
MARKET_CAP = 'tokens/v2/mcap',
}
35 changes: 28 additions & 7 deletions src/core/lib/osmosis/osmosis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { FailedResponse } from './responses/failed.response';
import { GSFResponse } from './responses/generic-success-failed.response';
import { HistoricalChartRes } from './responses/historical-chart.response';
import { HttpService } from '../http.service';
import { TokenInfoResponse } from './responses/token-info.response';
import { McapResponse } from './responses/mcap.response';

@Injectable()
export class OsmosisService {
Expand All @@ -21,6 +23,15 @@ export class OsmosisService {
return `${this.BASE_URL}/${endpoint}${params ? `?${params}` : ''}`;
}

private getWithErrorHandling<T>(url: string): Promise<T> {
return this.errorHandleWrapper(
this.httpService.get.bind(
null,
url,
),
);
}

async getHistoricalChart(
payload: GetHistoricalChartDto,
): Promise<HistoricalChartRes> {
Expand All @@ -29,17 +40,27 @@ export class OsmosisService {
payload.symbol,
);

return this.errorHandleWrapper(
this.httpService.get.bind(
null,
this.constructUrl(
endpoint,
createUrlParams({ tf: payload.range.toString() }),
),
return this.getWithErrorHandling(
this.constructUrl(
endpoint,
createUrlParams({ tf: payload.range.toString() }),
),
);
}

async getTokenInfo(symbol: string): Promise<TokenInfoResponse[]> {
const endpoint = Endpoints.TOKEN_BY_SYMBOL.replace(
RouteParam.SYMBOL,
symbol,
);

return this.getWithErrorHandling(this.constructUrl(endpoint));
}

async getMcap(): Promise<McapResponse[]> {
return this.getWithErrorHandling(this.constructUrl(Endpoints.MARKET_CAP));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async errorHandleWrapper<T>(fn: any): Promise<T> {
try {
Expand Down
4 changes: 4 additions & 0 deletions src/core/lib/osmosis/responses/mcap.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface McapResponse {
symbol: string;
market_cap: number;
}
13 changes: 13 additions & 0 deletions src/core/lib/osmosis/responses/token-info.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface TokenInfoResponse {
price: number;
denom: string;
symbol: string;
liquidity: number;
liquidity_24h_change: number;
volume_24h: number;
volume_24h_change: number;
name: string;
price_24h_change: number;
exponent: number;
display: string;
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { config } from '@core/config/config';
logger: new NestLoggerImpl(),
});

app.enableCors();
await app.listen(config.app.port, config.app.host);

await showAvailableRoutes(app);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ScheduleModule } from '@nestjs/schedule';
import { CacheModule } from '@nestjs/cache-manager';
import * as redisStore from 'cache-manager-redis-store';

import { PriceModule } from './price/price.module';
import { SupplyModule } from './supply/supply.module';
import { StackingModule } from './stacking/stacking.module';
import { config } from '@core/config/config';
import { TokenModule } from './token/token.module';

@Module({
imports: [
Expand All @@ -17,7 +17,7 @@ import { config } from '@core/config/config';
host: config.redis.host,
port: config.redis.port,
}),
PriceModule,
TokenModule,
SupplyModule,
StackingModule,
],
Expand Down
3 changes: 0 additions & 3 deletions src/modules/price/enums/price-endpoints.enum.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/modules/price/price.controller.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/modules/price/price.module.ts

This file was deleted.

Loading
Loading