@diviswap/sdk 1.7.6

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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +510 -0
  3. package/bin/create-diviswap-app.js +25 -0
  4. package/bin/diviswap-sdk.js +4 -0
  5. package/dist/cli/index.js +1888 -0
  6. package/dist/cli/templates/nextjs-app/actions.ts.hbs +259 -0
  7. package/dist/cli/templates/nextjs-app/api-hooks.ts.hbs +439 -0
  8. package/dist/cli/templates/nextjs-app/api-route.ts.hbs +502 -0
  9. package/dist/cli/templates/nextjs-app/auth-context.tsx.hbs +59 -0
  10. package/dist/cli/templates/nextjs-app/client.ts.hbs +116 -0
  11. package/dist/cli/templates/nextjs-app/dashboard-hooks.ts.hbs +180 -0
  12. package/dist/cli/templates/nextjs-app/example-page.tsx.hbs +276 -0
  13. package/dist/cli/templates/nextjs-app/hooks.ts.hbs +252 -0
  14. package/dist/cli/templates/nextjs-app/kyc-hooks.ts.hbs +87 -0
  15. package/dist/cli/templates/nextjs-app/kyc-wizard.css.hbs +433 -0
  16. package/dist/cli/templates/nextjs-app/kyc-wizard.tsx.hbs +711 -0
  17. package/dist/cli/templates/nextjs-app/layout-wrapper.tsx.hbs +13 -0
  18. package/dist/cli/templates/nextjs-app/layout.tsx.hbs +13 -0
  19. package/dist/cli/templates/nextjs-app/middleware.ts.hbs +49 -0
  20. package/dist/cli/templates/nextjs-app/provider-wrapper.tsx.hbs +8 -0
  21. package/dist/cli/templates/nextjs-app/provider.tsx.hbs +408 -0
  22. package/dist/cli/templates/nextjs-app/setup-provider.tsx.hbs +25 -0
  23. package/dist/cli/templates/nextjs-app/types.ts.hbs +159 -0
  24. package/dist/cli/templates/react/api-client-wrapper.ts.hbs +89 -0
  25. package/dist/cli/templates/react/example.tsx.hbs +69 -0
  26. package/dist/cli/templates/react/tanstack-hooks.ts.hbs +185 -0
  27. package/dist/cli/templates/webhooks/nextjs.hbs +98 -0
  28. package/dist/index.d.mts +91 -0
  29. package/dist/index.d.ts +91 -0
  30. package/dist/index.js +2339 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/index.mjs +2313 -0
  33. package/dist/index.mjs.map +1 -0
  34. package/dist/react/index.d.mts +192 -0
  35. package/dist/react/index.d.ts +192 -0
  36. package/dist/react/index.js +1083 -0
  37. package/dist/react/index.js.map +1 -0
  38. package/dist/react/index.mjs +1064 -0
  39. package/dist/react/index.mjs.map +1 -0
  40. package/dist/wallet-BEGvzNtB.d.mts +1614 -0
  41. package/dist/wallet-BEGvzNtB.d.ts +1614 -0
  42. package/package.json +102 -0
  43. package/src/cli/templates/index.ts +65 -0
  44. package/src/cli/templates/nextjs-app/actions.ts.hbs +259 -0
  45. package/src/cli/templates/nextjs-app/api-hooks.ts.hbs +439 -0
  46. package/src/cli/templates/nextjs-app/api-route.ts.hbs +502 -0
  47. package/src/cli/templates/nextjs-app/auth-context.tsx.hbs +59 -0
  48. package/src/cli/templates/nextjs-app/client.ts.hbs +116 -0
  49. package/src/cli/templates/nextjs-app/dashboard-hooks.ts.hbs +180 -0
  50. package/src/cli/templates/nextjs-app/example-page.tsx.hbs +276 -0
  51. package/src/cli/templates/nextjs-app/hooks.ts.hbs +252 -0
  52. package/src/cli/templates/nextjs-app/kyc-hooks.ts.hbs +87 -0
  53. package/src/cli/templates/nextjs-app/kyc-wizard.css.hbs +433 -0
  54. package/src/cli/templates/nextjs-app/kyc-wizard.tsx.hbs +711 -0
  55. package/src/cli/templates/nextjs-app/layout-wrapper.tsx.hbs +13 -0
  56. package/src/cli/templates/nextjs-app/layout.tsx.hbs +13 -0
  57. package/src/cli/templates/nextjs-app/middleware.ts.hbs +49 -0
  58. package/src/cli/templates/nextjs-app/provider-wrapper.tsx.hbs +8 -0
  59. package/src/cli/templates/nextjs-app/provider.tsx.hbs +408 -0
  60. package/src/cli/templates/nextjs-app/setup-provider.tsx.hbs +25 -0
  61. package/src/cli/templates/nextjs-app/types.ts.hbs +159 -0
  62. package/src/cli/templates/react/api-client-wrapper.ts.hbs +89 -0
  63. package/src/cli/templates/react/example.tsx.hbs +69 -0
  64. package/src/cli/templates/react/tanstack-hooks.ts.hbs +185 -0
  65. package/src/cli/templates/shared/client.ts +78 -0
  66. package/src/cli/templates/webhooks/nextjs.hbs +98 -0
@@ -0,0 +1,252 @@
1
+ import { useState, useEffect, useCallback } from 'react';
2
+ import { useDiviswap } from '{{providerImportPath}}';
3
+ import type { Transaction, Payee, TransactionStatus } from '{{typesImportPath}}';
4
+
5
+ // IMPORTANT: These hooks use direct SDK calls which may not work properly
6
+ // with partner authentication. For partner authentication, use the API route
7
+ // pattern demonstrated in api-hooks.ts instead.
8
+
9
+ // Re-export the main hook
10
+ export { useDiviswap };
11
+
12
+ // Convenience hook for authentication
13
+ export function useAuth() {
14
+ const { user, login, logout, register, loading, error } = useDiviswap();
15
+
16
+ return {
17
+ user,
18
+ login,
19
+ logout,
20
+ register,
21
+ loading,
22
+ error,
23
+ isAuthenticated: !!user,
24
+ isLoading: loading,
25
+ };
26
+ }
27
+
28
+ // Hook for managing transactions
29
+ export function useTransactions() {
30
+ const { createTransaction, getTransactions, error } = useDiviswap();
31
+ const [transactions, setTransactions] = useState<Transaction[]>([]);
32
+ const [loading, setLoading] = useState(false);
33
+
34
+ const fetchTransactions = useCallback(async (filters?: {
35
+ limit?: number;
36
+ offset?: number;
37
+ status?: TransactionStatus;
38
+ type?: 'onramp' | 'offramp';
39
+ }) => {
40
+ setLoading(true);
41
+ try {
42
+ const data = await getTransactions(filters);
43
+ setTransactions(data);
44
+ return data;
45
+ } catch (err) {
46
+ console.error('Failed to fetch transactions:', err);
47
+ throw err;
48
+ } finally {
49
+ setLoading(false);
50
+ }
51
+ }, [getTransactions]);
52
+
53
+ const create = useCallback(async (data: any) => {
54
+ setLoading(true);
55
+ try {
56
+ const transaction = await createTransaction(data);
57
+ // Refresh transactions list
58
+ await fetchTransactions();
59
+ return transaction;
60
+ } catch (err) {
61
+ console.error('Failed to create transaction:', err);
62
+ throw err;
63
+ } finally {
64
+ setLoading(false);
65
+ }
66
+ }, [createTransaction, fetchTransactions]);
67
+
68
+ return {
69
+ transactions,
70
+ loading,
71
+ error,
72
+ create,
73
+ refresh: fetchTransactions,
74
+ };
75
+ }
76
+
77
+ // Hook for managing payees
78
+ export function usePayees() {
79
+ const { getPayees, createPayee, deletePayee, error } = useDiviswap();
80
+ const [payees, setPayees] = useState<Payee[]>([]);
81
+ const [loading, setLoading] = useState(false);
82
+
83
+ const fetchPayees = useCallback(async () => {
84
+ setLoading(true);
85
+ try {
86
+ const data = await getPayees();
87
+ setPayees(data);
88
+ return data;
89
+ } catch (err) {
90
+ console.error('Failed to fetch payees:', err);
91
+ throw err;
92
+ } finally {
93
+ setLoading(false);
94
+ }
95
+ }, [getPayees]);
96
+
97
+ const create = useCallback(async (data: any) => {
98
+ setLoading(true);
99
+ try {
100
+ const payee = await createPayee(data);
101
+ // Refresh payees list
102
+ await fetchPayees();
103
+ return payee;
104
+ } catch (err) {
105
+ console.error('Failed to create payee:', err);
106
+ throw err;
107
+ } finally {
108
+ setLoading(false);
109
+ }
110
+ }, [createPayee, fetchPayees]);
111
+
112
+ const remove = useCallback(async (id: string) => {
113
+ setLoading(true);
114
+ try {
115
+ await deletePayee(id);
116
+ // Refresh payees list
117
+ await fetchPayees();
118
+ } catch (err) {
119
+ console.error('Failed to delete payee:', err);
120
+ throw err;
121
+ } finally {
122
+ setLoading(false);
123
+ }
124
+ }, [deletePayee, fetchPayees]);
125
+
126
+ // Auto-load payees on mount
127
+ useEffect(() => {
128
+ fetchPayees();
129
+ }, [fetchPayees]);
130
+
131
+ return {
132
+ payees,
133
+ loading,
134
+ error,
135
+ create,
136
+ remove,
137
+ refresh: fetchPayees,
138
+ };
139
+ }
140
+
141
+ {{#if (includes features "fees")}}
142
+ // Hook for fee calculations
143
+ export function useFeeCalculator() {
144
+ const { calculateFees } = useDiviswap();
145
+ const [fees, setFees] = useState<any>(null);
146
+ const [loading, setLoading] = useState(false);
147
+ const [error, setError] = useState<Error | null>(null);
148
+
149
+ const calculate = useCallback(async (
150
+ amount: number,
151
+ type: 'onramp' | 'offramp',
152
+ currency: string = 'USD'
153
+ ) => {
154
+ if (amount <= 0) {
155
+ setFees(null);
156
+ return null;
157
+ }
158
+
159
+ setLoading(true);
160
+ setError(null);
161
+
162
+ try {
163
+ const calculatedFees = await calculateFees(amount, type, currency);
164
+ setFees(calculatedFees);
165
+ return calculatedFees;
166
+ } catch (err) {
167
+ setError(err as Error);
168
+ setFees(null);
169
+ throw err;
170
+ } finally {
171
+ setLoading(false);
172
+ }
173
+ }, [calculateFees]);
174
+
175
+ return {
176
+ fees,
177
+ loading,
178
+ error,
179
+ calculate,
180
+ };
181
+ }
182
+ {{/if}}
183
+
184
+ {{#if (includes features "realtime")}}
185
+ // Hook for real-time transaction updates
186
+ export function useTransactionUpdates(transactionId?: string) {
187
+ const { socket, subscribeToTransaction, unsubscribeFromTransaction } = useDiviswap();
188
+ const [updates, setUpdates] = useState<any[]>([]);
189
+ const [latestStatus, setLatestStatus] = useState<TransactionStatus | null>(null);
190
+
191
+ useEffect(() => {
192
+ if (!socket || !transactionId) return;
193
+
194
+ // Subscribe to updates
195
+ subscribeToTransaction(transactionId);
196
+
197
+ // Listen for updates
198
+ const handleUpdate = (data: any) => {
199
+ if (data.transactionId === transactionId) {
200
+ setUpdates(prev => [...prev, data]);
201
+ if (data.status) {
202
+ setLatestStatus(data.status);
203
+ }
204
+ }
205
+ };
206
+
207
+ socket.on('transaction:update', handleUpdate);
208
+
209
+ // Cleanup
210
+ return () => {
211
+ unsubscribeFromTransaction(transactionId);
212
+ socket.off('transaction:update', handleUpdate);
213
+ };
214
+ }, [socket, transactionId, subscribeToTransaction, unsubscribeFromTransaction]);
215
+
216
+ return {
217
+ updates,
218
+ latestStatus,
219
+ isConnected: !!socket?.connected,
220
+ };
221
+ }
222
+ {{/if}}
223
+
224
+ // Hook for handling async operations with loading states
225
+ export function useAsyncOperation<T extends (...args: any[]) => Promise<any>>() {
226
+ const [loading, setLoading] = useState(false);
227
+ const [error, setError] = useState<Error | null>(null);
228
+
229
+ const execute = useCallback(async <TResult>(
230
+ operation: T,
231
+ ...args: Parameters<T>
232
+ ): Promise<TResult | undefined> => {
233
+ setLoading(true);
234
+ setError(null);
235
+
236
+ try {
237
+ const result = await operation(...args);
238
+ return result as TResult;
239
+ } catch (err) {
240
+ setError(err as Error);
241
+ throw err;
242
+ } finally {
243
+ setLoading(false);
244
+ }
245
+ }, []);
246
+
247
+ return {
248
+ loading,
249
+ error,
250
+ execute,
251
+ };
252
+ }
@@ -0,0 +1,87 @@
1
+ "use client";
2
+
3
+ import { useState } from 'react';
4
+ import { use{{prefix}}Auth } from '~/context/diviswap-auth-context';
5
+
6
+ interface KYCFormData {
7
+ firstName: string;
8
+ lastName: string;
9
+ email: string;
10
+ dobYear: string;
11
+ dobMonth: string;
12
+ dobDay: string;
13
+ idType: 'ssn' | 'tin' | 'passport';
14
+ idNumber: string;
15
+ addressLine1: string;
16
+ addressLine2?: string;
17
+ city: string;
18
+ state: string;
19
+ postalCode: string;
20
+ country: string;
21
+ }
22
+
23
+ export function use{{prefix}}KYC() {
24
+ const { customerId, customerEmail } = use{{prefix}}Auth();
25
+ const [isSubmitting, setIsSubmitting] = useState(false);
26
+
27
+ const submitKYC = async (formData: KYCFormData) => {
28
+ setIsSubmitting(true);
29
+
30
+ try {
31
+ // Format date of birth
32
+ const dateOfBirth = `${formData.dobYear}-${formData.dobMonth.padStart(2, '0')}-${formData.dobDay.padStart(2, '0')}`;
33
+
34
+ // Prepare KYC data
35
+ const kycData = {
36
+ personalInfo: {
37
+ firstName: formData.firstName,
38
+ lastName: formData.lastName,
39
+ email: formData.email,
40
+ dateOfBirth,
41
+ ssn: formData.idType === 'ssn' ? formData.idNumber.replace(/-/g, '') : undefined,
42
+ tin: formData.idType === 'tin' ? formData.idNumber : undefined,
43
+ passport: formData.idType === 'passport' ? formData.idNumber : undefined,
44
+ },
45
+ address: {
46
+ addressLine1: formData.addressLine1,
47
+ addressLine2: formData.addressLine2,
48
+ city: formData.city,
49
+ state: formData.state,
50
+ postalCode: formData.postalCode,
51
+ country: formData.country,
52
+ }
53
+ };
54
+
55
+ // Call API to submit KYC
56
+ const response = await fetch('/api/diviswap', {
57
+ method: 'POST',
58
+ headers: { 'Content-Type': 'application/json' },
59
+ body: JSON.stringify({
60
+ action: 'submitKYC',
61
+ customerId,
62
+ customerEmail,
63
+ ...kycData
64
+ }),
65
+ });
66
+
67
+ if (!response.ok) {
68
+ const error = await response.json();
69
+ throw new Error(error.error || 'KYC submission failed');
70
+ }
71
+
72
+ const result = await response.json();
73
+ return result;
74
+
75
+ } catch (error: any) {
76
+ console.error('KYC submission error:', error);
77
+ throw error;
78
+ } finally {
79
+ setIsSubmitting(false);
80
+ }
81
+ };
82
+
83
+ return {
84
+ submitKYC,
85
+ isSubmitting
86
+ };
87
+ }