@liberfi.io/ui-trade 0.1.1 → 0.1.2
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 +250 -5
- package/dist/index.d.mts +216 -3
- package/dist/index.d.ts +216 -3
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -7
package/README.md
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
# @liberfi.io/ui-trade
|
|
2
2
|
|
|
3
|
-
Trade hooks for the Liberfi React SDK. This package provides chain-agnostic swap logic
|
|
3
|
+
Trade hooks and swap widget for the Liberfi React SDK. This package provides chain-agnostic swap logic and a ready-to-use swap form component that works across Solana, Ethereum, and BSC.
|
|
4
|
+
|
|
5
|
+
The package is organized in two layers:
|
|
6
|
+
|
|
7
|
+
- **Hooks** (`useSwap`, `useSwapRoutePolling`, `useTxConfirmation`) — pure-logic building blocks with no UI side-effects, designed for IoC.
|
|
8
|
+
- **Swap Widget** (`SwapWidget` / `useSwapScript` / `SwapUI` / `SwapPreviewModal`) — a full swap form with preview-before-confirm flow, following the Script/Widget/UI three-layer architecture, composable at any level.
|
|
4
9
|
|
|
5
10
|
## Design Philosophy
|
|
6
11
|
|
|
7
|
-
- **IoC (Inversion of Control)** — Side-effects (toast, analytics, navigation) are injected via
|
|
12
|
+
- **IoC (Inversion of Control)** — Side-effects (toast, analytics, navigation) are injected via callbacks. Hooks never hardcode any feedback mechanism.
|
|
13
|
+
- **Script/Widget/UI architecture** — The swap form follows the monorepo's three-layer pattern: `useSwapScript` (data + state), `SwapWidget` (orchestration), `SwapUI` (presentation). Consumers can use the widget directly or compose Script + custom UI.
|
|
8
14
|
- **Chain-agnostic** — Works across Solana (Jupiter) and EVM chains (KyberSwap). Chain-specific differences (dex selection, tx format) are handled by the underlying `@liberfi.io/client` and `WalletAdapter` abstractions.
|
|
9
|
-
- **
|
|
10
|
-
- **Layered architecture** — Builds on `@liberfi.io/react` (data layer)
|
|
15
|
+
- **UI via `@liberfi.io/ui`** — Uses the shared UI library (`Button`, `Input`, `Avatar`, `Modal`, etc.) for consistent styling across the monorepo. No direct `@heroui/react` dependency.
|
|
16
|
+
- **Layered architecture** — Builds on `@liberfi.io/react` (data layer), `@liberfi.io/wallet-connector` (signing layer), and `@liberfi.io/ui` (presentation layer) without duplicating their logic.
|
|
11
17
|
|
|
12
18
|
## Installation
|
|
13
19
|
|
|
@@ -20,6 +26,7 @@ Peer dependencies the consumer must provide:
|
|
|
20
26
|
- `react` (>=18)
|
|
21
27
|
- `react-dom` (>=18)
|
|
22
28
|
- `@liberfi.io/react` (provides `DexClientProvider` and `useDexClient`)
|
|
29
|
+
- `@liberfi.io/ui` (provides UI components: `Button`, `Input`, `Modal`, `Avatar`, etc.)
|
|
23
30
|
- `@liberfi.io/wallet-connector` (provides `WalletAdapter` type)
|
|
24
31
|
|
|
25
32
|
## API Reference
|
|
@@ -44,6 +51,27 @@ Orchestrates the full swap flow: **route -> sign -> send**.
|
|
|
44
51
|
| `swap` | `(input: SwapInput) => Promise<SwapResult>` | Executes the swap. Resolves with `SwapResult` on success, throws on failure. |
|
|
45
52
|
| `isSwapping` | `boolean` | Whether a swap is currently in progress. |
|
|
46
53
|
|
|
54
|
+
#### `useSwapRoutePolling(params, options?)`
|
|
55
|
+
|
|
56
|
+
Polls for swap route quotes at a configurable interval. Built on `useSwapRouteQuery` (TanStack Query).
|
|
57
|
+
|
|
58
|
+
**Parameters:**
|
|
59
|
+
|
|
60
|
+
| Name | Type | Description |
|
|
61
|
+
| ------------------ | ------------------------- | ---------------------------------------------------------------------- |
|
|
62
|
+
| `params` | `Partial<API.SwapParams>` | Route parameters. Polling starts when all required fields are present. |
|
|
63
|
+
| `options.interval` | `number` | Polling interval in ms. Defaults to 12000. |
|
|
64
|
+
| `options.paused` | `boolean` | Pause polling (e.g. during swap execution). |
|
|
65
|
+
| `options.onError` | `(error: Error) => void` | Called when a route fetch fails. |
|
|
66
|
+
|
|
67
|
+
**Returns:** `{ route, isRouting, error }`
|
|
68
|
+
|
|
69
|
+
| Name | Type | Description |
|
|
70
|
+
| ----------- | ---------------------------- | --------------------------------- |
|
|
71
|
+
| `route` | `API.SwapRoute \| undefined` | Current route quote. |
|
|
72
|
+
| `isRouting` | `boolean` | Whether a route is being fetched. |
|
|
73
|
+
| `error` | `Error \| null` | Latest route fetch error. |
|
|
74
|
+
|
|
47
75
|
#### `useTxConfirmation(options?: UseTxConfirmationOptions)`
|
|
48
76
|
|
|
49
77
|
Tracks transaction confirmation via backend SSE. Designed to compose with `useSwap`.
|
|
@@ -65,6 +93,126 @@ Tracks transaction confirmation via backend SSE. Designed to compose with `useSw
|
|
|
65
93
|
| `clearAll` | `() => void` | Remove all tracked transactions. |
|
|
66
94
|
| `transactions` | `Map<string, TrackedTx>` | All tracked transactions with their current status. |
|
|
67
95
|
|
|
96
|
+
### Components (Swap Widget)
|
|
97
|
+
|
|
98
|
+
The swap widget follows the **Script / Widget / UI** three-layer architecture.
|
|
99
|
+
|
|
100
|
+
#### `SwapWidget`
|
|
101
|
+
|
|
102
|
+
Plug-and-play swap form. Calls `useSwapScript` internally and renders `SwapUI` with `SwapPreviewModal`. Clicking the swap button opens a preview modal; confirming in the modal executes the swap.
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
<SwapWidget
|
|
106
|
+
chain={Chain.SOLANA}
|
|
107
|
+
from="So11111111111111111111111111111111111111112"
|
|
108
|
+
to="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
|
|
109
|
+
onComplete={(result) => console.log(result)}
|
|
110
|
+
className="my-swap"
|
|
111
|
+
/>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Props (`SwapWidgetProps`):**
|
|
115
|
+
|
|
116
|
+
| Name | Type | Description |
|
|
117
|
+
| ------------- | --------------------------------------------------------- | ----------------------------- |
|
|
118
|
+
| `chain` | `Chain` | Target chain. |
|
|
119
|
+
| `from?` | `string` | Initial from-token address. |
|
|
120
|
+
| `to?` | `string` | Initial to-token address. |
|
|
121
|
+
| `onComplete?` | `(result: { success: boolean; txHash?: string }) => void` | Called when swap completes. |
|
|
122
|
+
| `className?` | `string` | External style customization. |
|
|
123
|
+
|
|
124
|
+
#### `useSwapScript(params: UseSwapScriptParams)`
|
|
125
|
+
|
|
126
|
+
Script layer hook. Encapsulates all data fetching, state management, and swap execution. No JSX, no UI imports. Use this to build a custom swap UI.
|
|
127
|
+
|
|
128
|
+
**Parameters (`UseSwapScriptParams`):**
|
|
129
|
+
|
|
130
|
+
| Name | Type | Description |
|
|
131
|
+
| ------------- | --------------------------------------------------------- | --------------------------- |
|
|
132
|
+
| `chain` | `Chain` | Target chain. |
|
|
133
|
+
| `from?` | `string` | Initial from-token address. |
|
|
134
|
+
| `to?` | `string` | Initial to-token address. |
|
|
135
|
+
| `onComplete?` | `(result: { success: boolean; txHash?: string }) => void` | Called when swap completes. |
|
|
136
|
+
|
|
137
|
+
**Returns (`UseSwapScriptResult`):**
|
|
138
|
+
|
|
139
|
+
| Name | Type | Description |
|
|
140
|
+
| --------------------- | ---------------------------------- | -------------------------------------------- |
|
|
141
|
+
| `fromTokenAddress` | `string` | Currently selected from-token address. |
|
|
142
|
+
| `toTokenAddress` | `string` | Currently selected to-token address. |
|
|
143
|
+
| `setFromTokenAddress` | `(addr: string) => void` | Update from-token address. |
|
|
144
|
+
| `setToTokenAddress` | `(addr: string) => void` | Update to-token address. |
|
|
145
|
+
| `fromToken` | `Token \| null` | From-token metadata. |
|
|
146
|
+
| `toToken` | `Token \| null` | To-token metadata. |
|
|
147
|
+
| `fromBalance` | `Portfolio \| null` | User's from-token balance. |
|
|
148
|
+
| `toBalance` | `Portfolio \| null` | User's to-token balance. |
|
|
149
|
+
| `amount` | `string \| undefined` | Human-readable amount. |
|
|
150
|
+
| `setAmount` | `(v: string \| undefined) => void` | Update amount. |
|
|
151
|
+
| `setHalfAmount` | `() => void` | Set amount to half of from-token balance. |
|
|
152
|
+
| `setMaxAmount` | `() => void` | Set amount to full from-token balance. |
|
|
153
|
+
| `amountInDecimals` | `string \| undefined` | Amount in smallest unit. |
|
|
154
|
+
| `amountInUsd` | `string \| undefined` | Amount in USD. |
|
|
155
|
+
| `outputAmount` | `string \| undefined` | Formatted output amount from route. |
|
|
156
|
+
| `outputAmountInUsd` | `string \| undefined` | Output amount in USD. |
|
|
157
|
+
| `route` | `API.SwapRoute \| undefined` | Current route quote. |
|
|
158
|
+
| `isRouting` | `boolean` | Whether a route is being fetched. |
|
|
159
|
+
| `routeError` | `Error \| null` | Route fetch error. |
|
|
160
|
+
| `swap` | `() => Promise<void>` | Execute the swap. |
|
|
161
|
+
| `isSwapping` | `boolean` | Whether a swap is in progress. |
|
|
162
|
+
| `txStatus` | `TxConfirmationStatus` | Confirmation status of the last transaction. |
|
|
163
|
+
| `isLoading` | `boolean` | Whether initial data is loading. |
|
|
164
|
+
|
|
165
|
+
#### `SwapUI`
|
|
166
|
+
|
|
167
|
+
Pure presentational component for the swap form. Renders the From/To token inputs with token selector buttons, balance display, Half/Max shortcuts, and a preview button. Styled with `@liberfi.io/ui` components and Tailwind CSS.
|
|
168
|
+
|
|
169
|
+
**Props (`SwapUIProps`):**
|
|
170
|
+
|
|
171
|
+
| Name | Type | Description |
|
|
172
|
+
| ------------------- | ---------------------------------- | --------------------------------- |
|
|
173
|
+
| `fromToken` | `Token \| null` | From-token metadata. |
|
|
174
|
+
| `toToken` | `Token \| null` | To-token metadata. |
|
|
175
|
+
| `fromBalance` | `Portfolio \| null` | User's from-token balance. |
|
|
176
|
+
| `toBalance` | `Portfolio \| null` | User's to-token balance. |
|
|
177
|
+
| `amount` | `string \| undefined` | Human-readable input amount. |
|
|
178
|
+
| `amountInUsd` | `string \| undefined` | Input amount in USD. |
|
|
179
|
+
| `onAmountChange` | `(v: string \| undefined) => void` | Amount change handler. |
|
|
180
|
+
| `onHalfAmount?` | `() => void` | Half balance shortcut handler. |
|
|
181
|
+
| `onMaxAmount?` | `() => void` | Max balance shortcut handler. |
|
|
182
|
+
| `outputAmount` | `string \| undefined` | Formatted output amount. |
|
|
183
|
+
| `outputAmountInUsd` | `string \| undefined` | Output amount in USD. |
|
|
184
|
+
| `onFromTokenSelect` | `(addr: string) => void` | From-token selection handler. |
|
|
185
|
+
| `onToTokenSelect` | `(addr: string) => void` | To-token selection handler. |
|
|
186
|
+
| `route` | `API.SwapRoute \| undefined` | Current swap route quote. |
|
|
187
|
+
| `isRouting` | `boolean` | Whether a route is being fetched. |
|
|
188
|
+
| `routeError` | `Error \| null` | Route error. |
|
|
189
|
+
| `onPreview` | `() => void` | Open preview modal handler. |
|
|
190
|
+
| `isSwapping` | `boolean` | Whether a swap is in progress. |
|
|
191
|
+
| `className?` | `string` | External style customization. |
|
|
192
|
+
|
|
193
|
+
#### `SwapPreviewModal`
|
|
194
|
+
|
|
195
|
+
Modal component that previews swap details before confirmation. Shows from/to token cards with amounts, an expandable route plan details section, and a confirm button.
|
|
196
|
+
|
|
197
|
+
**Props (`SwapPreviewModalProps`):**
|
|
198
|
+
|
|
199
|
+
| Name | Type | Description |
|
|
200
|
+
| ------------------- | ---------------------------- | --------------------------------- |
|
|
201
|
+
| `isOpen` | `boolean` | Whether the modal is open. |
|
|
202
|
+
| `onOpenChange` | `(isOpen: boolean) => void` | Modal open/close handler. |
|
|
203
|
+
| `fromToken` | `Token \| null` | From-token metadata. |
|
|
204
|
+
| `toToken` | `Token \| null` | To-token metadata. |
|
|
205
|
+
| `fromBalance` | `Portfolio \| null` | User's from-token balance. |
|
|
206
|
+
| `inputAmount` | `string \| undefined` | Input amount (human-readable). |
|
|
207
|
+
| `inputAmountInUsd` | `string \| undefined` | Input amount in USD. |
|
|
208
|
+
| `outputAmount` | `string \| undefined` | Output amount (human-readable). |
|
|
209
|
+
| `outputAmountInUsd` | `string \| undefined` | Output amount in USD. |
|
|
210
|
+
| `route` | `API.SwapRoute \| undefined` | Current swap route. |
|
|
211
|
+
| `isRouting` | `boolean` | Whether a route is being fetched. |
|
|
212
|
+
| `routeError` | `Error \| null` | Route error. |
|
|
213
|
+
| `onConfirm` | `() => void` | Confirm swap handler. |
|
|
214
|
+
| `isSwapping` | `boolean` | Whether a swap is in progress. |
|
|
215
|
+
|
|
68
216
|
### Types
|
|
69
217
|
|
|
70
218
|
#### `SwapInput`
|
|
@@ -128,6 +276,16 @@ interface UseTxConfirmationOptions {
|
|
|
128
276
|
}
|
|
129
277
|
```
|
|
130
278
|
|
|
279
|
+
#### `UseSwapRoutePollingOptions`
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
interface UseSwapRoutePollingOptions {
|
|
283
|
+
interval?: number; // default: 12000
|
|
284
|
+
paused?: boolean;
|
|
285
|
+
onError?: (error: Error) => void;
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
131
289
|
### Constants
|
|
132
290
|
|
|
133
291
|
#### `version`
|
|
@@ -138,7 +296,91 @@ const version: string; // e.g. "0.1.0"
|
|
|
138
296
|
|
|
139
297
|
## Usage Examples
|
|
140
298
|
|
|
141
|
-
###
|
|
299
|
+
### SwapWidget (Plug-and-Play)
|
|
300
|
+
|
|
301
|
+
```tsx
|
|
302
|
+
import { Chain } from "@liberfi.io/types";
|
|
303
|
+
import { SwapWidget } from "@liberfi.io/ui-trade";
|
|
304
|
+
|
|
305
|
+
function TradePage() {
|
|
306
|
+
return (
|
|
307
|
+
<SwapWidget
|
|
308
|
+
chain={Chain.SOLANA}
|
|
309
|
+
from="So11111111111111111111111111111111111111112"
|
|
310
|
+
to="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
|
|
311
|
+
onComplete={(result) => {
|
|
312
|
+
if (result.success) {
|
|
313
|
+
toast.success(`Swap confirmed: ${result.txHash}`);
|
|
314
|
+
} else {
|
|
315
|
+
toast.error("Swap failed");
|
|
316
|
+
}
|
|
317
|
+
}}
|
|
318
|
+
/>
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
### Custom UI with useSwapScript + SwapPreviewModal
|
|
324
|
+
|
|
325
|
+
```tsx
|
|
326
|
+
import { Chain } from "@liberfi.io/types";
|
|
327
|
+
import { useDisclosure } from "@liberfi.io/ui";
|
|
328
|
+
import { useSwapScript, SwapUI, SwapPreviewModal } from "@liberfi.io/ui-trade";
|
|
329
|
+
|
|
330
|
+
function MyCustomSwapForm() {
|
|
331
|
+
const script = useSwapScript({
|
|
332
|
+
chain: Chain.SOLANA,
|
|
333
|
+
from: "So11111111111111111111111111111111111111112",
|
|
334
|
+
to: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
|
|
335
|
+
onComplete: (result) => console.log("Done:", result),
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const { isOpen, onOpen, onOpenChange } = useDisclosure();
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
<>
|
|
342
|
+
<SwapUI
|
|
343
|
+
fromToken={script.fromToken}
|
|
344
|
+
toToken={script.toToken}
|
|
345
|
+
fromBalance={script.fromBalance}
|
|
346
|
+
toBalance={script.toBalance}
|
|
347
|
+
amount={script.amount}
|
|
348
|
+
amountInUsd={script.amountInUsd}
|
|
349
|
+
onAmountChange={script.setAmount}
|
|
350
|
+
onHalfAmount={script.setHalfAmount}
|
|
351
|
+
onMaxAmount={script.setMaxAmount}
|
|
352
|
+
outputAmount={script.outputAmount}
|
|
353
|
+
outputAmountInUsd={script.outputAmountInUsd}
|
|
354
|
+
onFromTokenSelect={script.setFromTokenAddress}
|
|
355
|
+
onToTokenSelect={script.setToTokenAddress}
|
|
356
|
+
route={script.route}
|
|
357
|
+
isRouting={script.isRouting}
|
|
358
|
+
routeError={script.routeError}
|
|
359
|
+
onPreview={onOpen}
|
|
360
|
+
isSwapping={script.isSwapping}
|
|
361
|
+
/>
|
|
362
|
+
<SwapPreviewModal
|
|
363
|
+
isOpen={isOpen}
|
|
364
|
+
onOpenChange={onOpenChange}
|
|
365
|
+
fromToken={script.fromToken}
|
|
366
|
+
toToken={script.toToken}
|
|
367
|
+
fromBalance={script.fromBalance}
|
|
368
|
+
inputAmount={script.amount}
|
|
369
|
+
inputAmountInUsd={script.amountInUsd}
|
|
370
|
+
outputAmount={script.outputAmount}
|
|
371
|
+
outputAmountInUsd={script.outputAmountInUsd}
|
|
372
|
+
route={script.route}
|
|
373
|
+
isRouting={script.isRouting}
|
|
374
|
+
routeError={script.routeError}
|
|
375
|
+
onConfirm={script.swap}
|
|
376
|
+
isSwapping={script.isSwapping}
|
|
377
|
+
/>
|
|
378
|
+
</>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Swap with Confirmation Tracking (Low-Level Hooks)
|
|
142
384
|
|
|
143
385
|
```tsx
|
|
144
386
|
import { Chain } from "@liberfi.io/types";
|
|
@@ -241,3 +483,6 @@ function EvmSwapButton() {
|
|
|
241
483
|
- Add `useSwapQuote` hook for fetching quotes without executing (read-only route info).
|
|
242
484
|
- Add ERC20 approval detection and `useTokenApproval` hook for EVM chains that don't support permit.
|
|
243
485
|
- Support `ExactOut` swap mode with output amount estimation.
|
|
486
|
+
- Add a skeleton loading component (`swap-skeleton.ui.tsx`) for the swap form.
|
|
487
|
+
- Add slippage / priority fee / anti-MEV configuration to `SwapWidget` props.
|
|
488
|
+
- Support token-select callback props on `SwapWidget` for integrating with external token picker UI.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _liberfi_io_types from '@liberfi.io/types';
|
|
2
|
+
import { Chain, API, Token, Portfolio } from '@liberfi.io/types';
|
|
2
3
|
import { WalletAdapter } from '@liberfi.io/wallet-connector';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Input parameters for the `swap()` function returned by {@link useSwap}.
|
|
@@ -50,6 +52,15 @@ interface UseSwapOptions {
|
|
|
50
52
|
/** Called when an error occurs at any phase of the swap flow. */
|
|
51
53
|
onError?: (error: Error, phase: SwapPhase) => void;
|
|
52
54
|
}
|
|
55
|
+
/** Options for the {@link useSwapRoutePolling} hook. */
|
|
56
|
+
interface UseSwapRoutePollingOptions {
|
|
57
|
+
/** Polling interval in ms. Defaults to 12000 (12s). */
|
|
58
|
+
interval?: number;
|
|
59
|
+
/** Pause polling (e.g. during swap execution). */
|
|
60
|
+
paused?: boolean;
|
|
61
|
+
/** Called when a route fetch fails. */
|
|
62
|
+
onError?: (error: Error) => void;
|
|
63
|
+
}
|
|
53
64
|
/** Confirmation status of a tracked transaction. */
|
|
54
65
|
type TxConfirmationStatus = "idle" | "pending" | "confirmed" | "failed";
|
|
55
66
|
/** Options for the {@link useTxConfirmation} hook. */
|
|
@@ -77,6 +88,21 @@ declare function useSwap(options?: UseSwapOptions): {
|
|
|
77
88
|
isSwapping: boolean;
|
|
78
89
|
};
|
|
79
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Hook that polls for swap route quotes at a configurable interval.
|
|
93
|
+
*
|
|
94
|
+
* Built on top of `useSwapRouteQuery` (TanStack Query) — automatically
|
|
95
|
+
* deduplicates requests, caches results, and invalidates when params change.
|
|
96
|
+
*
|
|
97
|
+
* Polling pauses when `paused` is true (e.g. during swap execution) or
|
|
98
|
+
* when required params are missing.
|
|
99
|
+
*/
|
|
100
|
+
declare function useSwapRoutePolling(params: Partial<API.SwapParams>, options?: UseSwapRoutePollingOptions): {
|
|
101
|
+
route: _liberfi_io_types.SwapRoute | undefined;
|
|
102
|
+
isRouting: boolean;
|
|
103
|
+
error: Error | null;
|
|
104
|
+
};
|
|
105
|
+
|
|
80
106
|
interface TrackedTx {
|
|
81
107
|
chain: Chain;
|
|
82
108
|
txHash: string;
|
|
@@ -104,6 +130,193 @@ declare function useTxConfirmation(options?: UseTxConfirmationOptions): {
|
|
|
104
130
|
transactions: Map<string, TrackedTx>;
|
|
105
131
|
};
|
|
106
132
|
|
|
133
|
+
/** Parameters for {@link useSwapScript}. */
|
|
134
|
+
interface UseSwapScriptParams {
|
|
135
|
+
/** Target chain */
|
|
136
|
+
chain: Chain;
|
|
137
|
+
/** Initial from-token address */
|
|
138
|
+
from?: string;
|
|
139
|
+
/** Initial to-token address */
|
|
140
|
+
to?: string;
|
|
141
|
+
/** Called when the swap completes (success or failure) */
|
|
142
|
+
onComplete?: (result: {
|
|
143
|
+
success: boolean;
|
|
144
|
+
txHash?: string;
|
|
145
|
+
}) => void;
|
|
146
|
+
}
|
|
147
|
+
/** Return value of {@link useSwapScript}. */
|
|
148
|
+
interface UseSwapScriptResult {
|
|
149
|
+
/** Currently selected from-token address */
|
|
150
|
+
fromTokenAddress: string;
|
|
151
|
+
/** Currently selected to-token address */
|
|
152
|
+
toTokenAddress: string;
|
|
153
|
+
/** Update the from-token address */
|
|
154
|
+
setFromTokenAddress: (addr: string) => void;
|
|
155
|
+
/** Update the to-token address */
|
|
156
|
+
setToTokenAddress: (addr: string) => void;
|
|
157
|
+
/** From-token metadata (name, symbol, decimals, price, ...) */
|
|
158
|
+
fromToken: Token | null;
|
|
159
|
+
/** To-token metadata */
|
|
160
|
+
toToken: Token | null;
|
|
161
|
+
/** User's from-token balance */
|
|
162
|
+
fromBalance: Portfolio | null;
|
|
163
|
+
/** User's to-token balance */
|
|
164
|
+
toBalance: Portfolio | null;
|
|
165
|
+
/** Human-readable amount entered by the user */
|
|
166
|
+
amount: string | undefined;
|
|
167
|
+
/** Update the amount */
|
|
168
|
+
setAmount: (v: string | undefined) => void;
|
|
169
|
+
/** Set amount to half of the from-token balance */
|
|
170
|
+
setHalfAmount: () => void;
|
|
171
|
+
/** Set amount to the full from-token balance */
|
|
172
|
+
setMaxAmount: () => void;
|
|
173
|
+
/** Amount converted to smallest unit using token decimals */
|
|
174
|
+
amountInDecimals: string | undefined;
|
|
175
|
+
/** Amount converted to USD using token price */
|
|
176
|
+
amountInUsd: string | undefined;
|
|
177
|
+
/** Formatted output amount from the route (human-readable) */
|
|
178
|
+
outputAmount: string | undefined;
|
|
179
|
+
/** Output amount converted to USD using to-token price */
|
|
180
|
+
outputAmountInUsd: string | undefined;
|
|
181
|
+
/** Current swap route quote */
|
|
182
|
+
route: API.SwapRoute | undefined;
|
|
183
|
+
/** Whether a route is currently being fetched */
|
|
184
|
+
isRouting: boolean;
|
|
185
|
+
/** Route fetch error, if any */
|
|
186
|
+
routeError: Error | null;
|
|
187
|
+
/** Execute the swap (route -> sign -> send) */
|
|
188
|
+
swap: () => Promise<void>;
|
|
189
|
+
/** Whether a swap is currently in progress */
|
|
190
|
+
isSwapping: boolean;
|
|
191
|
+
/** Confirmation status of the most recent transaction */
|
|
192
|
+
txStatus: TxConfirmationStatus;
|
|
193
|
+
/** Whether initial data (tokens, balances) is still loading */
|
|
194
|
+
isLoading: boolean;
|
|
195
|
+
}
|
|
196
|
+
/** Props for {@link SwapWidget}. */
|
|
197
|
+
interface SwapWidgetProps {
|
|
198
|
+
/** Target chain */
|
|
199
|
+
chain: Chain;
|
|
200
|
+
/** Initial from-token address */
|
|
201
|
+
from?: string;
|
|
202
|
+
/** Initial to-token address */
|
|
203
|
+
to?: string;
|
|
204
|
+
/** Called when the swap completes (success or failure) */
|
|
205
|
+
onComplete?: (result: {
|
|
206
|
+
success: boolean;
|
|
207
|
+
txHash?: string;
|
|
208
|
+
}) => void;
|
|
209
|
+
/** External style customization */
|
|
210
|
+
className?: string;
|
|
211
|
+
}
|
|
212
|
+
/** Props for {@link SwapUI}. */
|
|
213
|
+
interface SwapUIProps {
|
|
214
|
+
/** From-token metadata */
|
|
215
|
+
fromToken: Token | null;
|
|
216
|
+
/** To-token metadata */
|
|
217
|
+
toToken: Token | null;
|
|
218
|
+
/** User's from-token balance */
|
|
219
|
+
fromBalance: Portfolio | null;
|
|
220
|
+
/** User's to-token balance */
|
|
221
|
+
toBalance: Portfolio | null;
|
|
222
|
+
/** Human-readable amount */
|
|
223
|
+
amount: string | undefined;
|
|
224
|
+
/** Amount in USD */
|
|
225
|
+
amountInUsd: string | undefined;
|
|
226
|
+
/** Amount change handler */
|
|
227
|
+
onAmountChange: (v: string | undefined) => void;
|
|
228
|
+
/** Set amount to half of the from-token balance */
|
|
229
|
+
onHalfAmount?: () => void;
|
|
230
|
+
/** Set amount to the full from-token balance */
|
|
231
|
+
onMaxAmount?: () => void;
|
|
232
|
+
/** Formatted output amount (human-readable) */
|
|
233
|
+
outputAmount: string | undefined;
|
|
234
|
+
/** Output amount in USD */
|
|
235
|
+
outputAmountInUsd: string | undefined;
|
|
236
|
+
/** From-token selection handler */
|
|
237
|
+
onFromTokenSelect: (addr: string) => void;
|
|
238
|
+
/** To-token selection handler */
|
|
239
|
+
onToTokenSelect: (addr: string) => void;
|
|
240
|
+
/** Current swap route quote */
|
|
241
|
+
route: API.SwapRoute | undefined;
|
|
242
|
+
/** Whether a route is being fetched */
|
|
243
|
+
isRouting: boolean;
|
|
244
|
+
/** Route error */
|
|
245
|
+
routeError: Error | null;
|
|
246
|
+
/** Open preview modal before swap */
|
|
247
|
+
onPreview: () => void;
|
|
248
|
+
/** Whether a swap is in progress */
|
|
249
|
+
isSwapping: boolean;
|
|
250
|
+
/** External style customization */
|
|
251
|
+
className?: string;
|
|
252
|
+
}
|
|
253
|
+
/** Props for {@link SwapPreviewModal}. */
|
|
254
|
+
interface SwapPreviewModalProps {
|
|
255
|
+
/** Whether the modal is open */
|
|
256
|
+
isOpen: boolean;
|
|
257
|
+
/** Modal open/close change handler */
|
|
258
|
+
onOpenChange: (isOpen: boolean) => void;
|
|
259
|
+
/** From-token metadata */
|
|
260
|
+
fromToken: Token | null;
|
|
261
|
+
/** To-token metadata */
|
|
262
|
+
toToken: Token | null;
|
|
263
|
+
/** User's from-token balance */
|
|
264
|
+
fromBalance: Portfolio | null;
|
|
265
|
+
/** Input amount (human-readable) */
|
|
266
|
+
inputAmount: string | undefined;
|
|
267
|
+
/** Input amount in USD */
|
|
268
|
+
inputAmountInUsd: string | undefined;
|
|
269
|
+
/** Output amount (human-readable) */
|
|
270
|
+
outputAmount: string | undefined;
|
|
271
|
+
/** Output amount in USD */
|
|
272
|
+
outputAmountInUsd: string | undefined;
|
|
273
|
+
/** Current swap route */
|
|
274
|
+
route: API.SwapRoute | undefined;
|
|
275
|
+
/** Whether a route is being fetched */
|
|
276
|
+
isRouting: boolean;
|
|
277
|
+
/** Route error */
|
|
278
|
+
routeError: Error | null;
|
|
279
|
+
/** Confirm swap handler */
|
|
280
|
+
onConfirm: () => void;
|
|
281
|
+
/** Whether a swap is in progress */
|
|
282
|
+
isSwapping: boolean;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Script hook that encapsulates all data fetching and state management
|
|
287
|
+
* for the swap form.
|
|
288
|
+
*
|
|
289
|
+
* Pure TypeScript — no JSX, no UI imports.
|
|
290
|
+
*/
|
|
291
|
+
declare function useSwapScript(params: UseSwapScriptParams): UseSwapScriptResult;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Swap widget — thin orchestration layer.
|
|
295
|
+
*
|
|
296
|
+
* Calls `useSwapScript` to fetch all data, renders `SwapUI` for the form
|
|
297
|
+
* and `SwapPreviewModal` for pre-swap confirmation.
|
|
298
|
+
*
|
|
299
|
+
* For custom UIs, use `useSwapScript` directly with your own presentation.
|
|
300
|
+
*/
|
|
301
|
+
declare function SwapWidget({ chain, from, to, onComplete, className, }: SwapWidgetProps): react_jsx_runtime.JSX.Element;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Pure presentational component for the swap form.
|
|
305
|
+
*
|
|
306
|
+
* Receives all data and callbacks via props — no API calls, no subscriptions.
|
|
307
|
+
* Consumers can replace this component entirely while reusing `useSwapScript`.
|
|
308
|
+
*/
|
|
309
|
+
declare function SwapUI({ fromToken, toToken, fromBalance, toBalance, amount, amountInUsd, onAmountChange, onHalfAmount, onMaxAmount, outputAmount, outputAmountInUsd, onFromTokenSelect, onToTokenSelect, route, isRouting, routeError, onPreview, isSwapping, className, }: SwapUIProps): react_jsx_runtime.JSX.Element;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Modal that previews swap details before confirmation.
|
|
313
|
+
*
|
|
314
|
+
* Pure presentational — all data and callbacks received via props.
|
|
315
|
+
* Shows from/to token cards, expandable route plan details,
|
|
316
|
+
* and a confirm button.
|
|
317
|
+
*/
|
|
318
|
+
declare function SwapPreviewModal({ isOpen, onOpenChange, fromToken, toToken, fromBalance, inputAmount, inputAmountInUsd, outputAmount, outputAmountInUsd, route, isRouting, routeError, onConfirm, isSwapping, }: SwapPreviewModalProps): react_jsx_runtime.JSX.Element;
|
|
319
|
+
|
|
107
320
|
declare global {
|
|
108
321
|
interface Window {
|
|
109
322
|
__LIBERFI_VERSION__?: {
|
|
@@ -111,6 +324,6 @@ declare global {
|
|
|
111
324
|
};
|
|
112
325
|
}
|
|
113
326
|
}
|
|
114
|
-
declare const _default: "0.1.
|
|
327
|
+
declare const _default: "0.1.2";
|
|
115
328
|
|
|
116
|
-
export { type SwapInput, type SwapPhase, type SwapResult, type TxConfirmationStatus, type UseSwapOptions, type UseTxConfirmationOptions, useSwap, useTxConfirmation, _default as version };
|
|
329
|
+
export { type SwapInput, type SwapPhase, SwapPreviewModal, type SwapPreviewModalProps, type SwapResult, SwapUI, type SwapUIProps, SwapWidget, type SwapWidgetProps, type TxConfirmationStatus, type UseSwapOptions, type UseSwapRoutePollingOptions, type UseSwapScriptParams, type UseSwapScriptResult, type UseTxConfirmationOptions, useSwap, useSwapRoutePolling, useSwapScript, useTxConfirmation, _default as version };
|