Skip to content

Commit

Permalink
feat: token transaction volume (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradel authored Sep 25, 2024
1 parent 6b5d0de commit 51cf12d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-deers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackspulse/server": minor
---

Create new `/tokens/transaction-volume` route.
39 changes: 39 additions & 0 deletions apps/server/src/api/tokens/transaction-volume.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { z } from "zod";
import { sql } from "~/db/db";
import { apiCacheConfig } from "~/lib/api";
import { getValidatedQueryZod } from "~/lib/nitro";

const tokensTransactionVolumeRouteSchema = z.object({
token: z.string(),
});

export default defineCachedEventHandler(async (event) => {
const query = await getValidatedQueryZod(
event,
tokensTransactionVolumeRouteSchema,
);

const result = await sql`
SELECT
DATE_TRUNC('day', TO_TIMESTAMP(blocks.burn_block_time)) AS date,
SUM(ft_events.amount::numeric) AS daily_volume
FROM
ft_events
JOIN
blocks ON ft_events.index_block_hash = blocks.index_block_hash
WHERE
ft_events.asset_identifier = ${query.token}
AND ft_events.canonical = true
GROUP BY
DATE_TRUNC('day', TO_TIMESTAMP(blocks.burn_block_time))
ORDER BY
date;
`;

const stats = result.map((row) => ({
date: row.date.slice(0, 10),
daily_volume: row.daily_volume,
}));

return stats;
}, apiCacheConfig);

0 comments on commit 51cf12d

Please sign in to comment.