diff --git a/src/database/repositories/event-log.repository.ts b/src/database/repositories/event-log.repository.ts index b624635..ef6440d 100644 --- a/src/database/repositories/event-log.repository.ts +++ b/src/database/repositories/event-log.repository.ts @@ -203,4 +203,11 @@ export class EventLogRepository extends BaseRepository { return qb.getRawOne() as any; } + async getNumOfPendingTx(): Promise<{ network: ENetworkName; count: string }[]> { + const qb = this.createQb(); + qb.select(['network_received as network', 'count(1)']); + qb.where("status = 'waiting'"); + qb.groupBy('network_received'); + return qb.getRawMany(); + } } diff --git a/src/modules/crawler/job-unlock.provider.ts b/src/modules/crawler/job-unlock.provider.ts index d6d5ac2..f05e907 100644 --- a/src/modules/crawler/job-unlock.provider.ts +++ b/src/modules/crawler/job-unlock.provider.ts @@ -12,6 +12,7 @@ import { CommonConfigRepository } from '../../database/repositories/common-confi import { EventLogRepository } from '../../database/repositories/event-log.repository.js'; import { LoggerService } from '../../shared/modules/logger/logger.service.js'; import { QueueService } from '../../shared/modules/queue/queue.service.js'; +import { RedisClientService } from '../../shared/modules/redis/redis-client.service.js'; import { addDecimal } from '../../shared/utils/bignumber.js'; import { sleep } from '../../shared/utils/promise.js'; import { getNextDayInUnix, getTimeInFutureInMinutes } from '../../shared/utils/time.js'; @@ -28,6 +29,7 @@ export class JobUnlockProvider { private readonly eventLogRepository: EventLogRepository, private readonly commonConfigRepository: CommonConfigRepository, private tokenPriceCrawler: BatchJobGetPriceToken, + private readonly redisClient: RedisClientService, ) {} private logger = this.loggerService.getLogger('JOB_UNLOCK_PROVIDER'); @@ -36,6 +38,7 @@ export class JobUnlockProvider { this.getPendingTx(true), this.getPendingTx(false), this.tokenPriceCrawler.handleCrawlInterval(), + this.updateTotalPendingTxCount(), ]); } @@ -78,7 +81,22 @@ export class JobUnlockProvider { } catch (error) { this.logger.error(error); } finally { - await sleep(5); + await sleep(20); + } + } + } + private async updateTotalPendingTxCount() { + while (true) { + try { + const counts = await this.eventLogRepository.getNumOfPendingTx(); + this.logger.info(`SET_PENDING_TX_COUNT: ${JSON.stringify(counts)}`); + for (const { network, count } of counts) { + await this.redisClient.setCountWaitingTx(network, Number(count).valueOf()); + } + } catch (error) { + this.logger.warn('SET_PENDING_TX_COUNT', error); + } finally { + await sleep(60); } } } @@ -169,7 +187,6 @@ export class JobUnlockProvider { }, ); } - // TODO: fix this private async isPassDailyQuota(address: string, networkReceived: ENetworkName): Promise { const fromDecimal = this.configService.get( networkReceived === ENetworkName.MINA ? EEnvKey.DECIMAL_TOKEN_EVM : EEnvKey.DECIMAL_TOKEN_MINA, diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index fc6bc53..f1d4fb0 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -135,8 +135,8 @@ export class UsersService { } calcWaitingTime(receivedNetwork: ENetworkName, currentPendingTx: number): number { const receivedNetworkEstWaiting = { - [ENetworkName.MINA]: 10 * 60 * currentPendingTx, - [ENetworkName.ETH]: 10 * currentPendingTx, + [ENetworkName.MINA]: 10 * 60 * (1 + currentPendingTx), + [ENetworkName.ETH]: 10 * (1 + currentPendingTx), }; // total waiting tx * time_process_each + crawler delays from both lock and unlock return receivedNetworkEstWaiting[receivedNetwork] + 60 + 60 * 15; diff --git a/src/shared/modules/redis/redis-client.service.ts b/src/shared/modules/redis/redis-client.service.ts index 7c2ed69..a69392a 100644 --- a/src/shared/modules/redis/redis-client.service.ts +++ b/src/shared/modules/redis/redis-client.service.ts @@ -36,7 +36,7 @@ export class RedisClientService implements OnModuleInit, OnModuleDestroy { const res = await this.client.get(`waiting_tx_${network}`); return Number(res).valueOf(); } - public async initCountWaitingTx(network: ENetworkName, initValue: number) { + public async setCountWaitingTx(network: ENetworkName, initValue: number) { return this.client.set(`waiting_tx_${network}`, initValue); } public async incrCountWaitingTx(network: ENetworkName): Promise {