From ef089dabde3521ecce63f64cc24a47dd93256db6 Mon Sep 17 00:00:00 2001 From: surbhit14 Date: Wed, 16 Oct 2024 19:08:43 +0530 Subject: [PATCH] Add Schema and Types --- .../routes/operators/operatorController.ts | 15 +++++ .../src/schema/zod/schemas/operatorEvents.ts | 66 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 packages/api/src/schema/zod/schemas/operatorEvents.ts diff --git a/packages/api/src/routes/operators/operatorController.ts b/packages/api/src/routes/operators/operatorController.ts index 49673078..b536a9bd 100644 --- a/packages/api/src/routes/operators/operatorController.ts +++ b/packages/api/src/routes/operators/operatorController.ts @@ -7,6 +7,7 @@ import { WithAdditionalDataQuerySchema } from '../../schema/zod/schemas/withAddi import { SortByQuerySchema } from '../../schema/zod/schemas/sortByQuery' import { SearchByTextQuerySchema } from '../../schema/zod/schemas/searchByTextQuery' import { WithRewardsQuerySchema } from '../../schema/zod/schemas/withRewardsQuery' +import { OperatorEventQuerySchema } from '../../schema/zod/schemas/operatorevents' import { handleAndReturnErrorResponse } from '../../schema/errors' import { fetchRewardTokenPrices, fetchStrategyTokenPrices } from '../../utils/tokenPrices' import { @@ -20,6 +21,20 @@ import { holesky } from 'viem/chains' import Prisma from '@prisma/client' import prisma from '../../utils/prismaClient' +type EventRecordArgs = { + staker: string + strategy?: string + shares?: number +} + +type EventRecord = { + type: 'shares increased' | 'shares decreased' | 'delegation' | 'undelegation' + tx: string + blockNumber: number + blockTime: Date + args: EventRecordArgs +} + /** * Function for route /operators * Returns a list of all Operators with optional sorts & text search diff --git a/packages/api/src/schema/zod/schemas/operatorEvents.ts b/packages/api/src/schema/zod/schemas/operatorEvents.ts new file mode 100644 index 00000000..0dcb5ffd --- /dev/null +++ b/packages/api/src/schema/zod/schemas/operatorEvents.ts @@ -0,0 +1,66 @@ +import z from '../' + +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}$/ + +export const OperatorEventQuerySchema = z + .object({ + stakerAddress: z + .string() + .regex(/^0x[a-fA-F0-9]{40}$/, 'Invalid Ethereum address') + .optional() + .describe('The address of the staker') + .openapi({ example: '0x74ede5f75247fbdb9266d2b3a7be63b3db7611dd' }), + + strategyAddress: z + .string() + .regex(/^0x[a-fA-F0-9]{40}$/, 'Invalid Ethereum address') + .optional() + .describe('The contract address of the restaking strategy') + .openapi({ example: '0x0fe4f44bee93503346a3ac9ee5a26b130a5796d6' }), + + txHash: z + .string() + .regex(/^0x([A-Fa-f0-9]{64})$/, 'Invalid transaction hash') + .optional() + .describe('The transaction hash associated with the event') + .openapi({ example: '0xe95a203b1a91a908f9b9ce46459d101078c2c3cb' }), + + type: z + .enum(['shares increased', 'shares decreased', 'delegation', 'undelegation']) + .optional() + .describe('The type of the operator event') + .openapi({ example: 'shares increased' }), + + startAt: z + .string() + .optional() + .refine( + (val) => + !val || + ((isoRegex.test(val) || yyyymmddRegex.test(val)) && + !Number.isNaN(new Date(val).getTime())), + { + message: 'Invalid date format for startAt. Use YYYY-MM-DD or ISO 8601 format.' + } + ) + .default('') + .describe('Start date in ISO string format') + .openapi({ example: '2024-04-11T08:31:11.000' }) + }) + .refine( + (data) => { + if ( + (data.type === 'delegation' || data.type === 'undelegation') && + (data.strategyAddress || data.stakerAddress) + ) { + return false + } + return true + }, + { + message: + 'Neither strategyAddress nor stakerAddress should be provided for delegation or undelegation event types.', + path: ['strategyAddress', 'stakerAddress'] + } + )