Skip to content

Commit

Permalink
feat: upgrade token-metadata-api-client to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
pradel committed Oct 14, 2024
1 parent f42ad0a commit bcbc45c
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-bikes-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stackspulse": patch
---

Upgrade "@hirosystems/token-metadata-api-client" to v2.
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@dotenvx/dotenvx": "1.15.0",
"@hirosystems/token-metadata-api-client": "1.3.0",
"@hirosystems/token-metadata-api-client": "2.0.0",
"@radix-ui/themes": "3.0.5",
"@sentry/nextjs": "8.33.1",
"@stacks/stacks-blockchain-api-types": "7.14.1",
Expand Down
21 changes: 12 additions & 9 deletions apps/web/src/app/tokens/[token]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env } from "@/env";
import { stacksTokensApi } from "@/lib/stacks";
import { tokenMetadataClient } from "@/lib/stacks";
import { notFound } from "next/navigation";
import { ImageResponse } from "next/og";

Expand All @@ -18,14 +18,17 @@ interface PageProps {
}

export default async function Image({ params }: PageProps) {
const tokenInfo = await stacksTokensApi
.getFtMetadata(params.token)
.catch((error) => {
if (error.status === 404) {
return null;
}
throw error;
});
const metadata = await tokenMetadataClient.GET(
"/metadata/v1/ft/{principal}",
{
params: {
path: {
principal: params.token,
},
},
},
);
const tokenInfo = metadata?.data;
if (!tokenInfo) {
notFound();
}
Expand Down
44 changes: 24 additions & 20 deletions apps/web/src/app/tokens/[token]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TokenHoldersTable } from "@/components/Token/TokenHoldersTable";
import { TokenInfo } from "@/components/Token/TokenInfo";
import { TokenStats } from "@/components/Token/TokenStats";
import { TokenTransactionsVolume } from "@/components/Token/TokenTransactionsVolume";
import { stacksTokensApi } from "@/lib/stacks";
import { tokenMetadataClient } from "@/lib/stacks";
import { Container } from "@radix-ui/themes";
import type { Metadata } from "next";
import { notFound } from "next/navigation";
Expand All @@ -17,14 +17,17 @@ interface PageProps {
export async function generateMetadata({
params,
}: PageProps): Promise<Metadata> {
const tokenInfo = await stacksTokensApi
.getFtMetadata(params.token)
.catch((error) => {
if (error.status === 404) {
return null;
}
throw error;
});
const metadata = await tokenMetadataClient.GET(
"/metadata/v1/ft/{principal}",
{
params: {
path: {
principal: params.token,
},
},
},
);
const tokenInfo = metadata?.data;
if (!tokenInfo) {
notFound();
}
Expand All @@ -39,21 +42,22 @@ export async function generateMetadata({
}

export default async function ProtocolPage({ params }: PageProps) {
const tokenInfo = await stacksTokensApi
.getFtMetadata(params.token)
.catch((error) => {
if (error.status === 404) {
return null;
}
throw error;
});
const metadata = await tokenMetadataClient.GET(
"/metadata/v1/ft/{principal}",
{
params: {
path: {
principal: params.token,
},
},
},
);
const tokenInfo = metadata?.data;
if (!tokenInfo) {
notFound();
}

// TODO change once https://github.com/hirosystems/token-metadata-api/issues/268 is fixed
const token = (tokenInfo as unknown as { asset_identifier: string })
.asset_identifier;
const token = tokenInfo.asset_identifier;

return (
<Container size="2" className="px-4 pt-10">
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Token/TokenHoldersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useGetTokenHolders } from "@/hooks/api/useGetTokenHolders";
import type { FtMetadataResponse } from "@hirosystems/token-metadata-api-client";
import type { FtMetadataResponse } from "@/lib/stacks";
import { Card, Inset, Link, Separator, Table, Text } from "@radix-ui/themes";

interface TokenHoldersTableProps {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Token/TokenInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FtMetadataResponse } from "@hirosystems/token-metadata-api-client";
import type { FtMetadataResponse } from "@/lib/stacks";
import { Heading, Text } from "@radix-ui/themes";
import Image from "next/image";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Token/TokenStats.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useGetTokenHolders } from "@/hooks/api/useGetTokenHolders";
import type { FtMetadataResponse } from "@hirosystems/token-metadata-api-client";
import type { FtMetadataResponse } from "@/lib/stacks";
import { Card, Text } from "@radix-ui/themes";

interface TokenStatsProps {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Token/TokenTransactionsVolume.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useGetTransactionVolume } from "@/hooks/api/useGetTransactionVolume";
import type { FtMetadataResponse } from "@hirosystems/token-metadata-api-client";
import type { FtMetadataResponse } from "@/lib/stacks";
import { Card, Inset, Separator, Text } from "@radix-ui/themes";
import { useMemo } from "react";
import { AreaChart } from "../ui/AreaChart";
Expand Down
22 changes: 14 additions & 8 deletions apps/web/src/hooks/api/useGetFtMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { TokensApi } from "@hirosystems/token-metadata-api-client";
import { tokenMetadataClient } from "@/lib/stacks";
import { useQuery } from "@tanstack/react-query";

const api = new TokensApi();

export const useGetFtMetadata = (params: {
principal: string;
}) => {
export const useGetFtMetadata = (params: { principal: string }) => {
return useQuery({
queryKey: ["get-transactions", params.principal],
queryFn: async () => {
Expand All @@ -18,8 +14,18 @@ export const useGetFtMetadata = (params: {
symbol: "STX",
};
}
const result = await api.getFtMetadata(params.principal);
return result;
const metadata = await tokenMetadataClient.GET(
"/metadata/v1/ft/{principal}",
{
params: {
path: {
principal: params.principal,
},
},
},
);
const tokenInfo = metadata?.data;
return tokenInfo;
},
});
};
15 changes: 13 additions & 2 deletions apps/web/src/lib/stacks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { TokensApi } from "@hirosystems/token-metadata-api-client";
import { createClient } from "@hirosystems/token-metadata-api-client";

export const stacksTokensApi = new TokensApi({}, "https://api.hiro.so", fetch);
export const tokenMetadataClient = createClient({
baseUrl: "https://api.mainnet.hiro.so",
});

export interface FtMetadataResponse {
decimals?: number;
name?: string;
symbol?: string;
description?: string;
image_uri?: string;
image_thumbnail_uri?: string;
}
53 changes: 28 additions & 25 deletions pnpm-lock.yaml

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

0 comments on commit bcbc45c

Please sign in to comment.