Skip to content

Commit 9749df1

Browse files
authored
feat: endpoints for UI main page stats (#69)
1 parent 0f1ff0a commit 9749df1

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./get-tx-effect.js";
2+
export * from "./stats.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { count } from "drizzle-orm";
2+
import { getDb as db } from "../../../database/index.js";
3+
import { txEffect } from "../../../database/schema/index.js";
4+
5+
export const getTotalTxEffects = async (): Promise<number> => {
6+
const dbRes = await db().select({ count: count() }).from(txEffect).execute();
7+
return dbRes[0].count;
8+
};
9+
10+
export const getTotalTxEffectsLast24h = (): number => {
11+
// TODO: we need l2Block.header.globalVariables.timestamp as number to sum this
12+
return -1;
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./get-latest.js";
22
export * from "./get-block.js";
33
export * from "./store.js";
4+
export * from "./stats.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// import { getDb as db } from "../../../database/index.js";
2+
// import { l2Block } from "../../../database/schema/index.js";
3+
4+
// eslint-disable-next-line @typescript-eslint/require-await
5+
export const getAverageFees = async (): Promise<number> => {
6+
// TODO: we need l2Block.header.totalFees as number to average this
7+
return -1;
8+
};
9+
10+
// eslint-disable-next-line @typescript-eslint/require-await
11+
export const getAverageBlockTime = async (): Promise<number> => {
12+
// TODO: we need l2Block.header.globalVariables.timestamp as number to average this
13+
return -1;
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./get-contract-instance.js";
22
export * from "./get-contract-instances.js";
33
export * from "./store.js";
4+
export * from "./stats.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { count } from "drizzle-orm";
2+
import { getDb as db } from "../../../database/index.js";
3+
import { l2ContractClassRegistered } from "../../../database/schema/index.js";
4+
5+
export const getTotalContracts = async (): Promise<number> => {
6+
const dbRes = await db().select({ count: count() }).from(l2ContractClassRegistered).execute();
7+
return dbRes[0].count;
8+
};

services/explorer-api/src/http-server/routes/controller.ts

+41
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ export const GET_ROUTES = asyncHandler(async (_req, res) => {
7878
r.push(routes.contractInstance + "NOT FOUND");
7979
}
8080

81+
const statsRoutes = [
82+
routes.statsTotalTxEffects,
83+
routes.statsTotalTxEffectsLast24h,
84+
routes.statsTotalContracts,
85+
routes.statsAverageFees,
86+
routes.statsAverageBlockTime,
87+
];
88+
8189
const html = `
8290
<html>
8391
<head>
@@ -87,6 +95,9 @@ export const GET_ROUTES = asyncHandler(async (_req, res) => {
8795
<ul>
8896
${r.map((route) => `<li><a href=${SUB_PATH + route}>${route}</a></li>`).join("")}
8997
</ul>
98+
<br>
99+
<ul>
100+
${statsRoutes.map((route) => `<li><a href=${SUB_PATH + route}>${route}</a></li>`).join("")}
90101
</body>
91102
</html>
92103
`;
@@ -175,3 +186,33 @@ export const GET_L2_CONTRACT_INSTANCES_BY_BLOCK_HASH = asyncHandler(
175186
res.status(200).send(JSON.stringify(instances));
176187
}
177188
);
189+
190+
export const GET_STATS_TOTAL_TX_EFFECTS = asyncHandler(async (_req, res) => {
191+
const total = await db.l2TxEffect.getTotalTxEffects();
192+
if (!total) throw new Error("Total tx effects not found");
193+
res.status(200).send(JSON.stringify(total));
194+
});
195+
196+
export const GET_STATS_TOTAL_TX_EFFECTS_LAST_24H = asyncHandler((_req, res) => {
197+
const txEffects = db.l2TxEffect.getTotalTxEffectsLast24h();
198+
if (!txEffects) throw new Error("Tx effects not found");
199+
res.status(200).send(JSON.stringify(txEffects));
200+
});
201+
202+
export const GET_STATS_TOTAL_CONTRACTS = asyncHandler(async (_req, res) => {
203+
const total = await db.l2Contract.getTotalContracts();
204+
if (!total) throw new Error("Total contracts not found");
205+
res.status(200).send(JSON.stringify(total));
206+
});
207+
208+
export const GET_STATS_AVERAGE_FEES = asyncHandler(async (_req, res) => {
209+
const average = await db.l2Block.getAverageFees();
210+
if (!average) throw new Error("Average fees not found");
211+
res.status(200).send(JSON.stringify(average));
212+
});
213+
214+
export const GET_STATS_AVERAGE_BLOCK_TIME = asyncHandler(async (_req, res) => {
215+
const average = await db.l2Block.getAverageBlockTime();
216+
if (!average) throw new Error("Average block time not found");
217+
res.status(200).send(JSON.stringify(average));
218+
});

services/explorer-api/src/http-server/routes/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ export const init = ({
2222
router.get(routes.contractInstancesByBlockHash, controller.GET_L2_CONTRACT_INSTANCES_BY_BLOCK_HASH);
2323
router.get(routes.contractInstance, controller.GET_L2_CONTRACT_INSTANCE);
2424

25+
router.get(routes.statsTotalTxEffects, controller.GET_STATS_TOTAL_TX_EFFECTS);
26+
router.get(routes.statsTotalTxEffectsLast24h, controller.GET_STATS_TOTAL_TX_EFFECTS_LAST_24H);
27+
router.get(routes.statsTotalContracts, controller.GET_STATS_TOTAL_CONTRACTS);
28+
router.get(routes.statsAverageFees, controller.GET_STATS_AVERAGE_FEES);
29+
router.get(routes.statsAverageBlockTime, controller.GET_STATS_AVERAGE_BLOCK_TIME);
30+
2531
return router;
2632
};

services/explorer-api/src/http-server/routes/routes_and_validation.ts

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export const routes = {
2121
// contractInstancesByContractClassId: `/l2/contract-classes/:${contractClassId}/contract-instances`,
2222
contractInstancesByBlockHash: `/l2/blocks/:${blockHash}/contract-instances`,
2323
contractInstance: `/l2/contract-instances/:${address}`,
24+
25+
statsTotalTxEffects: '/l2/stats/total-tx-effects',
26+
statsTotalTxEffectsLast24h: '/l2/stats/tx-effects-last-24h',
27+
statsTotalContracts: '/l2/stats/total-contracts',
28+
statsAverageFees: '/l2/stats/average-fees',
29+
statsAverageBlockTime: '/l2/stats/average-block-time',
2430
};
2531

2632
export const getBlockByHeightOrHashSchema = z.object({

0 commit comments

Comments
 (0)