Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PJColombo
Copy link
Member

Checklist

  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

It adds a new boolean query parameter called slot to the getByBlockId procedure. When set, the provided block ID is treated as a slot.

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

Copy link

vercel bot commented Jan 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
blobscan-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 30, 2025 2:31pm
blobscan-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 30, 2025 2:31pm
4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
blobscan-gnosis ⬜️ Ignored (Inspect) Jan 30, 2025 2:31pm
blobscan-holesky ⬜️ Ignored (Inspect) Jan 30, 2025 2:31pm
blobscan-mainnet ⬜️ Ignored (Inspect) Jan 30, 2025 2:31pm
blobscan-sepolia ⬜️ Ignored (Inspect) Jan 30, 2025 2:31pm

Copy link

changeset-bot bot commented Jan 30, 2025

🦋 Changeset detected

Latest commit: 66093fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@blobscan/api Minor
@blobscan/rest-api-server Patch
@blobscan/web Patch

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);
Copy link
Member

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.

Comment on lines +28 to +71
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}"`,
});
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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}"`,
});
}

Copy link
Member

@PabloCastellano PabloCastellano left a 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?

@luis-herasme
Copy link
Member

Instead of expanding the scope of getByBlockId, why not create a new method?

I prefer this too

@PJColombo
Copy link
Member Author

Instead of expanding the scope of getByBlockId, why not create a new method?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants