Skip to content

Commit

Permalink
Merge pull request #157 from dappradar/change_grpc_to_restapi
Browse files Browse the repository at this point in the history
Change grpc to restapi
  • Loading branch information
Sonmezturk authored Jul 4, 2023
2 parents 9dc3f64 + 127d892 commit 5e68d34
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 221 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prebuild": "rimraf dist",
"dev": "APP_ENV=dev ts-node src/main.ts",
"prod": "APP_ENV=prod node dist/main",
"build": "rm -rf src/generated && mkdir src/generated && protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=esModuleInterop=true,nestJs=true,addNestjsRestParameter=true --ts_proto_out=src/generated proto/*.proto && tsc -p tsconfig.build.json",
"build": "rm -rf src/generated && mkdir src/generated && tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand Down
79 changes: 0 additions & 79 deletions proto/defi-providers.proto

This file was deleted.

37 changes: 13 additions & 24 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
import { Controller, UseFilters } from '@nestjs/common';
import { Body, Controller, Get, Post, Query, UseFilters } from '@nestjs/common';
import { AppService } from './app.service';
import { GrpcMethod } from '@nestjs/microservices';
import { GenericErrorFilter } from './genericErrorFilter';
import {
GetTvlRequest,
GetTvlReply,
GetPoolAndTokenVolumesRequest,
GetPoolAndTokenVolumesReply,
GetTokenDetailsReply,
GetTokenDetailsRequest,
HealthCheckRequest,
GetPoolAndTokenVolumesRequest,
GetTvlReply,
GetTvlRequest,
HealthCheckReply,
} from './generated/proto/defi-providers';
import { GenericRpcErrorFilter } from './genericRpcError';
} from './interfaces/IController';

@Controller()
@UseFilters(new GenericRpcErrorFilter())
@UseFilters(new GenericErrorFilter())
export class AppController {
constructor(private readonly appService: AppService) {}

@GrpcMethod('DefiProviders', 'GetTvl')
async getTvl(req: GetTvlRequest): Promise<GetTvlReply> {
@Get('tvl')
async getTvl(@Query() req: GetTvlRequest): Promise<GetTvlReply> {
return await this.appService.getTvl(req);
}

@GrpcMethod('DefiProviders', 'GetPoolAndTokenVolumes')
@Post('pool-token-volumes')
async getPoolAndTokenVolumes(
req: GetPoolAndTokenVolumesRequest,
@Body() req: GetPoolAndTokenVolumesRequest,
): Promise<GetPoolAndTokenVolumesReply> {
return await this.appService.getPoolAndTokenVolumes(req);
}

@GrpcMethod('DefiProviders', 'GetTokenDetails')
async getTokenDetails(
req: GetTokenDetailsRequest,
): Promise<GetTokenDetailsReply> {
return await this.appService.getTokenDetails(req);
}

@GrpcMethod('DefiProviders', 'HealthCheck')
async heathCheck(req: HealthCheckRequest): Promise<HealthCheckReply> {
@Get('health-check')
async heathCheck(req): Promise<HealthCheckReply> {
return { run: true };
}
}
3 changes: 1 addition & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { FactoryModule } from './factory/factory.module';
import { GenericInterceptor } from './generic.interceptor';
import { HealthController } from './health/health.controller';

@Module({
imports: [FactoryModule],
controllers: [AppController, HealthController],
controllers: [AppController],
providers: [
AppService,
{
Expand Down
17 changes: 6 additions & 11 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import {
GetTvlRequest,
GetTvlReply,
GetPoolAndTokenVolumesRequest,
GetPoolAndTokenVolumesReply,
GetTokenDetailsRequest,
GetPoolAndTokenVolumesRequest,
GetTokenDetailsReply,
} from './generated/proto/defi-providers';
GetTokenDetailsRequest,
GetTvlReply,
GetTvlRequest,
} from './interfaces/IController';

import { FactoryService } from './factory/factory.service';

@Injectable()
Expand All @@ -22,10 +23,4 @@ export class AppService {
): Promise<GetPoolAndTokenVolumesReply> {
return await this.factoryService.getPoolAndTokenVolumes(req);
}

async getTokenDetails(
req: GetTokenDetailsRequest,
): Promise<GetTokenDetailsReply> {
return await this.factoryService.getTokenDetails(req);
}
}
72 changes: 20 additions & 52 deletions src/factory/factory.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Injectable } from '@nestjs/common';
import BigNumber from 'bignumber.js';
import {
GetTvlRequest,
GetTvlReply,
GetPoolAndTokenVolumesRequest,
GetPoolAndTokenVolumesReply,
GetPoolAndTokenVolumesRequest,
GetTvlReply,
GetTvlRequest,
PoolVolume,
TokenVolume,
GetTokenDetailsRequest,
GetTokenDetailsReply,
} from '../generated/proto/defi-providers';
} from '../interfaces/IController';

import { RpcException } from '@nestjs/microservices';
import { Web3ProviderService } from '../web3Provider/web3Provider.service';
import { log } from '../util/logger/logger';
Expand All @@ -32,7 +31,7 @@ export class FactoryService {
req: GetTvlRequest,
timeoutErrorCount = 0,
): Promise<GetTvlReply> {
if (req.query.block === undefined) {
if (req.block === undefined) {
throw new RpcException('Block is undefined');
}
if (this.web3ProviderService.checkNodeUrl(req?.chain)) {
Expand All @@ -41,59 +40,37 @@ export class FactoryService {
const providerService: IProvider = await import(
this.getProviderServicePath(req.chain, req.provider, 'index')
);
const block = parseInt(req.block) - basicUtil.getDelay(req.chain);
const tvlData = await providerService.tvl({
web3: await this.web3ProviderService.getWeb3(req?.chain),
chain: req?.chain,
provider: req?.provider,
block,
date: req?.date,
});

const block = parseInt(req.query.block) - basicUtil.getDelay(req.chain);

try {
const tvlData = await providerService.tvl({
web3: await this.web3ProviderService.getWeb3(req?.chain),
chain: req?.chain,
provider: req?.provider,
block,
date: req.query?.date,
});

const balances = basicUtil.checkZeroBalance(tvlData.balances);
return { balances, poolBalances: tvlData.poolBalances };
} catch (err) {
log.error({
message: err?.message || '',
stack: err?.stack || '',
detail: `Error: chain: ${req.chain}, provider: ${req?.provider}, blocknumber: ${block}, `,
endpoint: 'getTvl',
});
if (err?.message?.toLowerCase() == 'timeout' && timeoutErrorCount < 3) {
log.error({
message: err?.message || '',
stack: err?.stack || '',
detail: `Error: web3Instance changed for chain: ${req.chain}, provider: ${req?.provider}, blocknumber: ${block} `,
endpoint: 'getTvl',
});
timeoutErrorCount++;
await this.web3ProviderService.changeInstance(req?.chain);
return this.getTvl(req, timeoutErrorCount);
}
}
const balances = basicUtil.checkZeroBalance(tvlData.balances);
return { balances, poolBalances: tvlData.poolBalances };
}

async getPoolAndTokenVolumes(
req: GetPoolAndTokenVolumesRequest,
): Promise<GetPoolAndTokenVolumesReply> {
if (req.query.block === undefined) {
if (req.block === undefined) {
throw new RpcException('Block is undefined');
}

const providerService: IProvider = await import(
this.getProviderServicePath(req.chain, req.provider, 'index')
);

const block = parseInt(req.query.block) - basicUtil.getDelay(req.chain);
const block = parseInt(req.block) - basicUtil.getDelay(req.chain);

const poolVolumes = await providerService.getPoolVolumes({
chain: req.chain,
provider: req.provider,
block,
pools: req.query.pools,
pools: req.pools,
});
for (const [, poolVolume] of Object.entries(poolVolumes)) {
poolVolume.volumes = poolVolume.volumes.map((volume) =>
Expand All @@ -106,7 +83,7 @@ export class FactoryService {
chain: req.chain,
provider: req.provider,
block,
tokens: req.query.tokens,
tokens: req.tokens,
});
for (const [, tokenVolume] of Object.entries(tokenVolumes)) {
tokenVolume.volume = BigNumber(tokenVolume.volume).toFixed();
Expand All @@ -116,15 +93,6 @@ export class FactoryService {
return { poolVolumes, tokenVolumes };
}

async getTokenDetails(
req: GetTokenDetailsRequest,
): Promise<GetTokenDetailsReply> {
const { address, name, symbol, decimals, logo } = await import(
this.getProviderServicePath(req.chain, req.provider, 'data.json')
);
return { address, name, symbol, decimals, logo };
}

getProviderServicePath(
chain: string,
provider: string,
Expand Down
4 changes: 3 additions & 1 deletion src/generic.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class GenericInterceptor implements NestInterceptor {
const endpoint = context.getHandler();
log.info({
message:
endpoint.name == 'getPoolAndTokenVolumes' ? '' : JSON.stringify(req),
endpoint.name == 'getPoolAndTokenVolumes'
? ''
: JSON.stringify(req.query),
endpoint: endpoint.name,
});
return next.handle().pipe();
Expand Down
13 changes: 5 additions & 8 deletions src/genericRpcError.ts → src/genericErrorFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ import { Observable, throwError } from 'rxjs';
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpStatus,
RpcExceptionFilter,
} from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
@Catch()
export class GenericRpcErrorFilter implements RpcExceptionFilter<RpcException> {
export class GenericErrorFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost): Observable<any> {
const ctx = host.switchToRpc();
const ctx = host.switchToHttp();

const errorResponse: any = {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
timestamp: new Date().toISOString(),
errorName: exception?.name,
message: exception?.message,
requestData: ctx.getData(),
requestData: JSON.stringify(ctx.getRequest().query),
};

log.error({
message: errorResponse?.message,
stack: exception?.stack || '',
detail: errorResponse?.errorResponsestatusCode,
endpoint: `path: ${host.getArgs()[2]?.call?.handler?.path} chain: ${
ctx.getData()?.chain
} provider: ${ctx.getData()?.provider}`,
endpoint: ctx.getRequest().url,
});
return throwError(() => errorResponse);
}
Expand Down
24 changes: 0 additions & 24 deletions src/health/health.controller.ts

This file was deleted.

Loading

0 comments on commit 5e68d34

Please sign in to comment.