@coin-voyage/shared 2.2.5-beta.5 → 2.2.5-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { ChainId, CurrencyBase } from "../types";
1
+ import type { ChainId, Currency, CurrencyBase } from "../types";
2
2
  export interface Token {
3
3
  name: string;
4
4
  address?: string;
@@ -12,19 +12,34 @@ export interface ChainMetadata {
12
12
  name: string;
13
13
  logoURI: string;
14
14
  }
15
+ interface ChainEntry {
16
+ chainId: ChainId;
17
+ name: string;
18
+ logoURI: string;
19
+ nativeCurrency?: {
20
+ symbol: string;
21
+ decimals: number;
22
+ } | null;
23
+ tokens: Token[];
24
+ }
25
+ interface TokenListResponse {
26
+ chains: ChainEntry[];
27
+ }
28
+ export declare function tokenToCurrency(token: Token): Currency;
15
29
  /**
16
- * Returns all chains from the token list.
30
+ * Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
17
31
  */
18
- export declare function getChains(): Promise<ChainMetadata[]>;
32
+ export declare function fetchTokenList(): Promise<TokenListResponse>;
19
33
  /**
20
- * Returns all tokens for the given chainId.
34
+ * Returns all chains from the token list.
21
35
  */
22
- export declare function tokensByChainId(chainId: ChainId): Promise<Token[]>;
36
+ export declare function getChains(chains: ChainEntry[]): ChainMetadata[];
23
37
  /**
24
- * Returns chain metadata for the given chainId.
38
+ * Returns all tokens for the given chainId.
25
39
  */
26
- export declare function getChainMetadata(chainId: ChainId): Promise<ChainMetadata | undefined>;
40
+ export declare function tokensByChainId(chains: ChainEntry[], chainId: ChainId): Token[];
27
41
  /**
28
42
  * Returns the token for the given chainId and address (undefined for native asset).
29
43
  */
30
- export declare function getToken(currency: CurrencyBase): Promise<Token | undefined>;
44
+ export declare function getToken(chains: ChainEntry[], currency: CurrencyBase): Token | undefined;
45
+ export {};
@@ -1,5 +1,18 @@
1
1
  const TOKEN_LIST_URL = "https://raw.githubusercontent.com/coin-voyage/token-list/main/tokenlist.json";
2
- async function fetchTokenList() {
2
+ export function tokenToCurrency(token) {
3
+ return {
4
+ chain_id: token.chainId,
5
+ address: token.address,
6
+ name: token.name,
7
+ ticker: token.ticker,
8
+ decimals: token.decimals,
9
+ image_uri: token.logoURI,
10
+ };
11
+ }
12
+ /**
13
+ * Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
14
+ */
15
+ export async function fetchTokenList() {
3
16
  const res = await fetch(TOKEN_LIST_URL);
4
17
  if (!res.ok)
5
18
  throw new Error(`Failed to fetch token list: ${res.status}`);
@@ -8,9 +21,8 @@ async function fetchTokenList() {
8
21
  /**
9
22
  * Returns all chains from the token list.
10
23
  */
11
- export async function getChains() {
12
- const data = await fetchTokenList();
13
- return data.chains.map((c) => ({
24
+ export function getChains(chains) {
25
+ return chains.map((c) => ({
14
26
  chainId: c.chainId,
15
27
  name: c.name,
16
28
  logoURI: c.logoURI,
@@ -19,25 +31,14 @@ export async function getChains() {
19
31
  /**
20
32
  * Returns all tokens for the given chainId.
21
33
  */
22
- export async function tokensByChainId(chainId) {
23
- const data = await fetchTokenList();
24
- const chain = data.chains.find((c) => c.chainId === chainId);
34
+ export function tokensByChainId(chains, chainId) {
35
+ const chain = chains.find((c) => c.chainId === chainId);
25
36
  return chain?.tokens ?? [];
26
37
  }
27
- /**
28
- * Returns chain metadata for the given chainId.
29
- */
30
- export async function getChainMetadata(chainId) {
31
- const data = await fetchTokenList();
32
- const chain = data.chains.find((c) => c.chainId === chainId);
33
- if (!chain)
34
- return undefined;
35
- return { chainId: chain.chainId, name: chain.name, logoURI: chain.logoURI };
36
- }
37
38
  /**
38
39
  * Returns the token for the given chainId and address (undefined for native asset).
39
40
  */
40
- export async function getToken(currency) {
41
- const tokens = await tokensByChainId(currency.chain_id);
41
+ export function getToken(chains, currency) {
42
+ const tokens = tokensByChainId(chains, currency.chain_id);
42
43
  return tokens.find((t) => !currency.address ? !t.address : t.address?.toLowerCase() === currency.address.toLowerCase());
43
44
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coin-voyage/shared",
3
3
  "description": "Shared utilities for Coin Voyage",
4
- "version": "2.2.5-beta.5",
4
+ "version": "2.2.5-beta.7",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {