@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.
- package/dist/common/token-list.d.ts +23 -8
- package/dist/common/token-list.js +20 -19
- package/package.json +1 -1
|
@@ -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
|
-
*
|
|
30
|
+
* Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
|
|
17
31
|
*/
|
|
18
|
-
export declare function
|
|
32
|
+
export declare function fetchTokenList(): Promise<TokenListResponse>;
|
|
19
33
|
/**
|
|
20
|
-
* Returns all
|
|
34
|
+
* Returns all chains from the token list.
|
|
21
35
|
*/
|
|
22
|
-
export declare function
|
|
36
|
+
export declare function getChains(chains: ChainEntry[]): ChainMetadata[];
|
|
23
37
|
/**
|
|
24
|
-
* Returns
|
|
38
|
+
* Returns all tokens for the given chainId.
|
|
25
39
|
*/
|
|
26
|
-
export declare function
|
|
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):
|
|
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
|
-
|
|
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
|
|
12
|
-
|
|
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
|
|
23
|
-
const
|
|
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
|
|
41
|
-
const tokens =
|
|
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
|
}
|