@coin-voyage/shared 2.4.3 → 2.4.4-beta.1
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/currency/currencies.d.ts +0 -2
- package/dist/currency/currencies.js +0 -3
- package/dist/currency/token-list.d.ts +6 -7
- package/dist/currency/token-list.js +16 -13
- package/dist/types/model.d.ts +3 -2
- package/dist/types/model.js +3 -1
- package/dist/utils/format.d.ts +0 -1
- package/dist/utils/format.js +0 -20
- package/dist/utils/plural.d.ts +1 -1
- package/dist/utils/plural.js +1 -6
- package/package.json +1 -1
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { CurrencyBase } from "../types";
|
|
2
1
|
export declare const FIAT_CURRENCIES: readonly ["USD", "EUR"];
|
|
3
2
|
export type FiatCurrency = (typeof FIAT_CURRENCIES)[number];
|
|
4
3
|
export interface CurrencyExchangeRate {
|
|
@@ -15,4 +14,3 @@ export declare const nonUsdCurrencies: {
|
|
|
15
14
|
currency: string;
|
|
16
15
|
decimals: number;
|
|
17
16
|
}[];
|
|
18
|
-
export declare function currencyID(currency: CurrencyBase): string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChainId } from "../types/enums";
|
|
2
|
-
import type
|
|
2
|
+
import { type Currency, type CurrencyBase } from "../types/model";
|
|
3
3
|
export interface Token {
|
|
4
4
|
name: string;
|
|
5
5
|
address?: string;
|
|
@@ -13,7 +13,7 @@ export interface ChainMetadata {
|
|
|
13
13
|
name: string;
|
|
14
14
|
logoURI: string;
|
|
15
15
|
}
|
|
16
|
-
interface ChainEntry {
|
|
16
|
+
export interface ChainEntry {
|
|
17
17
|
chainId: ChainId;
|
|
18
18
|
name: string;
|
|
19
19
|
logoURI: string;
|
|
@@ -23,14 +23,14 @@ interface ChainEntry {
|
|
|
23
23
|
} | null;
|
|
24
24
|
tokens: Token[];
|
|
25
25
|
}
|
|
26
|
-
interface TokenListResponse {
|
|
26
|
+
export interface TokenListResponse {
|
|
27
27
|
chains: ChainEntry[];
|
|
28
28
|
}
|
|
29
29
|
export declare function tokenToCurrency(token: Token): Currency;
|
|
30
30
|
/**
|
|
31
31
|
* Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
|
|
32
32
|
*/
|
|
33
|
-
export declare function fetchTokenList(): Promise<TokenListResponse>;
|
|
33
|
+
export declare function fetchTokenList(url?: string, init?: RequestInit): Promise<TokenListResponse>;
|
|
34
34
|
/**
|
|
35
35
|
* Returns all chains from the token list.
|
|
36
36
|
*/
|
|
@@ -38,9 +38,8 @@ export declare function getChains(chains: ChainEntry[]): ChainMetadata[];
|
|
|
38
38
|
/**
|
|
39
39
|
* Returns all tokens for the given chainId.
|
|
40
40
|
*/
|
|
41
|
-
export declare function tokensByChainId(chains: ChainEntry[], chainId: ChainId): Token[];
|
|
41
|
+
export declare function tokensByChainId(chains: readonly ChainEntry[], chainId: ChainId): Token[];
|
|
42
42
|
/**
|
|
43
43
|
* Returns the token for the given chainId and address (undefined for native asset).
|
|
44
44
|
*/
|
|
45
|
-
export declare function getToken(chains: ChainEntry[], currency: CurrencyBase): Token | undefined;
|
|
46
|
-
export {};
|
|
45
|
+
export declare function getToken(chains: readonly ChainEntry[], currency: CurrencyBase): Token | undefined;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { currencyID } from "
|
|
1
|
+
import { currencyID } from "../types/model";
|
|
2
2
|
const TOKEN_LIST_URL = "https://raw.githubusercontent.com/coin-voyage/token-list/main/tokenlist.json";
|
|
3
|
+
const normalizeAddress = (address) => address?.toLowerCase() ?? null;
|
|
3
4
|
export function tokenToCurrency(token) {
|
|
4
5
|
return {
|
|
5
6
|
id: currencyID({ chain_id: token.chainId, address: token.address }),
|
|
@@ -14,33 +15,35 @@ export function tokenToCurrency(token) {
|
|
|
14
15
|
/**
|
|
15
16
|
* Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
|
|
16
17
|
*/
|
|
17
|
-
export async function fetchTokenList() {
|
|
18
|
-
const res = await fetch(
|
|
18
|
+
export async function fetchTokenList(url = TOKEN_LIST_URL, init) {
|
|
19
|
+
const res = await fetch(url, init);
|
|
19
20
|
if (!res.ok)
|
|
20
|
-
throw new Error(`Failed to fetch token list: ${res.status}`);
|
|
21
|
-
return
|
|
21
|
+
throw new Error(`Failed to fetch token list: ${res.status} ${res.statusText}`);
|
|
22
|
+
return res.json();
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* Returns all chains from the token list.
|
|
25
26
|
*/
|
|
26
27
|
export function getChains(chains) {
|
|
27
|
-
return chains.map((
|
|
28
|
-
chainId
|
|
29
|
-
name
|
|
30
|
-
logoURI
|
|
28
|
+
return chains.map(({ chainId, name, logoURI }) => ({
|
|
29
|
+
chainId,
|
|
30
|
+
name,
|
|
31
|
+
logoURI,
|
|
31
32
|
}));
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Returns all tokens for the given chainId.
|
|
35
36
|
*/
|
|
36
37
|
export function tokensByChainId(chains, chainId) {
|
|
37
|
-
|
|
38
|
-
return chain?.tokens ?? [];
|
|
38
|
+
return chains.find((chain) => chain.chainId === chainId)?.tokens ?? [];
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* Returns the token for the given chainId and address (undefined for native asset).
|
|
42
42
|
*/
|
|
43
43
|
export function getToken(chains, currency) {
|
|
44
|
-
const
|
|
45
|
-
return
|
|
44
|
+
const currencyAddress = normalizeAddress(currency.address);
|
|
45
|
+
return tokensByChainId(chains, currency.chain_id).find((token) => {
|
|
46
|
+
const tokenAddress = normalizeAddress(token.address);
|
|
47
|
+
return tokenAddress === currencyAddress;
|
|
48
|
+
});
|
|
46
49
|
}
|
package/dist/types/model.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { FiatCurrency } from "../currency/currencies";
|
|
3
|
-
import { zPayOrderMetadata, zPayOrderSettings } from "../schemas/pay-order";
|
|
2
|
+
import type { FiatCurrency } from "../currency/currencies";
|
|
3
|
+
import type { zPayOrderMetadata, zPayOrderSettings } from "../schemas/pay-order";
|
|
4
4
|
import { ChainId, PaymentRail, PayOrderMode, PayOrderStatus, StepKind } from "./enums";
|
|
5
5
|
export type PayOrder = {
|
|
6
6
|
id: string;
|
|
@@ -34,6 +34,7 @@ export interface CurrencyBase {
|
|
|
34
34
|
chain_id: ChainId;
|
|
35
35
|
address?: string;
|
|
36
36
|
}
|
|
37
|
+
export declare function currencyID(currency: CurrencyBase): string;
|
|
37
38
|
export interface CurrencyWithAmount extends Currency {
|
|
38
39
|
currency_amount: CurrencyAmount;
|
|
39
40
|
}
|
package/dist/types/model.js
CHANGED
package/dist/utils/format.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export declare function capitalize(str: string): string;
|
|
2
2
|
export declare function truncateAddress(address?: string, length?: number): string;
|
|
3
3
|
export declare function truncateENSName(ensName: string, maxLength: number): string;
|
|
4
|
-
export declare function nFormatter(num: number, digits?: number): string;
|
package/dist/utils/format.js
CHANGED
|
@@ -13,23 +13,3 @@ export function truncateENSName(ensName, maxLength) {
|
|
|
13
13
|
}
|
|
14
14
|
return ensName;
|
|
15
15
|
}
|
|
16
|
-
export function nFormatter(num, digits = 2) {
|
|
17
|
-
if (num < 10000) {
|
|
18
|
-
return num.toFixed(2);
|
|
19
|
-
}
|
|
20
|
-
const lookup = [
|
|
21
|
-
{ value: 1, symbol: "" },
|
|
22
|
-
{ value: 1e3, symbol: "k" },
|
|
23
|
-
{ value: 1e6, symbol: "m" },
|
|
24
|
-
{ value: 1e9, symbol: "g" },
|
|
25
|
-
{ value: 1e12, symbol: "t" },
|
|
26
|
-
{ value: 1e15, symbol: "p" },
|
|
27
|
-
{ value: 1e18, symbol: "e" },
|
|
28
|
-
];
|
|
29
|
-
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
|
|
30
|
-
const item = lookup
|
|
31
|
-
.slice()
|
|
32
|
-
.reverse()
|
|
33
|
-
.find((item) => num >= item.value);
|
|
34
|
-
return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0";
|
|
35
|
-
}
|
package/dist/utils/plural.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const withPlural: (
|
|
1
|
+
export declare const withPlural: (quantity: number, singular: string, plural: string) => string;
|
package/dist/utils/plural.js
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
export const withPlural = (
|
|
2
|
-
if (totalQuantity == 1)
|
|
3
|
-
return `${totalQuantity} ${singular}`;
|
|
4
|
-
else
|
|
5
|
-
return `${totalQuantity} ${plural}`;
|
|
6
|
-
};
|
|
1
|
+
export const withPlural = (quantity, singular, plural) => `${quantity} ${quantity === 1 ? singular : plural}`;
|