Skip to content

Commit b39aab3

Browse files
committed
txs count route
1 parent e400582 commit b39aab3

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

src/handlers/getBridgeTxCounts.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { IResponse, successResponse } from "../utils/lambda-response";
2+
import wrap from "../utils/wrap";
3+
import bridgeNetworks from "../data/bridgeNetworkData";
4+
import { normalizeChain } from "../utils/normalizeChain";
5+
import { DEFAULT_TTL } from "../utils/cache";
6+
import { queryBridgeTxCounts24h } from "../utils/wrappa/postgres/query";
7+
8+
const secondsInDay = 24 * 60 * 60;
9+
10+
const getBridgeTxCounts = async (chain?: string) => {
11+
const queryChain = chain && chain !== "all" ? normalizeChain(chain) : undefined;
12+
const startTimestamp = Math.floor(Date.now() / 1000) - secondsInDay;
13+
const txCounts = await queryBridgeTxCounts24h(startTimestamp, queryChain);
14+
const txCountsByBridgeName = txCounts.reduce((acc, row) => {
15+
acc[row.bridge_name] = row;
16+
return acc;
17+
}, {} as Record<string, (typeof txCounts)[number]>);
18+
19+
return {
20+
bridges: bridgeNetworks.map(({ id, displayName, slug, bridgeDbName }) => {
21+
const counts = txCountsByBridgeName[bridgeDbName];
22+
const depositTxs24h = counts?.deposit_txs_24h ?? 0;
23+
const withdrawTxs24h = counts?.withdraw_txs_24h ?? 0;
24+
25+
return {
26+
id,
27+
displayName,
28+
slug,
29+
txsPrevDay: depositTxs24h + withdrawTxs24h,
30+
depositTxs24h,
31+
withdrawTxs24h,
32+
};
33+
}),
34+
};
35+
};
36+
37+
const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> => {
38+
const chain = event.pathParameters?.chain?.toLowerCase().replace(/%20/g, " ");
39+
const response = await getBridgeTxCounts(chain);
40+
return successResponse(response, DEFAULT_TTL);
41+
};
42+
43+
export default wrap(handler);

src/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import getBridgeVolumeBySlug from "../handlers/getBridgeVolumeBySlug";
66
import getBridges from "../handlers/getBridges";
77
import getBridge from "../handlers/getBridge";
88
import getBridgeChains from "../handlers/getBridgeChains";
9+
import getBridgeTxCounts from "../handlers/getBridgeTxCounts";
910
import getLargeTransactions from "../handlers/getLargeTransactions";
1011
import getLastBlocks from "../handlers/getLastBlocks";
1112
import getNetflows from "../handlers/getNetflows";
@@ -86,6 +87,7 @@ const start = async () => {
8687
server.get("/bridgedaystats/:timestamp/:chain", lambdaToFastify(getBridgeStatsOnDay));
8788
server.get("/bridgevolume/:chain", lambdaToFastify(getBridgeVolume));
8889
server.get("/bridgevolume/slug/:slug", lambdaToFastify(getBridgeVolumeBySlug));
90+
server.get("/bridgetxcounts/:chain", lambdaToFastify(getBridgeTxCounts));
8991
server.get("/bridges", lambdaToFastify(getBridges));
9092
server.get("/bridge/:id", lambdaToFastify(getBridge));
9193
server.get("/bridgechains", lambdaToFastify(getBridgeChains));

src/utils/wrappa/postgres/query.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ interface IAggregatedData {
4444
total_address_withdrawn: string[];
4545
}
4646

47+
interface IBridgeTxCounts24H {
48+
bridge_name: string;
49+
deposit_txs_24h: number;
50+
withdraw_txs_24h: number;
51+
}
52+
4753
type TimePeriod = "day" | "week" | "month";
4854

4955
const getBridgeID = async (bridgNetworkName: string, chain: string) => {
@@ -380,6 +386,95 @@ const getLast24HVolume = async (bridgeName: string, volumeType: VolumeType = "bo
380386
return totalVolume / 2;
381387
};
382388

389+
const queryBridgeTxCounts24h = async (startTimestamp: number, chain?: string) => {
390+
if (!chain) {
391+
return await sql<IBridgeTxCounts24H[]>`
392+
SELECT
393+
c.bridge_name,
394+
CAST(
395+
COALESCE(
396+
SUM(
397+
CASE
398+
WHEN t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
399+
ELSE 0
400+
END
401+
),
402+
0
403+
) AS INTEGER
404+
) AS deposit_txs_24h,
405+
CAST(
406+
COALESCE(
407+
SUM(
408+
CASE
409+
WHEN NOT t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
410+
ELSE 0
411+
END
412+
),
413+
0
414+
) AS INTEGER
415+
) AS withdraw_txs_24h
416+
FROM bridges.config c
417+
LEFT JOIN bridges.transactions t
418+
ON t.bridge_id = c.id
419+
AND t.ts >= to_timestamp(${startTimestamp})
420+
GROUP BY c.bridge_name
421+
ORDER BY c.bridge_name;
422+
`;
423+
}
424+
425+
return await sql<IBridgeTxCounts24H[]>`
426+
SELECT
427+
c.bridge_name,
428+
CAST(
429+
COALESCE(
430+
SUM(
431+
CASE
432+
WHEN c.chain = ${chain} THEN
433+
CASE
434+
WHEN t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
435+
ELSE 0
436+
END
437+
WHEN c.destination_chain = ${chain} THEN
438+
CASE
439+
WHEN NOT t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
440+
ELSE 0
441+
END
442+
ELSE 0
443+
END
444+
),
445+
0
446+
) AS INTEGER
447+
) AS deposit_txs_24h,
448+
CAST(
449+
COALESCE(
450+
SUM(
451+
CASE
452+
WHEN c.chain = ${chain} THEN
453+
CASE
454+
WHEN NOT t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
455+
ELSE 0
456+
END
457+
WHEN c.destination_chain = ${chain} THEN
458+
CASE
459+
WHEN t.is_deposit THEN COALESCE(t.txs_counted_as, 1)
460+
ELSE 0
461+
END
462+
ELSE 0
463+
END
464+
),
465+
0
466+
) AS INTEGER
467+
) AS withdraw_txs_24h
468+
FROM bridges.config c
469+
LEFT JOIN bridges.transactions t
470+
ON t.bridge_id = c.id
471+
AND t.ts >= to_timestamp(${startTimestamp})
472+
WHERE (c.chain = ${chain} OR c.destination_chain = ${chain})
473+
GROUP BY c.bridge_name
474+
ORDER BY c.bridge_name;
475+
`;
476+
};
477+
383478
const getNetflows = async (period: TimePeriod) => {
384479
let tableAndWhere = sql``;
385480
switch (period) {
@@ -484,6 +579,7 @@ export {
484579
queryAggregatedDailyTimestampRange,
485580
queryAggregatedHourlyTimestampRange,
486581
getLast24HVolume,
582+
queryBridgeTxCounts24h,
487583
getNetflows,
488584
queryAggregatedTokensInRange,
489585
};

0 commit comments

Comments
 (0)