Skip to content

Commit

Permalink
feat: create new token holders route (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradel authored Sep 27, 2024
1 parent 82a6e5d commit e46cb5c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/odd-foxes-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackspulse/server": minor
---

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

const tokensHoldersRouteSchema = z.object({
token: z.string(),
limit: z.number().min(1).max(100).optional(),
offset: z.number().min(0).optional(),
});

type TokensHoldersRouteResponse = {
total_supply: string;
total: number;
results: {
address: string;
balance: string;
}[];
};

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

const limit = query.limit ?? 20;
const offset = query.offset ?? 0;

const data: TokensHoldersRouteResponse = (
await stacksClient.GET("/extended/v1/tokens/ft/{token}/holders", {
params: {
path: { token: query.token },
query: { limit, offset },
},
})
).data;

return data;
}, apiCacheConfig);
7 changes: 6 additions & 1 deletion apps/server/src/api/tokens/transaction-volume.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const tokensTransactionVolumeRouteSchema = z.object({
token: z.string(),
});

type TokensTransactionVolumeRouteResponse = {
date: string;
daily_volume: string;
}[];

export default defineCachedEventHandler(async (event) => {
const query = await getValidatedQueryZod(
event,
Expand All @@ -30,7 +35,7 @@ ORDER BY
date;
`;

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

0 comments on commit e46cb5c

Please sign in to comment.