@deloraprotocol/widget 1.0.0 → 1.0.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A React widget component library for crypto trading UI, migrated from delora-exchange (Angular). Includes Trade widget, token selection, network selection, and slippage panel. **Consumers do not need Tailwind** — the library ships compiled CSS.
4
4
 
5
- **Note:** Wallet connection is not implemented. The widget shows placeholder UI for swap actions.
5
+ **Note:** Wallet connection and transaction execution remain host-managed. The widget can request connect, emit quotes, and notify the host when the user clicks `Approve` or `Swap`.
6
6
 
7
7
  ## Installation
8
8
 
@@ -20,26 +20,37 @@ import "@deloraprotocol/widget/styles.css";
20
20
 
21
21
  ## Usage
22
22
 
23
- ### Trade Widget
24
-
25
- ```tsx
23
+ ### Trade Widget
24
+
25
+ ```tsx
26
26
  import { TradeWidget } from "@deloraprotocol/widget";
27
-
28
- <TradeWidget
29
- theme="dark"
30
- config={{
31
- dataBaseUrl: "https://your-app.com", // or "" for same-origin
32
- apiBase: "https://api.delora.build",
33
- }}
34
- />
35
- ```
36
-
37
- `dataBaseUrl` must point to a server that hosts:
38
- - `/data/networks.json`
39
- - `/data/tokens.json`
40
- - `/data/tokens_search.json`
41
-
42
- Use `""` for same-origin (e.g. when data is in your app's public folder).
27
+
28
+ <TradeWidget
29
+ theme="dark"
30
+ config={{
31
+ apiBase: "https://api.delora.build",
32
+ isWalletConnected: connected,
33
+ onConnectWallet: openWalletModal,
34
+ }}
35
+ initialBuyToken={{ chainId: 1, address: "0x..." }}
36
+ lockBuyToken
37
+ onQuote={(quote) => console.log("quote", quote)}
38
+ onApprove={(payload) => console.log("approve", payload)}
39
+ onSwap={(payload) => console.log("swap", payload)}
40
+ onError={(error) => console.error(error)}
41
+ />
42
+ ```
43
+
44
+ `apiBase` is used for:
45
+ - `/v1/chains`
46
+ - `/v1/tokens`
47
+ - `/v1/quotes`
48
+
49
+ If omitted, the widget defaults to `https://api.delora.build`.
50
+
51
+ `dataBaseUrl` is no longer required for metadata fetching. Keep it only if you need
52
+ to resolve relative image URLs from a custom source. For explicit control over image
53
+ resolution, pass `assetBaseUrl`.
43
54
 
44
55
  ### Simple Widget (MyWidget)
45
56
 
@@ -54,12 +65,19 @@ import { MyWidget } from "@deloraprotocol/widget";
54
65
 
55
66
  ## API
56
67
 
57
- ### Exports
58
-
59
- - `MyWidget` — the widget component
60
- - `MyWidgetTheme` — `"light" | "dark"`
61
- - `MyWidgetVars` — typed object for token overrides
62
- - `MyWidgetProps` — component props
68
+ ### Exports
69
+
70
+ - `MyWidget` — the widget component
71
+ - `TradeWidget` — trade widget with token/network selection and quote flow
72
+ - `MyWidgetTheme` — `"light" | "dark"`
73
+ - `MyWidgetVars` — typed object for token overrides
74
+ - `MyWidgetProps` — component props
75
+ - `TradeWidgetProps`, `TradeWidgetConfig`
76
+ - `TradeWidgetTokenSelection`
77
+ - `TradeWidgetQuotePayload`
78
+ - `TradeWidgetActionPayload`
79
+ - `TradeWidgetErrorPayload`
80
+ - `Token`, `Network`
63
81
 
64
82
  ### `MyWidgetProps`
65
83
 
@@ -70,7 +88,51 @@ import { MyWidget } from "@deloraprotocol/widget";
70
88
  | `className` | `string` | Applied to root (for host layout/styling) |
71
89
  | `style` | `React.CSSProperties` | Applied last for advanced overrides |
72
90
 
73
- Extends `React.HTMLAttributes<HTMLDivElement>` for other div props.
91
+ Extends `React.HTMLAttributes<HTMLDivElement>` for other div props.
92
+
93
+ ### `TradeWidgetProps`
94
+
95
+ `TradeWidgetProps` extends normal root `div` props except DOM `onError`, which is reserved for the widget error callback below.
96
+
97
+ | Prop | Type | Description |
98
+ |----------|-------------------|--------------------------------------------------|
99
+ | `config` | `TradeWidgetConfig` | Widget runtime config |
100
+ | `theme` | `"light" \| "dark"` | Built-in theme (default: `"dark"`) |
101
+ | `vars` | `Partial<MyWidgetVars>` | Override theme tokens via CSS variables |
102
+ | `initialSellToken` | `{ chainId: number; address: string }` | Preselect sell token |
103
+ | `initialBuyToken` | `{ chainId: number; address: string }` | Preselect buy token |
104
+ | `initialSellNetworkId` | `number` | Preselect sell network |
105
+ | `initialBuyNetworkId` | `number` | Preselect buy network |
106
+ | `lockSellToken` | `boolean` | Prevent changing sell token |
107
+ | `lockBuyToken` | `boolean` | Prevent changing buy token |
108
+ | `lockSellNetwork` | `boolean` | Prevent changing sell network |
109
+ | `lockBuyNetwork` | `boolean` | Prevent changing buy network |
110
+ | `onQuote` | `(payload) => void` | Called when a new quote is resolved |
111
+ | `onApprove` | `(payload) => void` | Called when the user presses `Approve` |
112
+ | `onSwap` | `(payload) => void` | Called when the user presses `Swap` |
113
+ | `onError` | `(payload) => void` | Called for metadata, selection, and quote errors |
114
+
115
+ ### `TradeWidgetConfig`
116
+
117
+ | Prop | Type | Description |
118
+ |----------|-------------------|--------------------------------------------------|
119
+ | `apiBase` | `string` | Delora API base for `/v1/chains`, `/v1/tokens`, `/v1/quotes` |
120
+ | `assetBaseUrl` | `string` | Optional base for resolving relative image URLs |
121
+ | `dataBaseUrl` | `string` | Legacy image-base fallback |
122
+ | `isWalletConnected` | `boolean` | Controls the main action button state |
123
+ | `onConnectWallet` | `() => void` | Called when the user clicks `Connect Wallet` |
124
+
125
+ ### Callback payloads
126
+
127
+ - `onQuote(payload)` returns selected sell/buy network, selected sell/buy token, current sell/buy amounts, slippage, and the raw Delora `quote`.
128
+ - `onApprove(payload)` and `onSwap(payload)` return the current selection plus `quote`, `txData`, `price`, and `gasCostUSD`.
129
+ - `onError(payload)` returns `{ source, message, status?, statusCode?, error? }`, where `source` is one of `metadata`, `selection`, or `quote`.
130
+
131
+ ### Locking behavior
132
+
133
+ - `lockBuyToken` / `lockSellToken` disable token picking and also freeze the related network, because the token already implies a chain.
134
+ - `lockBuyNetwork` / `lockSellNetwork` disable network changes but keep token selection available within the locked chain.
135
+ - Any lock also disables the center "swap sides" button to avoid silently breaking host-provided constraints.
74
136
 
75
137
  ## Why no Tailwind required in host
76
138
 
@@ -161,7 +223,7 @@ cd playground && npm install
161
223
  npm run playground:dev
162
224
  ```
163
225
 
164
- This runs the library watcher and the playground dev server together. The playground uses the built output from `dist/`.
226
+ This runs the library watcher and the playground dev server together. The playground uses the built output from `dist/`.
165
227
 
166
228
  **When errors occur or the UI shows stale/broken code:** run `npm run build` separately to rebuild the library, then restart or refresh the playground. The playground does not auto-rebuild the library.
167
229
 
package/dist/index.d.ts CHANGED
@@ -49,6 +49,34 @@ export declare interface Network {
49
49
  nativeCurrency: INativeCurrency;
50
50
  }
51
51
 
52
+ declare interface QuoteResponse {
53
+ calldata?: {
54
+ to?: string;
55
+ data?: string;
56
+ value?: string;
57
+ };
58
+ gas?: {
59
+ gasPrice?: string;
60
+ gasLimit?: string;
61
+ maxFeePerGas?: string;
62
+ maxPriorityFeePerGas?: string;
63
+ };
64
+ inputAmount?: string;
65
+ outputAmount?: string;
66
+ fees?: {
67
+ total?: {
68
+ amount: string;
69
+ decimals?: number;
70
+ };
71
+ breakdown?: Array<{
72
+ type: string;
73
+ amount: string;
74
+ gasPrice?: string;
75
+ }>;
76
+ };
77
+ [key: string]: unknown;
78
+ }
79
+
52
80
  export declare interface Token {
53
81
  symbol: string;
54
82
  imageUrl: string;
@@ -61,10 +89,27 @@ export declare interface Token {
61
89
 
62
90
  export declare function TradeWidget(props: TradeWidgetProps): JSX.Element;
63
91
 
92
+ export declare interface TradeWidgetActionPayload {
93
+ action: "approve" | "swap";
94
+ sellNetwork: Network;
95
+ buyNetwork: Network;
96
+ sellToken: Token;
97
+ buyToken: Token;
98
+ sellAmount: string;
99
+ buyAmount: string;
100
+ slippage: number;
101
+ quote: QuoteResponse | null;
102
+ txData: QuoteResponse["calldata"] | null;
103
+ price: number;
104
+ gasCostUSD: string | null;
105
+ }
106
+
64
107
  export declare interface TradeWidgetConfig {
65
- dataBaseUrl: string;
108
+ /** Legacy asset base or custom relative-image base. Metadata is loaded from apiBase/v1/... */
109
+ dataBaseUrl?: string;
110
+ /** Base URL of Delora API used for chains, tokens, and quotes. Defaults to https://api.delora.build */
66
111
  apiBase?: string;
67
- /** Base URL for resolving relative image paths (e.g. /img/...). Defaults to dataBaseUrl or origin. */
112
+ /** Base URL for resolving relative image paths (e.g. /img/...). Defaults to dataBaseUrl, apiBase, or origin. */
68
113
  assetBaseUrl?: string;
69
114
  /** Whether a wallet is connected. When false, the main button shows "Connect Wallet" (Angular: isWalletConnected). */
70
115
  isWalletConnected?: boolean;
@@ -72,8 +117,44 @@ export declare interface TradeWidgetConfig {
72
117
  onConnectWallet?: () => void;
73
118
  }
74
119
 
75
- export declare interface TradeWidgetProps extends MyWidgetProps {
120
+ export declare interface TradeWidgetErrorPayload {
121
+ source: "metadata" | "quote" | "selection";
122
+ message: string;
123
+ status?: number;
124
+ statusCode?: number;
125
+ error?: unknown;
126
+ }
127
+
128
+ export declare interface TradeWidgetProps extends Omit<MyWidgetProps, "onError"> {
76
129
  config: TradeWidgetConfig;
130
+ initialSellToken?: TradeWidgetTokenSelection;
131
+ initialBuyToken?: TradeWidgetTokenSelection;
132
+ initialSellNetworkId?: number;
133
+ initialBuyNetworkId?: number;
134
+ lockBuyToken?: boolean;
135
+ lockSellToken?: boolean;
136
+ lockBuyNetwork?: boolean;
137
+ lockSellNetwork?: boolean;
138
+ onApprove?: (payload: TradeWidgetActionPayload) => void;
139
+ onSwap?: (payload: TradeWidgetActionPayload) => void;
140
+ onQuote?: (payload: TradeWidgetQuotePayload) => void;
141
+ onError?: (payload: TradeWidgetErrorPayload) => void;
142
+ }
143
+
144
+ export declare interface TradeWidgetQuotePayload {
145
+ sellNetwork: Network;
146
+ buyNetwork: Network;
147
+ sellToken: Token;
148
+ buyToken: Token;
149
+ sellAmount: string;
150
+ buyAmount: string;
151
+ slippage: number;
152
+ quote: QuoteResponse;
153
+ }
154
+
155
+ export declare interface TradeWidgetTokenSelection {
156
+ chainId: number;
157
+ address: string;
77
158
  }
78
159
 
79
160
  export { }