@unifold/connect-react 0.1.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 ADDED
@@ -0,0 +1,383 @@
1
+ # @unifold/connect-react
2
+
3
+ The complete React SDK for integrating crypto deposits and onramp functionality into your application. Simple, powerful, and fully customizable.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @unifold/connect-react
9
+ # or
10
+ pnpm add @unifold/connect-react
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Wrap your app with UnifoldProvider
16
+
17
+ ```tsx
18
+ import { UnifoldProvider } from '@unifold/connect-react';
19
+
20
+ function App() {
21
+ return (
22
+ <UnifoldProvider
23
+ publishableKey="pk_test_your_key"
24
+ config={{
25
+ modalTitle: 'Deposit Crypto',
26
+ hideDepositTracker: false,
27
+ }}
28
+ >
29
+ <YourApp />
30
+ </UnifoldProvider>
31
+ );
32
+ }
33
+ ```
34
+
35
+ ### 2. Call beginDeposit() anywhere in your app
36
+
37
+ ```tsx
38
+ import { useUnifold } from '@unifold/connect-react';
39
+
40
+ function DepositButton() {
41
+ const { beginDeposit } = useUnifold();
42
+
43
+ const handleDeposit = async () => {
44
+ try {
45
+ // beginDeposit returns a Promise
46
+ const result = await beginDeposit({
47
+ // Required fields
48
+ userId: 'user_123', // Your user's unique identifier
49
+ destinationChainId: '137', // Polygon
50
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
51
+ destinationTokenSymbol: 'USDC',
52
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
53
+
54
+ // Optional callbacks (fired immediately)
55
+ onSuccess: ({ message }) => {
56
+ console.log('Immediate callback:', message);
57
+ },
58
+ onError: ({ message }) => {
59
+ console.error('Immediate callback error:', message);
60
+ },
61
+ });
62
+
63
+ // Handle the result from the promise
64
+ console.log('Deposit completed!', result);
65
+ alert('Success: ' + result.message);
66
+ } catch (error) {
67
+ // Handle the error from the promise
68
+ console.error('Deposit failed:', error);
69
+ alert('Error: ' + error.message);
70
+ }
71
+ };
72
+
73
+ return <button onClick={handleDeposit}>Deposit</button>;
74
+ }
75
+ ```
76
+
77
+ That's it! The modal with full deposit UI will appear automatically.
78
+
79
+ ## API Reference
80
+
81
+ ### UnifoldProvider
82
+
83
+ Wraps your application and provides the Unifold context.
84
+
85
+ **Props:**
86
+ - `publishableKey` (required): Your Unifold API publishable key
87
+ - `config` (optional): Configuration object
88
+ - `modalTitle` (optional): Custom title for the deposit modal
89
+ - `hideDepositTracker` (optional): Hide the deposit tracker option
90
+ - `appearance` (optional): Theme appearance - `'light'` | `'dark'` | `'auto'` (defaults to `'dark'`)
91
+ - `'light'`: Force light mode
92
+ - `'dark'`: Force dark mode (default)
93
+ - `'auto'`: Use system preference and respond to changes
94
+ - `children` (required): Your React components
95
+
96
+ ```tsx
97
+ <UnifoldProvider
98
+ publishableKey="pk_test_..."
99
+ config={{
100
+ modalTitle: 'Deposit Crypto',
101
+ hideDepositTracker: false,
102
+ appearance: 'dark', // 'light' | 'dark' | 'auto' (defaults to 'dark')
103
+ }}
104
+ >
105
+ {children}
106
+ </UnifoldProvider>
107
+ ```
108
+
109
+ ### useUnifold()
110
+
111
+ Hook that provides access to the deposit functionality.
112
+
113
+ **Returns:**
114
+ - `publishableKey`: The current publishable key
115
+ - `beginDeposit(config)`: Function to launch the deposit modal (returns a Promise)
116
+ - `closeDeposit()`: Function to programmatically close the modal
117
+
118
+ ```tsx
119
+ const { beginDeposit, closeDeposit, publishableKey } = useUnifold();
120
+ ```
121
+
122
+ ### beginDeposit(config)
123
+
124
+ Launches the deposit modal with the specified configuration. **Returns a Promise** that resolves when the deposit completes or rejects on error/cancellation.
125
+
126
+ **Parameters (DepositConfig):**
127
+
128
+ | Parameter | Type | Required | Description |
129
+ |-----------|------|----------|-------------|
130
+ | `userId` | string | ✅ | Your user's unique identifier for wallet creation/tracking |
131
+ | `destinationChainId` | string | ✅ | Target blockchain chain ID |
132
+ | `destinationTokenAddress` | string | ✅ | Token contract address |
133
+ | `destinationTokenSymbol` | string | ✅ | Token symbol (e.g., "USDC") |
134
+ | `recipientAddress` | string | ✅ | Recipient wallet address |
135
+ | `onSuccess` | function | - | Success callback (fired immediately) |
136
+ | `onError` | function | - | Error callback (fired immediately) |
137
+
138
+ **Returns:** `Promise<DepositResult>`
139
+
140
+ **DepositResult:**
141
+ ```typescript
142
+ interface DepositResult {
143
+ message: string;
144
+ transaction?: unknown;
145
+ executionId?: string;
146
+ }
147
+ ```
148
+
149
+ **DepositError:**
150
+ ```typescript
151
+ interface DepositError {
152
+ message: string;
153
+ error?: unknown;
154
+ code?: string; // e.g., 'DEPOSIT_CANCELLED', 'POLLING_ERROR'
155
+ }
156
+ ```
157
+
158
+ **Example:**
159
+
160
+ ```tsx
161
+ // Promise-based (async/await)
162
+ try {
163
+ const result = await beginDeposit({
164
+ userId: 'user_123',
165
+ destinationChainId: '137',
166
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
167
+ destinationTokenSymbol: 'USDC',
168
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
169
+ });
170
+ console.log('Success:', result);
171
+ } catch (error) {
172
+ console.error('Error:', error);
173
+ }
174
+
175
+ // Hybrid (promise + callbacks)
176
+ const depositPromise = beginDeposit({
177
+ userId: 'user_123',
178
+ destinationChainId: '137',
179
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
180
+ destinationTokenSymbol: 'USDC',
181
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
182
+ onSuccess: (data) => {
183
+ // Immediate callback
184
+ showToast('Deposit initiated!');
185
+ },
186
+ onError: (error) => {
187
+ // Immediate callback
188
+ showToast('Error: ' + error.message);
189
+ },
190
+ });
191
+
192
+ // Later handling
193
+ depositPromise
194
+ .then(result => console.log('Completed:', result))
195
+ .catch(error => console.error('Failed:', error));
196
+ ```
197
+
198
+ ## Features
199
+
200
+ - ✅ **Multi-chain Support** - Ethereum, Polygon, Arbitrum, Optimism, Solana, Bitcoin
201
+ - ✅ **Fiat Onramp** - Credit/debit card purchases via Meld
202
+ - ✅ **Auto-swap** - Automatic token conversion
203
+ - ✅ **QR Codes** - Mobile wallet support
204
+ - ✅ **Deposit Tracking** - Real-time status updates
205
+ - ✅ **TypeScript** - Full type definitions
206
+ - ✅ **SSR-safe** - Works with Next.js and other frameworks
207
+ - ✅ **Customizable** - Configure per-transaction
208
+
209
+ ## Advanced Usage
210
+
211
+ ### Pattern 1: Promise-based (Recommended for Modern Apps)
212
+
213
+ Use async/await for clean, linear code flow:
214
+
215
+ ```tsx
216
+ const { beginDeposit } = useUnifold();
217
+
218
+ const handleDeposit = async () => {
219
+ try {
220
+ const result = await beginDeposit({
221
+ destinationChainId: '137',
222
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
223
+ destinationTokenSymbol: 'USDC',
224
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
225
+ });
226
+
227
+ // Update your UI state
228
+ setDepositStatus('completed');
229
+ showSuccessMessage(result.message);
230
+ } catch (error) {
231
+ // Handle errors
232
+ setDepositStatus('failed');
233
+ showErrorMessage(error.message);
234
+ }
235
+ };
236
+ ```
237
+
238
+ ### Pattern 2: Callback-based (Fire-and-Forget)
239
+
240
+ Use callbacks for immediate side effects without awaiting:
241
+
242
+ ```tsx
243
+ const { beginDeposit } = useUnifold();
244
+
245
+ const handleDeposit = () => {
246
+ beginDeposit({
247
+ destinationChainId: '137',
248
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
249
+ destinationTokenSymbol: 'USDC',
250
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
251
+ onSuccess: (data) => {
252
+ console.log('Deposit initiated:', data);
253
+ showToast('Deposit in progress!');
254
+ },
255
+ onError: (error) => {
256
+ console.error('Deposit failed:', error);
257
+ showToast('Error: ' + error.message);
258
+ },
259
+ });
260
+
261
+ // Code continues immediately without waiting
262
+ console.log('Deposit modal opened');
263
+ };
264
+ ```
265
+
266
+ ### Pattern 3: Hybrid (Promise + Callbacks)
267
+
268
+ Combine both for maximum flexibility:
269
+
270
+ ```tsx
271
+ const { beginDeposit } = useUnifold();
272
+
273
+ const handleDeposit = async () => {
274
+ try {
275
+ // Get the promise
276
+ const depositPromise = beginDeposit({
277
+ destinationChainId: '137',
278
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
279
+ destinationTokenSymbol: 'USDC',
280
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
281
+
282
+ // Immediate callbacks for real-time feedback
283
+ onSuccess: (data) => {
284
+ showToast('Deposit detected!');
285
+ trackAnalyticsEvent('deposit_initiated', data);
286
+ },
287
+ onError: (error) => {
288
+ showToast('Error: ' + error.message);
289
+ },
290
+ });
291
+
292
+ // Continue other work immediately
293
+ setModalOpen(true);
294
+
295
+ // Await the final result
296
+ const result = await depositPromise;
297
+
298
+ // Update final state
299
+ setDepositComplete(true);
300
+ navigateToSuccessPage(result);
301
+ } catch (error) {
302
+ setDepositError(error);
303
+ }
304
+ };
305
+ ```
306
+
307
+ ### Programmatic Control
308
+
309
+ Close the modal programmatically:
310
+
311
+ ```tsx
312
+ const { beginDeposit, closeDeposit } = useUnifold();
313
+
314
+ // Open modal
315
+ beginDeposit({
316
+ destinationChainId: '137',
317
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
318
+ destinationTokenSymbol: 'USDC',
319
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
320
+ });
321
+
322
+ // Close modal after 10 seconds
323
+ setTimeout(() => {
324
+ closeDeposit();
325
+ }, 10000);
326
+ ```
327
+
328
+ **Note:** Closing the modal via `closeDeposit()` or clicking outside will reject the promise with code `'DEPOSIT_CANCELLED'`.
329
+
330
+ ## TypeScript
331
+
332
+ Full TypeScript support with type definitions included:
333
+
334
+ ```tsx
335
+ import {
336
+ UnifoldProvider,
337
+ useUnifold,
338
+ DepositConfig,
339
+ UnifoldConnectProviderConfig
340
+ } from '@unifold/connect-react';
341
+
342
+ // Provider config
343
+ const providerConfig: UnifoldConnectProviderConfig = {
344
+ publishableKey: 'pk_test_...',
345
+ config: {
346
+ modalTitle: 'Deposit',
347
+ hideDepositTracker: false,
348
+ },
349
+ };
350
+
351
+ // Deposit config
352
+ const depositConfig: DepositConfig = {
353
+ destinationChainId: '137',
354
+ destinationTokenAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
355
+ destinationTokenSymbol: 'USDC',
356
+ recipientAddress: '0x606C49ca2Fa4982F07016265040F777eD3DA3160',
357
+ onSuccess: (data) => console.log(data),
358
+ // TypeScript will validate all fields
359
+ };
360
+
361
+ const { beginDeposit } = useUnifold();
362
+ beginDeposit(depositConfig);
363
+ ```
364
+
365
+ ## Architecture
366
+
367
+ `@unifold/connect-react` is built on a clean, modular architecture:
368
+
369
+ ```
370
+ @unifold/connect-react (this package)
371
+ ├─ @unifold/react-provider (base context)
372
+ └─ @unifold/ui-react (UI components)
373
+ ```
374
+
375
+ ## License
376
+
377
+ MIT
378
+
379
+ ## Support
380
+
381
+ - 📧 Email: support@unifold.io
382
+ - 🐛 Issues: [GitHub Issues](https://github.com/unifold/unifold-platform/issues)
383
+ - 📖 Documentation: [unifold.io/docs](https://unifold.io/docs)
Binary file
Binary file
Binary file
Binary file
package/dist/index.css ADDED
@@ -0,0 +1 @@
1
+ /* src/styles.css */
@@ -0,0 +1,40 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+ export { Button, ButtonProps } from '@unifold/ui-react';
4
+
5
+ interface UnifoldConnectProviderConfig {
6
+ publishableKey: string;
7
+ config?: {
8
+ modalTitle?: string;
9
+ hideDepositTracker?: boolean;
10
+ /** Theme appearance: 'light', 'dark', or 'auto' (system preference). Defaults to 'dark' */
11
+ appearance?: 'light' | 'dark' | 'auto';
12
+ };
13
+ }
14
+ interface DepositResult {
15
+ message: string;
16
+ transaction?: unknown;
17
+ executionId?: string;
18
+ }
19
+ interface DepositError {
20
+ message: string;
21
+ error?: unknown;
22
+ code?: string;
23
+ }
24
+ interface DepositConfig {
25
+ userId: string;
26
+ destinationChainId: string;
27
+ destinationTokenAddress: string;
28
+ destinationTokenSymbol: string;
29
+ recipientAddress: string;
30
+ onSuccess?: (data: DepositResult) => void;
31
+ onError?: (error: DepositError) => void;
32
+ }
33
+ declare function UnifoldProvider({ children, publishableKey, config, }: React.PropsWithChildren<UnifoldConnectProviderConfig>): react_jsx_runtime.JSX.Element;
34
+ declare function useUnifold(): {
35
+ publishableKey: string;
36
+ beginDeposit: (config: DepositConfig) => Promise<DepositResult>;
37
+ closeDeposit: () => void;
38
+ };
39
+
40
+ export { type DepositConfig, type DepositError, type DepositResult, type UnifoldConnectProviderConfig, UnifoldProvider, useUnifold };
@@ -0,0 +1,40 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+ export { Button, ButtonProps } from '@unifold/ui-react';
4
+
5
+ interface UnifoldConnectProviderConfig {
6
+ publishableKey: string;
7
+ config?: {
8
+ modalTitle?: string;
9
+ hideDepositTracker?: boolean;
10
+ /** Theme appearance: 'light', 'dark', or 'auto' (system preference). Defaults to 'dark' */
11
+ appearance?: 'light' | 'dark' | 'auto';
12
+ };
13
+ }
14
+ interface DepositResult {
15
+ message: string;
16
+ transaction?: unknown;
17
+ executionId?: string;
18
+ }
19
+ interface DepositError {
20
+ message: string;
21
+ error?: unknown;
22
+ code?: string;
23
+ }
24
+ interface DepositConfig {
25
+ userId: string;
26
+ destinationChainId: string;
27
+ destinationTokenAddress: string;
28
+ destinationTokenSymbol: string;
29
+ recipientAddress: string;
30
+ onSuccess?: (data: DepositResult) => void;
31
+ onError?: (error: DepositError) => void;
32
+ }
33
+ declare function UnifoldProvider({ children, publishableKey, config, }: React.PropsWithChildren<UnifoldConnectProviderConfig>): react_jsx_runtime.JSX.Element;
34
+ declare function useUnifold(): {
35
+ publishableKey: string;
36
+ beginDeposit: (config: DepositConfig) => Promise<DepositResult>;
37
+ closeDeposit: () => void;
38
+ };
39
+
40
+ export { type DepositConfig, type DepositError, type DepositResult, type UnifoldConnectProviderConfig, UnifoldProvider, useUnifold };