-
Notifications
You must be signed in to change notification settings - Fork 50
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
feat(api): add support for fetching blocks by slot #700
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🦋 Changeset detectedLatest commit: 66093fb The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
}; | ||
} | ||
|
||
const blockHashResult = blockHashSchema.safeParse(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The blockHashSchema
will never succeed if the blockNumberOrSlotSchema
has already failed.
const blockNumberOrSlotSchema = z.union([ | ||
z | ||
.string() | ||
.refine((value) => !hashSchema.safeParse(value).success) | ||
.pipe(z.coerce.number().positive().int()), | ||
z.number().positive().int(), | ||
]); | ||
const blockIdSchema = z.union([blockHashSchema, blockNumberOrSlotSchema]); | ||
|
||
const inputSchema = z | ||
.object({ | ||
id: blockIdSchema, | ||
slot: z.boolean().optional(), | ||
}) | ||
.merge(withTypeFilterSchema) | ||
.merge(createExpandsSchema(["transaction", "blob", "blob_data"])); | ||
|
||
const outputSchema = serializedBlockSchema; | ||
|
||
function buildBlockWhereClause( | ||
{ id, slot: isSlot }: z.output<typeof inputSchema>, | ||
filters: Filters | ||
): Prisma.BlockWhereInput { | ||
const blockNumberOrSlotRes = blockNumberOrSlotSchema.safeParse(id); | ||
|
||
if (blockNumberOrSlotRes.success) { | ||
return { | ||
[isSlot ? "slot" : "number"]: blockNumberOrSlotRes.data, | ||
transactionForks: filters.blockType, | ||
}; | ||
} | ||
|
||
const blockHashResult = blockHashSchema.safeParse(id); | ||
|
||
if (blockHashResult.data) { | ||
return { hash: blockHashResult.data }; | ||
} | ||
|
||
throw new TRPCError({ | ||
code: "BAD_REQUEST", | ||
message: `Invalid block id "${id}"`, | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const blockNumberOrSlotSchema = z.union([ | |
z | |
.string() | |
.refine((value) => !hashSchema.safeParse(value).success) | |
.pipe(z.coerce.number().positive().int()), | |
z.number().positive().int(), | |
]); | |
const blockIdSchema = z.union([blockHashSchema, blockNumberOrSlotSchema]); | |
const inputSchema = z | |
.object({ | |
id: blockIdSchema, | |
slot: z.boolean().optional(), | |
}) | |
.merge(withTypeFilterSchema) | |
.merge(createExpandsSchema(["transaction", "blob", "blob_data"])); | |
const outputSchema = serializedBlockSchema; | |
function buildBlockWhereClause( | |
{ id, slot: isSlot }: z.output<typeof inputSchema>, | |
filters: Filters | |
): Prisma.BlockWhereInput { | |
const blockNumberOrSlotRes = blockNumberOrSlotSchema.safeParse(id); | |
if (blockNumberOrSlotRes.success) { | |
return { | |
[isSlot ? "slot" : "number"]: blockNumberOrSlotRes.data, | |
transactionForks: filters.blockType, | |
}; | |
} | |
const blockHashResult = blockHashSchema.safeParse(id); | |
if (blockHashResult.data) { | |
return { hash: blockHashResult.data }; | |
} | |
throw new TRPCError({ | |
code: "BAD_REQUEST", | |
message: `Invalid block id "${id}"`, | |
}); | |
} | |
const blockIdSchema = z.string().pipe(z.coerce.number().positive().int()); | |
const inputSchema = z | |
.object({ | |
id: blockIdSchema, | |
slot: z.boolean().optional(), | |
}) | |
.merge(withTypeFilterSchema) | |
.merge(createExpandsSchema(["transaction", "blob", "blob_data"])); | |
const outputSchema = serializedBlockSchema; | |
function buildBlockWhereClause( | |
{ id, slot: isSlot }: z.output<typeof inputSchema>, | |
filters: Filters | |
): Prisma.BlockWhereInput { | |
const blockHashResult = blockHashSchema.safeParse(id); | |
if (blockHashResult.success) { | |
return { hash: blockHashResult.data }; | |
} | |
const blockId = blockIdSchema.safeParse(id); | |
if (blockId.success) { | |
return { | |
[isSlot ? "slot" : "number"]: blockId.data, | |
transactionForks: filters.blockType, | |
}; | |
} | |
throw new TRPCError({ | |
code: "BAD_REQUEST", | |
message: `Invalid block id "${id}"`, | |
}); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of expanding the scope of getByBlockId
, why not create a new method?
I prefer this too |
Because I want to expose this procedure as a REST endpoint, having two endpoints that essentially perform the same function—returning a block based on an ID—feels redundant. |
Checklist
Description
It adds a new boolean query parameter called
slot
to thegetByBlockId
procedure. When set, the provided block ID is treated as a slot.Motivation and Context (Optional)
Related Issue (Optional)
Screenshots (if appropriate):