Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event history #58

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Transform } from 'class-transformer';
import { IsNotEmpty, Max, Min } from 'class-validator';

export class GetMonitorEventHistoryDto {
@ApiProperty()
@ApiProperty({ required: true })
@IsNotEmpty()
monitorId: string;

Expand All @@ -28,6 +28,28 @@ export class GetMonitorEventHistoryDto {
offset: number;
}

export class GetMonitorEventHistoryByAssociatedAddressDto extends GetMonitorEventHistoryDto {
@ApiProperty({ required: true })
@IsNotEmpty()
associatedAddress: string;
}

export class GetMonitorEventHistoryByHashDto extends GetMonitorEventHistoryDto {
@ApiProperty({ required: true })
@IsNotEmpty()
hash: string;
}

export class GetMonitorEventHistoryByEventIdDto {
@ApiProperty({ required: true })
@IsNotEmpty()
monitorId: string;

@ApiProperty({ required: true })
@IsNotEmpty()
eventId: string;
}

export class MonitorEventHistoryResponseDto {
@ApiResponseProperty()
eventId: string; // md5 of message exclude timestamp and confirm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Request } from 'express';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { User } from '../users/schemas/user.schema';
import {
GetMonitorEventHistoryByAssociatedAddressDto,
GetMonitorEventHistoryByEventIdDto,
GetMonitorEventHistoryByHashDto,
GetMonitorEventHistoryDto,
MonitorEventHistoryResponseDto,
} from './dto/event_history.dto';
Expand All @@ -33,4 +36,49 @@ export class EventHistoryController {
body,
);
}

@ApiOperation({ summary: 'Get Monitor Event History By Transaction Hash' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Get('by-hash')
@ApiOkResponse({ type: [MonitorEventHistoryResponseDto] })
async getMonitorEventHistoryByTxnHash(
@Req() req: Request,
@Query() body: GetMonitorEventHistoryByHashDto,
): Promise<MonitorEventHistoryResponseDto[]> {
return await this.eventHistoryService.getMonitorEventHistoryByTxnHash(
req.user as User,
body,
);
}

@ApiOperation({ summary: 'Get Monitor Event History By Event Id' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Get('by-event-id')
@ApiOkResponse({ type: [MonitorEventHistoryResponseDto] })
async getMonitorEventHistoryByEventId(
@Req() req: Request,
@Query() body: GetMonitorEventHistoryByEventIdDto,
): Promise<MonitorEventHistoryResponseDto> {
return await this.eventHistoryService.getMonitorEventHistoryByEventId(
req.user as User,
body,
);
}

@ApiOperation({ summary: 'Get Monitor Event History By Associated Address' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Get('by-address')
@ApiOkResponse({ type: [MonitorEventHistoryResponseDto] })
async getMonitorEventHistoryByAssociatedAddress(
@Req() req: Request,
@Query() body: GetMonitorEventHistoryByAssociatedAddressDto,
): Promise<MonitorEventHistoryResponseDto[]> {
return await this.eventHistoryService.getMonitorEventHistoryByAssociatedAddress(
req.user as User,
body,
);
}
}
162 changes: 162 additions & 0 deletions app/apps/onebox/src/modules/event_history/event_history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Injectable, Logger } from '@nestjs/common';
import { MonitorService } from '../monitor/monitor.service';
import { User } from '../users/schemas/user.schema';
import {
GetMonitorEventHistoryByAssociatedAddressDto,
GetMonitorEventHistoryByEventIdDto,
GetMonitorEventHistoryByHashDto,
GetMonitorEventHistoryDto,
MonitorEventHistoryResponseDto,
} from './dto/event_history.dto';
Expand Down Expand Up @@ -69,4 +72,163 @@ export class EventHistoryService {
this.logger.error(`network ${monitor.network} not supported`);
throw ErrorCode.INTERNAL_SERVER_ERROR.asException();
}

async getMonitorEventHistoryByTxnHash(
user: User,
request: GetMonitorEventHistoryByHashDto,
): Promise<MonitorEventHistoryResponseDto[]> {
const monitor = await this.monitorService.findAndAuthMonitor(
user,
request.monitorId,
);

if (!monitor) {
throw ErrorCode.MONITOR_NOT_FOUND.asException();
}
if (monitor.network === MonitorNetwork.Ethereum) {
return this.ethEventHistoryRepository
.findEventHistoryByMonitorAndHash(
monitor.monitorId,
request.hash,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}
if (monitor.network === MonitorNetwork.Polygon) {
return this.polygonEventHistoryRepository
.findEventHistoryByMonitorAndHash(
monitor.monitorId,
request.hash,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}

if (monitor.network === MonitorNetwork.Avalanche) {
return this.avaxEventHistoryRepository
.findEventHistoryByMonitorAndHash(
monitor.monitorId,
request.hash,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}

this.logger.error(`network ${monitor.network} not supported`);
throw ErrorCode.INTERNAL_SERVER_ERROR.asException();
}

async getMonitorEventHistoryByAssociatedAddress(
user: User,
request: GetMonitorEventHistoryByAssociatedAddressDto,
): Promise<MonitorEventHistoryResponseDto[]> {
const monitor = await this.monitorService.findAndAuthMonitor(
user,
request.monitorId,
);

if (!monitor) {
throw ErrorCode.MONITOR_NOT_FOUND.asException();
}
if (monitor.network === MonitorNetwork.Ethereum) {
return this.ethEventHistoryRepository
.findByMonitorAndAssociatedAddress(
monitor.monitorId,
request.associatedAddress,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}
if (monitor.network === MonitorNetwork.Polygon) {
return this.polygonEventHistoryRepository
.findEventHistoryByMonitorAndHash(
monitor.monitorId,
request.associatedAddress,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}

if (monitor.network === MonitorNetwork.Avalanche) {
return this.avaxEventHistoryRepository
.findEventHistoryByMonitorAndHash(
monitor.monitorId,
request.associatedAddress,
request.limit,
request.offset,
)
.then((response) => {
return response.map((event) =>
MonitorEventHistoryResponseDto.from(event),
);
});
}

this.logger.error(`network ${monitor.network} not supported`);
throw ErrorCode.INTERNAL_SERVER_ERROR.asException();
}

async getMonitorEventHistoryByEventId(
user: User,
request: GetMonitorEventHistoryByEventIdDto,
): Promise<MonitorEventHistoryResponseDto> {
const monitor = await this.monitorService.findAndAuthMonitor(
user,
request.monitorId,
);

if (!monitor) {
throw ErrorCode.MONITOR_NOT_FOUND.asException();
}
if (monitor.network === MonitorNetwork.Ethereum) {
return this.ethEventHistoryRepository
.findByEventId(request.eventId)
.then((response) => {
return MonitorEventHistoryResponseDto.from(response);
});
}
if (monitor.network === MonitorNetwork.Polygon) {
return this.polygonEventHistoryRepository
.findByEventId(request.eventId)
.then((response) => {
return MonitorEventHistoryResponseDto.from(response);
});
}

if (monitor.network === MonitorNetwork.Avalanche) {
return this.avaxEventHistoryRepository
.findByEventId(request.eventId)
.then((response) => {
return MonitorEventHistoryResponseDto.from(response);
});
}

this.logger.error(`network ${monitor.network} not supported`);
throw ErrorCode.INTERNAL_SERVER_ERROR.asException();
}
}
4 changes: 2 additions & 2 deletions app/apps/onebox/src/modules/monitor/dto/monitor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
export class MonitorConditionDto {
@ApiProperty({ default: true })
native: boolean;
@ApiProperty({ default: true })
@ApiProperty({ default: false })
internal: boolean;
@ApiProperty({ default: true })
erc721: boolean;
Expand Down Expand Up @@ -104,7 +104,7 @@ export class CreateMonitorDto {
@ApiProperty({
example: {
native: true,
internal: true,
internal: false,
erc721: true,
erc20: true,
specific: false,
Expand Down
4 changes: 4 additions & 0 deletions app/libs/global/src/global.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class GlobalController {
...SupportedChain.BSC,
enable: process.env.BSC_DISABLE === 'false',
},
{
...SupportedChain.MANTLE,
enable: process.env.MANTLE_DISABLE === 'false',
},
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export class EventHistoryRepository {
}

async findByMonitorAndAssociatedAddress(
monitor: string,
monitorId: string,
associatedAddress: string,
limit: number,
offset: number,
): Promise<EventHistory[]> {
return this.model.find({
monitorId: monitor,
associatedAddress: associatedAddress,
});
return this.model
.find({ monitorId: monitorId, associatedAddress: associatedAddress })
.limit(limit)
.skip(offset)
.sort({ dateCreated: -1 });
}

async getEventHistory(
Expand All @@ -50,8 +53,14 @@ export class EventHistoryRepository {
async findEventHistoryByMonitorAndHash(
monitorId: string,
hash: string,
limit: number,
offset: number,
): Promise<EventHistory[]> {
return this.model.find({ monitorId: monitorId, hash: hash });
return this.model
.find({ monitorId: monitorId, hash: hash })
.limit(limit)
.skip(offset)
.sort({ dateCreated: -1 });
}

async pushConfirmDeliveryId(eventId: string, deliveryId: string) {
Expand Down
2 changes: 1 addition & 1 deletion app/libs/utils/src/supportedChain.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class SupportedChain {
idHex: '0x2105',
name: 'Base',
confirmationBlock: 50,
explorer: 'https://basescan.org/',
explorer: 'https://basescan.org',
nativeCurrency: 'BASE',
};

Expand Down