Skip to content

Commit

Permalink
feat: move stacking dao query to API (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradel authored Aug 11, 2024
1 parent 1d2876f commit f856fd5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/wet-trees-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"stackspulse": patch
"@stackspulse/server": patch
---

Migrate `/api/protocols/stackingdao` route.
78 changes: 78 additions & 0 deletions apps/server/src/api/protocols/stackingdao/index.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";

type StackingDAOProtocolStatsResponse = {
month: string;
deposits: number;
withdrawals: number;
}[];

export default defineCachedEventHandler(async (event) => {
const result = await sql`
WITH monthly_blocks AS (
SELECT
DATE_TRUNC('month', TO_TIMESTAMP(burn_block_time)) AS month,
MIN(block_height) AS min_block_height,
MAX(block_height) AS max_block_height
FROM
blocks
GROUP BY
DATE_TRUNC('month', TO_TIMESTAMP(burn_block_time))
),
deposits AS (
SELECT
mb.month,
SUM(se.amount) AS deposits
FROM
monthly_blocks mb
JOIN
stx_events se ON se.block_height BETWEEN mb.min_block_height AND mb.max_block_height
WHERE
se.recipient = 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.reserve-v1'
AND se.sender NOT LIKE 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG%'
AND canonical = TRUE
AND microblock_canonical = TRUE
GROUP BY
mb.month
),
withdrawals AS (
SELECT
mb.month,
SUM(se.amount) AS withdrawals
FROM
monthly_blocks mb
JOIN
stx_events se ON se.block_height BETWEEN mb.min_block_height AND mb.max_block_height
WHERE
se.sender = 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.reserve-v1'
AND se.recipient NOT LIKE 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG%'
AND canonical = TRUE
AND microblock_canonical = TRUE
GROUP BY
mb.month
)
SELECT
mb.month,
COALESCE(d.deposits, 0) AS deposits,
COALESCE(w.withdrawals, 0) AS withdrawals
FROM
monthly_blocks mb
LEFT JOIN
deposits d ON mb.month = d.month
LEFT JOIN
withdrawals w ON mb.month = w.month
ORDER BY
mb.month ASC
`;

const stats: StackingDAOProtocolStatsResponse = result.map((row) => ({
// format of the month is "2021-08-01 00:00:00+00"
// we want to output "2021-08"
month: row.month.slice(0, 7),
deposits: Number.parseInt(row.deposits),
withdrawals: Number.parseInt(row.withdrawals),
}));

return stats;
}, apiCacheConfig);
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { db } from "@/db/db";
import { transactionTable } from "@/db/schema";
import { eq, sql } from "drizzle-orm";
import { env } from "@/env";
import type { StackingDAOProtocolStatsResponse } from "@/lib/api";
import { DepositWithdrawBarChartClient } from "./DepositWithdrawBarChartClient";

const getData = async () => {
const query = db
.select({
month: sql<string>`strftime('%Y-%m', timestamp, 'unixepoch') as month`,
depositsAmount: sql<number>`sum(case when action = 'stackingdao-deposit' then json->>'outAmount' else 0 end) as depositsAmount`,
withdrawalsAmount: sql<number>`sum(case when action = 'stackingdao-withdraw' then json->>'inAmount' else 0 end) as withdrawalsAmount`,
})
.from(transactionTable)
.where(eq(transactionTable.protocol, "stackingdao"))
.groupBy(sql`month`);

const stats = await query;
return stats;
};

export const DepositWithdrawBarChart = async () => {
const stats = await getData();
const stats: StackingDAOProtocolStatsResponse = await fetch(
`${env.NEXT_PUBLIC_API_URL}/api/protocols/stackingdao`,
).then((res) => res.json());

const formattedData: {
date: string;
withdrawals: number;
deposits: number;
}[] = stats.map((d) => ({
date: d.month,
withdrawals: d.withdrawalsAmount,
deposits: d.depositsAmount,
withdrawals: d.withdrawals,
deposits: d.deposits,
}));

return <DepositWithdrawBarChartClient data={formattedData} />;
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ export type TransactionStatsRouteResponse = {
export type TransactionStatsRouteQuery = {
protocol?: Protocol;
};

/**
* `/api/protocols/stackingdao`
*/

export type StackingDAOProtocolStatsResponse = {
month: string;
deposits: number;
withdrawals: number;
}[];

0 comments on commit f856fd5

Please sign in to comment.