Skip to content

Commit

Permalink
feat: timestamp and block number on txEffect (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipHarald authored Oct 11, 2024
1 parent f8ba8cb commit 8e457eb
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 41 deletions.
33 changes: 33 additions & 0 deletions packages/types/src/aztec/deluxe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { z } from "zod";
import { ChicmozL2Block, chicmozL2BlockSchema } from "./l2Block.js";
import {
chicmozL2ContractClassRegisteredEventSchema,
chicmozL2ContractInstanceDeployedEventSchema,
} from "./l2Contract.js";
import { chicmozL2TxEffectSchema } from "./l2TxEffect.js";

export const chicmozL2ContractInstanceDeluxeSchema = z.object({
...chicmozL2ContractInstanceDeployedEventSchema.shape,
...chicmozL2ContractClassRegisteredEventSchema.shape,
blockHeight: chicmozL2BlockSchema.shape.height.optional(),
});

export type ChicmozL2ContractInstanceDeluxe = z.infer<
typeof chicmozL2ContractInstanceDeluxeSchema
>;

export const chicmozL2TxEffectDeluxeSchema = z.object({
...chicmozL2TxEffectSchema.shape,
blockHeight: z.lazy(() => chicmozL2BlockSchema.shape.height),
timestamp: z.lazy(
() =>
chicmozL2BlockSchema.shape.header.shape.globalVariables.shape.timestamp
),
});

export type ChicmozL2TxEffectDeluxe = z.infer<
typeof chicmozL2TxEffectSchema & {
blockHeight: ChicmozL2Block["height"];
timestamp: ChicmozL2Block["header"]["globalVariables"]["timestamp"];
}
>;
1 change: 1 addition & 0 deletions packages/types/src/aztec/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./l2Block.js";
export * from "./l2Contract.js";
export * from "./l2TxEffect.js";
export * from "./deluxe.js";
export { frNumberSchema } from "./utils.js";

export { type NodeInfo } from "@aztec/aztec.js";
10 changes: 0 additions & 10 deletions packages/types/src/aztec/l2Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,3 @@ export const chicmozL2ContractClassRegisteredEventSchema = z.object({
export type ChicmozL2ContractClassRegisteredEvent = z.infer<
typeof chicmozL2ContractClassRegisteredEventSchema
>;

export const chicmozL2ContractInstanceDeluxeSchema = z.object({
...chicmozL2ContractInstanceDeployedEventSchema.shape,
...chicmozL2ContractClassRegisteredEventSchema.shape,
blockHeight: chicmozL2BlockSchema.shape.height.optional(),
});

export type ChicmozL2ContractInstanceDeluxe = z.infer<
typeof chicmozL2ContractInstanceDeluxeSchema
>;
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
ChicmozL2TxEffect,
ChicmozL2TxEffectDeluxe,
EncryptedLogEntry,
HexString,
NoteEncryptedLogEntry,
UnencryptedLogEntry,
chicmozL2TxEffectSchema,
chicmozL2TxEffectDeluxeSchema,
encryptedLogEntrySchema,
noteEncryptedLogEntrySchema,
unencryptedLogEntrySchema,
Expand All @@ -13,9 +14,10 @@ import { and, asc, eq, getTableColumns } from "drizzle-orm";
import { z } from "zod";
import { getDb as db } from "../../../database/index.js";
import {
body,
bodyToTxEffects,
functionLogs,
globalVariables,
header,
l2Block,
logs,
publicDataWrite,
Expand Down Expand Up @@ -154,7 +156,7 @@ export const getTxEffectNestedById = async (
export const getTxEffectByBlockHeightAndIndex = async (
blockHeight: number,
txEffectIndex: number
): Promise<ChicmozL2TxEffect | null> => {
): Promise<ChicmozL2TxEffectDeluxe | null> => {
const res = await _getTxEffects({
blockHeight,
txEffectIndex,
Expand All @@ -168,21 +170,24 @@ export const getTxEffectByBlockHeightAndIndex = async (

export const getTxEffectsByBlockHeight = async (
height: number
): Promise<ChicmozL2TxEffect[]> => {
): Promise<ChicmozL2TxEffectDeluxe[]> => {
return _getTxEffects({ blockHeight: height, getType: GetTypes.BlockHeight });
};

const _getTxEffects = async (
args: GetTxEffectByBlockHeightAndIndex | GetTxEffectsByBlockHeight
) => {
): Promise<ChicmozL2TxEffectDeluxe[]> => {
const joinQuery = db()
.select({
txEffect: getTableColumns(txEffect),
...getTableColumns(txEffect),
blockHeight: l2Block.height,
timestamp: globalVariables.timestamp,
})
.from(l2Block)
.innerJoin(body, eq(l2Block.bodyId, body.id))
.innerJoin(bodyToTxEffects, eq(body.id, bodyToTxEffects.bodyId))
.innerJoin(txEffect, eq(bodyToTxEffects.txEffectId, txEffect.id));
.innerJoin(bodyToTxEffects, eq(txEffect.id, bodyToTxEffects.txEffectId))
.innerJoin(l2Block, eq(bodyToTxEffects.bodyId, l2Block.bodyId))
.innerJoin(header, eq(l2Block.headerId, header.id))
.innerJoin(globalVariables, eq(header.globalVariablesId, globalVariables.id))

let whereQuery;

Expand All @@ -208,28 +213,34 @@ const _getTxEffects = async (

const txEffects = await Promise.all(
dbRes.map(async (txEffect) => {
const nestedData = await getTxEffectNestedById(txEffect.txEffect.id);
const nestedData = await getTxEffectNestedById(txEffect.id);
return {
...txEffect.txEffect,
...txEffect,
...nestedData,
};
})
);

return z
.array(chicmozL2TxEffectSchema)
.array(chicmozL2TxEffectDeluxeSchema)
.parseAsync(txEffects)
.catch(dbParseErrorCallback);
};

export const getTxeffectByHash = async (
hash: HexString,
): Promise<ChicmozL2TxEffect | null> => {
): Promise<ChicmozL2TxEffectDeluxe | null> => {
const dbRes = await db()
.select({
...getTableColumns(txEffect),
blockHeight: l2Block.height,
timestamp: globalVariables.timestamp
})
.from(txEffect)
.innerJoin(bodyToTxEffects, eq(txEffect.id, bodyToTxEffects.txEffectId))
.innerJoin(l2Block, eq(bodyToTxEffects.bodyId, l2Block.bodyId))
.innerJoin(header, eq(l2Block.headerId, header.id))
.innerJoin(globalVariables, eq(header.globalVariablesId, globalVariables.id))
.where(eq(txEffect.hash, hash))
.limit(1)
.execute();
Expand All @@ -238,7 +249,7 @@ export const getTxeffectByHash = async (

const nestedData = await getTxEffectNestedById(dbRes[0].id);

return chicmozL2TxEffectSchema
return chicmozL2TxEffectDeluxeSchema
.parseAsync({
...dbRes[0],
...nestedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import {
chicmozL2BlockSchema,
chicmozL2ContractInstanceDeluxeSchema,
chicmozL2TxEffectSchema,
chicmozL2TxEffectDeluxeSchema,
} from "@chicmoz-pkg/types";
import { generateSchema } from "@anatine/zod-openapi";

Expand Down Expand Up @@ -34,7 +34,7 @@ export const txEffectResponse = {
description: "Successful response",
content: {
"application/json": {
schema: generateSchema(chicmozL2TxEffectSchema),
schema: generateSchema(chicmozL2TxEffectDeluxeSchema),
},
},
},
Expand All @@ -45,7 +45,7 @@ export const txEffectResponseArray = {
description: "Successful response",
content: {
"application/json": {
schema: generateSchema(z.array(chicmozL2TxEffectSchema)),
schema: generateSchema(z.array(chicmozL2TxEffectDeluxeSchema)),
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions services/explorer-ui/src/api/tx-effect.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {
chicmozL2TxEffectSchema,
type ChicmozL2TxEffect,
chicmozL2TxEffectDeluxeSchema,
type ChicmozL2TxEffectDeluxe,
} from "@chicmoz-pkg/types";
import { z } from "zod";
import { aztecExplorer } from "~/service/constants";
import client, { validateResponse } from "./client";

export const TxEffectsAPI = {
getTxEffectByHash: async (hash: string): Promise<ChicmozL2TxEffect> => {
getTxEffectByHash: async (hash: string): Promise<ChicmozL2TxEffectDeluxe> => {
const response = await client.get(
`${aztecExplorer.getL2TxEffectByHash}/${hash}`,
);
return validateResponse(chicmozL2TxEffectSchema, response.data);
return validateResponse(chicmozL2TxEffectDeluxeSchema, response.data);
},
getTxEffectsByHeight: async (
height: number,
): Promise<ChicmozL2TxEffect[]> => {
): Promise<ChicmozL2TxEffectDeluxe[]> => {
const response = await client.get(
aztecExplorer.getL2TxEffectsByHeight(height),
);
return validateResponse(z.array(chicmozL2TxEffectSchema), response.data);
return validateResponse(z.array(chicmozL2TxEffectDeluxeSchema), response.data);
},
getTxEffectByHeightAndIndex: async (
height: number,
Expand Down
6 changes: 3 additions & 3 deletions services/explorer-ui/src/hooks/tx-effect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type ChicmozL2TxEffect } from "@chicmoz-pkg/types";
import { type ChicmozL2TxEffectDeluxe } from "@chicmoz-pkg/types";
import { type UseQueryResult, useQuery } from "@tanstack/react-query";
import { TxEffectsAPI } from "~/api";

export const useGetTxEffectByHash = (
id: string,
): UseQueryResult<ChicmozL2TxEffect, Error> => {
return useQuery<ChicmozL2TxEffect, Error>({
): UseQueryResult<ChicmozL2TxEffectDeluxe, Error> => {
return useQuery<ChicmozL2TxEffectDeluxe, Error>({
queryKey: ["txEffectByHash", id],
queryFn: () => TxEffectsAPI.getTxEffectByHash(id),
});
Expand Down
9 changes: 4 additions & 5 deletions services/explorer-ui/src/pages/tx-effect-details/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChicmozL2TxEffect } from "@chicmoz-pkg/types";
import { type ChicmozL2TxEffectDeluxe } from "@chicmoz-pkg/types";

export const getTxEffectData = (data: ChicmozL2TxEffect) => [
export const getTxEffectData = (data: ChicmozL2TxEffectDeluxe) => [
{
label: "HASH",
value: data.hash,
Expand All @@ -9,7 +9,6 @@ export const getTxEffectData = (data: ChicmozL2TxEffect) => [
label: "TRANSACTION FEE",
value: data.transactionFee.toString(),
},
//TODO: get actual Block number and time stamp
{ label: "BLOCK NUMBER", value: "-1" },
{ label: "TIMESTAMP", value: "-1" },
{ label: "BLOCK NUMBER", value: data.blockHeight.toString() },
{ label: "TIMESTAMP", value: data.timestamp.toString() },
];

0 comments on commit 8e457eb

Please sign in to comment.