Skip to content

Commit

Permalink
Merge pull request #229 from helius-labs/i/search-by-token-symbol
Browse files Browse the repository at this point in the history
[Feature] Add Search By Token Symbol Functionality
  • Loading branch information
owenventer authored Dec 12, 2023
2 parents ac7b9eb + 50358a9 commit cb9a4f3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/components/modals/help.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
["globe", ".sol, .abc, .poor, .bonk domains"],
["person", "Wallet/Account addresses"],
["coins", "Token addresses"],
["dots", "Token symbols (case sensitive)"],
["lightning", "Transaction signatures"],
];
</script>

<p class="mb-3">Use the search bar to look up any of the following.</p>
<p class="mb-3">Use the search bar to look up any of the following:</p>

<strong class="uppercase">Supported Searches</strong>

Expand Down
16 changes: 16 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ export interface Modal {
export type Modals = keyof typeof modals;

export type NullableProp<T> = T | null | undefined;

export interface JupiterToken {
name: string;
symbol: string;
address: string;
decimals: number;
logoURI: string;
}

export interface TokenMap {
[symbol: string]: string;
}

export type RecognizedTokens = {
[key: string]: string;
};
33 changes: 33 additions & 0 deletions src/lib/util/get-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { JupiterToken, TokenMap } from "$lib/types";
import { recognizedTokens } from "./recognized-tokens";

const getJupiterTokens = async (): Promise<TokenMap> => {
try {
const data = await fetch(`https://token.jup.ag/all`);
const jsonData = await data.json();

if (!Array.isArray(jsonData)) {
throw new Error("Unexpected data format from Jupiter API");
}

const tokens: JupiterToken[] = jsonData as JupiterToken[];
const tokenMap = tokens.reduce((acc: TokenMap, token: JupiterToken) => {
if (
(recognizedTokens[token.symbol] &&
recognizedTokens[token.symbol] === token.address) ||
!recognizedTokens[token.symbol]
) {
acc[token.symbol] = token.address;
}
return acc;
}, {});

return tokenMap;
} catch (error: any) {
throw new Error(
`Failed to fetch tokens from Jupiter with error: ${error}`
);
}
};

export default getJupiterTokens;
6 changes: 6 additions & 0 deletions src/lib/util/recognized-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { RecognizedTokens } from "$lib/types";

export const recognizedTokens: RecognizedTokens = {
FOXY: "FoXyMu5xwXre7zEoSvzViRk3nGawHUp9kUh97y2NDhcq",
USDC: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
};
13 changes: 13 additions & 0 deletions src/lib/xray/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { PublicKey } from "@solana/web3.js";
import { TldParser } from "@onsol/tldparser";
import { browser } from "$app/environment";

import getJupiterTokens from "$lib/util/get-tokens";

export interface SearchResult {
url: string;
address: string;
Expand Down Expand Up @@ -52,6 +54,9 @@ export const search = async (
}
const isMainnetValue = network !== "devnet";

// For token symbols
const tokenSymbols = await getJupiterTokens();

if (isValidPublicKey(query)) {
const pubkey = new PublicKey(query);
const account = await connection.getParsedAccountInfo(pubkey);
Expand Down Expand Up @@ -118,6 +123,14 @@ export const search = async (
url: `/account/${owner}`,
valid: true,
};
} else if (Object.keys(tokenSymbols).find((key) => key === query)) {
return {
address: tokenSymbols[query],
search: query,
type: "token",
url: `/token/${tokenSymbols[query]}`,
valid: true,
};
}

return searchDefaults;
Expand Down

0 comments on commit cb9a4f3

Please sign in to comment.