Skip to content

Commit

Permalink
Merge pull request #20 from sotatek-dev/refactor/lint
Browse files Browse the repository at this point in the history
feat: remove unused code
  • Loading branch information
Sotatek-TanHoang authored Jun 13, 2024
2 parents 8f521f8 + 250f9b1 commit 6200579
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 61 deletions.
3 changes: 1 addition & 2 deletions src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
37 changes: 2 additions & 35 deletions src/core/paginate-typeorm.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
toPageDto<Dto>(this: T[], pageMetaDto: PageOptionsDto, itemCount: number): PageDto<Dto>;
}
}

declare module 'typeorm' {
interface QueryBuilder<Entity> {
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;
};
6 changes: 4 additions & 2 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {}
}

Expand Down
40 changes: 18 additions & 22 deletions src/shared/modules/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,25 @@ export class DefaultContract {
}

public async estimateGas(method: string, param: Array<any>, specifySignerIndex?: number): Promise<number> {
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(
Expand Down Expand Up @@ -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 };
}
}
Expand Down Expand Up @@ -182,7 +178,7 @@ export class DefaultContract {
error: null,
data: response,
};
} catch (error) {
} catch (error: any) {
return { success: false, error, data: null };
}
}
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand Down

0 comments on commit 6200579

Please sign in to comment.