Skip to content

Commit

Permalink
Merge pull request #224 from sotatek-dev/feat/est-api
Browse files Browse the repository at this point in the history
feat: job update pending tx count
  • Loading branch information
Sotatek-TanHoang authored Dec 27, 2024
2 parents 3ac9e23 + fe0b1e8 commit c9014e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/database/repositories/event-log.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,11 @@ export class EventLogRepository extends BaseRepository<EventLog> {

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();
}
}
21 changes: 19 additions & 2 deletions src/modules/crawler/job-unlock.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand All @@ -36,6 +38,7 @@ export class JobUnlockProvider {
this.getPendingTx(true),
this.getPendingTx(false),
this.tokenPriceCrawler.handleCrawlInterval(),
this.updateTotalPendingTxCount(),
]);
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -169,7 +187,6 @@ export class JobUnlockProvider {
},
);
}
// TODO: fix this
private async isPassDailyQuota(address: string, networkReceived: ENetworkName): Promise<boolean> {
const fromDecimal = this.configService.get(
networkReceived === ENetworkName.MINA ? EEnvKey.DECIMAL_TOKEN_EVM : EEnvKey.DECIMAL_TOKEN_MINA,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/modules/redis/redis-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
Expand Down

0 comments on commit c9014e4

Please sign in to comment.