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

Dev to Main v0.3.8 #338

Merged
merged 5 commits into from
Jan 28, 2025
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
46 changes: 28 additions & 18 deletions packages/api/src/routes/avs/avsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
AvsRegistrationEventQuerySchema,
RewardsEventQuerySchema
} from '../../schema/zod/schemas/eventSchemas'
import { MinTvlQuerySchema } from '../../schema/zod/schemas/minTvlQuerySchema'

/**
* Function for route /avs
Expand All @@ -35,6 +36,7 @@ import {
export async function getAllAVS(req: Request, res: Response) {
// Validate pagination query
const queryCheck = PaginationQuerySchema.and(WithTvlQuerySchema)
.and(MinTvlQuerySchema)
.and(SortByQuerySchema)
.and(WithCuratedMetadata)
.and(SearchByTextQuerySchema)
Expand All @@ -49,6 +51,7 @@ export async function getAllAVS(req: Request, res: Response) {
skip,
take,
withTvl,
minTvl,
withCuratedMetadata,
sortByTvl,
sortByTotalStakers,
Expand Down Expand Up @@ -76,7 +79,8 @@ export async function getAllAVS(req: Request, res: Response) {
const avsRecords = await prisma.avs.findMany({
where: {
...getAvsFilterQuery(true),
...searchFilterQuery
...searchFilterQuery,
...(minTvl ? { tvlEth: { gte: minTvl } } : {})
},
include: {
curatedMetadata: withCuratedMetadata,
Expand Down Expand Up @@ -104,7 +108,8 @@ export async function getAllAVS(req: Request, res: Response) {
const avsCount = await prisma.avs.count({
where: {
...getAvsFilterQuery(true),
...searchFilterQuery
...searchFilterQuery,
...(minTvl ? { tvlEth: { gte: minTvl } } : {})
}
})

Expand Down Expand Up @@ -395,6 +400,7 @@ export async function getAVSStakers(req: Request, res: Response) {
export async function getAVSOperators(req: Request, res: Response) {
// Validate query and params
const queryCheck = PaginationQuerySchema.and(WithTvlQuerySchema)
.and(MinTvlQuerySchema)
.and(SortByQuerySchema)
.and(SearchByTextQuerySchema)
.safeParse(req.query)
Expand All @@ -409,7 +415,8 @@ export async function getAVSOperators(req: Request, res: Response) {

try {
const { address } = req.params
const { skip, take, withTvl, sortOperatorsByTvl, searchByText, searchMode } = queryCheck.data
const { skip, take, withTvl, minTvl, sortOperatorsByTvl, searchByText, searchMode } =
queryCheck.data
const searchFilterQuery = getOperatorSearchQuery(searchByText, searchMode, 'partial')

const avs = await prisma.avs.findUniqueOrThrow({
Expand All @@ -435,7 +442,8 @@ export async function getAVSOperators(req: Request, res: Response) {
},
{
...searchFilterQuery
}
},
...(minTvl ? [{ tvlEth: { gte: minTvl } }] : [])
] as Prisma.Prisma.OperatorWhereInput[]
},
orderBy: sortOperatorsByTvl
Expand All @@ -447,20 +455,22 @@ export async function getAVSOperators(req: Request, res: Response) {
take
})

const total = searchByText
? await prisma.operator.count({
where: {
AND: [
{
address: { in: avs.operators.map((o) => o.operatorAddress) }
},
{
...searchFilterQuery
}
] as Prisma.Prisma.OperatorWhereInput[]
}
})
: avs.operators.length
const total =
searchByText || minTvl
? await prisma.operator.count({
where: {
AND: [
{
address: { in: avs.operators.map((o) => o.operatorAddress) }
},
{
...searchFilterQuery
},
...(minTvl ? [{ tvlEth: { gte: minTvl } }] : [])
] as Prisma.Prisma.OperatorWhereInput[]
}
})
: avs.operators.length

const strategiesWithSharesUnderlying = withTvl ? await getStrategiesWithShareUnderlying() : []

Expand Down
19 changes: 16 additions & 3 deletions packages/api/src/routes/operators/operatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Prisma from '@prisma/client'
import prisma from '../../utils/prismaClient'
import { fetchTokenPrices } from '../../utils/tokenPrices'
import { fetchDelegationEvents, fetchRegistrationEvents } from '../../utils/eventUtils'
import { MinTvlQuerySchema } from '../../schema/zod/schemas/minTvlQuerySchema'

/**
* Function for route /operators
Expand All @@ -32,6 +33,7 @@ import { fetchDelegationEvents, fetchRegistrationEvents } from '../../utils/even
export async function getAllOperators(req: Request, res: Response) {
// Validate pagination query
const result = PaginationQuerySchema.and(WithTvlQuerySchema)
.and(MinTvlQuerySchema)
.and(SortByQuerySchema)
.and(SearchByTextQuerySchema)
.safeParse(req.query)
Expand All @@ -42,6 +44,7 @@ export async function getAllOperators(req: Request, res: Response) {
skip,
take,
withTvl,
minTvl,
sortByTvl,
sortByTotalStakers,
sortByTotalAvs,
Expand All @@ -66,7 +69,8 @@ export async function getAllOperators(req: Request, res: Response) {
// Fetch records and apply search/sort
const operatorRecords = await prisma.operator.findMany({
where: {
...searchFilterQuery
...searchFilterQuery,
...(minTvl ? { tvlEth: { gte: minTvl } } : {})
},
include: {
avs: {
Expand All @@ -88,7 +92,8 @@ export async function getAllOperators(req: Request, res: Response) {
// Count records
const operatorCount = await prisma.operator.count({
where: {
...searchFilterQuery
...searchFilterQuery,
...(minTvl ? { tvlEth: { gte: minTvl } } : {})
}
})

Expand Down Expand Up @@ -595,6 +600,14 @@ async function calculateOperatorApy(operator: any) {

// Iterate through each strategy and calculate all its rewards
for (const strategyAddress of avs.avs.restakeableStrategies) {
// Omit strategy where the Operator doesn't have shares
if (
!operator.shares.find(
(share) => share.strategyAddress.toLowerCase() === strategyAddress.toLowerCase()
)
)
continue

const strategyTvl = tvlStrategiesEth[strategyAddress.toLowerCase()] || 0
if (strategyTvl === 0) continue

Expand Down Expand Up @@ -677,7 +690,7 @@ async function calculateOperatorApy(operator: any) {

avsApyMap.set(avs.avs.address, {
avsAddress: avs.avs.address,
maxApy: avs.avs.maxApy,
maxApy: Math.max(...Array.from(strategyApyMap.values()).map((data) => data.apy)),
strategyApys: Array.from(strategyApyMap.entries()).map(([strategyAddress, data]) => ({
strategyAddress,
apy: data.apy,
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/routes/stakers/stakerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ export async function getStaker(req: Request, res: Response) {
const strategiesWithSharesUnderlying =
withTvl || withRewards ? await getStrategiesWithShareUnderlying() : []
const tvl = withTvl ? sharesToTVL(staker.shares, strategiesWithSharesUnderlying) : undefined
const tvlStrategiesEth = withTvl
? sharesToTVLStrategies(staker.shares, strategiesWithSharesUnderlying)
: null
const tvlStrategiesEth =
withTvl || withRewards
? sharesToTVLStrategies(staker.shares, strategiesWithSharesUnderlying)
: null

res.send({
...staker,
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/schema/zod/schemas/minTvlQuerySchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import z from '..'

export const MinTvlQuerySchema = z.object({
minTvl: z
.string()
.optional()
.transform((val) => (val !== undefined ? Number(val) : undefined))
.refine((val) => val === undefined || !isNaN(val), {
message: 'minTvl must be a valid number'
})
.describe(
'Return only the entities (Operators or AVS) having TVL (in ETH) greater than the specified value'
)
.openapi({ example: '0' })
})
Loading
Loading