From 250f9b1c413428cfe691c57789df65740009c55c Mon Sep 17 00:00:00 2001 From: Sotatek-TanHoang Date: Wed, 12 Jun 2024 17:27:57 +0700 Subject: [PATCH] feat: remove unused code remove unused pagination code adjust tsconfig.json --- src/console.ts | 3 +- src/core/paginate-typeorm.ts | 37 ++--------------------- src/modules/users/users.service.ts | 6 ++-- src/shared/modules/web3/web3.service.ts | 40 +++++++++++-------------- tsconfig.json | 2 ++ 5 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/console.ts b/src/console.ts index 4748ff7..4a4d097 100644 --- a/src/console.ts +++ b/src/console.ts @@ -13,10 +13,9 @@ bootstrap.init().then(async app => { await app.init(); await bootstrap.boot(); await app.close(); - process.exit(0); } catch (e) { console.error(e); await app.close(); - process.exit(1); + process.exitCode = -1; } }); diff --git a/src/core/paginate-typeorm.ts b/src/core/paginate-typeorm.ts index 0b2188d..36af35b 100644 --- a/src/core/paginate-typeorm.ts +++ b/src/core/paginate-typeorm.ts @@ -1,40 +1,7 @@ -import { Brackets, QueryBuilder } from 'typeorm'; - import { PageMetaDto } from '@shared/dtos/page-meta.dto'; import { PageOptionsDto } from '@shared/dtos/page-options.dto'; import { PageDto } from '@shared/dtos/page.dto'; -import { transformQuery } from '@shared/utils/transform-query'; - -declare global { - interface Array { - toPageDto(this: T[], pageMetaDto: PageOptionsDto, itemCount: number): PageDto; - } -} -declare module 'typeorm' { - interface QueryBuilder { - searchByString(q: string, columnNames: string[]): this; - } +export function toPageDto(data: any[], pageMetaDto: PageOptionsDto, itemCount: number) { + return new PageDto(data, new PageMetaDto(pageMetaDto, itemCount)); } - -Array.prototype.toPageDto = function (pageMetaDto: PageOptionsDto, itemCount: number) { - return new PageDto(this, new PageMetaDto(pageMetaDto, itemCount)); -}; - -QueryBuilder.prototype.searchByString = function (q, columnNames) { - if (!q) { - return this; - } - - this.andWhere( - new Brackets(qb => { - for (const item of columnNames) { - qb.orWhere(`LOWER(${item}) LIKE :q`); - } - }), - ); - - this.setParameter('q', `%${transformQuery(q.toString())}%`); - - return this; -}; diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 37e833f..ba9ac5c 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -11,6 +11,8 @@ import { ENetworkName } from '@constants/blockchain.constant'; import { EEnvKey } from '@constants/env.constant'; import { EError } from '@constants/error.constant'; +import { toPageDto } from '@core/paginate-typeorm'; + import { TokenPair } from '@modules/users/entities/tokenpair.entity'; import { httpBadRequest } from '@shared/exceptions/http-exeption'; @@ -48,14 +50,14 @@ export class UsersService { async getHistoriesOfUser(address: string, options) { try { const [data, count] = await this.eventLogRepository.getHistoriesOfUser(address, options); - return data.toPageDto(options, count); + return toPageDto(data, options, count); } catch (error) {} } async getHistories(options) { try { const [data, count] = await this.eventLogRepository.getHistories(options); - return data.toPageDto(options, count); + return toPageDto(data, options, count); } catch (error) {} } diff --git a/src/shared/modules/web3/web3.service.ts b/src/shared/modules/web3/web3.service.ts index f8b6269..f77de81 100644 --- a/src/shared/modules/web3/web3.service.ts +++ b/src/shared/modules/web3/web3.service.ts @@ -81,29 +81,25 @@ export class DefaultContract { } public async estimateGas(method: string, param: Array, specifySignerIndex?: number): Promise { - try { - const signer = this.rpcService.web3.eth.accounts.privateKeyToAccount( - this.rpcService.privateKeys[specifySignerIndex ?? 0], - ); + const signer = this.rpcService.web3.eth.accounts.privateKeyToAccount( + this.rpcService.privateKeys[specifySignerIndex ?? 0], + ); - const data = this.contract.methods[method](...param).encodeABI(); - const gasPrice = await this.rpcService.web3.eth.getGasPrice(); - const nonce = await this.rpcService.getNonce(signer.address); + const data = this.contract.methods[method](...param).encodeABI(); + const gasPrice = await this.rpcService.web3.eth.getGasPrice(); + const nonce = await this.rpcService.getNonce(signer.address); - // gas estimation - const rawTx = { - nonce: nonce, - gasPrice: toHex(toBN(gasPrice)), - from: signer.address, - to: this.contractAddress, - data: data, - }; + // gas estimation + const rawTx = { + nonce: nonce, + gasPrice: toHex(toBN(gasPrice)), + from: signer.address, + to: this.contractAddress, + data: data, + }; - const gasLimit = await this.rpcService.web3.eth.estimateGas(rawTx as any); - return gasLimit; - } catch (error) { - return error; - } + const gasLimit = await this.rpcService.web3.eth.estimateGas(rawTx as any); + return gasLimit; } public async write( @@ -141,7 +137,7 @@ export class DefaultContract { error: null, data: await this.rpcService.web3.eth.sendSignedTransaction(signedTx.rawTransaction), }; - } catch (error) { + } catch (error: any) { return { success: false, error, data: null }; } } @@ -182,7 +178,7 @@ export class DefaultContract { error: null, data: response, }; - } catch (error) { + } catch (error: any) { return { success: false, error, data: null }; } } diff --git a/tsconfig.json b/tsconfig.json index eb0aaef..a04c8da 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,8 @@ "compilerOptions": { "module": "commonjs", "declaration": true, + "strict": true, + "noUncheckedIndexedAccess": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true,