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: add category field #551

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/afraid-cheetahs-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@blobscan/api": minor
"@blobscan/db": minor
---

Added `category` column to `Transaction` model
2 changes: 1 addition & 1 deletion packages/api/enums.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { BlobStorage, Rollup } from "@blobscan/db/prisma/enums";
export { BlobStorage, Category, Rollup } from "@blobscan/db/prisma/enums";
9 changes: 9 additions & 0 deletions packages/api/src/middlewares/withExpands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { z } from "@blobscan/zod";

import { t } from "../trpc-client";
import {
categorySchema,
dataStorageReferencesSelect,
rollupSchema,
serializeBlobDataStorageReferences,
serializeCategory,
serializeDecimal,
serializeRollup,
serializedBlobDataStorageReferenceSchema,
Expand All @@ -29,6 +31,7 @@ const expandedTransactionSelect = Prisma.validator<Prisma.TransactionSelect>()({
toId: true,
gasPrice: true,
maxFeePerBlobGas: true,
category: true,
rollup: true,
index: true,
});
Expand Down Expand Up @@ -118,6 +121,7 @@ export const serializedExpandedTransactionSchema = z
to: z.string().optional(),
maxFeePerBlobGas: z.string().optional(),
blobAsCalldataGasUsed: z.string().optional(),
category: categorySchema.optional(),
rollup: rollupSchema.nullable().optional(),
index: z.number().nonnegative().optional(),
})
Expand Down Expand Up @@ -228,6 +232,7 @@ export function serializeExpandedTransaction(
maxFeePerBlobGas,
fromId,
toId,
category,
rollup,
blobAsCalldataGasFee,
blobGasBaseFee,
Expand Down Expand Up @@ -255,6 +260,10 @@ export function serializeExpandedTransaction(
expandedTransaction.to = toId;
}

if (category) {
expandedTransaction.category = serializeCategory(category);
}

if (rollup) {
expandedTransaction.rollup = serializeRollup(rollup);
}
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/routers/blob/common/selects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function createBlobSelect(expands: Expands) {
transaction: {
select: {
rollup: true,
category: true,
...(expands.transaction?.select ?? {}),
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/routers/blob/common/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
serializeBlobDataStorageReferences,
serializeDate,
isEmptyObject,
serializeCategory,
} from "../../../utils";

type BaseBlob = Pick<
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/routers/indexer/indexData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
WithoutTimestampFields,
} from "@blobscan/db";
import { Prisma } from "@blobscan/db";
import { Category } from "@blobscan/db/prisma/enums";
import { env } from "@blobscan/env";
import { getRollupByAddress } from "@blobscan/rollups";

Expand Down Expand Up @@ -91,6 +92,7 @@ export function createDBTransactions({

const blobGasPrice = calculateBlobGasPrice(block.excessBlobGas);
const rollup = getRollupByAddress(from, env.CHAIN_ID);
const category = rollup ? Category.ROLLUP : Category.OTHER;

return {
blockHash: block.hash,
Expand All @@ -105,6 +107,7 @@ export function createDBTransactions({
maxFeePerBlobGas: bigIntToDecimal(maxFeePerBlobGas),
blobAsCalldataGasUsed: bigIntToDecimal(blobGasAsCalldataUsed),
rollup,
category,
};
}
);
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/routers/tx/common/selects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const baseTransactionSelect =
blobAsCalldataGasUsed: true,
gasPrice: true,
maxFeePerBlobGas: true,
category: true,
rollup: true,
blockHash: true,
blockNumber: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/routers/tx/common/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
serializedDerivedTxBlobGasFieldsSchema,
calculateDerivedTxBlobGasFields,
serializeDate,
categorySchema,
serializeCategory,
} from "../../../utils";
import type { FullQueriedTransaction, BaseTransaction } from "./selects";

Expand All @@ -28,6 +30,7 @@ const baseSerializedTransactionFieldsSchema = z.object({
to: z.string(),
maxFeePerBlobGas: z.string(),
blobAsCalldataGasUsed: z.string(),
category: categorySchema,
rollup: rollupSchema.nullable(),
blobs: z.array(
z
Expand Down Expand Up @@ -80,6 +83,7 @@ export function serializeBaseTransactionFields(
index,
fromId,
toId,
category,
rollup,
blobs,
block,
Expand All @@ -96,6 +100,7 @@ export function serializeBaseTransactionFields(
from: fromId,
blobAsCalldataGasUsed: serializeDecimal(txQuery.blobAsCalldataGasUsed),
maxFeePerBlobGas: serializeDecimal(txQuery.maxFeePerBlobGas),
category: serializeCategory(category),
rollup: serializeRollup(rollup),
blobs: blobs.map(({ blob, blobHash, index }) => {
return {
Expand Down
28 changes: 27 additions & 1 deletion packages/api/src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BlobStorage, Rollup } from "@blobscan/db/prisma/enums";
import { BlobStorage, Category, Rollup } from "@blobscan/db/prisma/enums";
import { z } from "@blobscan/zod";

const zodBlobStorageEnums = [
Expand Down Expand Up @@ -29,6 +29,8 @@ const zodRollupEnums = [
"zora",
] as const;

const zodCategoryEnum = ["other", "rollup"] as const;

/**
* This is a type-safe way to get the enum values as we can't use `Object.values`
* on zod enums directly
Expand All @@ -46,6 +48,13 @@ const missingBlobStorageEnums = Object.values(BlobStorage).filter(
)
);

const missingCategoryEnums = Object.values(Category).filter(
(r) =>
!zodCategoryEnum.includes(
r.toLowerCase() as (typeof zodCategoryEnum)[number]
)
);

if (missingRollupEnums.length) {
throw new Error(
`Zod rollup enums is not in sync with Prisma rollup enums. Missing zod enums: ${missingRollupEnums.join(
Expand All @@ -62,10 +71,20 @@ if (missingBlobStorageEnums.length) {
);
}

if (missingCategoryEnums.length) {
throw new Error(
`Zod category enums is not in sync with Prisma category enums. Missing zod enums: ${missingCategoryEnums.join(
", "
)}`
);
}

export type ZodRollupEnum = (typeof zodRollupEnums)[number];

export type ZodBlobStorageEnum = (typeof zodBlobStorageEnums)[number];

export type ZodCategoryEnum = (typeof zodCategoryEnum)[number];

export const blobStorageSchema = z.enum(zodBlobStorageEnums);

// Use string and refine it as TRPC OpenAPI doesn't support enums yet
Expand All @@ -76,6 +95,13 @@ export const rollupSchema = z
})
.transform((value) => value as ZodRollupEnum);

export const categorySchema = z
.string()
.refine((value) => {
return zodCategoryEnum.includes(value as ZodCategoryEnum);
})
.transform((value) => value as ZodCategoryEnum);

export const nullableRollupSchema = z
.string()
.refine((value) => {
Expand Down
12 changes: 10 additions & 2 deletions packages/api/src/utils/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import type {
Prisma,
BlobDataStorageReference as DBBlobDataStorageReference,
} from "@blobscan/db/prisma/client";
import type { BlobStorage, Rollup } from "@blobscan/db/prisma/enums";
import type { BlobStorage, Category, Rollup } from "@blobscan/db/prisma/enums";
import { env } from "@blobscan/env";
import { z } from "@blobscan/zod";

import { blobStorageSchema } from "./schemas";
import type { ZodBlobStorageEnum, ZodRollupEnum } from "./schemas";
import type {
ZodBlobStorageEnum,
ZodCategoryEnum,
ZodRollupEnum,
} from "./schemas";

export function serializeDecimal(decimal: Prisma.Decimal): string {
return decimal.toFixed();
Expand All @@ -27,6 +31,10 @@ export function serializeBlobStorage(
return blobStorage.toLowerCase() as ZodBlobStorageEnum;
}

export function serializeCategory(category: Category): ZodCategoryEnum {
return category.toLowerCase() as ZodCategoryEnum;
}

export function buildBlobDataUrl(
blobStorage: BlobStorage,
blobDataUri: string
Expand Down
Loading
Loading