Skip to content

Commit

Permalink
Operator Event API Patch (#274)
Browse files Browse the repository at this point in the history
* Add /delegation to route path

* Use getStrategiesWithShareUnderlying

* Modify share calculation logic

* Changes to add withEthValue flag which returns ethValue

* Add check for sharesUnderlying.ethPrice and remove Number typecast

---------

Co-authored-by: Udit <25996904+uditdc@users.noreply.github.com>
  • Loading branch information
surbhit14 and uditdc authored Oct 25, 2024
1 parent fcac90d commit 601615c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
40 changes: 23 additions & 17 deletions packages/api/src/routes/operators/operatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { withOperatorShares } from '../../utils/operatorShares'
import Prisma from '@prisma/client'
import prisma from '../../utils/prismaClient'
import { fetchTokenPrices } from '../../utils/tokenPrices'
import { WithTokenDataQuerySchema } from '../../schema/zod/schemas/withTokenDataQuery'
import { getSharesToUnderlying } from '../../../../seeder/src/utils/strategies'

type EventRecordArgs = {
staker: string
Expand All @@ -34,6 +32,7 @@ type EventRecord = {
args: EventRecordArgs
underlyingToken?: string
underlyingValue?: number
ethValue?: number
}

/**
Expand Down Expand Up @@ -397,9 +396,7 @@ export async function getOperatorRewards(req: Request, res: Response) {
* @param res
*/
export async function getOperatorEvents(req: Request, res: Response) {
const result = OperatorEventQuerySchema.and(WithTokenDataQuerySchema)
.and(PaginationQuerySchema)
.safeParse(req.query)
const result = OperatorEventQuerySchema.and(PaginationQuerySchema).safeParse(req.query)
if (!result.success) {
return handleAndReturnErrorResponse(req, res, result.error)
}
Expand All @@ -413,6 +410,7 @@ export async function getOperatorEvents(req: Request, res: Response) {
startAt,
endAt,
withTokenData,
withEthValue,
skip,
take
} = result.data
Expand Down Expand Up @@ -459,7 +457,7 @@ export async function getOperatorEvents(req: Request, res: Response) {
const fetchEventsForTypes = async (types: string[]) => {
const results = await Promise.all(
types.map((eventType) =>
fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, skip, take)
fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, withEthValue, skip, take)
)
)
return results
Expand Down Expand Up @@ -688,6 +686,7 @@ async function fetchAndMapEvents(
eventType: string,
baseFilterQuery: any,
withTokenData: boolean,
withEthValue: boolean,
skip: number,
take: number
): Promise<{ eventRecords: EventRecord[]; eventCount: number }> {
Expand Down Expand Up @@ -719,13 +718,15 @@ async function fetchAndMapEvents(
orderBy: { blockNumber: 'desc' }
})

const tokenPrices = withTokenData ? await fetchTokenPrices() : undefined
const sharesToUnderlying = withTokenData ? await getSharesToUnderlying() : undefined
const strategiesWithSharesUnderlying = withTokenData
? await getStrategiesWithShareUnderlying()
: undefined

const eventRecords = await Promise.all(
eventLogs.map(async (event) => {
let underlyingToken: string | undefined
let underlyingValue: number | undefined
let ethValue: number | undefined

if (
withTokenData &&
Expand All @@ -738,18 +739,22 @@ async function fetchAndMapEvents(
}
})

if (strategy && sharesToUnderlying) {
if (strategy && strategiesWithSharesUnderlying) {
underlyingToken = strategy.underlyingToken
const sharesMultiplier = Number(sharesToUnderlying.get(event.strategy.toLowerCase()))
const strategyTokenPrice = tokenPrices?.find(
(tp) => tp.address.toLowerCase() === strategy.underlyingToken.toLowerCase()

const sharesUnderlying = strategiesWithSharesUnderlying.find(
(s) => s.strategyAddress.toLowerCase() === event.strategy.toLowerCase()
)

if (sharesMultiplier && strategyTokenPrice) {
if (sharesUnderlying) {
underlyingValue =
(Number(event.shares) / Math.pow(10, strategyTokenPrice.decimals)) *
sharesMultiplier *
strategyTokenPrice.ethPrice
Number(
(BigInt(event.shares) * BigInt(sharesUnderlying.sharesToUnderlying)) / BigInt(1e18)
) / 1e18

if (withEthValue && sharesUnderlying.ethPrice) {
ethValue = underlyingValue * sharesUnderlying.ethPrice
}
}
}
}
Expand All @@ -767,7 +772,8 @@ async function fetchAndMapEvents(
...(withTokenData && {
underlyingToken: underlyingToken?.toLowerCase(),
underlyingValue
})
}),
...(withEthValue && { ethValue })
}
})
)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/routes/operators/operatorRoutes.ts
Original file line number Diff line number Diff line change
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', routeCache.cacheSeconds(120), getOperatorEvents)
router.get('/:address/events/delegation', routeCache.cacheSeconds(120), getOperatorEvents)

// Protected routes
router.get(
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/schema/zod/schemas/operatorEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import z from '../'
import { WithTokenDataQuerySchema, WithEthValueQuerySchema } from './withTokenDataQuery'

const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
const yyyymmddRegex = /^\d{4}-\d{2}-\d{2}$/
Expand Down Expand Up @@ -117,6 +118,20 @@ export const OperatorEventQuerySchema = z
.describe('End date in ISO string format')
.openapi({ example: '2024-04-12T08:31:11.000' })
})
.merge(WithTokenDataQuerySchema)
.merge(WithEthValueQuerySchema)
.refine(
(data) => {
if (data.withEthValue && !data.withTokenData) {
return false
}
return true
},
{
message: "'withEthValue' requires 'withTokenData' to be enabled.",
path: ['withEthValue']
}
)
.refine(
(data) => {
if ((data.type === 'DELEGATION' || data.type === 'UNDELEGATION') && data.strategyAddress) {
Expand Down
9 changes: 9 additions & 0 deletions packages/api/src/schema/zod/schemas/withTokenDataQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ export const WithTokenDataQuerySchema = z.object({
.transform((val) => val === 'true')
.openapi({ example: 'false' })
})

export const WithEthValueQuerySchema = z.object({
withEthValue: z
.enum(['true', 'false'])
.default('false')
.describe('Toggle whether the route should return value denominated in ETH')
.transform((val) => val === 'true')
.openapi({ example: 'false' })
})

0 comments on commit 601615c

Please sign in to comment.