Skip to content

Commit

Permalink
Merge pull request #35 from Eigen-Explorer/feature/switch-operator-sh…
Browse files Browse the repository at this point in the history
…ares-to-stakers

Reinclude operator shares, seed with impacted staker data
  • Loading branch information
uditdc authored Apr 29, 2024
2 parents c8a5986 + 1feb17f commit 617e194
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 120 deletions.
125 changes: 62 additions & 63 deletions packages/api/src/routes/avs/avsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,40 @@ export async function getAllAVS(req: Request, res: Response) {
include: {
operator: {
include: {
stakers: {
include: {
shares: true
}
}
shares: true
}
}
}
}
}
})

const data = avsRecords.map((avs) => {
let tvl = 0
let totalStakers = 0
const totalOperators = avs.operators.length
const data = await Promise.all(
avsRecords.map(async (avs) => {
let tvl = 0

avs.operators.map((avsOperator) => {
const operator = withOperatorTvl(avsOperator.operator)
const totalOperators = avs.operators.length
const totalStakers = await prisma.staker.count({
where: {
operatorAddress: { in: avs.operators.map((o) => o.operatorAddress) }
}
})

tvl += operator.tvl
totalStakers += operator.totalStakers
})
avs.operators.map((avsOperator) => {
const operator = withOperatorTvl(avsOperator.operator)

return {
...avs,
operators: undefined,
tvl,
totalOperators,
totalStakers
}
})
tvl += operator.tvl
})

return {
...avs,
operators: undefined,
tvl,
totalOperators,
totalStakers
}
})
)

res.send({
data,
Expand Down Expand Up @@ -143,11 +145,7 @@ export async function getAVS(req: Request, res: Response) {
include: {
operator: {
include: {
stakers: {
include: {
shares: true
}
}
shares: true
}
}
}
Expand All @@ -156,31 +154,33 @@ export async function getAVS(req: Request, res: Response) {
})

let tvl = 0
let totalStakers = 0
const totalOperators = avs.operators.length
const sharesMap: IMap<string, string> = new Map()
const totalOperators = avs.operators.length
const totalStakers = await prisma.staker.count({
where: {
operatorAddress: { in: avs.operators.map((o) => o.operatorAddress) }
}
})

avs.operators
.map((avsOperator) => avsOperator.operator)
.map((operator) => withOperatorTvlAndShares(operator))
.map((operator) => {
operator.shares.map((s) => {
if (!sharesMap.has(s.strategyAddress)) {
sharesMap.set(s.strategyAddress, '0')
}
avs.operators.map((avsOperator) => {
const operator = withOperatorTvlAndShares(avsOperator.operator)

sharesMap.set(
s.strategyAddress,
(
BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares)
).toString()
)
})
operator.shares.map((s) => {
if (!sharesMap.has(s.strategyAddress)) {
sharesMap.set(s.strategyAddress, '0')
}

tvl += operator.tvl
totalStakers += operator.totalStakers
sharesMap.set(
s.strategyAddress,
(
BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares)
).toString()
)
})

tvl += operator.tvl
})

res.send({
...avs,
shares: Array.from(sharesMap, ([strategyAddress, shares]) => ({
Expand Down Expand Up @@ -281,36 +281,35 @@ export async function getAVSOperators(req: Request, res: Response) {
const { id } = req.params
const avs = await prisma.avs.findUniqueOrThrow({
where: { address: id },
include: { operators: true }
})

const operatorAddresses = avs.operators
.filter((o) => o.isActive)
.map((o) => o.operatorAddress)

const operatorsCount = await prisma.operator.count({
where: { address: { in: operatorAddresses } }
include: {
operators: {
where: { isActive: true }
}
}
})

const operatorsRecords = await prisma.operator.findMany({
where: { address: { in: operatorAddresses } },
where: { address: { in: avs.operators.map((o) => o.operatorAddress) } },
skip,
take,
include: {
stakers: {
include: {
shares: true
}
}
shares: true,
stakers: true
}
})

const data = operatorsRecords.map((operator) => withOperatorTvl(operator))
const data = operatorsRecords
.map((operator) => ({
...operator,
stakers: undefined,
totalStakers: operator.stakers.length
}))
.map((operator) => withOperatorTvl(operator))

res.send({
data,
meta: {
total: operatorsCount,
total: avs.operators.length,
skip,
take
}
Expand Down
76 changes: 35 additions & 41 deletions packages/api/src/routes/operators/operatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Request, Response } from 'express'
import prisma from '../../utils/prismaClient'
import { PaginationQuerySchema } from '../../schema/zod/schemas/paginationQuery'
import { handleAndReturnErrorResponse } from '../../schema/errors'
import { StakerStrategyShares } from '@prisma/client'
import { OperatorStrategyShares } from '@prisma/client'
import { IMap } from '../../schema/generic'

/**
Expand All @@ -26,17 +26,18 @@ export async function getAllOperators(req: Request, res: Response) {
skip,
take,
include: {
stakers: {
include: {
shares: true
}
}
shares: true,
stakers: true
}
})

const operators = operatorRecords.map((operator) =>
withOperatorTvl(operator)
)
const operators = operatorRecords
.map((operator) => ({
...operator,
stakers: undefined,
totalStakers: operator.stakers.length
}))
.map((operator) => withOperatorTvl(operator))

res.send({
data: operators,
Expand Down Expand Up @@ -64,67 +65,61 @@ export async function getOperator(req: Request, res: Response) {
const operator = await prisma.operator.findUniqueOrThrow({
where: { address: id },
include: {
stakers: {
include: {
shares: true
}
}
shares: true,
stakers: true
}
})

let tvl = 0

operator.stakers.map((staker) => {
staker.shares.map((s) => {
tvl += Number(s.shares) / 1e18
})
operator.shares.map((s) => {
tvl += Number(s.shares) / 1e18
})

res.send(withOperatorTvlAndShares(operator))
res.send({
...withOperatorTvlAndShares(operator),
stakers: undefined,
totalStakers: operator.stakers.length
})
} catch (error) {
handleAndReturnErrorResponse(req, res, error)
}
}

// Helper methods
export function withOperatorTvl(operator: {
stakers: { shares: StakerStrategyShares[] }[]
shares: OperatorStrategyShares[]
}) {
let tvl = 0

operator.stakers.map((staker) => {
staker.shares.map((s) => {
tvl += Number(s.shares) / 1e18
})
operator.shares.map((s) => {
tvl += Number(s.shares) / 1e18
})

return {
...operator,
tvl,
totalStakers: operator.stakers.length,
stakers: undefined
shares: undefined,
tvl
}
}

export function withOperatorTvlAndShares(operator: {
stakers: { shares: StakerStrategyShares[] }[]
shares: OperatorStrategyShares[]
}) {
let tvl = 0
const sharesMap: IMap<string, string> = new Map()

operator.stakers.map((staker) => {
staker.shares.map((s) => {
if (!sharesMap.has(s.strategyAddress)) {
sharesMap.set(s.strategyAddress, '0')
}
operator.shares.map((s) => {
if (!sharesMap.has(s.strategyAddress)) {
sharesMap.set(s.strategyAddress, '0')
}

sharesMap.set(
s.strategyAddress,
(BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares)).toString()
)
sharesMap.set(
s.strategyAddress,
(BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares)).toString()
)

tvl += Number(s.shares) / 1e18
})
tvl += Number(s.shares) / 1e18
})

return {
Expand All @@ -134,7 +129,6 @@ export function withOperatorTvlAndShares(operator: {
strategyAddress,
shares
})),
tvl,
totalStakers: operator.stakers.length
tvl
}
}

This file was deleted.

10 changes: 10 additions & 0 deletions packages/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ model Operator {
metadataX String?
avs AvsOperator[]
shares OperatorStrategyShares[]
stakers Staker[]
}

model OperatorStrategyShares {
Operator Operator @relation(fields: [operatorAddress], references: [address])
operatorAddress String
strategyAddress String
shares String
@@id([operatorAddress, strategyAddress])
}

model Staker {
address String @id @unique
Expand Down
Loading

0 comments on commit 617e194

Please sign in to comment.