From cbc86393c3177d5e905e5d6e63f12c2fb37fd614 Mon Sep 17 00:00:00 2001 From: cauta Date: Fri, 19 Apr 2024 14:51:13 +0700 Subject: [PATCH] fix bug, confirm event history handle error on send transaction to webhook service --- .../src/ethereum/ethereum.service.ts | 38 ++++++++++++------- .../repositories/event_history.repository.ts | 5 ++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/apps/monitor-service/src/ethereum/ethereum.service.ts b/app/apps/monitor-service/src/ethereum/ethereum.service.ts index 783a33a..42da928 100644 --- a/app/apps/monitor-service/src/ethereum/ethereum.service.ts +++ b/app/apps/monitor-service/src/ethereum/ethereum.service.ts @@ -216,7 +216,6 @@ export class EthereumService { tokenId: string, type: WebhookType, ) { - // @todo check condition of monitor and event log if it match for (const address of addresses) { const monitor = await this.findMonitor(address.monitorId); // ignore monitor condition on erc721 @@ -239,7 +238,10 @@ export class EthereumService { tokenId, ); - const response = await this.dispatchMessageToWebhook(monitor, transaction); + const response = await this.dispatchMessageToWebhook( + monitor, + transaction, + ); this.saveHistory(transaction, response); this.logger.debug( @@ -257,7 +259,6 @@ export class EthereumService { value: string, type: WebhookType, ) { - // @todo check condition of monitor and event log if it match for (const address of addresses) { const monitor = await this.findMonitor(address.monitorId); // ignore monitor condition on erc20 @@ -281,7 +282,7 @@ export class EthereumService { ); const response = await this.dispatchMessageToWebhook(monitor, txnHistory); - this.saveHistory(txnHistory, response); + await this.saveHistory(txnHistory, response); this.logger.debug( `Confirmed: ${confirm} ERC20 transfer ${type.toUpperCase()}:\n${JSON.stringify( @@ -292,17 +293,28 @@ export class EthereumService { } private async saveHistory( - transaction: EventHistory, + event: EventHistory, delivery: DispatchWebhookResponse, ) { - // @todo handle when dispatch message to webhook service - if (!transaction.confirm) { - transaction.deliveryIds = [delivery.id]; - await this.eventHistoryRepository.saveEventHistory(transaction); + let deliveryId: string; + if (!delivery) { + this.logger.error( + `Save event ${event.confirm ? 'CONFIRMED' : 'DETECT'} ${ + event.eventId + } with error can not dispatch this event`, + ); + deliveryId = 'ERROR'; + } else { + deliveryId = delivery.id; + } + + if (!event.confirm) { + event.deliveryIds = [deliveryId]; + await this.eventHistoryRepository.saveEventHistory(event); } else { - this.eventHistoryRepository.pushDeliveryId( - transaction.eventId, - delivery.id, + await this.eventHistoryRepository.pushConfirmDeliveryId( + event.eventId, + deliveryId, ); } } @@ -353,7 +365,7 @@ export class EthereumService { this.logger.debug( `Dispatch webhook successfully response: ${JSON.stringify(respone)}`, ); - this.projectQuotaService.increaseUsed(monitor.projectId); + await this.projectQuotaService.increaseUsed(monitor.projectId); return respone; } catch (error) { this.logger.error( diff --git a/app/libs/shared_modules/src/event_history/repositories/event_history.repository.ts b/app/libs/shared_modules/src/event_history/repositories/event_history.repository.ts index c65fe0b..c595713 100644 --- a/app/libs/shared_modules/src/event_history/repositories/event_history.repository.ts +++ b/app/libs/shared_modules/src/event_history/repositories/event_history.repository.ts @@ -54,13 +54,16 @@ export class EventHistoryRepository { return this.model.find({ monitorId: monitorId, hash: hash }); } - async pushDeliveryId(eventId: string, deliveryId: string) { + async pushConfirmDeliveryId(eventId: string, deliveryId: string) { return this.model.updateOne( { eventId: eventId }, { $push: { deliveryIds: deliveryId, }, + $set: { + confirm: true, + }, }, ); }