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

Operator Event API Pagination bug fix #287

Closed
wants to merge 2 commits into from
Closed
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
51 changes: 19 additions & 32 deletions packages/api/src/routes/operators/operatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ export async function getOperatorRewards(req: Request, res: Response) {
}

/**
* Function for route /operators/:address/events
* Fetches and returns a list of events for a specific operator with optional filters
* Function for route /operators/:address/events/delegation
* Fetches and returns a list of delegation-related events for a specific operator with optional filters
*
* @param req
* @param res
*/
export async function getOperatorEvents(req: Request, res: Response) {
export async function getOperatorDelegationEvents(req: Request, res: Response) {
const result = OperatorEventQuerySchema.and(PaginationQuerySchema).safeParse(req.query)
if (!result.success) {
return handleAndReturnErrorResponse(req, res, result.error)
Expand Down Expand Up @@ -445,35 +445,31 @@ export async function getOperatorEvents(req: Request, res: Response) {
}
}

let eventRecords: EventRecord[] = []
let eventCount = 0

const eventTypesToFetch = type
? [type]
: strategyAddress
? ['SHARES_INCREASED', 'SHARES_DECREASED']
: ['SHARES_INCREASED', 'SHARES_DECREASED', 'DELEGATION', 'UNDELEGATION']

const fetchEventsForTypes = async (types: string[]) => {
const results = await Promise.all(
types.map((eventType) =>
fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, withEthValue, skip, take)
const allEvents = (
await Promise.all(
eventTypesToFetch.map((eventType) =>
fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, withEthValue)
)
)
return results
}
).flatMap((result) => result.eventRecords)

const results = await fetchEventsForTypes(eventTypesToFetch)

eventRecords = results.flatMap((result) => result.eventRecords)
eventRecords = eventRecords
.sort((a, b) => (b.blockNumber > a.blockNumber ? 1 : -1))
.slice(0, take)
const sortedEvents = allEvents.sort((a, b) => {
if (b.blockNumber > a.blockNumber) return 1
if (b.blockNumber < a.blockNumber) return -1
return 0
})

eventCount = results.reduce((acc, result) => acc + result.eventCount, 0)
const paginatedEvents = sortedEvents.slice(skip, skip + take)
const eventCount = allEvents.length

const response = {
data: eventRecords,
data: paginatedEvents,
meta: {
total: eventCount,
skip,
Expand Down Expand Up @@ -686,10 +682,8 @@ async function fetchAndMapEvents(
eventType: string,
baseFilterQuery: any,
withTokenData: boolean,
withEthValue: boolean,
skip: number,
take: number
): Promise<{ eventRecords: EventRecord[]; eventCount: number }> {
withEthValue: boolean
): Promise<{ eventRecords: EventRecord[] }> {
const modelName = (() => {
switch (eventType) {
case 'SHARES_INCREASED':
Expand All @@ -707,14 +701,8 @@ async function fetchAndMapEvents(

const model = prisma[modelName] as any

const eventCount = await model.count({
where: baseFilterQuery
})

const eventLogs = await model.findMany({
where: baseFilterQuery,
skip,
take,
orderBy: { blockNumber: 'desc' }
})

Expand Down Expand Up @@ -779,7 +767,6 @@ async function fetchAndMapEvents(
)

return {
eventRecords,
eventCount
eventRecords
}
}
4 changes: 2 additions & 2 deletions packages/api/src/routes/operators/operatorRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getOperator,
getAllOperatorAddresses,
getOperatorRewards,
getOperatorEvents,
getOperatorDelegationEvents,
invalidateMetadata
} from './operatorController'
import { authenticateJWT } from '../../utils/jwtUtils'
Expand Down Expand Up @@ -109,7 +109,7 @@ router.get('/:address', routeCache.cacheSeconds(120), getOperator)

router.get('/:address/rewards', routeCache.cacheSeconds(120), getOperatorRewards)

router.get('/:address/events/delegation', routeCache.cacheSeconds(120), getOperatorEvents)
router.get('/:address/events/delegation', routeCache.cacheSeconds(120), getOperatorDelegationEvents)

// Protected routes
router.get(
Expand Down
Loading