@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.
- package/LICENSE +92 -0
- package/README.md +605 -0
- package/dist/chunk-R3BJRALM.mjs +489 -0
- package/dist/chunk-R3BJRALM.mjs.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +519 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +29 -0
- package/dist/index.mjs.map +1 -0
- package/dist/sui/config.d.ts +52 -0
- package/dist/sui/hooks.d.ts +159 -0
- package/dist/sui/index.d.mts +389 -0
- package/dist/sui/index.d.ts +389 -0
- package/dist/sui/index.js +519 -0
- package/dist/sui/index.js.map +1 -0
- package/dist/sui/index.mjs +29 -0
- package/dist/sui/index.mjs.map +1 -0
- package/dist/sui/provider.d.ts +189 -0
- package/dist/sui/types.d.ts +100 -0
- package/dist/sui/utils.d.ts +35 -0
- package/package.json +104 -0
- package/src/index.ts +16 -0
- package/src/sui/config.ts +90 -0
- package/src/sui/hooks.ts +174 -0
- package/src/sui/index.ts +40 -0
- package/src/sui/provider.tsx +540 -0
- package/src/sui/types.ts +105 -0
- package/src/sui/utils.ts +213 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
4
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
5
|
+
|
|
6
|
+
-----------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
Parameters
|
|
9
|
+
|
|
10
|
+
Licensor: Obelisk Labs
|
|
11
|
+
|
|
12
|
+
Licensed Work: Dubhe
|
|
13
|
+
The Licensed Work is (c) 2023 Obelisk Labs
|
|
14
|
+
|
|
15
|
+
-----------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
Terms
|
|
18
|
+
|
|
19
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
20
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
21
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
22
|
+
production use.
|
|
23
|
+
|
|
24
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
25
|
+
available distribution of a specific version of the Licensed Work under this
|
|
26
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
27
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
28
|
+
above terminate.
|
|
29
|
+
|
|
30
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
31
|
+
currently in effect as described in this License, you must purchase a
|
|
32
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
33
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
34
|
+
|
|
35
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
36
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
37
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
38
|
+
for each version of the Licensed Work released by Licensor.
|
|
39
|
+
|
|
40
|
+
You must conspicuously display this License on each original or modified copy
|
|
41
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
42
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
43
|
+
License apply to your use of that work.
|
|
44
|
+
|
|
45
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
46
|
+
terminate your rights under this License for the current and all other
|
|
47
|
+
versions of the Licensed Work.
|
|
48
|
+
|
|
49
|
+
This License does not grant you any right in any trademark or logo of
|
|
50
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
51
|
+
Licensor as expressly required by this License).
|
|
52
|
+
|
|
53
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
54
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
55
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
56
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
57
|
+
TITLE.
|
|
58
|
+
|
|
59
|
+
MariaDB hereby grants you permission to use this License’s text to license
|
|
60
|
+
your works, and to refer to it using the trademark "Business Source License",
|
|
61
|
+
as long as you comply with the Covenants of Licensor below.
|
|
62
|
+
|
|
63
|
+
-----------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
Covenants of Licensor
|
|
66
|
+
|
|
67
|
+
In consideration of the right to use this License’s text and the "Business
|
|
68
|
+
Source License" name and trademark, Licensor covenants to MariaDB, and to all
|
|
69
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
70
|
+
|
|
71
|
+
1. To specify as the Change License the GPL Version 2.0 or any later version,
|
|
72
|
+
or a license that is compatible with GPL Version 2.0 or a later version,
|
|
73
|
+
where "compatible" means that software provided under the Change License can
|
|
74
|
+
be included in a program with software provided under GPL Version 2.0 or a
|
|
75
|
+
later version. Licensor may specify additional Change Licenses without
|
|
76
|
+
limitation.
|
|
77
|
+
|
|
78
|
+
2. To either: (a) specify an additional grant of rights to use that does not
|
|
79
|
+
impose any additional restriction on the right granted in this License, as
|
|
80
|
+
the Additional Use Grant; or (b) insert the text "None".
|
|
81
|
+
|
|
82
|
+
3. To specify a Change Date.
|
|
83
|
+
|
|
84
|
+
4. Not to modify this License in any other way.
|
|
85
|
+
|
|
86
|
+
-----------------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
Notice
|
|
89
|
+
|
|
90
|
+
The Business Source License (this document, or the "License") is not an Open
|
|
91
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
92
|
+
under an Open Source License, as stated in this License.
|
package/README.md
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
# @0xobelisk/react
|
|
2
|
+
|
|
3
|
+
Modern React integration for Dubhe framework with Provider pattern and multi-chain support.
|
|
4
|
+
|
|
5
|
+
## 🚀 Features
|
|
6
|
+
|
|
7
|
+
- **🎯 Provider Pattern** - Single initialization with React Context sharing
|
|
8
|
+
- **⚡ Performance Optimized** - useRef pattern prevents re-initialization
|
|
9
|
+
- **🛡️ Type Safety** - Complete TypeScript support with strict typing
|
|
10
|
+
- **🌐 Multi-Chain Ready** - Extensible architecture for multiple blockchains
|
|
11
|
+
- **📦 Individual Hooks** - Granular access to specific functionality
|
|
12
|
+
- **🔧 Configuration Driven** - Flexible setup with smart defaults
|
|
13
|
+
|
|
14
|
+
## 📦 Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# Core React package
|
|
18
|
+
npm install @0xobelisk/react
|
|
19
|
+
|
|
20
|
+
# Peer dependencies
|
|
21
|
+
npm install react react-dom @0xobelisk/sui-client zod
|
|
22
|
+
|
|
23
|
+
# Optional dependencies for enhanced features
|
|
24
|
+
npm install @0xobelisk/graphql-client @0xobelisk/ecs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Requirements
|
|
28
|
+
|
|
29
|
+
- **React**: 18.0.0+ or 19.0.0+
|
|
30
|
+
- **Node.js**: 18.0.0+
|
|
31
|
+
- **TypeScript**: 5.0+ (recommended)
|
|
32
|
+
|
|
33
|
+
## 🌐 Multi-Chain Support
|
|
34
|
+
|
|
35
|
+
| Blockchain | Status | Import Path |
|
|
36
|
+
| ---------- | -------------- | ------------------------- |
|
|
37
|
+
| **Sui** | ✅ Ready | `@0xobelisk/react/sui` |
|
|
38
|
+
| **Aptos** | 🚧 Coming Soon | `@0xobelisk/react/aptos` |
|
|
39
|
+
| **Initia** | 🚧 Coming Soon | `@0xobelisk/react/initia` |
|
|
40
|
+
|
|
41
|
+
## 🚀 Quick Start
|
|
42
|
+
|
|
43
|
+
### Basic Provider Setup
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import React from 'react';
|
|
47
|
+
import { DubheProvider, useDubhe } from '@0xobelisk/react/sui';
|
|
48
|
+
import { Transaction } from '@0xobelisk/sui-client';
|
|
49
|
+
import metadata from './contracts/metadata.json';
|
|
50
|
+
|
|
51
|
+
// App root with Provider
|
|
52
|
+
function App() {
|
|
53
|
+
const config = {
|
|
54
|
+
network: 'devnet' as const,
|
|
55
|
+
packageId: process.env.NEXT_PUBLIC_PACKAGE_ID!,
|
|
56
|
+
metadata,
|
|
57
|
+
credentials: {
|
|
58
|
+
secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY // ⚠️ LOCAL DEVELOPMENT ONLY // ⚠️ LOCAL DEVELOPMENT ONLY
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<DubheProvider config={config}>
|
|
64
|
+
<MyDApp />
|
|
65
|
+
</DubheProvider>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Component using shared clients
|
|
70
|
+
function MyDApp() {
|
|
71
|
+
const { contract, address, network } = useDubhe();
|
|
72
|
+
|
|
73
|
+
const handleTransaction = async () => {
|
|
74
|
+
try {
|
|
75
|
+
const tx = new Transaction();
|
|
76
|
+
const result = await contract.tx.counter_system.increment({ tx });
|
|
77
|
+
console.log('Success:', result.digest);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error('Transaction failed:', error);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<div>
|
|
85
|
+
<p>Connected: {address}</p>
|
|
86
|
+
<p>Network: {network}</p>
|
|
87
|
+
<button onClick={handleTransaction}>Execute Transaction</button>
|
|
88
|
+
</div>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## 🎯 Provider Pattern
|
|
94
|
+
|
|
95
|
+
### DubheProvider Component
|
|
96
|
+
|
|
97
|
+
The `DubheProvider` uses React Context with useRef pattern to ensure single client initialization:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { DubheProvider } from '@0xobelisk/react/sui';
|
|
101
|
+
import type { DubheConfig } from '@0xobelisk/react/sui';
|
|
102
|
+
|
|
103
|
+
function App() {
|
|
104
|
+
const config: DubheConfig = {
|
|
105
|
+
network: 'devnet',
|
|
106
|
+
packageId: '0x123...',
|
|
107
|
+
metadata: contractMetadata,
|
|
108
|
+
// Optional features
|
|
109
|
+
dubheSchemaId: '0xabc...', // Optional: Dubhe Schema ID
|
|
110
|
+
dubheMetadata: dubheConfigMetadata, // Enables GraphQL + ECS
|
|
111
|
+
credentials: {
|
|
112
|
+
secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY // ⚠️ LOCAL DEVELOPMENT ONLY // ⚠️ LOCAL DEVELOPMENT ONLY
|
|
113
|
+
},
|
|
114
|
+
endpoints: {
|
|
115
|
+
fullnodeUrls: ['https://fullnode.devnet.sui.io:443'], // Optional: Custom RPC endpoints
|
|
116
|
+
graphql: process.env.NEXT_PUBLIC_GRAPHQL_URL,
|
|
117
|
+
websocket: process.env.NEXT_PUBLIC_GRAPHQL_WS_URL,
|
|
118
|
+
grpc: 'http://localhost:8080' // Optional: Custom gRPC endpoint
|
|
119
|
+
},
|
|
120
|
+
options: {
|
|
121
|
+
enableBatchOptimization: true,
|
|
122
|
+
cacheTimeout: 5000,
|
|
123
|
+
debounceMs: 100,
|
|
124
|
+
reconnectOnError: true
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<DubheProvider config={config}>
|
|
130
|
+
<YourApp />
|
|
131
|
+
</DubheProvider>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Key Benefits of Provider Pattern
|
|
137
|
+
|
|
138
|
+
- **Single Initialization**: Clients created once using useRef
|
|
139
|
+
- **Context Sharing**: All components access same instances
|
|
140
|
+
- **No Re-renders**: useRef prevents unnecessary re-initialization
|
|
141
|
+
- **Type Safety**: Full TypeScript support across the tree
|
|
142
|
+
|
|
143
|
+
## 🪝 Hook Usage
|
|
144
|
+
|
|
145
|
+
### Primary Hook: useDubhe()
|
|
146
|
+
|
|
147
|
+
Access all Dubhe features in one hook:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { useDubhe } from '@0xobelisk/react/sui';
|
|
151
|
+
|
|
152
|
+
function MyComponent() {
|
|
153
|
+
const {
|
|
154
|
+
contract, // Dubhe contract instance
|
|
155
|
+
graphqlClient, // GraphQL client (if dubheMetadata provided)
|
|
156
|
+
ecsWorld, // ECS World (if GraphQL available)
|
|
157
|
+
address, // User address
|
|
158
|
+
network, // Current network
|
|
159
|
+
packageId, // Contract package ID
|
|
160
|
+
metadata // Contract metadata
|
|
161
|
+
} = useDubhe();
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<div>
|
|
165
|
+
<p>Contract: {contract ? '✅' : '❌'}</p>
|
|
166
|
+
<p>GraphQL: {graphqlClient ? '✅' : '❌'}</p>
|
|
167
|
+
<p>ECS: {ecsWorld ? '✅' : '❌'}</p>
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Individual Hooks
|
|
174
|
+
|
|
175
|
+
For components that only need specific functionality:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import React, { useEffect } from 'react';
|
|
179
|
+
import { useDubheContract, useDubheGraphQL, useDubheECS } from '@0xobelisk/react/sui';
|
|
180
|
+
import { Transaction } from '@0xobelisk/sui-client';
|
|
181
|
+
|
|
182
|
+
// Contract-only component
|
|
183
|
+
function TransactionComponent() {
|
|
184
|
+
const contract = useDubheContract();
|
|
185
|
+
|
|
186
|
+
const executeTransaction = async () => {
|
|
187
|
+
const tx = new Transaction();
|
|
188
|
+
await contract.tx.my_system.my_method({ tx });
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
return <button onClick={executeTransaction}>Execute</button>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// GraphQL-only component
|
|
195
|
+
function DataComponent() {
|
|
196
|
+
const graphqlClient = useDubheGraphQL();
|
|
197
|
+
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
if (graphqlClient) {
|
|
200
|
+
graphqlClient.query({ query: '{ entities { id } }' }).then((result) => console.log(result));
|
|
201
|
+
}
|
|
202
|
+
}, [graphqlClient]);
|
|
203
|
+
|
|
204
|
+
return <div>Data component</div>;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ECS-only component
|
|
208
|
+
function ECSComponent() {
|
|
209
|
+
const ecsWorld = useDubheECS();
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
if (ecsWorld) {
|
|
213
|
+
ecsWorld.getComponent('MyComponent').then((components) => console.log(components));
|
|
214
|
+
}
|
|
215
|
+
}, [ecsWorld]);
|
|
216
|
+
|
|
217
|
+
return <div>ECS component</div>;
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## ⚙️ Configuration Reference
|
|
222
|
+
|
|
223
|
+
### DubheConfig Interface
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
interface DubheConfig {
|
|
227
|
+
/** Network type */
|
|
228
|
+
network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
|
|
229
|
+
|
|
230
|
+
/** Contract package ID */
|
|
231
|
+
packageId: string;
|
|
232
|
+
|
|
233
|
+
/** Contract metadata (required for contract instantiation) */
|
|
234
|
+
metadata: SuiMoveNormalizedModules;
|
|
235
|
+
|
|
236
|
+
/** Dubhe Schema ID (optional, for enhanced features) */
|
|
237
|
+
dubheSchemaId?: string;
|
|
238
|
+
|
|
239
|
+
/** Dubhe metadata (enables GraphQL/ECS features) */
|
|
240
|
+
dubheMetadata?: any;
|
|
241
|
+
|
|
242
|
+
/** Authentication credentials */
|
|
243
|
+
credentials?: {
|
|
244
|
+
secretKey?: string; // ⚠️ LOCAL DEVELOPMENT ONLY - see security warning below
|
|
245
|
+
mnemonics?: string; // ⚠️ LOCAL DEVELOPMENT ONLY - see security warning below
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/** Service endpoints configuration */
|
|
249
|
+
endpoints?: {
|
|
250
|
+
graphql?: string; // Default: 'http://localhost:4000/graphql'
|
|
251
|
+
websocket?: string; // Default: 'ws://localhost:4000/graphql'
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** Performance and behavior options */
|
|
255
|
+
options?: {
|
|
256
|
+
enableBatchOptimization?: boolean; // Default: true
|
|
257
|
+
cacheTimeout?: number; // Default: 5000ms
|
|
258
|
+
debounceMs?: number; // Default: 100ms
|
|
259
|
+
reconnectOnError?: boolean; // Default: true
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Environment Variable Configuration
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
// .env.local
|
|
268
|
+
NEXT_PUBLIC_NETWORK=devnet
|
|
269
|
+
NEXT_PUBLIC_PACKAGE_ID=0x123...
|
|
270
|
+
NEXT_PUBLIC_PRIVATE_KEY=suiprivkey...
|
|
271
|
+
NEXT_PUBLIC_GRAPHQL_URL=http://localhost:4000/graphql
|
|
272
|
+
NEXT_PUBLIC_GRAPHQL_WS_URL=ws://localhost:4000/graphql
|
|
273
|
+
|
|
274
|
+
// App configuration
|
|
275
|
+
function App() {
|
|
276
|
+
const config = {
|
|
277
|
+
network: process.env.NEXT_PUBLIC_NETWORK as NetworkType,
|
|
278
|
+
packageId: process.env.NEXT_PUBLIC_PACKAGE_ID!,
|
|
279
|
+
metadata: contractMetadata,
|
|
280
|
+
credentials: {
|
|
281
|
+
secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY // ⚠️ LOCAL DEVELOPMENT ONLY // ⚠️ LOCAL DEVELOPMENT ONLY
|
|
282
|
+
},
|
|
283
|
+
endpoints: {
|
|
284
|
+
graphql: process.env.NEXT_PUBLIC_GRAPHQL_URL,
|
|
285
|
+
websocket: process.env.NEXT_PUBLIC_GRAPHQL_WS_URL
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
return (
|
|
290
|
+
<DubheProvider config={config}>
|
|
291
|
+
<MyApp />
|
|
292
|
+
</DubheProvider>
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## 🔐 Security Warning
|
|
298
|
+
|
|
299
|
+
> **⚠️ IMPORTANT SECURITY NOTICE**
|
|
300
|
+
>
|
|
301
|
+
> **`secretKey` and `mnemonics` are for LOCAL DEVELOPMENT ONLY!**
|
|
302
|
+
>
|
|
303
|
+
> - ❌ **NEVER use secretKey/mnemonics in production**
|
|
304
|
+
> - ❌ **NEVER commit private keys to version control**
|
|
305
|
+
> - ❌ **NEVER expose private keys in client-side code**
|
|
306
|
+
>
|
|
307
|
+
> **For production applications:**
|
|
308
|
+
>
|
|
309
|
+
> - ✅ Use official wallet providers for transaction signing
|
|
310
|
+
> - ✅ Implement proper wallet integration (Sui Wallet, etc.)
|
|
311
|
+
> - ✅ Let users connect their own wallets securely
|
|
312
|
+
>
|
|
313
|
+
> **Proper wallet integration example:**
|
|
314
|
+
>
|
|
315
|
+
> ```typescript
|
|
316
|
+
> // For production - use wallet providers instead of secretKey
|
|
317
|
+
> const config = {
|
|
318
|
+
> network: 'mainnet',
|
|
319
|
+
> packageId: process.env.NEXT_PUBLIC_PACKAGE_ID!,
|
|
320
|
+
> metadata
|
|
321
|
+
> // ✅ NO credentials - let wallet handle signing
|
|
322
|
+
> };
|
|
323
|
+
> ```
|
|
324
|
+
>
|
|
325
|
+
> Keep your private keys safe! If compromised, attackers can steal all funds.
|
|
326
|
+
|
|
327
|
+
## 📚 Practical Examples
|
|
328
|
+
|
|
329
|
+
### Basic Contract Usage
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
import React, { useState } from 'react';
|
|
333
|
+
import { DubheProvider, useDubhe } from '@0xobelisk/react/sui';
|
|
334
|
+
import { Transaction } from '@0xobelisk/sui-client';
|
|
335
|
+
|
|
336
|
+
function CounterApp() {
|
|
337
|
+
const { contract, address } = useDubhe();
|
|
338
|
+
const [counter, setCounter] = useState(0);
|
|
339
|
+
|
|
340
|
+
const queryCounter = async () => {
|
|
341
|
+
try {
|
|
342
|
+
const result = await contract.query.counter_system.get({});
|
|
343
|
+
setCounter(result);
|
|
344
|
+
} catch (error) {
|
|
345
|
+
console.error('Query failed:', error);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const incrementCounter = async () => {
|
|
350
|
+
try {
|
|
351
|
+
const tx = new Transaction();
|
|
352
|
+
const result = await contract.tx.counter_system.increment({ tx });
|
|
353
|
+
console.log('Transaction:', result.digest);
|
|
354
|
+
await queryCounter(); // Refresh counter
|
|
355
|
+
} catch (error) {
|
|
356
|
+
console.error('Transaction failed:', error);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
return (
|
|
361
|
+
<div>
|
|
362
|
+
<h1>Counter: {counter}</h1>
|
|
363
|
+
<p>Address: {address}</p>
|
|
364
|
+
<button onClick={queryCounter}>Refresh</button>
|
|
365
|
+
<button onClick={incrementCounter}>Increment</button>
|
|
366
|
+
</div>
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### GraphQL Integration
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
import React, { useEffect, useState } from 'react';
|
|
375
|
+
import { useDubhe } from '@0xobelisk/react/sui';
|
|
376
|
+
|
|
377
|
+
function DataComponent() {
|
|
378
|
+
const { graphqlClient, ecsWorld } = useDubhe();
|
|
379
|
+
const [entities, setEntities] = useState([]);
|
|
380
|
+
|
|
381
|
+
// Real-time subscription
|
|
382
|
+
useEffect(() => {
|
|
383
|
+
if (graphqlClient) {
|
|
384
|
+
const subscription = graphqlClient
|
|
385
|
+
.subscribe({
|
|
386
|
+
query: `
|
|
387
|
+
subscription {
|
|
388
|
+
entities {
|
|
389
|
+
id
|
|
390
|
+
components {
|
|
391
|
+
type
|
|
392
|
+
value
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
`
|
|
397
|
+
})
|
|
398
|
+
.subscribe((result) => {
|
|
399
|
+
setEntities(result.data.entities);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
return () => subscription.unsubscribe();
|
|
403
|
+
}
|
|
404
|
+
}, [graphqlClient]);
|
|
405
|
+
|
|
406
|
+
// ECS component queries
|
|
407
|
+
const queryComponents = async () => {
|
|
408
|
+
if (ecsWorld) {
|
|
409
|
+
const components = await ecsWorld.getComponent('CounterComponent');
|
|
410
|
+
console.log('Components:', components);
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
return (
|
|
415
|
+
<div>
|
|
416
|
+
<h2>Real-time Entities: {entities.length}</h2>
|
|
417
|
+
<button onClick={queryComponents}>Query Components</button>
|
|
418
|
+
|
|
419
|
+
{graphqlClient ? (
|
|
420
|
+
<p>✅ GraphQL Connected</p>
|
|
421
|
+
) : (
|
|
422
|
+
<p>❌ GraphQL Unavailable (add dubheMetadata to config)</p>
|
|
423
|
+
)}
|
|
424
|
+
</div>
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### Error Handling
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
import React from 'react';
|
|
433
|
+
import { DubheProvider, useDubhe } from '@0xobelisk/react/sui';
|
|
434
|
+
|
|
435
|
+
function ErrorHandlingExample() {
|
|
436
|
+
const { contract } = useDubhe();
|
|
437
|
+
|
|
438
|
+
const handleTransactionWithRetry = async () => {
|
|
439
|
+
const maxRetries = 3;
|
|
440
|
+
let attempt = 0;
|
|
441
|
+
|
|
442
|
+
while (attempt < maxRetries) {
|
|
443
|
+
try {
|
|
444
|
+
const tx = new Transaction();
|
|
445
|
+
const result = await contract.tx.my_system.my_method({ tx });
|
|
446
|
+
console.log('Success:', result.digest);
|
|
447
|
+
return result;
|
|
448
|
+
} catch (error) {
|
|
449
|
+
attempt++;
|
|
450
|
+
console.error(`Attempt ${attempt} failed:`, error);
|
|
451
|
+
|
|
452
|
+
if (attempt === maxRetries) {
|
|
453
|
+
throw error;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Wait before retry
|
|
457
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
return <button onClick={handleTransactionWithRetry}>Execute with Retry</button>;
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
## 🔧 TypeScript Support
|
|
467
|
+
|
|
468
|
+
### Type Exports
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
import type {
|
|
472
|
+
NetworkType,
|
|
473
|
+
DubheConfig,
|
|
474
|
+
ClientConfig, // Alias for DubheConfig (for consistency with @0xobelisk/client)
|
|
475
|
+
DubheReturn,
|
|
476
|
+
ContractReturn // Alias for DubheReturn
|
|
477
|
+
} from '@0xobelisk/react/sui';
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
### Type-Safe Configuration
|
|
481
|
+
|
|
482
|
+
```typescript
|
|
483
|
+
const config: DubheConfig = {
|
|
484
|
+
network: 'devnet', // Type-safe network selection
|
|
485
|
+
packageId: '0x123...',
|
|
486
|
+
metadata: contractMetadata, // Typed metadata
|
|
487
|
+
credentials: {
|
|
488
|
+
secretKey: process.env.NEXT_PUBLIC_PRIVATE_KEY // ⚠️ LOCAL DEVELOPMENT ONLY
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const result: DubheReturn = useDubhe();
|
|
493
|
+
// Full type safety for all return values
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
## 🛠️ Development
|
|
497
|
+
|
|
498
|
+
```bash
|
|
499
|
+
# Watch mode for development
|
|
500
|
+
npm run watch
|
|
501
|
+
|
|
502
|
+
# Type checking
|
|
503
|
+
npm run type-check
|
|
504
|
+
|
|
505
|
+
# Build package
|
|
506
|
+
npm run build
|
|
507
|
+
|
|
508
|
+
# Run tests
|
|
509
|
+
npm run test
|
|
510
|
+
|
|
511
|
+
# Format code
|
|
512
|
+
npm run format
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
## 📖 API Reference
|
|
516
|
+
|
|
517
|
+
### DubheReturn Interface
|
|
518
|
+
|
|
519
|
+
```typescript
|
|
520
|
+
interface DubheReturn {
|
|
521
|
+
contract: Dubhe; // Enhanced contract instance
|
|
522
|
+
graphqlClient: DubheGraphqlClient | null; // GraphQL client (if enabled)
|
|
523
|
+
grpcClient: DubheGrpcClient; // gRPC client
|
|
524
|
+
ecsWorld: DubheECSWorld | null; // ECS World (if enabled)
|
|
525
|
+
metadata: SuiMoveNormalizedModules; // Contract metadata
|
|
526
|
+
network: NetworkType; // Current network
|
|
527
|
+
packageId: string; // Package ID
|
|
528
|
+
dubheSchemaId?: string; // Schema ID (if provided)
|
|
529
|
+
address: string; // User address
|
|
530
|
+
options?: DubheOptions; // Configuration options
|
|
531
|
+
metrics?: DubheMetrics; // Performance metrics
|
|
532
|
+
}
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
### DubheConfig Interface
|
|
536
|
+
|
|
537
|
+
Configuration aligned with `@0xobelisk/client` for consistency:
|
|
538
|
+
|
|
539
|
+
```typescript
|
|
540
|
+
interface DubheConfig {
|
|
541
|
+
// Required fields
|
|
542
|
+
network: NetworkType; // 'mainnet' | 'testnet' | 'devnet' | 'localnet'
|
|
543
|
+
packageId: string; // Contract package ID
|
|
544
|
+
metadata: any; // Contract metadata (from dubhe schemagen)
|
|
545
|
+
|
|
546
|
+
// Optional fields
|
|
547
|
+
dubheSchemaId?: string; // Dubhe Schema ID for enhanced features
|
|
548
|
+
dubheMetadata?: any; // Dubhe metadata for GraphQL/ECS features
|
|
549
|
+
|
|
550
|
+
// Credentials
|
|
551
|
+
credentials?: {
|
|
552
|
+
secretKey?: string; // Private key (base64 or hex)
|
|
553
|
+
mnemonics?: string; // 12 or 24 word mnemonic phrase
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// Endpoints
|
|
557
|
+
endpoints?: {
|
|
558
|
+
fullnodeUrls?: string[]; // Full node RPC URLs (multiple for redundancy)
|
|
559
|
+
graphql?: string; // GraphQL endpoint (default: http://localhost:4000/graphql)
|
|
560
|
+
websocket?: string; // WebSocket endpoint (default: ws://localhost:4000/graphql)
|
|
561
|
+
grpc?: string; // gRPC endpoint (default: http://localhost:8080)
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// Options
|
|
565
|
+
options?: {
|
|
566
|
+
enableBatchOptimization?: boolean; // Default: true
|
|
567
|
+
cacheTimeout?: number; // Default: 5000 (ms)
|
|
568
|
+
debounceMs?: number; // Default: 100 (ms)
|
|
569
|
+
reconnectOnError?: boolean; // Default: true
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// Type alias for consistency with @0xobelisk/client
|
|
574
|
+
type ClientConfig = DubheConfig;
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
### Available Hooks
|
|
578
|
+
|
|
579
|
+
- `useDubhe()` → `DubheReturn` - Complete Dubhe ecosystem
|
|
580
|
+
- `useDubheContract()` → `Dubhe` - Contract instance only
|
|
581
|
+
- `useDubheGraphQL()` → `DubheGraphqlClient | null` - GraphQL client only
|
|
582
|
+
- `useDubheECS()` → `DubheECSWorld | null` - ECS World only
|
|
583
|
+
- `useContract()` → `DubheReturn` - Alias for `useDubhe()`
|
|
584
|
+
|
|
585
|
+
## 🚨 Error Handling
|
|
586
|
+
|
|
587
|
+
The package includes comprehensive error handling:
|
|
588
|
+
|
|
589
|
+
```typescript
|
|
590
|
+
// Configuration validation
|
|
591
|
+
try {
|
|
592
|
+
const { contract } = useDubhe();
|
|
593
|
+
} catch (error) {
|
|
594
|
+
console.error('Configuration error:', error.message);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Provider context validation
|
|
598
|
+
function MyComponent() {
|
|
599
|
+
try {
|
|
600
|
+
const dubhe = useDubhe();
|
|
601
|
+
} catch (error) {
|
|
602
|
+
// Error: useDubhe must be used within a DubheProvider
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
```
|