Skip to content
Draft
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
4 changes: 3 additions & 1 deletion apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"version": "0.17.1",
"private": true,
"scripts": {
"build": "nitro build",
"build": "nitro build && tsc --noEmit",
"dev": "dotenvx run --env-file=.env.development.local --env-file=.env.development -- nitro dev --port 3001",
"prepare": "nitro prepare",
"stacks-node:db:pull": "dotenvx run --env-file=.env.production.local --env-file=.env.production -- prisma db pull",
"prisma:generate": "dotenvx run --env-file=.env.production.local -- prisma generate --sql",
"deploy": "fly deploy --remote-only"
},
"dependencies": {
"@dotenvx/dotenvx": "1.25.1",
"@libsql/client": "0.8.0",
"@prisma/client": "6.0.0",
"@sentry/node": "8.41.0",
"@stacks/blockchain-api-client": "8.2.2",
"@stacks/stacks-blockchain-api-types": "7.14.1",
Expand Down
1 change: 1 addition & 0 deletions apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["typedSql"]
}

datasource db {
Expand Down
59 changes: 59 additions & 0 deletions apps/server/prisma/sql/stackingDAOStats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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
WHERE
block_height >= 132118
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
COALESCE(d.month, w.month) AS month,
COALESCE(d.deposits, 0) AS deposits,
COALESCE(w.withdrawals, 0) AS withdrawals
FROM
deposits d
FULL OUTER JOIN
withdrawals w ON d.month = w.month
WHERE
COALESCE(d.deposits, 0) > 0 OR COALESCE(w.withdrawals, 0) > 0
ORDER BY
month ASC
67 changes: 4 additions & 63 deletions apps/server/src/api/protocols/stackingdao/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,15 @@
import { sql } from "~/db/db";
import { stackingDAOStats } from "@prisma/client/sql";
import { apiCacheConfig } from "~/lib/api";
import { prisma } from "~/lib/prisma";

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
WHERE
block_height >= 132118
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
COALESCE(d.month, w.month) AS month,
COALESCE(d.deposits, 0) AS deposits,
COALESCE(w.withdrawals, 0) AS withdrawals
FROM
deposits d
FULL OUTER JOIN
withdrawals w ON d.month = w.month
WHERE
COALESCE(d.deposits, 0) > 0 OR COALESCE(w.withdrawals, 0) > 0
ORDER BY
month ASC
`;
export default defineCachedEventHandler(async () => {
const result = await prisma.$queryRawTyped(stackingDAOStats());
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

results are nullable, related to prisma/prisma#25268


const stats: StackingDAOProtocolStatsResponse = result.map((row) => ({
// format of the month is "2021-08-01 00:00:00+00"
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PrismaClient } from "@prisma/client";

const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };

export const prisma = globalForPrisma.prisma || new PrismaClient();

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "root",
"private": true,
"packageManager": "pnpm@9.7.0",
"packageManager": "pnpm@9.14.4",
"scripts": {
"build": "pnpm turbo run build",
"format": "biome check --write --no-errors-on-unmatched",
Expand Down
45 changes: 23 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.