From 3c7d747a1484d0cb10af21df117bb9a990109334 Mon Sep 17 00:00:00 2001 From: Udit <25996904+uditdc@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:31:24 +0530 Subject: [PATCH] Remove operator shares seeders, lookup staker shares for operators --- packages/api/src/routes/avs/avsController.ts | 472 +++++++++--------- .../routes/operators/operatorController.ts | 203 +++++--- packages/api/src/schema/generic.ts | 5 + .../migration.sql | 11 + .../prisma/migrations/migration_lock.toml | 3 + packages/prisma/schema.prisma | 10 - packages/seeder/src/index.ts | 2 - packages/seeder/src/seedOperatorShares.ts | 153 ------ packages/seeder/src/seedStakers.ts | 22 +- 9 files changed, 390 insertions(+), 491 deletions(-) create mode 100644 packages/prisma/migrations/20240429123242_remove_operator_shares/migration.sql create mode 100644 packages/prisma/migrations/migration_lock.toml delete mode 100644 packages/seeder/src/seedOperatorShares.ts diff --git a/packages/api/src/routes/avs/avsController.ts b/packages/api/src/routes/avs/avsController.ts index f6401f7b..5af37d2f 100644 --- a/packages/api/src/routes/avs/avsController.ts +++ b/packages/api/src/routes/avs/avsController.ts @@ -1,9 +1,13 @@ -import prisma from '../../utils/prismaClient'; -import type { Request, Response } from 'express'; -import { getEigenContracts } from '../../data/address'; -import { PaginationQuerySchema } from '../../schema/zod/schemas/paginationQuery'; -import { handleAndReturnErrorResponse } from '../../schema/errors'; -import { EthereumAddressSchema } from '../../schema/zod/schemas/avs'; +import prisma from '../../utils/prismaClient' +import type { Request, Response } from 'express' +import { PaginationQuerySchema } from '../../schema/zod/schemas/paginationQuery' +import { handleAndReturnErrorResponse } from '../../schema/errors' +import { EthereumAddressSchema } from '../../schema/zod/schemas/avs' +import { + withOperatorTvl, + withOperatorTvlAndShares +} from '../operators/operatorController' +import { IMap } from '../../schema/generic' /** * Route to get a list of all AVSs @@ -12,53 +16,69 @@ import { EthereumAddressSchema } from '../../schema/zod/schemas/avs'; * @param res */ export async function getAllAVS(req: Request, res: Response) { - // Validate pagination query - const result = PaginationQuerySchema.safeParse(req.query); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - const { skip, take } = result.data; - - try { - // Fetch count and record - const avsCount = await prisma.avs.count(); - const avsRecords = await prisma.avs.findMany({ - skip, - take, - include: { operators: true }, - }); - - const data = await Promise.all( - avsRecords.map(async (avs) => { - const operatorAddresses = avs.operators - .filter((o) => o.isActive) - .map((o) => o.operatorAddress); - - const totalOperators = operatorAddresses.length; - const totalStakers = await prisma.staker.count({ - where: { operatorAddress: { in: operatorAddresses } }, - }); - - return { - ...avs, - totalOperators, - totalStakers, - operators: undefined, - }; - }) - ); - - res.send({ - data, - meta: { - total: avsCount, - skip, - take, - }, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + // Validate pagination query + const result = PaginationQuerySchema.safeParse(req.query) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + const { skip, take } = result.data + + try { + // Fetch count and record + const avsCount = await prisma.avs.count() + const avsRecords = await prisma.avs.findMany({ + skip, + take, + include: { + operators: { + where: { isActive: true }, + include: { + operator: { + include: { + stakers: { + include: { + shares: true + } + } + } + } + } + } + } + }) + + const data = avsRecords.map((avs) => { + let tvl = 0 + let totalStakers = 0 + const totalOperators = avs.operators.length + + avs.operators.map((avsOperator) => { + const operator = withOperatorTvl(avsOperator.operator) + + tvl += operator.tvl + totalStakers += operator.totalStakers + }) + + return { + ...avs, + operators: undefined, + tvl, + totalOperators, + totalStakers + } + }) + + res.send({ + data, + meta: { + total: avsCount, + skip, + take + } + }) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } } /** @@ -68,36 +88,36 @@ export async function getAllAVS(req: Request, res: Response) { * @param res */ export async function getAllAVSAddresses(req: Request, res: Response) { - // Validate pagination query - const result = PaginationQuerySchema.safeParse(req.query); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - const { skip, take } = result.data; - - try { - // Fetch count and records - const avsCount = await prisma.avs.count(); - const avsRecords = await prisma.avs.findMany({ skip, take }); - - // Simplified map (assuming avs.address is not asynchronous) - const data = avsRecords.map((avs) => ({ - name: avs.metadataName, - address: avs.address, - })); - - // Send response with data and metadata - res.send({ - data, - meta: { - total: avsCount, - skip, - take, - }, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + // Validate pagination query + const result = PaginationQuerySchema.safeParse(req.query) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + const { skip, take } = result.data + + try { + // Fetch count and records + const avsCount = await prisma.avs.count() + const avsRecords = await prisma.avs.findMany({ skip, take }) + + // Simplified map (assuming avs.address is not asynchronous) + const data = avsRecords.map((avs) => ({ + name: avs.metadataName, + address: avs.address + })) + + // Send response with data and metadata + res.send({ + data, + meta: { + total: avsCount, + skip, + take + } + }) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } } /** @@ -107,74 +127,74 @@ export async function getAllAVSAddresses(req: Request, res: Response) { * @param res */ export async function getAVS(req: Request, res: Response) { - const { id } = req.params; - - const result = EthereumAddressSchema.safeParse(id); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - - try { - const avs = await prisma.avs.findUniqueOrThrow({ - where: { address: id }, - include: { operators: true }, - }); - - const strategyKeys = Object.keys(getEigenContracts().Strategies); - const strategyContracts = strategyKeys.map((s) => - getEigenContracts().Strategies[s].strategyContract.toLowerCase() - ) as `0x${string}`[]; - strategyContracts.push('0xbeaC0eeEeeeeEEeEeEEEEeeEEeEeeeEeeEEBEaC0'); - - const shares = strategyContracts.map((sc) => ({ - shares: '0', - strategy: sc, - })); - - const operatorAddresses = avs.operators - .filter((o) => o.isActive) - .map((o) => o.operatorAddress); - - const operatorRecords = await prisma.operator.findMany({ - where: { address: { in: operatorAddresses } }, - select: { shares: true }, - }); - - let tvl = 0; - const totalOperators = operatorAddresses.length; - const totalStakers = await prisma.staker.count({ - where: { operatorAddress: { in: operatorAddresses } }, - }); - - operatorRecords.map((o) => { - o.shares.map((os) => { - const foundShare = shares.find( - (s) => - s.strategy.toLowerCase() === - os.strategyAddress.toLowerCase() - ); - - if (foundShare) { - const shares = - BigInt(foundShare.shares) + BigInt(os.shares); - foundShare.shares = shares.toString(); - } - - tvl += Number(os.shares) / 1e18; - }); - }); - - res.send({ - ...avs, - shares, - tvl, - totalOperators, - totalStakers, - operators: undefined, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + const { id } = req.params + + const result = EthereumAddressSchema.safeParse(id) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + + try { + const avs = await prisma.avs.findUniqueOrThrow({ + where: { address: id }, + include: { + operators: { + where: { isActive: true }, + include: { + operator: { + include: { + stakers: { + include: { + shares: true + } + } + } + } + } + } + } + }) + + let tvl = 0 + let totalStakers = 0 + const totalOperators = avs.operators.length + const sharesMap: IMap = new Map() + + 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') + } + + sharesMap.set( + s.strategyAddress, + ( + BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares) + ).toString() + ) + }) + + tvl += operator.tvl + totalStakers += operator.totalStakers + }) + + res.send({ + ...avs, + shares: Array.from(sharesMap, ([strategyAddress, shares]) => ({ + strategyAddress, + shares + })), + tvl, + totalOperators, + totalStakers, + operators: undefined + }) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } } /** @@ -185,61 +205,61 @@ export async function getAVS(req: Request, res: Response) { * @returns */ export async function getAVSStakers(req: Request, res: Response) { - // Validate pagination query - const result = PaginationQuerySchema.safeParse(req.query); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - const { skip, take } = result.data; - - try { - 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 stakersCount = await prisma.staker.count({ - where: { operatorAddress: { in: operatorAddresses } }, - }); - - const stakersRecords = await prisma.staker.findMany({ - where: { operatorAddress: { in: operatorAddresses } }, - skip, - take, - include: { shares: true }, - }); - - const data = await Promise.all( - stakersRecords.map((staker) => { - let tvl = 0; - - staker.shares.map((ss) => { - tvl += Number(BigInt(ss.shares)) / 1e18; - }); - - return { - ...staker, - tvl, - }; - }) - ); - - res.send({ - data, - meta: { - total: stakersCount, - skip, - take, - }, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + // Validate pagination query + const result = PaginationQuerySchema.safeParse(req.query) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + const { skip, take } = result.data + + try { + 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 stakersCount = await prisma.staker.count({ + where: { operatorAddress: { in: operatorAddresses } } + }) + + const stakersRecords = await prisma.staker.findMany({ + where: { operatorAddress: { in: operatorAddresses } }, + skip, + take, + include: { shares: true } + }) + + const data = await Promise.all( + stakersRecords.map((staker) => { + let tvl = 0 + + staker.shares.map((ss) => { + tvl += Number(BigInt(ss.shares)) / 1e18 + }) + + return { + ...staker, + tvl + } + }) + ) + + res.send({ + data, + meta: { + total: stakersCount, + skip, + take + } + }) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } } /** @@ -252,11 +272,11 @@ export async function getAVSStakers(req: Request, res: Response) { export async function getAVSOperators(req: Request, res: Response) { try { // Validate pagination query - const result = PaginationQuerySchema.safeParse(req.query); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - const { skip, take } = result.data; + const result = PaginationQuerySchema.safeParse(req.query) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + const { skip, take } = result.data const { id } = req.params const avs = await prisma.avs.findUniqueOrThrow({ @@ -276,28 +296,16 @@ export async function getAVSOperators(req: Request, res: Response) { where: { address: { in: operatorAddresses } }, skip, take, - include: { shares: true } + include: { + stakers: { + include: { + shares: true + } + } + } }) - const data = await Promise.all( - operatorsRecords.map(async (operator) => { - let tvl = 0 - - const totalStakers = await prisma.staker.count({ - where: { operatorAddress: operator.address } - }) - - operator.shares.map((os) => { - tvl += Number(BigInt(os.shares)) / 1e18 - }) - - return { - ...operator, - tvl, - totalStakers - } - }) - ) + const data = operatorsRecords.map((operator) => withOperatorTvl(operator)) res.send({ data, @@ -308,6 +316,6 @@ export async function getAVSOperators(req: Request, res: Response) { } }) } catch (error) { - handleAndReturnErrorResponse(req, res, error); + handleAndReturnErrorResponse(req, res, error) } } diff --git a/packages/api/src/routes/operators/operatorController.ts b/packages/api/src/routes/operators/operatorController.ts index 53db2c70..1ad43397 100644 --- a/packages/api/src/routes/operators/operatorController.ts +++ b/packages/api/src/routes/operators/operatorController.ts @@ -1,8 +1,9 @@ -import type { Request, Response } from 'express'; -import prisma from '../../utils/prismaClient'; -// import { PaginationQuerySchema } from '../../schema/generic' -import { PaginationQuerySchema } from '../../schema/zod/schemas/paginationQuery'; -import { handleAndReturnErrorResponse } from '../../schema/errors'; +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 { IMap } from '../../schema/generic' /** * Route to get a list of all operators @@ -11,54 +12,43 @@ import { handleAndReturnErrorResponse } from '../../schema/errors'; * @param res */ export async function getAllOperators(req: Request, res: Response) { - // Validate pagination query - const result = PaginationQuerySchema.safeParse(req.query); - if (!result.success) { - return handleAndReturnErrorResponse(req, res, result.error); - } - const { skip, take } = result.data; - - try { - // Fetch count and record - const operatorCount = await prisma.operator.count(); - const operatorRecords = await prisma.operator.findMany({ - skip, - take, - include: { shares: true }, - }); - - const operators = await Promise.all( - operatorRecords.map(async (operator) => { - let tvl = 0; - const shares = operator.shares; - const totalStakers = await prisma.staker.count({ - where: { operatorAddress: operator.address }, - }); - - shares.map((s) => { - tvl += Number(s.shares) / 1e18; - }); - - return { - ...operator, - tvl, - totalStakers, - stakers: undefined, - }; - }) - ); - - res.send({ - data: operators, - meta: { - total: operatorCount, - skip, - take, - }, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + // Validate pagination query + const result = PaginationQuerySchema.safeParse(req.query) + if (!result.success) { + return handleAndReturnErrorResponse(req, res, result.error) + } + const { skip, take } = result.data + + try { + // Fetch count and record + const operatorCount = await prisma.operator.count() + const operatorRecords = await prisma.operator.findMany({ + skip, + take, + include: { + stakers: { + include: { + shares: true + } + } + } + }) + + const operators = operatorRecords.map((operator) => + withOperatorTvl(operator) + ) + + res.send({ + data: operators, + meta: { + total: operatorCount, + skip, + take + } + }) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } } /** @@ -68,32 +58,83 @@ export async function getAllOperators(req: Request, res: Response) { * @param res */ export async function getOperator(req: Request, res: Response) { - try { - const { id } = req.params; - - const operator = await prisma.operator.findUniqueOrThrow({ - where: { address: id }, - include: { shares: true }, - }); - - const totalStakers = await prisma.staker.count({ - where: { operatorAddress: operator.address }, - }); - - let tvl = 0; - const shares = operator.shares; - - shares.map((s) => { - tvl += Number(s.shares) / 1e18; - }); - - res.send({ - ...operator, - tvl, - totalStakers, - stakers: undefined, - }); - } catch (error) { - handleAndReturnErrorResponse(req, res, error); - } + try { + const { id } = req.params + + const operator = await prisma.operator.findUniqueOrThrow({ + where: { address: id }, + include: { + stakers: { + include: { + shares: true + } + } + } + }) + + let tvl = 0 + + operator.stakers.map((staker) => { + staker.shares.map((s) => { + tvl += Number(s.shares) / 1e18 + }) + }) + + res.send(withOperatorTvlAndShares(operator)) + } catch (error) { + handleAndReturnErrorResponse(req, res, error) + } +} + +// Helper methods +export function withOperatorTvl(operator: { + stakers: { shares: StakerStrategyShares[] }[] +}) { + let tvl = 0 + + operator.stakers.map((staker) => { + staker.shares.map((s) => { + tvl += Number(s.shares) / 1e18 + }) + }) + + return { + ...operator, + tvl, + totalStakers: operator.stakers.length, + stakers: undefined + } +} + +export function withOperatorTvlAndShares(operator: { + stakers: { shares: StakerStrategyShares[] }[] +}) { + let tvl = 0 + const sharesMap: IMap = new Map() + + operator.stakers.map((staker) => { + staker.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() + ) + + tvl += Number(s.shares) / 1e18 + }) + }) + + return { + ...operator, + stakers: undefined, + shares: Array.from(sharesMap, ([strategyAddress, shares]) => ({ + strategyAddress, + shares + })), + tvl, + totalStakers: operator.stakers.length + } } diff --git a/packages/api/src/schema/generic.ts b/packages/api/src/schema/generic.ts index 132c325d..f6e33d74 100644 --- a/packages/api/src/schema/generic.ts +++ b/packages/api/src/schema/generic.ts @@ -6,3 +6,8 @@ export const PaginationQuerySchema = Joi.object<{ skip: number; take: number }>( take: Joi.number().default(12).min(1) } ) + +// Fix for broken types +export interface IMap extends Map { + get(key: K): V +} diff --git a/packages/prisma/migrations/20240429123242_remove_operator_shares/migration.sql b/packages/prisma/migrations/20240429123242_remove_operator_shares/migration.sql new file mode 100644 index 00000000..54740d08 --- /dev/null +++ b/packages/prisma/migrations/20240429123242_remove_operator_shares/migration.sql @@ -0,0 +1,11 @@ +/* + Warnings: + + - You are about to drop the `OperatorStrategyShares` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "OperatorStrategyShares" DROP CONSTRAINT "OperatorStrategyShares_operatorAddress_fkey"; + +-- DropTable +DROP TABLE "OperatorStrategyShares"; diff --git a/packages/prisma/migrations/migration_lock.toml b/packages/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..fbffa92c --- /dev/null +++ b/packages/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma index 29992a9f..721d37b0 100644 --- a/packages/prisma/schema.prisma +++ b/packages/prisma/schema.prisma @@ -54,19 +54,9 @@ 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 diff --git a/packages/seeder/src/index.ts b/packages/seeder/src/index.ts index cf3d128a..2fa7a536 100644 --- a/packages/seeder/src/index.ts +++ b/packages/seeder/src/index.ts @@ -7,7 +7,6 @@ import { seedOperators } from './seedOperators' import { seedPods } from './seedPods' import { seedValidatorsRestake } from './seedValidatorsRestake' import { seedStakers } from './seedStakers' -import { seedOperatorShares } from './seedOperatorShares' import { getViemClient } from './utils/viemClient' console.log('Initializing seeder ...') @@ -26,7 +25,6 @@ async function seedAvsLoop() { await seedOperators(targetBlock) await seedAvsOperators(targetBlock) await seedStakers(targetBlock) - await seedOperatorShares(targetBlock) await delay(120) // Wait for 2 minutes (120 seconds) } diff --git a/packages/seeder/src/seedOperatorShares.ts b/packages/seeder/src/seedOperatorShares.ts deleted file mode 100644 index 414b59f6..00000000 --- a/packages/seeder/src/seedOperatorShares.ts +++ /dev/null @@ -1,153 +0,0 @@ -import { parseAbiItem } from 'viem' -import { getEigenContracts } from './data/address' -import { getViemClient } from './utils/viemClient' -import { getPrismaClient } from './utils/prismaClient' -import { - baseBlock, - bulkUpdateDbTransactions, - fetchLastSyncBlock, - loopThroughBlocks, - saveLastSyncBlock -} from './utils/seeder' - -const blockSyncKey = 'lastSyncedBlock_operatorShares' - -// Fix for broken types -interface IMap extends Map { - get(key: K): V -} - -export async function seedOperatorShares(toBlock?: bigint, fromBlock?: bigint) { - console.log('Seeding operator shares ...') - - const viemClient = getViemClient() - const prismaClient = getPrismaClient() - const operatorShares: IMap = - new Map() - - const firstBlock = fromBlock - ? fromBlock - : await fetchLastSyncBlock(blockSyncKey) - const lastBlock = toBlock ? toBlock : await viemClient.getBlockNumber() - - // Loop through evm logs - await loopThroughBlocks(firstBlock, lastBlock, async (fromBlock, toBlock) => { - const logs = await viemClient.getLogs({ - address: getEigenContracts().DelegationManager, - events: [ - parseAbiItem( - 'event OperatorSharesIncreased(address indexed operator, address staker, address strategy, uint256 shares)' - ), - parseAbiItem( - 'event OperatorSharesDecreased(address indexed operator, address staker, address strategy, uint256 shares)' - ) - ], - fromBlock, - toBlock - }) - - for (const l in logs) { - const log = logs[l] - - const operatorAddress = String(log.args.operator).toLowerCase() - const strategyAddress = String(log.args.strategy).toLowerCase() - const shares = log.args.shares - if (!shares) continue - - if (!operatorShares.has(operatorAddress)) { - operatorShares.set(operatorAddress, []) - } - - let foundSharesIndex = operatorShares - .get(operatorAddress) - .findIndex((os) => os.strategy.toLowerCase() === strategyAddress) - - if (foundSharesIndex !== undefined && foundSharesIndex === -1) { - operatorShares - .get(operatorAddress) - .push({ shares: '0', strategy: strategyAddress }) - - foundSharesIndex = operatorShares - .get(operatorAddress) - .findIndex((os) => os.strategy.toLowerCase() === strategyAddress) - } - - if (log.eventName === 'OperatorSharesIncreased') { - operatorShares.get(operatorAddress)[foundSharesIndex].shares = ( - BigInt(operatorShares.get(operatorAddress)[foundSharesIndex].shares) + - BigInt(shares) - ).toString() - } else if (log.eventName === 'OperatorSharesDecreased') { - operatorShares.get(operatorAddress)[foundSharesIndex].shares = ( - BigInt(operatorShares.get(operatorAddress)[foundSharesIndex].shares) - - BigInt(shares) - ).toString() - } - } - - console.log( - `Operator shares updated between blocks ${fromBlock} ${toBlock}: ${logs.length}` - ) - }) - - // biome-ignore lint/suspicious/noExplicitAny: - const dbTransactions: any[] = [] - - if (firstBlock === baseBlock) { - // Clear existing table - dbTransactions.push(prismaClient.operatorStrategyShares.deleteMany()) - - const newOperatorShares: { - operatorAddress: string - strategyAddress: string - shares: string - }[] = [] - - for (const [operatorAddress, shares] of operatorShares) { - shares.map((share) => { - newOperatorShares.push({ - operatorAddress, - strategyAddress: share.strategy, - shares: share.shares - }) - }) - } - - dbTransactions.push( - prismaClient.operatorStrategyShares.createMany({ - data: newOperatorShares, - skipDuplicates: true - }) - ) - } else { - for (const [operatorAddress, shares] of operatorShares) { - shares.map((share) => { - dbTransactions.push( - prismaClient.operatorStrategyShares.upsert({ - where: { - operatorAddress_strategyAddress: { - operatorAddress, - strategyAddress: share.strategy - } - }, - create: { - operatorAddress, - strategyAddress: share.strategy, - shares: share.shares - }, - update: { - shares: share.shares - } - }) - ) - }) - } - } - - await bulkUpdateDbTransactions(dbTransactions) - - // Storing last sycned block - await saveLastSyncBlock(blockSyncKey, lastBlock) - - console.log('Seeded operator shares:', operatorShares.size) -} diff --git a/packages/seeder/src/seedStakers.ts b/packages/seeder/src/seedStakers.ts index fb78779e..2c40404d 100644 --- a/packages/seeder/src/seedStakers.ts +++ b/packages/seeder/src/seedStakers.ts @@ -26,7 +26,7 @@ export async function seedStakers(toBlock?: bigint, fromBlock?: bigint) { string, { operatorAddress: string | null - shares: { shares: string; strategy: string }[] + shares: { shares: bigint; strategy: string }[] } > = new Map() @@ -88,7 +88,7 @@ export async function seedStakers(toBlock?: bigint, fromBlock?: bigint) { if (foundSharesIndex !== undefined && foundSharesIndex === -1) { stakers .get(stakerAddress) - .shares.push({ shares: '0', strategy: strategyAddress }) + .shares.push({ shares: 0n, strategy: strategyAddress }) foundSharesIndex = stakers .get(stakerAddress) @@ -98,15 +98,11 @@ export async function seedStakers(toBlock?: bigint, fromBlock?: bigint) { } if (log.eventName === 'OperatorSharesIncreased') { - stakers.get(stakerAddress).shares[foundSharesIndex].shares = ( - BigInt(stakers.get(stakerAddress).shares[foundSharesIndex].shares) + - BigInt(shares) - ).toString() + stakers.get(stakerAddress).shares[foundSharesIndex].shares = + stakers.get(stakerAddress).shares[foundSharesIndex].shares + shares } else if (log.eventName === 'OperatorSharesDecreased') { - stakers.get(stakerAddress).shares[foundSharesIndex].shares = ( - BigInt(stakers.get(stakerAddress).shares[foundSharesIndex].shares) - - BigInt(shares) - ).toString() + stakers.get(stakerAddress).shares[foundSharesIndex].shares = + stakers.get(stakerAddress).shares[foundSharesIndex].shares - shares } } } @@ -142,7 +138,7 @@ export async function seedStakers(toBlock?: bigint, fromBlock?: bigint) { newStakerShares.push({ stakerAddress, strategyAddress: share.strategy, - shares: share.shares + shares: share.shares.toString() }) }) } @@ -188,10 +184,10 @@ export async function seedStakers(toBlock?: bigint, fromBlock?: bigint) { create: { stakerAddress, strategyAddress: share.strategy, - shares: share.shares + shares: share.shares.toString() }, update: { - shares: share.shares + shares: share.shares.toString() } }) )