@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,62 @@
|
|
|
1
|
+
import { type NexusClient, type OnAllowanceHookData, type OnIntentHookData } from "@avail-project/nexus-core";
|
|
2
|
+
import { type Dispatch, type RefObject, type SetStateAction } from "react";
|
|
3
|
+
import { type TransactionStatus } from "../tx/types";
|
|
4
|
+
import { type SourceSelectionValidation, type TransactionFlowExecutor, type TransactionFlowInputs, type BridgeStepType } from "../types/transaction-flow";
|
|
5
|
+
interface NexusErrorInfo {
|
|
6
|
+
code: string;
|
|
7
|
+
message: string;
|
|
8
|
+
context?: unknown;
|
|
9
|
+
details?: unknown;
|
|
10
|
+
}
|
|
11
|
+
type NexusErrorHandler = (error: unknown) => NexusErrorInfo;
|
|
12
|
+
interface UseTransactionExecutionProps {
|
|
13
|
+
operationName: "bridge" | "transfer";
|
|
14
|
+
nexusSDK: NexusClient | null;
|
|
15
|
+
intent: RefObject<OnIntentHookData | null>;
|
|
16
|
+
allowance: RefObject<OnAllowanceHookData | null>;
|
|
17
|
+
inputs: TransactionFlowInputs;
|
|
18
|
+
configuredMaxAmount?: string;
|
|
19
|
+
allAvailableSourceChainIds: number[];
|
|
20
|
+
sourceChainsForSdk?: number[];
|
|
21
|
+
sourceSelectionKey: string;
|
|
22
|
+
sourceSelection: SourceSelectionValidation;
|
|
23
|
+
loading: boolean;
|
|
24
|
+
txError: string | null;
|
|
25
|
+
areInputsValid: boolean;
|
|
26
|
+
executeTransaction: TransactionFlowExecutor;
|
|
27
|
+
getMaxForCurrentSelection: () => Promise<string | undefined>;
|
|
28
|
+
onStepsList: (steps: BridgeStepType[]) => void;
|
|
29
|
+
onStepComplete: (step: BridgeStepType) => void;
|
|
30
|
+
resetSteps: () => void;
|
|
31
|
+
setStatus: (status: TransactionStatus) => void;
|
|
32
|
+
resetInputs: () => void;
|
|
33
|
+
setRefreshing: Dispatch<SetStateAction<boolean>>;
|
|
34
|
+
setIsDialogOpen: Dispatch<SetStateAction<boolean>>;
|
|
35
|
+
setTxError: Dispatch<SetStateAction<string | null>>;
|
|
36
|
+
setLastExplorerUrl: Dispatch<SetStateAction<string>>;
|
|
37
|
+
setSelectedSourceChains: Dispatch<SetStateAction<number[] | null>>;
|
|
38
|
+
setAppliedSourceSelectionKey: Dispatch<SetStateAction<string>>;
|
|
39
|
+
stopwatch: {
|
|
40
|
+
start: () => void;
|
|
41
|
+
stop: () => void;
|
|
42
|
+
reset: () => void;
|
|
43
|
+
};
|
|
44
|
+
handleNexusError: NexusErrorHandler;
|
|
45
|
+
onStart?: () => void;
|
|
46
|
+
onComplete?: (explorerUrl?: string) => void;
|
|
47
|
+
onError?: (message: string) => void;
|
|
48
|
+
fetchBalance: () => Promise<void>;
|
|
49
|
+
notifyHistoryRefresh?: () => void;
|
|
50
|
+
}
|
|
51
|
+
export declare function useTransactionExecution({ operationName, nexusSDK, intent, allowance, inputs, configuredMaxAmount, allAvailableSourceChainIds, sourceChainsForSdk, sourceSelectionKey, sourceSelection, loading, txError, areInputsValid, executeTransaction, getMaxForCurrentSelection, onStepsList, onStepComplete, resetSteps, setStatus, resetInputs, setRefreshing, setIsDialogOpen, setTxError, setLastExplorerUrl, setSelectedSourceChains, setAppliedSourceSelectionKey, stopwatch, handleNexusError, onStart, onComplete, onError, fetchBalance, notifyHistoryRefresh, }: UseTransactionExecutionProps): {
|
|
52
|
+
refreshIntent: (options?: {
|
|
53
|
+
reportError?: boolean;
|
|
54
|
+
}) => Promise<boolean>;
|
|
55
|
+
handleTransaction: () => Promise<void>;
|
|
56
|
+
startTransaction: () => void;
|
|
57
|
+
commitAmount: () => Promise<void>;
|
|
58
|
+
reset: () => void;
|
|
59
|
+
invalidatePendingExecution: () => void;
|
|
60
|
+
};
|
|
61
|
+
export {};
|
|
62
|
+
//# sourceMappingURL=useTransactionExecution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransactionExecution.d.ts","sourceRoot":"","sources":["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AAED,wBAAgB,uBAAuB,CAAC,EACtC,aAAa,EACb,QAAQ,EACR,MAAM,EACN,SAAS,EACT,MAAM,EACN,mBAAmB,EACnB,0BAA0B,EAC1B,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,EACf,OAAO,EACP,OAAO,EACP,cAAc,EACd,kBAAkB,EAClB,yBAAyB,EACzB,WAAW,EACX,cAAc,EACd,UAAU,EACV,SAAS,EACT,WAAW,EACX,aAAa,EACb,eAAe,EACf,UAAU,EACV,kBAAkB,EAClB,uBAAuB,EACvB,4BAA4B,EAC5B,SAAS,EACT,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,OAAO,EACP,YAAY,EACZ,oBAAoB,GACrB,EAAE,4BAA4B;8BAIU;QAAE,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE;;;;;;EA8RjE"}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import {} from "@avail-project/nexus-core";
|
|
2
|
+
import { useCallback, useRef, } from "react";
|
|
3
|
+
import {} from "../tx/types";
|
|
4
|
+
import {} from "../types/transaction-flow";
|
|
5
|
+
export function useTransactionExecution({ operationName, nexusSDK, intent, allowance, inputs, configuredMaxAmount, allAvailableSourceChainIds, sourceChainsForSdk, sourceSelectionKey, sourceSelection, loading, txError, areInputsValid, executeTransaction, getMaxForCurrentSelection, onStepsList, onStepComplete, resetSteps, setStatus, resetInputs, setRefreshing, setIsDialogOpen, setTxError, setLastExplorerUrl, setSelectedSourceChains, setAppliedSourceSelectionKey, stopwatch, handleNexusError, onStart, onComplete, onError, fetchBalance, notifyHistoryRefresh, }) {
|
|
6
|
+
const commitLockRef = useRef(false);
|
|
7
|
+
const runIdRef = useRef(0);
|
|
8
|
+
const refreshIntent = async (options) => {
|
|
9
|
+
if (!intent.current)
|
|
10
|
+
return false;
|
|
11
|
+
const activeRunId = runIdRef.current;
|
|
12
|
+
setRefreshing(true);
|
|
13
|
+
try {
|
|
14
|
+
const updated = await intent.current.refresh(sourceChainsForSdk);
|
|
15
|
+
if (activeRunId !== runIdRef.current)
|
|
16
|
+
return false;
|
|
17
|
+
if (updated) {
|
|
18
|
+
intent.current.intent = updated;
|
|
19
|
+
}
|
|
20
|
+
setAppliedSourceSelectionKey(sourceSelectionKey);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (activeRunId !== runIdRef.current)
|
|
25
|
+
return false;
|
|
26
|
+
console.error("Transaction failed:", error);
|
|
27
|
+
if (options?.reportError) {
|
|
28
|
+
const message = "Unable to refresh source selection. Please try again.";
|
|
29
|
+
setTxError(message);
|
|
30
|
+
onError?.(message);
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
if (activeRunId === runIdRef.current) {
|
|
36
|
+
setRefreshing(false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const onSuccess = async (explorerUrl) => {
|
|
41
|
+
stopwatch.stop();
|
|
42
|
+
setStatus("success");
|
|
43
|
+
onComplete?.(explorerUrl);
|
|
44
|
+
intent.current = null;
|
|
45
|
+
allowance.current = null;
|
|
46
|
+
resetInputs();
|
|
47
|
+
setRefreshing(false);
|
|
48
|
+
setSelectedSourceChains(null);
|
|
49
|
+
setAppliedSourceSelectionKey("ALL");
|
|
50
|
+
await fetchBalance();
|
|
51
|
+
notifyHistoryRefresh?.();
|
|
52
|
+
};
|
|
53
|
+
const handleTransaction = async () => {
|
|
54
|
+
if (commitLockRef.current)
|
|
55
|
+
return;
|
|
56
|
+
commitLockRef.current = true;
|
|
57
|
+
const currentRunId = ++runIdRef.current;
|
|
58
|
+
let didEnterExecutingState = false;
|
|
59
|
+
const cleanupSupersededExecution = () => {
|
|
60
|
+
if (!didEnterExecutingState)
|
|
61
|
+
return;
|
|
62
|
+
setRefreshing(false);
|
|
63
|
+
setIsDialogOpen(false);
|
|
64
|
+
setLastExplorerUrl("");
|
|
65
|
+
stopwatch.stop();
|
|
66
|
+
stopwatch.reset();
|
|
67
|
+
resetSteps();
|
|
68
|
+
setStatus("idle");
|
|
69
|
+
};
|
|
70
|
+
try {
|
|
71
|
+
if (!inputs?.amount ||
|
|
72
|
+
!inputs?.recipient ||
|
|
73
|
+
!inputs?.chain ||
|
|
74
|
+
!inputs?.token) {
|
|
75
|
+
console.error("Missing required inputs");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (!nexusSDK) {
|
|
79
|
+
const message = "Nexus SDK not initialized";
|
|
80
|
+
setTxError(message);
|
|
81
|
+
onError?.(message);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (allAvailableSourceChainIds.length === 0) {
|
|
85
|
+
const message = "No eligible source chains available for the selected token and destination.";
|
|
86
|
+
setTxError(message);
|
|
87
|
+
onError?.(message);
|
|
88
|
+
setStatus("error");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const parsedAmount = Number(inputs.amount);
|
|
92
|
+
if (!Number.isFinite(parsedAmount) || parsedAmount <= 0) {
|
|
93
|
+
const message = "Enter a valid amount greater than 0.";
|
|
94
|
+
setTxError(message);
|
|
95
|
+
onError?.(message);
|
|
96
|
+
setStatus("error");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const amountBigInt = nexusSDK.convertTokenReadableAmountToBigInt(inputs.amount, inputs.token, inputs.chain);
|
|
100
|
+
if (configuredMaxAmount) {
|
|
101
|
+
const configuredMaxRaw = nexusSDK.convertTokenReadableAmountToBigInt(configuredMaxAmount, inputs.token, inputs.chain);
|
|
102
|
+
if (amountBigInt > configuredMaxRaw) {
|
|
103
|
+
const message = `Amount exceeds maximum limit of ${configuredMaxAmount} ${inputs.token}.`;
|
|
104
|
+
setTxError(message);
|
|
105
|
+
onError?.(message);
|
|
106
|
+
setStatus("error");
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const maxForCurrentSelection = await getMaxForCurrentSelection();
|
|
111
|
+
if (currentRunId !== runIdRef.current)
|
|
112
|
+
return;
|
|
113
|
+
if (!maxForCurrentSelection) {
|
|
114
|
+
const message = `Unable to determine max ${operationName} amount for selected sources. Please try again.`;
|
|
115
|
+
setTxError(message);
|
|
116
|
+
onError?.(message);
|
|
117
|
+
setStatus("error");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const maxForSelectionRaw = nexusSDK.convertTokenReadableAmountToBigInt(maxForCurrentSelection, inputs.token, inputs.chain);
|
|
121
|
+
if (amountBigInt > maxForSelectionRaw) {
|
|
122
|
+
const message = `Selected sources can provide up to ${maxForCurrentSelection} ${inputs.token}. Reduce amount or enable more sources.`;
|
|
123
|
+
setTxError(message);
|
|
124
|
+
onError?.(message);
|
|
125
|
+
setStatus("error");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
setStatus("executing");
|
|
129
|
+
didEnterExecutingState = true;
|
|
130
|
+
setTxError(null);
|
|
131
|
+
onStart?.();
|
|
132
|
+
setLastExplorerUrl("");
|
|
133
|
+
setAppliedSourceSelectionKey(sourceSelectionKey);
|
|
134
|
+
const onEvent = (event) => {
|
|
135
|
+
if (currentRunId !== runIdRef.current)
|
|
136
|
+
return;
|
|
137
|
+
if (event.type === "plan_preview" || event.type === "plan_confirmed") {
|
|
138
|
+
const steps = event.plan.steps.map((step) => ({
|
|
139
|
+
...step,
|
|
140
|
+
type: step.type.toUpperCase(),
|
|
141
|
+
typeID: step.type.toUpperCase(),
|
|
142
|
+
completed: false,
|
|
143
|
+
}));
|
|
144
|
+
onStepsList(steps);
|
|
145
|
+
}
|
|
146
|
+
if (event.type === "plan_progress") {
|
|
147
|
+
if (event.stepType === "request_signing" &&
|
|
148
|
+
event.state === "completed") {
|
|
149
|
+
stopwatch.start();
|
|
150
|
+
}
|
|
151
|
+
const completed = event.state === "completed" ||
|
|
152
|
+
event.state === "confirmed" ||
|
|
153
|
+
event.state === "submitted";
|
|
154
|
+
if (completed) {
|
|
155
|
+
onStepComplete({
|
|
156
|
+
...event.step,
|
|
157
|
+
type: event.stepType.toUpperCase(),
|
|
158
|
+
typeID: event.stepType.toUpperCase(),
|
|
159
|
+
completed: true,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const transactionResult = await executeTransaction({
|
|
165
|
+
token: inputs.token,
|
|
166
|
+
amount: amountBigInt,
|
|
167
|
+
toChainId: inputs.chain,
|
|
168
|
+
recipient: inputs.recipient,
|
|
169
|
+
sourceChains: sourceChainsForSdk,
|
|
170
|
+
onEvent,
|
|
171
|
+
});
|
|
172
|
+
if (currentRunId !== runIdRef.current) {
|
|
173
|
+
cleanupSupersededExecution();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (!transactionResult) {
|
|
177
|
+
throw new Error("Transaction rejected by user");
|
|
178
|
+
}
|
|
179
|
+
setLastExplorerUrl(transactionResult.explorerUrl);
|
|
180
|
+
await onSuccess(transactionResult.explorerUrl);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (currentRunId !== runIdRef.current) {
|
|
184
|
+
cleanupSupersededExecution();
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const { message, code, context, details } = handleNexusError(error);
|
|
188
|
+
console.error(`Fast ${operationName} transaction failed:`, {
|
|
189
|
+
code,
|
|
190
|
+
message,
|
|
191
|
+
context,
|
|
192
|
+
details,
|
|
193
|
+
});
|
|
194
|
+
intent.current?.deny();
|
|
195
|
+
intent.current = null;
|
|
196
|
+
allowance.current = null;
|
|
197
|
+
setTxError(message);
|
|
198
|
+
onError?.(message);
|
|
199
|
+
setIsDialogOpen(false);
|
|
200
|
+
setSelectedSourceChains(null);
|
|
201
|
+
setRefreshing(false);
|
|
202
|
+
stopwatch.stop();
|
|
203
|
+
stopwatch.reset();
|
|
204
|
+
resetSteps();
|
|
205
|
+
void fetchBalance();
|
|
206
|
+
setStatus("error");
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
commitLockRef.current = false;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
const reset = () => {
|
|
213
|
+
runIdRef.current += 1;
|
|
214
|
+
intent.current?.deny();
|
|
215
|
+
intent.current = null;
|
|
216
|
+
allowance.current = null;
|
|
217
|
+
resetInputs();
|
|
218
|
+
setStatus("idle");
|
|
219
|
+
setRefreshing(false);
|
|
220
|
+
setSelectedSourceChains(null);
|
|
221
|
+
setAppliedSourceSelectionKey("ALL");
|
|
222
|
+
setLastExplorerUrl("");
|
|
223
|
+
stopwatch.stop();
|
|
224
|
+
stopwatch.reset();
|
|
225
|
+
resetSteps();
|
|
226
|
+
};
|
|
227
|
+
const startTransaction = () => {
|
|
228
|
+
if (!intent.current)
|
|
229
|
+
return;
|
|
230
|
+
if (allAvailableSourceChainIds.length === 0) {
|
|
231
|
+
const message = "No eligible source chains available for the selected token and destination.";
|
|
232
|
+
setTxError(message);
|
|
233
|
+
onError?.(message);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (sourceSelection.isBelowRequired && inputs?.token) {
|
|
237
|
+
const message = `Selected sources are not enough. Add ${sourceSelection.missingToProceed} ${inputs.token} more to make this transaction.`;
|
|
238
|
+
setTxError(message);
|
|
239
|
+
onError?.(message);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
void (async () => {
|
|
243
|
+
const refreshed = await refreshIntent({ reportError: true });
|
|
244
|
+
if (!refreshed || !intent.current)
|
|
245
|
+
return;
|
|
246
|
+
intent.current.allow();
|
|
247
|
+
setIsDialogOpen(true);
|
|
248
|
+
setTxError(null);
|
|
249
|
+
})();
|
|
250
|
+
};
|
|
251
|
+
const commitAmount = async () => {
|
|
252
|
+
if (intent.current || loading || txError || !areInputsValid)
|
|
253
|
+
return;
|
|
254
|
+
await handleTransaction();
|
|
255
|
+
};
|
|
256
|
+
const invalidatePendingExecution = useCallback(() => {
|
|
257
|
+
runIdRef.current += 1;
|
|
258
|
+
if (intent.current) {
|
|
259
|
+
intent.current.deny();
|
|
260
|
+
intent.current = null;
|
|
261
|
+
}
|
|
262
|
+
setRefreshing(false);
|
|
263
|
+
setAppliedSourceSelectionKey("ALL");
|
|
264
|
+
}, [intent, setAppliedSourceSelectionKey, setRefreshing]);
|
|
265
|
+
return {
|
|
266
|
+
refreshIntent,
|
|
267
|
+
handleTransaction,
|
|
268
|
+
startTransaction,
|
|
269
|
+
commitAmount,
|
|
270
|
+
reset,
|
|
271
|
+
invalidatePendingExecution,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type NexusNetwork, type NexusClient, type OnAllowanceHookData, type OnIntentHookData, type TokenBalance } from "@avail-project/nexus-core";
|
|
2
|
+
import { type RefObject } from "react";
|
|
3
|
+
import { type Address } from "viem";
|
|
4
|
+
import { type TransactionStatus } from "../tx/types";
|
|
5
|
+
import { type SourceCoverageState, type TransactionFlowExecutor, type TransactionFlowInputs, type TransactionFlowPrefill, type TransactionFlowType, type BridgeStepType } from "../types/transaction-flow";
|
|
6
|
+
interface BaseTransactionFlowProps {
|
|
7
|
+
type: TransactionFlowType;
|
|
8
|
+
network: NexusNetwork;
|
|
9
|
+
nexusSDK: NexusClient | null;
|
|
10
|
+
intent: RefObject<OnIntentHookData | null>;
|
|
11
|
+
allowance: RefObject<OnAllowanceHookData | null>;
|
|
12
|
+
bridgableBalance: TokenBalance[] | null;
|
|
13
|
+
prefill?: TransactionFlowPrefill;
|
|
14
|
+
onComplete?: (explorerUrl?: string) => void;
|
|
15
|
+
onStart?: () => void;
|
|
16
|
+
onError?: (message: string) => void;
|
|
17
|
+
fetchBalance: () => Promise<void>;
|
|
18
|
+
maxAmount?: string | number;
|
|
19
|
+
isSourceMenuOpen?: boolean;
|
|
20
|
+
notifyHistoryRefresh?: () => void;
|
|
21
|
+
executeTransaction: TransactionFlowExecutor;
|
|
22
|
+
}
|
|
23
|
+
export interface UseTransactionFlowProps extends BaseTransactionFlowProps {
|
|
24
|
+
connectedAddress?: Address;
|
|
25
|
+
}
|
|
26
|
+
export declare function useTransactionFlow(props: UseTransactionFlowProps): {
|
|
27
|
+
inputs: TransactionFlowInputs;
|
|
28
|
+
setInputs: (next: TransactionFlowInputs | Partial<TransactionFlowInputs>) => void;
|
|
29
|
+
timer: number;
|
|
30
|
+
setIsDialogOpen: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
31
|
+
setTxError: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
32
|
+
loading: boolean;
|
|
33
|
+
refreshing: boolean;
|
|
34
|
+
isDialogOpen: boolean;
|
|
35
|
+
txError: string | null;
|
|
36
|
+
handleTransaction: () => Promise<void>;
|
|
37
|
+
reset: () => void;
|
|
38
|
+
filteredBridgableBalance: {
|
|
39
|
+
breakdown: any;
|
|
40
|
+
balance: string;
|
|
41
|
+
value: string;
|
|
42
|
+
chainBalances: import("@avail-project/nexus-core").ChainBalance[];
|
|
43
|
+
currencyId?: number;
|
|
44
|
+
decimals: number;
|
|
45
|
+
logo: string;
|
|
46
|
+
name: string;
|
|
47
|
+
symbol: string;
|
|
48
|
+
} | undefined;
|
|
49
|
+
startTransaction: () => void;
|
|
50
|
+
commitAmount: () => Promise<void>;
|
|
51
|
+
lastExplorerUrl: string;
|
|
52
|
+
steps: import("..").GenericStep<BridgeStepType>[];
|
|
53
|
+
status: TransactionStatus;
|
|
54
|
+
availableSources: any;
|
|
55
|
+
selectedSourceChains: any;
|
|
56
|
+
toggleSourceChain: (chainId: number) => void;
|
|
57
|
+
isSourceSelectionInsufficient: boolean;
|
|
58
|
+
isSourceSelectionBelowSafetyBuffer: boolean;
|
|
59
|
+
isSourceSelectionReadyForAccept: boolean;
|
|
60
|
+
sourceCoverageState: SourceCoverageState;
|
|
61
|
+
sourceCoveragePercent: number;
|
|
62
|
+
missingToProceed: string;
|
|
63
|
+
missingToSafety: string;
|
|
64
|
+
selectedTotal: string;
|
|
65
|
+
requiredTotal: string;
|
|
66
|
+
requiredSafetyTotal: string;
|
|
67
|
+
maxAvailableAmount: string | undefined;
|
|
68
|
+
isInputsValid: boolean;
|
|
69
|
+
};
|
|
70
|
+
export {};
|
|
71
|
+
//# sourceMappingURL=useTransactionFlow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransactionFlow.d.ts","sourceRoot":"","sources":["../../../.build-src/common/hooks/useTransactionFlow.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAClB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAOL,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,KAAK,OAAO,EAAyB,MAAM,MAAM,CAAC;AAO3D,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,uBAAuB,EAC5B,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAC3B,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACpB,MAAM,2BAA2B,CAAC;AAUnC,UAAU,wBAAwB;IAChC,IAAI,EAAE,mBAAmB,CAAC;IAC1B,OAAO,EAAE,YAAY,CAAC;IACtB,QAAQ,EAAE,WAAW,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,SAAS,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC;IAC3C,SAAS,EAAE,SAAS,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;IACjD,gBAAgB,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC;IACxC,OAAO,CAAC,EAAE,sBAAsB,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MA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