@0xobelisk/react 1.2.0-pre.100

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.
@@ -0,0 +1,389 @@
1
+ import { Dubhe, SuiMoveNormalizedModules } from '@0xobelisk/sui-client';
2
+ import { DubheGraphqlClient } from '@0xobelisk/graphql-client';
3
+ import { DubheECSWorld } from '@0xobelisk/ecs';
4
+ import { DubheGrpcClient } from '@0xobelisk/grpc-client';
5
+ import * as react_jsx_runtime from 'react/jsx-runtime';
6
+ import { ReactNode } from 'react';
7
+
8
+ /**
9
+ * Network type
10
+ */
11
+ type NetworkType = 'mainnet' | 'testnet' | 'devnet' | 'localnet';
12
+ /**
13
+ * Modern Dubhe client configuration for auto-initialization
14
+ * Aligned with @0xobelisk/client configuration for consistency
15
+ */
16
+ interface DubheConfig {
17
+ /** Network type */
18
+ network: NetworkType;
19
+ /** Contract package ID */
20
+ packageId: string;
21
+ /** Contract metadata (required for contract instantiation) */
22
+ metadata: any;
23
+ /** Dubhe Schema ID (optional, for enhanced features) */
24
+ dubheSchemaId?: string;
25
+ /** Dubhe metadata (enables GraphQL/ECS features) */
26
+ dubheMetadata?: any;
27
+ /** Authentication credentials */
28
+ credentials?: {
29
+ /** Private key (base64 or hex string) */
30
+ secretKey?: string;
31
+ /** Mnemonic phrase (12 or 24 words) */
32
+ mnemonics?: string;
33
+ };
34
+ /** Service endpoints configuration */
35
+ endpoints?: {
36
+ /** Full node RPC URLs (can provide multiple for redundancy) */
37
+ fullnodeUrls?: string[];
38
+ /** GraphQL endpoint URL */
39
+ graphql?: string;
40
+ /** WebSocket endpoint URL for subscriptions */
41
+ websocket?: string;
42
+ /** gRPC endpoint URL */
43
+ grpc?: string;
44
+ /** Dubhe Channel API URL */
45
+ channelUrl?: string;
46
+ };
47
+ /** Performance and behavior options */
48
+ options?: {
49
+ /** Enable batch query optimization */
50
+ enableBatchOptimization?: boolean;
51
+ /** Default cache timeout (milliseconds) */
52
+ cacheTimeout?: number;
53
+ /** Request debounce delay (milliseconds) */
54
+ debounceMs?: number;
55
+ /** Auto-reconnect on WebSocket errors */
56
+ reconnectOnError?: boolean;
57
+ };
58
+ }
59
+ /**
60
+ * Type alias for consistency with @0xobelisk/client package
61
+ */
62
+ type ClientConfig = DubheConfig;
63
+ /**
64
+ * Return type for the main useDubhe hook
65
+ */
66
+ interface DubheReturn {
67
+ /** Dubhe contract instance */
68
+ contract: Dubhe;
69
+ /** GraphQL client (always available, uses default localhost endpoint if not configured) */
70
+ graphqlClient: DubheGraphqlClient;
71
+ /** gRPC client (always available, uses default localhost endpoint if not configured) */
72
+ grpcClient: DubheGrpcClient;
73
+ /** ECS World instance (always available, depends on GraphQL client) */
74
+ ecsWorld: DubheECSWorld;
75
+ /** Contract metadata */
76
+ metadata: SuiMoveNormalizedModules;
77
+ /** Network type */
78
+ network: NetworkType;
79
+ /** Package ID */
80
+ packageId: string;
81
+ /** Dubhe Schema ID (if provided) */
82
+ dubheSchemaId?: string;
83
+ /** User address */
84
+ address: string;
85
+ /** Configuration options used */
86
+ options?: {
87
+ enableBatchOptimization?: boolean;
88
+ cacheTimeout?: number;
89
+ debounceMs?: number;
90
+ reconnectOnError?: boolean;
91
+ };
92
+ /** Performance metrics */
93
+ metrics?: {
94
+ initTime?: number;
95
+ requestCount?: number;
96
+ lastActivity?: number;
97
+ };
98
+ }
99
+ /**
100
+ * Compatibility alias for DubheReturn
101
+ * @deprecated Use DubheReturn instead for better consistency
102
+ */
103
+ type ContractReturn = DubheReturn;
104
+
105
+ /**
106
+ * Configuration Management for Dubhe React Integration
107
+ *
108
+ * Features:
109
+ * - Type-safe configuration interface
110
+ * - Configuration validation and error handling
111
+ * - Smart merging of defaults and explicit config
112
+ * - No environment variable handling (developers should handle environment variables themselves)
113
+ */
114
+
115
+ /**
116
+ * Default configuration object with sensible defaults
117
+ */
118
+ declare const DEFAULT_CONFIG: Partial<DubheConfig>;
119
+ /**
120
+ * Configuration Hook: useDubheConfig
121
+ *
122
+ * Merges defaults with explicit configuration provided by the developer
123
+ *
124
+ * Note: Environment variables should be handled by the developer before passing to this hook
125
+ *
126
+ * @param config - Complete or partial configuration object
127
+ * @returns Complete, validated DubheConfig
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * // Basic usage with explicit config
132
+ * const config = useDubheConfig({
133
+ * network: 'testnet',
134
+ * packageId: '0x123...',
135
+ * metadata: contractMetadata,
136
+ * credentials: {
137
+ * secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY // Handle env vars yourself
138
+ * }
139
+ * });
140
+ *
141
+ * // With helper function to handle environment variables
142
+ * const getConfigFromEnv = () => ({
143
+ * network: process.env.NEXT_PUBLIC_NETWORK as NetworkType,
144
+ * packageId: process.env.NEXT_PUBLIC_PACKAGE_ID,
145
+ * credentials: {
146
+ * secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY
147
+ * }
148
+ * });
149
+ *
150
+ * const config = useDubheConfig({
151
+ * ...getConfigFromEnv(),
152
+ * metadata: contractMetadata
153
+ * });
154
+ * ```
155
+ */
156
+ declare function useDubheConfig(config: Partial<DubheConfig>): DubheConfig;
157
+
158
+ /**
159
+ * Utility Functions for Dubhe Configuration Management
160
+ *
161
+ * Features:
162
+ * - Configuration validation and error handling
163
+ * - Smart configuration merging with proper type safety
164
+ * - Type-safe configuration validation
165
+ */
166
+
167
+ /**
168
+ * Merge multiple configuration objects with proper deep merging
169
+ * Later configurations override earlier ones
170
+ *
171
+ * @param baseConfig - Base configuration (usually defaults)
172
+ * @param overrideConfig - Override configuration (user provided)
173
+ * @returns Merged configuration
174
+ */
175
+ declare function mergeConfigurations(baseConfig: Partial<DubheConfig>, overrideConfig?: Partial<DubheConfig>): Partial<DubheConfig>;
176
+ /**
177
+ * Validate configuration and ensure required fields are present
178
+ * Throws descriptive errors for missing required fields
179
+ *
180
+ * @param config - Configuration to validate
181
+ * @returns Validated and typed configuration
182
+ * @throws Error if required fields are missing or invalid
183
+ */
184
+ declare function validateConfig(config: Partial<DubheConfig>): DubheConfig;
185
+ /**
186
+ * Generate a configuration summary for debugging
187
+ * Hides sensitive information like private keys
188
+ *
189
+ * @param config - Configuration to summarize
190
+ * @returns Safe configuration summary
191
+ */
192
+ declare function getConfigSummary(config: DubheConfig): object;
193
+
194
+ /**
195
+ * Props interface for DubheProvider component
196
+ */
197
+ interface DubheProviderProps {
198
+ /** Configuration for Dubhe initialization */
199
+ config: Partial<DubheConfig>;
200
+ /** Child components that will have access to Dubhe clients */
201
+ children: ReactNode;
202
+ }
203
+ /**
204
+ * DubheProvider Component - useRef Pattern Implementation
205
+ *
206
+ * This Provider uses useRef to store client instances, ensuring they are:
207
+ * 1. Created only once during component lifecycle
208
+ * 2. Persisted across re-renders without re-initialization
209
+ * 3. Shared efficiently via React Context
210
+ *
211
+ * Key advantages over useMemo:
212
+ * - useRef guarantees single initialization (useMemo can re-run on dependency changes)
213
+ * - No dependency array needed (eliminates potential re-initialization bugs)
214
+ * - Better performance for heavy client objects
215
+ * - Clearer separation of concerns via Provider pattern
216
+ *
217
+ * @param props - Provider props containing config and children
218
+ * @returns Provider component wrapping children with Dubhe context
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * // App root setup
223
+ * function App() {
224
+ * const dubheConfig = {
225
+ * network: 'devnet',
226
+ * packageId: '0x123...',
227
+ * metadata: contractMetadata,
228
+ * credentials: {
229
+ * secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY
230
+ * }
231
+ * };
232
+ *
233
+ * return (
234
+ * <DubheProvider config={dubheConfig}>
235
+ * <MyApplication />
236
+ * </DubheProvider>
237
+ * );
238
+ * }
239
+ * ```
240
+ */
241
+ declare function DubheProvider({ config, children }: DubheProviderProps): react_jsx_runtime.JSX.Element;
242
+
243
+ /**
244
+ * Primary Hook: useDubhe
245
+ *
246
+ * Uses Provider pattern to access shared Dubhe clients with guaranteed single initialization.
247
+ * Must be used within a DubheProvider.
248
+ *
249
+ * @returns Complete Dubhe ecosystem with contract, GraphQL, ECS, and metadata
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * // App setup with Provider
254
+ * function App() {
255
+ * const config = {
256
+ * network: 'devnet',
257
+ * packageId: '0x123...',
258
+ * metadata: contractMetadata,
259
+ * credentials: {
260
+ * secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY
261
+ * }
262
+ * };
263
+ *
264
+ * return (
265
+ * <DubheProvider config={config}>
266
+ * <MyDApp />
267
+ * </DubheProvider>
268
+ * );
269
+ * }
270
+ *
271
+ * // Component usage
272
+ * function MyDApp() {
273
+ * const { contract, address } = useDubhe();
274
+ * return <div>Connected as {address}</div>;
275
+ * }
276
+ * ```
277
+ */
278
+ declare function useDubhe(): DubheReturn;
279
+ /**
280
+ * Individual Instance Hook: useDubheContract
281
+ *
282
+ * Returns only the Dubhe contract instance from Provider context.
283
+ * More efficient than useDubhe() when only contract access is needed.
284
+ *
285
+ * @returns Dubhe contract instance
286
+ *
287
+ * @example
288
+ * ```typescript
289
+ * function TransactionComponent() {
290
+ * const contract = useDubheContract();
291
+ *
292
+ * const handleTransaction = async () => {
293
+ * const tx = new Transaction();
294
+ * await contract.tx.my_system.my_method({ tx });
295
+ * };
296
+ *
297
+ * return <button onClick={handleTransaction}>Execute</button>;
298
+ * }
299
+ * ```
300
+ */
301
+ declare function useDubheContract(): Dubhe;
302
+ /**
303
+ * Individual Instance Hook: useDubheGraphQL
304
+ *
305
+ * Returns only the GraphQL client from Provider context.
306
+ * More efficient than useDubhe() when only GraphQL access is needed.
307
+ *
308
+ * @returns GraphQL client instance (always available with default localhost endpoint)
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * function DataComponent() {
313
+ * const graphqlClient = useDubheGraphQL();
314
+ *
315
+ * useEffect(() => {
316
+ * graphqlClient.query({ ... }).then(setData);
317
+ * }, [graphqlClient]);
318
+ *
319
+ * return <div>{data && JSON.stringify(data)}</div>;
320
+ * }
321
+ * ```
322
+ */
323
+ declare function useDubheGraphQL(): DubheGraphqlClient;
324
+ /**
325
+ * Individual Instance Hook: useDubheECS
326
+ *
327
+ * Returns only the ECS World instance from Provider context.
328
+ * More efficient than useDubhe() when only ECS access is needed.
329
+ *
330
+ * @returns ECS World instance (always available, depends on GraphQL client)
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * function ECSComponent() {
335
+ * const ecsWorld = useDubheECS();
336
+ *
337
+ * useEffect(() => {
338
+ * ecsWorld.getComponent('MyComponent').then(setComponent);
339
+ * }, [ecsWorld]);
340
+ *
341
+ * return <div>ECS Component Data</div>;
342
+ * }
343
+ * ```
344
+ */
345
+ declare function useDubheECS(): DubheECSWorld;
346
+ /**
347
+ * Hook for dynamic configuration updates
348
+ *
349
+ * Provides methods to update provider configuration at runtime
350
+ *
351
+ * @returns Object with updateConfig, resetClients methods and current config
352
+ *
353
+ * @example
354
+ * ```typescript
355
+ * function NetworkSwitcher() {
356
+ * const { updateConfig, config } = useDubheConfigUpdate();
357
+ *
358
+ * const switchToTestnet = () => {
359
+ * updateConfig({
360
+ * network: 'testnet',
361
+ * packageId: '0xTestnetPackageId...'
362
+ * });
363
+ * };
364
+ *
365
+ * return (
366
+ * <div>
367
+ * <p>Network: {config.network}</p>
368
+ * <button onClick={switchToTestnet}>Switch to Testnet</button>
369
+ * </div>
370
+ * );
371
+ * }
372
+ * ```
373
+ */
374
+ declare function useDubheConfigUpdate(): {
375
+ updateConfig: (newConfig: Partial<DubheConfig>) => void;
376
+ resetClients: (options?: {
377
+ resetContract?: boolean;
378
+ resetGraphql?: boolean;
379
+ resetGrpc?: boolean;
380
+ resetEcs?: boolean;
381
+ }) => void;
382
+ config: DubheConfig;
383
+ };
384
+ /**
385
+ * Compatibility alias for useDubhe
386
+ */
387
+ declare const useContract: typeof useDubhe;
388
+
389
+ export { type ClientConfig, type ContractReturn, DEFAULT_CONFIG, type DubheConfig, DubheProvider, type DubheReturn, type NetworkType, getConfigSummary, mergeConfigurations, useContract, useDubhe, useDubheConfig, useDubheConfigUpdate, useDubheContract, useDubheECS, useDubheGraphQL, validateConfig };