Skip to content

Commit

Permalink
fix bug, confirm event history
Browse files Browse the repository at this point in the history
handle error on send transaction to webhook service
  • Loading branch information
cauta committed Apr 22, 2024
1 parent a736ffb commit cbc8639
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
38 changes: 25 additions & 13 deletions app/apps/monitor-service/src/ethereum/ethereum.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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,
);
}
}
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
);
}
Expand Down

0 comments on commit cbc8639

Please sign in to comment.