Skip to content

Commit c6b0896

Browse files
authored
Merge pull request #83 from macalinao/igm/token-info-from-ctl
Support using token info from certified token list and image URIs
2 parents ee675fc + da7f729 commit c6b0896

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

.changeset/dry-humans-sip.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@macalinao/gill-extra": minor
3+
"example-dapp": patch
4+
---
5+
6+
Update token info fetching to fetch image URIs and from certified token list fallback

apps/example-dapp/src/routes/examples/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ const ExamplesIndexPage: React.FC = () => {
6262
</Link>
6363
</CardContent>
6464
</Card>
65+
66+
<Card>
67+
<CardHeader>
68+
<CardTitle>All Tokens</CardTitle>
69+
<CardDescription>
70+
View comprehensive token information including supply and decimals
71+
</CardDescription>
72+
</CardHeader>
73+
<CardContent>
74+
<Link to="/examples/tokens">
75+
<Button className="w-full">View Example</Button>
76+
</Link>
77+
</CardContent>
78+
</Card>
6579
</div>
6680
</div>
6781
);

apps/example-dapp/src/routes/examples/tokens.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const Route = createFileRoute("/examples/tokens")({
2020
// Define token mint addresses
2121
const TOKENS = [
2222
address("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"), // USDC
23+
address("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"), // USDT
24+
address("AuQaustGiaqxRvj2gtCdrd22PBzTn8kM3kEPEkZCtuDw"), // ADX (no metadata JSON, just an image)
2325
address("So11111111111111111111111111111111111111112"), // Wrapped SOL
2426
address("JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN"), // Jupiter
2527
address("METAewgxyPbgwsseH8T16a39CQ5VyVxZi9zXiDPY18m"), // META

packages/gill-extra/src/fetch-token-info.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,27 @@ export async function fetchTokenInfo({
3838
try {
3939
const response = await fetch(uri);
4040
if (response.ok) {
41-
const result = tokenMetadataSchema.safeParse(await response.json());
41+
const contentType = response.headers.get("content-type");
4242

43-
if (result.success) {
44-
// Override with data from URI JSON
45-
metadataAccountData = {
46-
name: result.data.name,
47-
symbol: result.data.symbol,
48-
};
49-
if (result.data.image) {
50-
metadataUriJson = { image: result.data.image };
51-
}
43+
// If the URI is an image, use it directly as the image URI
44+
if (contentType?.startsWith("image")) {
45+
metadataUriJson = { image: uri };
5246
} else {
53-
console.error("Invalid token metadata:", result.error);
47+
// Otherwise, try to parse it as JSON
48+
const result = tokenMetadataSchema.safeParse(await response.json());
49+
50+
if (result.success) {
51+
// Override with data from URI JSON
52+
metadataAccountData = {
53+
name: result.data.name,
54+
symbol: result.data.symbol,
55+
};
56+
if (result.data.image) {
57+
metadataUriJson = { image: result.data.image };
58+
}
59+
} else {
60+
console.error("Invalid token metadata:", result.error);
61+
}
5462
}
5563
}
5664
} catch (error) {
@@ -59,10 +67,36 @@ export async function fetchTokenInfo({
5967
}
6068

6169
// Create token info with all collected data
62-
return createTokenInfo({
70+
const tokenInfo = createTokenInfo({
6371
mint: mint.address,
6472
mintAccount: { decimals },
6573
metadataAccount: metadataAccountData,
6674
metadataUriJson,
6775
});
76+
77+
// Fallback: Try to fetch from certified token list if no icon URL
78+
if (!tokenInfo.iconURL) {
79+
const certifiedTokenInfoUrl = `https://raw.githubusercontent.com/CLBExchange/certified-token-list/refs/heads/master/101/${mint.address}.json`;
80+
try {
81+
const response = await fetch(certifiedTokenInfoUrl);
82+
if (response.ok) {
83+
const data = (await response.json()) as {
84+
name: string;
85+
symbol: string;
86+
logoURI: string;
87+
};
88+
if (!tokenInfo.name) {
89+
tokenInfo.name = data.name;
90+
}
91+
if (!tokenInfo.symbol) {
92+
tokenInfo.symbol = data.symbol;
93+
}
94+
tokenInfo.iconURL = data.logoURI;
95+
}
96+
} catch (error) {
97+
console.warn("Could not fetch certified token info:", error);
98+
}
99+
}
100+
101+
return tokenInfo;
68102
}

0 commit comments

Comments
 (0)