@avail-project/widgets 0.0.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/README.md +34 -0
- package/dist/common/components/ErrorBoundary.d.ts +44 -0
- package/dist/common/components/ErrorBoundary.d.ts.map +1 -0
- package/dist/common/components/ErrorBoundary.js +65 -0
- package/dist/common/hooks/useDebouncedCallback.d.ts +13 -0
- package/dist/common/hooks/useDebouncedCallback.d.ts.map +1 -0
- package/dist/common/hooks/useDebouncedCallback.js +42 -0
- package/dist/common/hooks/useDebouncedValue.d.ts +5 -0
- package/dist/common/hooks/useDebouncedValue.d.ts.map +1 -0
- package/dist/common/hooks/useDebouncedValue.js +15 -0
- package/dist/common/hooks/useInterval.d.ts +11 -0
- package/dist/common/hooks/useInterval.d.ts.map +1 -0
- package/dist/common/hooks/useInterval.js +25 -0
- package/dist/common/hooks/useLatest.d.ts +12 -0
- package/dist/common/hooks/useLatest.d.ts.map +1 -0
- package/dist/common/hooks/useLatest.js +19 -0
- package/dist/common/hooks/useNexusError.d.ts +20 -0
- package/dist/common/hooks/useNexusError.d.ts.map +1 -0
- package/dist/common/hooks/useNexusError.js +120 -0
- package/dist/common/hooks/usePolling.d.ts +7 -0
- package/dist/common/hooks/usePolling.d.ts.map +1 -0
- package/dist/common/hooks/usePolling.js +29 -0
- package/dist/common/hooks/useStableCallback.d.ts +6 -0
- package/dist/common/hooks/useStableCallback.d.ts.map +1 -0
- package/dist/common/hooks/useStableCallback.js +13 -0
- package/dist/common/hooks/useStopwatch.d.ts +17 -0
- package/dist/common/hooks/useStopwatch.d.ts.map +1 -0
- package/dist/common/hooks/useStopwatch.js +44 -0
- package/dist/common/hooks/useTransactionExecution.d.ts +62 -0
- package/dist/common/hooks/useTransactionExecution.d.ts.map +1 -0
- package/dist/common/hooks/useTransactionExecution.js +273 -0
- package/dist/common/hooks/useTransactionFlow.d.ts +71 -0
- package/dist/common/hooks/useTransactionFlow.d.ts.map +1 -0
- package/dist/common/hooks/useTransactionFlow.js +435 -0
- package/dist/common/index.d.ts +17 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +16 -0
- package/dist/common/tx/steps.d.ts +19 -0
- package/dist/common/tx/steps.d.ts.map +1 -0
- package/dist/common/tx/steps.js +89 -0
- package/dist/common/tx/types.d.ts +11 -0
- package/dist/common/tx/types.d.ts.map +1 -0
- package/dist/common/tx/types.js +19 -0
- package/dist/common/tx/useTransactionSteps.d.ts +21 -0
- package/dist/common/tx/useTransactionSteps.d.ts.map +1 -0
- package/dist/common/tx/useTransactionSteps.js +40 -0
- package/dist/common/types/transaction-flow.d.ts +48 -0
- package/dist/common/types/transaction-flow.d.ts.map +1 -0
- package/dist/common/types/transaction-flow.js +2 -0
- package/dist/common/utils/constant.d.ts +99 -0
- package/dist/common/utils/constant.d.ts.map +1 -0
- package/dist/common/utils/constant.js +370 -0
- package/dist/common/utils/token-pricing.d.ts +33 -0
- package/dist/common/utils/token-pricing.d.ts.map +1 -0
- package/dist/common/utils/token-pricing.js +255 -0
- package/dist/common/utils/transaction-flow.d.ts +35 -0
- package/dist/common/utils/transaction-flow.d.ts.map +1 -0
- package/dist/common/utils/transaction-flow.js +65 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +8 -0
- package/dist/nexus/NexusProvider.d.ts +50 -0
- package/dist/nexus/NexusProvider.d.ts.map +1 -0
- package/dist/nexus/NexusProvider.js +572 -0
- package/dist/nexus-widget/components/address-identicon.d.ts +5 -0
- package/dist/nexus-widget/components/address-identicon.d.ts.map +1 -0
- package/dist/nexus-widget/components/address-identicon.js +39 -0
- package/dist/nexus-widget/components/amount-input-unified.d.ts +18 -0
- package/dist/nexus-widget/components/amount-input-unified.d.ts.map +1 -0
- package/dist/nexus-widget/components/amount-input-unified.js +88 -0
- package/dist/nexus-widget/components/deposit-idle-form.d.ts +29 -0
- package/dist/nexus-widget/components/deposit-idle-form.d.ts.map +1 -0
- package/dist/nexus-widget/components/deposit-idle-form.js +506 -0
- package/dist/nexus-widget/components/nexus-widget-progress-screen.d.ts +36 -0
- package/dist/nexus-widget/components/nexus-widget-progress-screen.d.ts.map +1 -0
- package/dist/nexus-widget/components/nexus-widget-progress-screen.js +737 -0
- package/dist/nexus-widget/components/pay-using-selector.d.ts +9 -0
- package/dist/nexus-widget/components/pay-using-selector.d.ts.map +1 -0
- package/dist/nexus-widget/components/pay-using-selector.js +37 -0
- package/dist/nexus-widget/components/pay-with-sources.d.ts +11 -0
- package/dist/nexus-widget/components/pay-with-sources.d.ts.map +1 -0
- package/dist/nexus-widget/components/pay-with-sources.js +266 -0
- package/dist/nexus-widget/components/receive-asset-selector.d.ts +28 -0
- package/dist/nexus-widget/components/receive-asset-selector.d.ts.map +1 -0
- package/dist/nexus-widget/components/receive-asset-selector.js +1178 -0
- package/dist/nexus-widget/components/recipient-input.d.ts +10 -0
- package/dist/nexus-widget/components/recipient-input.d.ts.map +1 -0
- package/dist/nexus-widget/components/recipient-input.js +44 -0
- package/dist/nexus-widget/components/send-idle-form.d.ts +29 -0
- package/dist/nexus-widget/components/send-idle-form.d.ts.map +1 -0
- package/dist/nexus-widget/components/send-idle-form.js +548 -0
- package/dist/nexus-widget/components/status-alerts.d.ts +7 -0
- package/dist/nexus-widget/components/status-alerts.d.ts.map +1 -0
- package/dist/nexus-widget/components/status-alerts.js +17 -0
- package/dist/nexus-widget/components/swap-asset-selector.d.ts +74 -0
- package/dist/nexus-widget/components/swap-asset-selector.d.ts.map +1 -0
- package/dist/nexus-widget/components/swap-asset-selector.js +1910 -0
- package/dist/nexus-widget/components/swap-idle-form.d.ts +29 -0
- package/dist/nexus-widget/components/swap-idle-form.d.ts.map +1 -0
- package/dist/nexus-widget/components/swap-idle-form.js +1289 -0
- package/dist/nexus-widget/components/swap-intent-preview.d.ts +95 -0
- package/dist/nexus-widget/components/swap-intent-preview.d.ts.map +1 -0
- package/dist/nexus-widget/components/swap-intent-preview.js +1050 -0
- package/dist/nexus-widget/nexus-widget.d.ts +4 -0
- package/dist/nexus-widget/nexus-widget.d.ts.map +1 -0
- package/dist/nexus-widget/nexus-widget.js +7819 -0
- package/dist/nexus-widget/sdk-types.d.ts +13 -0
- package/dist/nexus-widget/sdk-types.d.ts.map +1 -0
- package/dist/nexus-widget/sdk-types.js +1 -0
- package/dist/nexus-widget/theme.d.ts +156 -0
- package/dist/nexus-widget/theme.d.ts.map +1 -0
- package/dist/nexus-widget/theme.js +159 -0
- package/dist/nexus-widget/types.d.ts +141 -0
- package/dist/nexus-widget/types.d.ts.map +1 -0
- package/dist/nexus-widget/types.js +1 -0
- package/dist/nexus-widget/utils/citrea-tokens.d.ts +82 -0
- package/dist/nexus-widget/utils/citrea-tokens.d.ts.map +1 -0
- package/dist/nexus-widget/utils/citrea-tokens.js +136 -0
- package/dist/nexus-widget/utils/deposit-source-selection.d.ts +55 -0
- package/dist/nexus-widget/utils/deposit-source-selection.d.ts.map +1 -0
- package/dist/nexus-widget/utils/deposit-source-selection.js +219 -0
- package/dist/swaps/components/stacked-token-icons.d.ts +15 -0
- package/dist/swaps/components/stacked-token-icons.d.ts.map +1 -0
- package/dist/swaps/components/stacked-token-icons.js +38 -0
- package/dist/swaps/components/step-flow.d.ts +32 -0
- package/dist/swaps/components/step-flow.d.ts.map +1 -0
- package/dist/swaps/components/step-flow.js +54 -0
- package/dist/swaps/components/token-icon.d.ts +11 -0
- package/dist/swaps/components/token-icon.d.ts.map +1 -0
- package/dist/swaps/components/token-icon.js +16 -0
- package/dist/swaps/components/transaction-progress.d.ts +43 -0
- package/dist/swaps/components/transaction-progress.d.ts.map +1 -0
- package/dist/swaps/components/transaction-progress.js +94 -0
- package/dist/ui/button.d.ts +11 -0
- package/dist/ui/button.d.ts.map +1 -0
- package/dist/ui/button.js +34 -0
- package/dist/ui/dialog.d.ts +17 -0
- package/dist/ui/dialog.d.ts.map +1 -0
- package/dist/ui/dialog.js +47 -0
- package/package.json +60 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useMemo, useRef, useState } from "react";
|
|
2
|
+
import { getStepKey } from "./types";
|
|
3
|
+
import { computeAllCompleted, mergeStepComplete, mergeStepsList, seedSteps, } from "./steps";
|
|
4
|
+
/**
|
|
5
|
+
* Manages transaction steps with utilities to seed from expected steps,
|
|
6
|
+
* replace the list on "steps list" events, and mark individual steps complete.
|
|
7
|
+
*/
|
|
8
|
+
export function useTransactionSteps(options = {}) {
|
|
9
|
+
const { expected } = options;
|
|
10
|
+
const [steps, setSteps] = useState(() => expected ? seedSteps(expected) : []);
|
|
11
|
+
const lastSignatureRef = useRef("");
|
|
12
|
+
const onStepsList = (list) => {
|
|
13
|
+
const signature = list.map((step) => getStepKey(step)).join("|");
|
|
14
|
+
if (lastSignatureRef.current === signature) {
|
|
15
|
+
setSteps((prev) => mergeStepsList(prev, list));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
lastSignatureRef.current = signature;
|
|
19
|
+
setSteps((prev) => mergeStepsList(prev, list));
|
|
20
|
+
};
|
|
21
|
+
const onStepComplete = (step) => {
|
|
22
|
+
setSteps((prev) => mergeStepComplete(prev, step));
|
|
23
|
+
};
|
|
24
|
+
const seed = (expectedSteps) => {
|
|
25
|
+
setSteps(seedSteps(expectedSteps));
|
|
26
|
+
};
|
|
27
|
+
const reset = () => {
|
|
28
|
+
setSteps(expected ? seedSteps(expected) : []);
|
|
29
|
+
lastSignatureRef.current = "";
|
|
30
|
+
};
|
|
31
|
+
const allCompleted = useMemo(() => computeAllCompleted(steps), [steps]);
|
|
32
|
+
return {
|
|
33
|
+
steps,
|
|
34
|
+
allCompleted,
|
|
35
|
+
onStepsList,
|
|
36
|
+
onStepComplete,
|
|
37
|
+
seed,
|
|
38
|
+
reset,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type BridgeEvent } from "@avail-project/nexus-core";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
export type TransactionFlowType = "bridge" | "transfer";
|
|
4
|
+
export type BridgeStepType = {
|
|
5
|
+
type: string;
|
|
6
|
+
typeID: string;
|
|
7
|
+
completed?: boolean;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
export type SwapStepType = {
|
|
11
|
+
type: string;
|
|
12
|
+
typeID: string;
|
|
13
|
+
completed?: boolean;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
};
|
|
16
|
+
export interface TransactionFlowInputs {
|
|
17
|
+
chain: number;
|
|
18
|
+
token: string;
|
|
19
|
+
amount?: string;
|
|
20
|
+
recipient?: `0x${string}`;
|
|
21
|
+
}
|
|
22
|
+
export interface TransactionFlowPrefill {
|
|
23
|
+
token: string;
|
|
24
|
+
chainId: number;
|
|
25
|
+
amount?: string;
|
|
26
|
+
recipient?: Address;
|
|
27
|
+
}
|
|
28
|
+
export type TransactionFlowEvent = BridgeEvent;
|
|
29
|
+
export type TransactionFlowOnEvent = (event: BridgeEvent) => void;
|
|
30
|
+
export interface TransactionFlowExecuteParams {
|
|
31
|
+
token: string;
|
|
32
|
+
amount: bigint;
|
|
33
|
+
toChainId: number;
|
|
34
|
+
recipient: `0x${string}`;
|
|
35
|
+
sourceChains?: number[];
|
|
36
|
+
onEvent: TransactionFlowOnEvent;
|
|
37
|
+
}
|
|
38
|
+
export type TransactionFlowExecutor = (params: TransactionFlowExecuteParams) => Promise<{
|
|
39
|
+
explorerUrl: string;
|
|
40
|
+
} | null>;
|
|
41
|
+
export type SourceCoverageState = "healthy" | "warning" | "error";
|
|
42
|
+
export interface SourceSelectionValidation {
|
|
43
|
+
coverageState: SourceCoverageState;
|
|
44
|
+
isBelowRequired: boolean;
|
|
45
|
+
missingToProceed: string;
|
|
46
|
+
missingToSafety: string;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=transaction-flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-flow.d.ts","sourceRoot":"","sources":["../../../.build-src/common/types/transaction-flow.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,UAAU,CAAC;AAExD,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB,CAAC;AAEF,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,KAAK,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,MAAM,oBAAoB,GAAG,WAAW,CAAC;AAE/C,MAAM,MAAM,sBAAsB,GAAG,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;AAElE,MAAM,WAAW,4BAA4B;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,KAAK,MAAM,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,OAAO,EAAE,sBAAsB,CAAC;CACjC;AAED,MAAM,MAAM,uBAAuB,GAAG,CACpC,MAAM,EAAE,4BAA4B,KACjC,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAAC;AAE7C,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;AAElE,MAAM,WAAW,yBAAyB;IACxC,aAAa,EAAE,mBAAmB,CAAC;IACnC,eAAe,EAAE,OAAO,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;CACzB"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export declare const SUPPORTED_CHAINS: {
|
|
2
|
+
readonly ETHEREUM: 1;
|
|
3
|
+
readonly BASE: 8453;
|
|
4
|
+
readonly ARBITRUM: 42161;
|
|
5
|
+
readonly OPTIMISM: 10;
|
|
6
|
+
readonly POLYGON: 137;
|
|
7
|
+
readonly AVALANCHE: 43114;
|
|
8
|
+
readonly SCROLL: 534352;
|
|
9
|
+
readonly MEGAETH: 4326;
|
|
10
|
+
readonly KAIA: 8217;
|
|
11
|
+
readonly BNB: 56;
|
|
12
|
+
readonly MONAD: 143;
|
|
13
|
+
readonly HYPEREVM: 999;
|
|
14
|
+
readonly CITREA: 4114;
|
|
15
|
+
readonly SEPOLIA: 11155111;
|
|
16
|
+
readonly BASE_SEPOLIA: 84532;
|
|
17
|
+
readonly ARBITRUM_SEPOLIA: 421614;
|
|
18
|
+
readonly OPTIMISM_SEPOLIA: 11155420;
|
|
19
|
+
readonly POLYGON_AMOY: 80002;
|
|
20
|
+
readonly MONAD_TESTNET: 10143;
|
|
21
|
+
};
|
|
22
|
+
interface ChainMetadata {
|
|
23
|
+
blockExplorerUrls: string[];
|
|
24
|
+
logo: string;
|
|
25
|
+
name: string;
|
|
26
|
+
nativeCurrency: {
|
|
27
|
+
decimals: number;
|
|
28
|
+
name: string;
|
|
29
|
+
symbol: string;
|
|
30
|
+
};
|
|
31
|
+
rpcUrls?: string[];
|
|
32
|
+
}
|
|
33
|
+
export declare const CHAIN_METADATA: Record<number, ChainMetadata>;
|
|
34
|
+
export declare const TOKEN_METADATA: {
|
|
35
|
+
readonly ETH: {
|
|
36
|
+
readonly decimals: 18;
|
|
37
|
+
readonly name: "Ethereum";
|
|
38
|
+
readonly symbol: "ETH";
|
|
39
|
+
readonly logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/eth/logo.png";
|
|
40
|
+
};
|
|
41
|
+
readonly USDC: {
|
|
42
|
+
readonly decimals: 6;
|
|
43
|
+
readonly name: "USD Coin";
|
|
44
|
+
readonly symbol: "USDC";
|
|
45
|
+
readonly logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/usdc/logo.png";
|
|
46
|
+
};
|
|
47
|
+
readonly USDT: {
|
|
48
|
+
readonly decimals: 6;
|
|
49
|
+
readonly name: "Tether USD";
|
|
50
|
+
readonly symbol: "USDT";
|
|
51
|
+
readonly logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/usdt/logo.png";
|
|
52
|
+
};
|
|
53
|
+
readonly DAI: {
|
|
54
|
+
readonly decimals: 18;
|
|
55
|
+
readonly name: "Dai Stablecoin";
|
|
56
|
+
readonly symbol: "DAI";
|
|
57
|
+
readonly logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/dai/logo.png";
|
|
58
|
+
};
|
|
59
|
+
readonly WBTC: {
|
|
60
|
+
readonly decimals: 8;
|
|
61
|
+
readonly name: "Wrapped BTC";
|
|
62
|
+
readonly symbol: "WBTC";
|
|
63
|
+
readonly logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/wbtc/logo.png";
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export declare const SHORT_CHAIN_NAME: Record<number, string>;
|
|
67
|
+
export type SdkChainWithSwapSupport = {
|
|
68
|
+
chain?: {
|
|
69
|
+
id?: number | null;
|
|
70
|
+
} | null;
|
|
71
|
+
id?: number | null;
|
|
72
|
+
swapSupported?: boolean | null;
|
|
73
|
+
};
|
|
74
|
+
export type SdkChainListWithSwapSupport = readonly SdkChainWithSwapSupport[] | null | undefined;
|
|
75
|
+
export declare function getSdkSwapSupportedChainIds(chains: SdkChainListWithSwapSupport): Set<number> | null;
|
|
76
|
+
export declare function isSwapSupportedBySdkChainList(chainId: number | null | undefined, chains: SdkChainListWithSwapSupport): boolean;
|
|
77
|
+
export declare function getShortChainName(chainId?: number, fallbackName?: string): string;
|
|
78
|
+
export declare const TOKEN_IMAGES: Record<string, string>;
|
|
79
|
+
/**
|
|
80
|
+
* Compute an amount string for fraction buttons (25%, 50%, 75%, 100%).
|
|
81
|
+
*
|
|
82
|
+
* @param balanceStr - user's balance as a human decimal string (e.g. "12.345") OR as base-unit integer string if `balanceIsBaseUnits` true
|
|
83
|
+
* @param fraction - fraction e.g. 0.25, 0.5, 0.75, 1
|
|
84
|
+
* @param decimals - token decimals (6 for USDC/USDT, 18 for ETH)
|
|
85
|
+
* @param safetyMargin - 0.01 for 1% default
|
|
86
|
+
* @param balanceIsBaseUnits - if true, balanceStr is already base units integer string (wei / smallest unit)
|
|
87
|
+
* @returns decimal string clipped to token decimals (rounded down)
|
|
88
|
+
*/
|
|
89
|
+
export declare function computeAmountFromFraction(balanceStr: string, fraction: number, decimals: number, safetyMargin?: number, balanceIsBaseUnits?: boolean): string;
|
|
90
|
+
export declare const usdFormatter: Intl.NumberFormat;
|
|
91
|
+
/**
|
|
92
|
+
* Formats USD values for UI.
|
|
93
|
+
* Values between 0 and 0.001 are shown as "< $0.001".
|
|
94
|
+
* Values between 0.001 and 0.01 are shown with 3 decimals.
|
|
95
|
+
*/
|
|
96
|
+
export declare function formatUsdForDisplay(value: number): string;
|
|
97
|
+
export declare const TOKEN_CONTRACT_ADDRESSES: Record<string, Record<number, `0x${string}`>>;
|
|
98
|
+
export {};
|
|
99
|
+
//# sourceMappingURL=constant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../../../.build-src/common/utils/constant.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;CAoBnB,CAAC;AAEX,UAAU,aAAa;IACrB,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE;QACd,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAkHxD,CAAC;AAEF,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BjB,CAAC;AAEX,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAsB1C,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG;IACpC,KAAK,CAAC,EAAE;QAAE,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GAAG,IAAI,CAAC;IACtC,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,aAAa,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,SAAS,uBAAuB,EAAE,GAClC,IAAI,GACJ,SAAS,CAAC;AAYd,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,2BAA2B,GAClC,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAyBpB;AAED,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAClC,MAAM,EAAE,2BAA2B,GAClC,OAAO,CAWT;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,CAAC,EAAE,MAAM,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,MAAM,CAKR;AAED,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAW/C,CAAC;AAOF;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,YAAY,SAAwB,EACpC,kBAAkB,UAAQ,GACzB,MAAM,CAyCR;AAED,eAAO,MAAM,YAAY,mBAKvB,CAAC;AASH;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiBzD;AAED,eAAO,MAAM,wBAAwB,EAwChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,MAAM,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { formatUnits, parseUnits } from "viem";
|
|
2
|
+
export const SUPPORTED_CHAINS = {
|
|
3
|
+
ETHEREUM: 1,
|
|
4
|
+
BASE: 8453,
|
|
5
|
+
ARBITRUM: 42_161,
|
|
6
|
+
OPTIMISM: 10,
|
|
7
|
+
POLYGON: 137,
|
|
8
|
+
AVALANCHE: 43_114,
|
|
9
|
+
SCROLL: 534_352,
|
|
10
|
+
MEGAETH: 4326,
|
|
11
|
+
KAIA: 8217,
|
|
12
|
+
BNB: 56,
|
|
13
|
+
MONAD: 143,
|
|
14
|
+
HYPEREVM: 999,
|
|
15
|
+
CITREA: 4114,
|
|
16
|
+
SEPOLIA: 11_155_111,
|
|
17
|
+
BASE_SEPOLIA: 84_532,
|
|
18
|
+
ARBITRUM_SEPOLIA: 421_614,
|
|
19
|
+
OPTIMISM_SEPOLIA: 11_155_420,
|
|
20
|
+
POLYGON_AMOY: 80_002,
|
|
21
|
+
MONAD_TESTNET: 10_143,
|
|
22
|
+
};
|
|
23
|
+
export const CHAIN_METADATA = {
|
|
24
|
+
[SUPPORTED_CHAINS.ETHEREUM]: {
|
|
25
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/ethereum/logo.png",
|
|
26
|
+
name: "Ethereum",
|
|
27
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
28
|
+
blockExplorerUrls: ["https://etherscan.io"],
|
|
29
|
+
},
|
|
30
|
+
[SUPPORTED_CHAINS.BASE]: {
|
|
31
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/base/logo.png",
|
|
32
|
+
name: "Base",
|
|
33
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
34
|
+
blockExplorerUrls: ["https://basescan.org"],
|
|
35
|
+
},
|
|
36
|
+
[SUPPORTED_CHAINS.ARBITRUM]: {
|
|
37
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/arbitrum/logo.png",
|
|
38
|
+
name: "Arbitrum",
|
|
39
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
40
|
+
blockExplorerUrls: ["https://arbiscan.io"],
|
|
41
|
+
},
|
|
42
|
+
[SUPPORTED_CHAINS.OPTIMISM]: {
|
|
43
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/optimism/logo.png",
|
|
44
|
+
name: "Optimism",
|
|
45
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
46
|
+
blockExplorerUrls: ["https://optimistic.etherscan.io"],
|
|
47
|
+
},
|
|
48
|
+
[SUPPORTED_CHAINS.POLYGON]: {
|
|
49
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/polygon/logo.png",
|
|
50
|
+
name: "Polygon",
|
|
51
|
+
nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
|
|
52
|
+
blockExplorerUrls: ["https://polygonscan.com"],
|
|
53
|
+
},
|
|
54
|
+
[SUPPORTED_CHAINS.AVALANCHE]: {
|
|
55
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/avalanche/logo.png",
|
|
56
|
+
name: "Avalanche",
|
|
57
|
+
nativeCurrency: { name: "AVAX", symbol: "AVAX", decimals: 18 },
|
|
58
|
+
blockExplorerUrls: ["https://snowtrace.io"],
|
|
59
|
+
},
|
|
60
|
+
[SUPPORTED_CHAINS.SCROLL]: {
|
|
61
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/scroll/logo.png",
|
|
62
|
+
name: "Scroll",
|
|
63
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
64
|
+
blockExplorerUrls: ["https://scrollscan.com"],
|
|
65
|
+
},
|
|
66
|
+
[SUPPORTED_CHAINS.MEGAETH]: {
|
|
67
|
+
logo: "https://files.availproject.org/fastbridge/megaeth/megaeth-favicon.svg",
|
|
68
|
+
name: "MegaETH",
|
|
69
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
70
|
+
blockExplorerUrls: ["https://mega.etherscan.io"],
|
|
71
|
+
rpcUrls: ["https://rpcs.avail.so/megaeth"],
|
|
72
|
+
},
|
|
73
|
+
[SUPPORTED_CHAINS.KAIA]: {
|
|
74
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/kaia/logo.png",
|
|
75
|
+
name: "Kaia",
|
|
76
|
+
nativeCurrency: { name: "KAIA", symbol: "KAIA", decimals: 18 },
|
|
77
|
+
blockExplorerUrls: ["https://kaiascan.io"],
|
|
78
|
+
rpcUrls: ["https://rpcs.avail.so/kaia"],
|
|
79
|
+
},
|
|
80
|
+
[SUPPORTED_CHAINS.MONAD]: {
|
|
81
|
+
logo: "https://files.availproject.org/fastbridge/monad/monad-favicon.svg",
|
|
82
|
+
name: "Monad",
|
|
83
|
+
nativeCurrency: { name: "Monad", symbol: "MON", decimals: 18 },
|
|
84
|
+
blockExplorerUrls: ["https://monadscan.com"],
|
|
85
|
+
rpcUrls: ["https://rpcs.avail.so/monad"],
|
|
86
|
+
},
|
|
87
|
+
[SUPPORTED_CHAINS.HYPEREVM]: {
|
|
88
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/hyperevm/logo.png",
|
|
89
|
+
name: "HyperEVM",
|
|
90
|
+
nativeCurrency: { name: "Hype", symbol: "HYPE", decimals: 18 },
|
|
91
|
+
blockExplorerUrls: ["https://hyperevmscan.io"],
|
|
92
|
+
rpcUrls: ["https://rpcs.avail.so/hyperevm"],
|
|
93
|
+
},
|
|
94
|
+
[SUPPORTED_CHAINS.BNB]: {
|
|
95
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/bnb/logo.png",
|
|
96
|
+
name: "BNB Smart Chain",
|
|
97
|
+
nativeCurrency: { name: "BNB", symbol: "BNB", decimals: 18 },
|
|
98
|
+
blockExplorerUrls: ["https://bscscan.com"],
|
|
99
|
+
},
|
|
100
|
+
[SUPPORTED_CHAINS.CITREA]: {
|
|
101
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/citrea/logo.png",
|
|
102
|
+
name: "Citrea",
|
|
103
|
+
nativeCurrency: { name: "cBTC", symbol: "cBTC", decimals: 18 },
|
|
104
|
+
blockExplorerUrls: ["https://explorer.citrea.xyz"],
|
|
105
|
+
},
|
|
106
|
+
// Testnets
|
|
107
|
+
[SUPPORTED_CHAINS.SEPOLIA]: {
|
|
108
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/ethereum/logo.png",
|
|
109
|
+
name: "Sepolia",
|
|
110
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
111
|
+
blockExplorerUrls: ["https://sepolia.etherscan.io"],
|
|
112
|
+
},
|
|
113
|
+
[SUPPORTED_CHAINS.BASE_SEPOLIA]: {
|
|
114
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/base/logo.png",
|
|
115
|
+
name: "Base Sepolia",
|
|
116
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
117
|
+
blockExplorerUrls: ["https://sepolia.basescan.org"],
|
|
118
|
+
},
|
|
119
|
+
[SUPPORTED_CHAINS.ARBITRUM_SEPOLIA]: {
|
|
120
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/arbitrum/logo.png",
|
|
121
|
+
name: "Arbitrum Sepolia",
|
|
122
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
123
|
+
blockExplorerUrls: ["https://sepolia.arbiscan.io"],
|
|
124
|
+
},
|
|
125
|
+
[SUPPORTED_CHAINS.OPTIMISM_SEPOLIA]: {
|
|
126
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/optimism/logo.png",
|
|
127
|
+
name: "Optimism Sepolia",
|
|
128
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
129
|
+
blockExplorerUrls: ["https://sepolia-optimism.etherscan.io"],
|
|
130
|
+
},
|
|
131
|
+
[SUPPORTED_CHAINS.POLYGON_AMOY]: {
|
|
132
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/chains/polygon/logo.png",
|
|
133
|
+
name: "Polygon Amoy",
|
|
134
|
+
nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
|
|
135
|
+
blockExplorerUrls: ["https://amoy.polygonscan.com"],
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
export const TOKEN_METADATA = {
|
|
139
|
+
ETH: {
|
|
140
|
+
decimals: 18,
|
|
141
|
+
name: "Ethereum",
|
|
142
|
+
symbol: "ETH",
|
|
143
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/eth/logo.png",
|
|
144
|
+
},
|
|
145
|
+
USDC: {
|
|
146
|
+
decimals: 6,
|
|
147
|
+
name: "USD Coin",
|
|
148
|
+
symbol: "USDC",
|
|
149
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/usdc/logo.png",
|
|
150
|
+
},
|
|
151
|
+
USDT: {
|
|
152
|
+
decimals: 6,
|
|
153
|
+
name: "Tether USD",
|
|
154
|
+
symbol: "USDT",
|
|
155
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/usdt/logo.png",
|
|
156
|
+
},
|
|
157
|
+
DAI: {
|
|
158
|
+
decimals: 18,
|
|
159
|
+
name: "Dai Stablecoin",
|
|
160
|
+
symbol: "DAI",
|
|
161
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/dai/logo.png",
|
|
162
|
+
},
|
|
163
|
+
WBTC: {
|
|
164
|
+
decimals: 8,
|
|
165
|
+
name: "Wrapped BTC",
|
|
166
|
+
symbol: "WBTC",
|
|
167
|
+
logo: "https://raw.githubusercontent.com/availproject/nexus-assets/main/tokens/wbtc/logo.png",
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
export const SHORT_CHAIN_NAME = {
|
|
171
|
+
[SUPPORTED_CHAINS.ETHEREUM]: "Ethereum",
|
|
172
|
+
[SUPPORTED_CHAINS.BASE]: "Base",
|
|
173
|
+
[SUPPORTED_CHAINS.ARBITRUM]: "Arbitrum",
|
|
174
|
+
[SUPPORTED_CHAINS.OPTIMISM]: "Optimism",
|
|
175
|
+
[SUPPORTED_CHAINS.POLYGON]: "Polygon",
|
|
176
|
+
[SUPPORTED_CHAINS.AVALANCHE]: "Avalanche",
|
|
177
|
+
[SUPPORTED_CHAINS.SCROLL]: "Scroll",
|
|
178
|
+
[SUPPORTED_CHAINS.MEGAETH]: "MegaETH",
|
|
179
|
+
[SUPPORTED_CHAINS.KAIA]: "Kaia",
|
|
180
|
+
[SUPPORTED_CHAINS.BNB]: "BNB",
|
|
181
|
+
[SUPPORTED_CHAINS.MONAD]: "Monad",
|
|
182
|
+
[SUPPORTED_CHAINS.HYPEREVM]: "HyperEVM",
|
|
183
|
+
[SUPPORTED_CHAINS.CITREA]: "Citrea",
|
|
184
|
+
// [SUPPORTED_CHAINS.TRON]: "Tron",
|
|
185
|
+
[SUPPORTED_CHAINS.SEPOLIA]: "Sepolia",
|
|
186
|
+
[SUPPORTED_CHAINS.BASE_SEPOLIA]: "Base Sepolia",
|
|
187
|
+
[SUPPORTED_CHAINS.ARBITRUM_SEPOLIA]: "Arbitrum Sepolia",
|
|
188
|
+
[SUPPORTED_CHAINS.OPTIMISM_SEPOLIA]: "Optimism Sepolia",
|
|
189
|
+
[SUPPORTED_CHAINS.POLYGON_AMOY]: "Polygon Amoy",
|
|
190
|
+
[SUPPORTED_CHAINS.MONAD_TESTNET]: "Monad Testnet",
|
|
191
|
+
// [SUPPORTED_CHAINS.TRON_SHASTA]: "Tron Shasta",
|
|
192
|
+
};
|
|
193
|
+
const getSdkChainId = (chain) => {
|
|
194
|
+
const rawId = chain.id ?? chain.chain?.id;
|
|
195
|
+
if (rawId == null) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
const chainId = Number(rawId);
|
|
199
|
+
return Number.isInteger(chainId) && chainId > 0 ? chainId : undefined;
|
|
200
|
+
};
|
|
201
|
+
export function getSdkSwapSupportedChainIds(chains) {
|
|
202
|
+
if (!chains?.length) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
const hasExplicitSwapSupport = chains.some((chain) => typeof chain.swapSupported === "boolean");
|
|
206
|
+
if (!hasExplicitSwapSupport) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
const supportedIds = new Set();
|
|
210
|
+
for (const chain of chains) {
|
|
211
|
+
if (chain.swapSupported !== true) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const chainId = getSdkChainId(chain);
|
|
215
|
+
if (chainId) {
|
|
216
|
+
supportedIds.add(chainId);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return supportedIds;
|
|
220
|
+
}
|
|
221
|
+
export function isSwapSupportedBySdkChainList(chainId, chains) {
|
|
222
|
+
if (!chainId) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const sdkSupportedIds = getSdkSwapSupportedChainIds(chains);
|
|
226
|
+
if (sdkSupportedIds) {
|
|
227
|
+
return sdkSupportedIds.has(Number(chainId));
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
export function getShortChainName(chainId, fallbackName) {
|
|
232
|
+
if (!chainId) {
|
|
233
|
+
return fallbackName ?? "";
|
|
234
|
+
}
|
|
235
|
+
return SHORT_CHAIN_NAME[chainId] ?? fallbackName ?? String(chainId);
|
|
236
|
+
}
|
|
237
|
+
export const TOKEN_IMAGES = {
|
|
238
|
+
BNB: "https://assets.coingecko.com/coins/images/825/large/bnb-icon2_2x.png",
|
|
239
|
+
KAIA: "https://assets.coingecko.com/asset_platforms/images/9672/large/kaia.png",
|
|
240
|
+
SOPH: "https://assets.coingecko.com/coins/images/38680/large/sophon_logo_200.png",
|
|
241
|
+
USDC: "https://coin-images.coingecko.com/coins/images/6319/large/usdc.png",
|
|
242
|
+
USDM: "https://raw.githubusercontent.com/availproject/nexus-assets/refs/heads/main/tokens/usdm/logo.png",
|
|
243
|
+
USDS: "https://assets.coingecko.com/coins/images/39926/standard/usds.webp?1726666683",
|
|
244
|
+
USDT: "https://coin-images.coingecko.com/coins/images/35023/large/USDT.png",
|
|
245
|
+
"USD₮0": "https://coin-images.coingecko.com/coins/images/35023/large/USDT.png",
|
|
246
|
+
WETH: "https://assets.coingecko.com/coins/images/279/large/ethereum.png?1595348880",
|
|
247
|
+
};
|
|
248
|
+
const DEFAULT_SAFETY_MARGIN = 0.01; // 1%
|
|
249
|
+
const TRAILING_DECIMAL_ZERO_REGEX = /(\.\d*?[1-9])0+$/u;
|
|
250
|
+
const ZERO_DECIMAL_REGEX = /\.0+$/u;
|
|
251
|
+
const LONE_DECIMAL_POINT_REGEX = /^\.$/u;
|
|
252
|
+
/**
|
|
253
|
+
* Compute an amount string for fraction buttons (25%, 50%, 75%, 100%).
|
|
254
|
+
*
|
|
255
|
+
* @param balanceStr - user's balance as a human decimal string (e.g. "12.345") OR as base-unit integer string if `balanceIsBaseUnits` true
|
|
256
|
+
* @param fraction - fraction e.g. 0.25, 0.5, 0.75, 1
|
|
257
|
+
* @param decimals - token decimals (6 for USDC/USDT, 18 for ETH)
|
|
258
|
+
* @param safetyMargin - 0.01 for 1% default
|
|
259
|
+
* @param balanceIsBaseUnits - if true, balanceStr is already base units integer string (wei / smallest unit)
|
|
260
|
+
* @returns decimal string clipped to token decimals (rounded down)
|
|
261
|
+
*/
|
|
262
|
+
export function computeAmountFromFraction(balanceStr, fraction, decimals, safetyMargin = DEFAULT_SAFETY_MARGIN, balanceIsBaseUnits = false) {
|
|
263
|
+
if (!balanceStr) {
|
|
264
|
+
return "0";
|
|
265
|
+
}
|
|
266
|
+
// parse balance into base units (BigInt)
|
|
267
|
+
const balanceUnits = balanceIsBaseUnits
|
|
268
|
+
? BigInt(balanceStr)
|
|
269
|
+
: parseUnits(balanceStr, decimals);
|
|
270
|
+
if (balanceUnits === BigInt(0)) {
|
|
271
|
+
return "0";
|
|
272
|
+
}
|
|
273
|
+
// Use an integer precision multiplier to avoid FP issues
|
|
274
|
+
const PREC = 1_000_000; // 1e6 precision for fraction & safety margin
|
|
275
|
+
const safetyMul = BigInt(Math.max(0, Math.floor((1 - safetyMargin) * PREC))); // (1 - safetyMargin) * PREC
|
|
276
|
+
const fractionMul = BigInt(Math.max(0, Math.floor(fraction * PREC))); // fraction * PREC
|
|
277
|
+
// Apply safety margin: floor(balance * (1 - safetyMargin))
|
|
278
|
+
const maxAfterSafety = (balanceUnits * safetyMul) / BigInt(PREC);
|
|
279
|
+
// Apply fraction and floor: floor(maxAfterSafety * fraction)
|
|
280
|
+
let desiredUnits = (maxAfterSafety * fractionMul) / BigInt(PREC);
|
|
281
|
+
// Extra clamp just in case
|
|
282
|
+
if (desiredUnits > balanceUnits) {
|
|
283
|
+
desiredUnits = balanceUnits;
|
|
284
|
+
}
|
|
285
|
+
if (desiredUnits < BigInt(0)) {
|
|
286
|
+
desiredUnits = BigInt(0);
|
|
287
|
+
}
|
|
288
|
+
// format back to human readable decimal string with token decimals (formatUnits truncates/keeps decimals)
|
|
289
|
+
// formatUnits will produce exactly decimals digits if fractional part exists; we'll strip trailing zeros.
|
|
290
|
+
const raw = formatUnits(desiredUnits, decimals);
|
|
291
|
+
// strip trailing zeros and possible trailing dot
|
|
292
|
+
return raw
|
|
293
|
+
.replace(TRAILING_DECIMAL_ZERO_REGEX, "$1")
|
|
294
|
+
.replace(ZERO_DECIMAL_REGEX, "")
|
|
295
|
+
.replace(LONE_DECIMAL_POINT_REGEX, "0");
|
|
296
|
+
}
|
|
297
|
+
export const usdFormatter = new Intl.NumberFormat("en-US", {
|
|
298
|
+
style: "currency",
|
|
299
|
+
currency: "USD",
|
|
300
|
+
minimumFractionDigits: 2,
|
|
301
|
+
maximumFractionDigits: 2,
|
|
302
|
+
});
|
|
303
|
+
const usdFormatterPrecise = new Intl.NumberFormat("en-US", {
|
|
304
|
+
style: "currency",
|
|
305
|
+
currency: "USD",
|
|
306
|
+
minimumFractionDigits: 3,
|
|
307
|
+
maximumFractionDigits: 3,
|
|
308
|
+
});
|
|
309
|
+
/**
|
|
310
|
+
* Formats USD values for UI.
|
|
311
|
+
* Values between 0 and 0.001 are shown as "< $0.001".
|
|
312
|
+
* Values between 0.001 and 0.01 are shown with 3 decimals.
|
|
313
|
+
*/
|
|
314
|
+
export function formatUsdForDisplay(value) {
|
|
315
|
+
if (!Number.isFinite(value)) {
|
|
316
|
+
return usdFormatter.format(0);
|
|
317
|
+
}
|
|
318
|
+
const absValue = Math.abs(value);
|
|
319
|
+
if (absValue === 0) {
|
|
320
|
+
return usdFormatter.format(0);
|
|
321
|
+
}
|
|
322
|
+
if (absValue < 0.001) {
|
|
323
|
+
return "< $0.001";
|
|
324
|
+
}
|
|
325
|
+
if (absValue < 0.01) {
|
|
326
|
+
return usdFormatterPrecise.format(value);
|
|
327
|
+
}
|
|
328
|
+
return usdFormatter.format(value);
|
|
329
|
+
}
|
|
330
|
+
export const TOKEN_CONTRACT_ADDRESSES = {
|
|
331
|
+
USDC: {
|
|
332
|
+
1: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
|
333
|
+
8453: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
334
|
+
137: "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
|
|
335
|
+
42161: "0xaf88d065e77c8cc2239327c5edb3a432268e5831",
|
|
336
|
+
10: "0x0b2c639c533813f4aa9d7837caf62653d097ff85",
|
|
337
|
+
534352: "0x06efdbff2a14a7c8e15944d1f4a48f9f95f663a4",
|
|
338
|
+
43114: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
339
|
+
56: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
|
|
340
|
+
999: "0xb88339CB7199b77E23DB6E890353E22632Ba630f",
|
|
341
|
+
143: "0x754704Bc059F8C67012fEd69BC8A327a5aafb603",
|
|
342
|
+
4114: "0xE045e6c36cF77FAA2CfB54466D71A3aEF7bbE839",
|
|
343
|
+
11155111: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238",
|
|
344
|
+
84532: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
345
|
+
421614: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
|
|
346
|
+
11155420: "0x5fd84259d66Cd46123540766Be93DFE6D43130D7",
|
|
347
|
+
80002: "0x41E94Eb019C0762f9Bfcf9Fb1E58725BfB0e7582",
|
|
348
|
+
10143: "0xf817257fed379853cDe0fa4F97AB987181B1E5Ea",
|
|
349
|
+
5115: "0xb669dC8cC6D044307Ba45366C0c836eC3c7e31AA",
|
|
350
|
+
},
|
|
351
|
+
USDT: {
|
|
352
|
+
1: "0xdac17f958d2ee523a2206206994597c13d831ec7",
|
|
353
|
+
137: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
|
|
354
|
+
42161: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
|
|
355
|
+
8217: "0xd077a400968890eacc75cdc901f0356c943e4fdb",
|
|
356
|
+
10: "0x94b008aa00579c1307b0ef2c499ad98a8ce58e58",
|
|
357
|
+
534352: "0xf55bec9cafdbe8730f096aa55dad6d22d44099df",
|
|
358
|
+
43114: "0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7",
|
|
359
|
+
56: "0x55d398326f99059fF775485246999027B3197955",
|
|
360
|
+
999: "0xB8CE59FC3717ada4C02eaDF9682A9e934F625ebb",
|
|
361
|
+
4114: "0x9f3096Bac87e7F03DC09b0B416eB0DF837304dc4",
|
|
362
|
+
4326: "0xB8CE59FC3717ada4C02eaDF9682A9e934F625ebb",
|
|
363
|
+
421614: "0xF954d4A5859b37De88a91bdbb8Ad309056FB04B1",
|
|
364
|
+
11155420: "0x6462693c2F21AC0E517f12641D404895030F7426",
|
|
365
|
+
10143: "0x1c56F176D6735888fbB6f8bD9ADAd8Ad7a023a0b",
|
|
366
|
+
},
|
|
367
|
+
USDM: {
|
|
368
|
+
4326: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
369
|
+
},
|
|
370
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SupportedChainsAndTokensResult } from "@avail-project/nexus-core";
|
|
2
|
+
export declare const DEFAULT_COINBASE_PRICE_REQUEST_TIMEOUT_MS = 4000;
|
|
3
|
+
export declare const USD_PEGGED_FALLBACK_RATE = 1;
|
|
4
|
+
export declare const DEFAULT_USD_PEGGED_TOKEN_SYMBOLS: readonly ["USDT", "USDC", "USDC.E", "USDT.E", "USDS", "DAI", "CTUSD", "JUSD", "USDM", "FDUSD", "BUSD", "TUSD", "PYUSD", "GUSD", "SVJUSD", "LUSD", "USDE", "USDP"];
|
|
5
|
+
/**
|
|
6
|
+
* Wrapped / derivative token → base token pegging map.
|
|
7
|
+
* When Coinbase has no direct price for a token, we resolve the price
|
|
8
|
+
* of the base symbol and treat it as 1:1.
|
|
9
|
+
*
|
|
10
|
+
* Keys and values are UPPERCASE (normalised).
|
|
11
|
+
*/
|
|
12
|
+
export declare const TOKEN_PRICE_PEGS: Readonly<Record<string, string>>;
|
|
13
|
+
/**
|
|
14
|
+
* Custom error thrown when a token's USD price cannot be determined
|
|
15
|
+
* through any resolution path (SDK rates, Coinbase API, or pegging fallback).
|
|
16
|
+
*/
|
|
17
|
+
export declare class TokenPricingError extends Error {
|
|
18
|
+
readonly tokenSymbol: string;
|
|
19
|
+
constructor(tokenSymbol: string);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the base symbol for a given token via the pegging map.
|
|
23
|
+
* Returns `null` if no peg is defined.
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveBaseSymbol(tokenSymbol: string): string | null;
|
|
26
|
+
export declare function normalizeTokenSymbol(tokenSymbol: string): string;
|
|
27
|
+
export declare function getUsdRatePegSymbol(tokenSymbol: string): string | null;
|
|
28
|
+
export declare function toFinitePositiveNumber(value: unknown): number | null;
|
|
29
|
+
export declare function getCoinbaseSymbolCandidates(tokenSymbol: string): string[];
|
|
30
|
+
export declare function buildUsdPeggedSymbolSet(supportedChains: SupportedChainsAndTokensResult | null, baseSymbols?: Iterable<string>): Set<string>;
|
|
31
|
+
export declare function fetchCoinbaseUsdRate(tokenSymbol: string, requestTimeoutMs?: number): Promise<number | null>;
|
|
32
|
+
export declare function fetchCoinGeckoUsdRate(tokenSymbol: string, requestTimeoutMs?: number): Promise<number | null>;
|
|
33
|
+
//# sourceMappingURL=token-pricing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-pricing.d.ts","sourceRoot":"","sources":["../../../.build-src/common/utils/token-pricing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,2BAA2B,CAAC;AAShF,eAAO,MAAM,yCAAyC,OAAQ,CAAC;AAC/D,eAAO,MAAM,wBAAwB,IAAI,CAAC;AAC1C,eAAO,MAAM,gCAAgC,mKAmBnC,CAAC;AAEX;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CA6CpD,CAAC;AAEX;;;GAGG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,SAAgB,WAAW,EAAE,MAAM,CAAC;gBAExB,WAAW,EAAE,MAAM;CAKhC;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGpE;AAuCD,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAEhE;AAeD,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKtE;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAMpE;AAED,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,CAkBzE;AA8CD,wBAAgB,uBAAuB,CACrC,eAAe,EAAE,8BAA8B,GAAG,IAAI,EACtD,WAAW,GAAE,QAAQ,CAAC,MAAM,CAAoC,GAC/D,GAAG,CAAC,MAAM,CAAC,CAqBb;AAqBD,wBAAsB,oBAAoB,CACxC,WAAW,EAAE,MAAM,EACnB,gBAAgB,SAA4C,GAC3D,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAwBxB;AAuBD,wBAAsB,qBAAqB,CACzC,WAAW,EAAE,MAAM,EACnB,gBAAgB,SAA4C,GAC3D,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA+BxB"}
|