Skip to content

Commit

Permalink
Add Schema and Types
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhit14 committed Oct 16, 2024
1 parent c12f23c commit ef089da
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/api/src/routes/operators/operatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
66 changes: 66 additions & 0 deletions packages/api/src/schema/zod/schemas/operatorEvents.ts
Original file line number Diff line number Diff line change
@@ -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']
}
)

0 comments on commit ef089da

Please sign in to comment.