@c7-digital/react 0.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.
Files changed (3) hide show
  1. package/LICENSE +17 -0
  2. package/README.md +266 -0
  3. package/package.json +44 -0
package/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ Copyright [2025] [C7.digital]
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # @c7-digital/react
2
+
3
+ React hooks for Daml ledger integration using Canton JSON API v2
4
+
5
+ ## Features
6
+
7
+ - 🔧 **Type-safe**: Full TypeScript support with branded types from Canton API
8
+ - âš¡ **Real-time**: WebSocket streaming for live contract updates
9
+ - 🎣 **React Hooks**: Modern React patterns with hooks for ledger operations
10
+ - 📦 **Lightweight**: Minimal dependencies
11
+ - **Replacement**: `@daml/react`.
12
+
13
+ ## Versioning
14
+
15
+ This package follows the Canton SDK versioning scheme, matching the `@c7-digital/ledger` package it depends on.
16
+
17
+ **Current version**: `3.3.0-snapshot.20250507.0`
18
+
19
+ - Versioned to match the Canton SDK and `@c7-digital/ledger` package
20
+ - Ensures compatibility with the specific Canton OpenAPI types
21
+ - When Canton releases a new version, both packages will be updated together
22
+
23
+ **Version compatibility**: Install the version that matches your Canton participant node version.
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ pnpm install @c7-digital/react @c7-digital/ledger
29
+ ```
30
+
31
+ ## Build Output
32
+
33
+ The library builds to the `dist/` folder with:
34
+
35
+ - `dist/index.js` - Main entry point
36
+ - `dist/index.d.ts` - TypeScript declarations
37
+ - `dist/**/*.d.ts.map` - Source maps for debugging
38
+
39
+ ## Quick Start
40
+
41
+ ### 1. Setup the Provider
42
+
43
+ Wrap your app with `DamlLedger` provider:
44
+
45
+ ```tsx
46
+ import { DamlLedger } from "@c7-digital/react";
47
+ import { Ledger } from "@c7-digital/ledger";
48
+
49
+ const ledger = new Ledger({
50
+ baseUrl: "http://localhost:7575",
51
+ token: "your-jwt-token",
52
+ party: "Alice::1220...",
53
+ });
54
+
55
+ function App() {
56
+ return (
57
+ <DamlLedger ledger={ledger} party="Alice::1220...">
58
+ <MyComponent />
59
+ </DamlLedger>
60
+ );
61
+ }
62
+ ```
63
+
64
+ ### 2. Query Contracts
65
+
66
+ Use `useQuery` to fetch active contracts:
67
+
68
+ ```tsx
69
+ import { useQuery } from "@c7-digital/react";
70
+
71
+ function MyComponent() {
72
+ const { contracts, loading, error, reload } = useQuery(
73
+ "#domain-verification-model:InternetDomainName:VerificationRequest"
74
+ );
75
+
76
+ if (loading) return <div>Loading...</div>;
77
+ if (error) return <div>Error: {error.message}</div>;
78
+
79
+ // Filter contracts at the application level
80
+ const exampleContracts = contracts.filter(
81
+ (contract) => contract.domain === "example.com"
82
+ );
83
+
84
+ return (
85
+ <div>
86
+ <h2>Verification Requests ({exampleContracts.length})</h2>
87
+ {exampleContracts.map((contract, i) => (
88
+ <div key={i}>{contract.domain}</div>
89
+ ))}
90
+ <button onClick={reload}>Refresh</button>
91
+ </div>
92
+ );
93
+ }
94
+ ```
95
+
96
+ ### 3. Submit Commands
97
+
98
+ Use `useLedger` to get the ledger instance and `useUser` for the current user information:
99
+
100
+ ```tsx
101
+ import { useLedger, useUser } from "@c7-digital/react";
102
+
103
+ function ApprovalComponent({ contractId }: { contractId: string }) {
104
+ const ledger = useLedger();
105
+ const { user, loading, error } = useUser();
106
+
107
+ const handleApprove = async () => {
108
+ try {
109
+ await ledger.exercise({
110
+ templateId:
111
+ "#domain-verification-model:InternetDomainName:VerificationRequest",
112
+ contractId,
113
+ choice: "AcceptRequest",
114
+ choiceArgument: {
115
+ dnsTxtPayload: "domain-verification=abc123",
116
+ },
117
+ });
118
+ } catch (error) {
119
+ console.error("Failed to approve:", error);
120
+ }
121
+ };
122
+
123
+ if (loading) return <div>Loading user...</div>;
124
+ if (error) return <div>Error: {error.message}</div>;
125
+
126
+ return (
127
+ <button onClick={handleApprove}>Approve Request (as {user?.userId})</button>
128
+ );
129
+ }
130
+ ```
131
+
132
+ ### 4. Real-time Streaming
133
+
134
+ Use `useStreamQuery` for live updates:
135
+
136
+ ```tsx
137
+ import { useStreamQuery } from "@c7-digital/react";
138
+
139
+ function LiveContracts() {
140
+ const { contracts, loading, connected, error } = useStreamQuery(
141
+ "#domain-verification-model:InternetDomainName:VerificationRequest"
142
+ );
143
+
144
+ return (
145
+ <div>
146
+ <div>Status: {connected ? "🟢 Connected" : "🔴 Disconnected"}</div>
147
+ <div>Contracts: {contracts.length}</div>
148
+ {contracts.map((contract, i) => (
149
+ <div key={i}>{JSON.stringify(contract)}</div>
150
+ ))}
151
+ </div>
152
+ );
153
+ }
154
+ ```
155
+
156
+ ## API Reference
157
+
158
+ ### Components
159
+
160
+ #### `DamlLedger`
161
+
162
+ Provider component that enables ledger hooks.
163
+
164
+ ```tsx
165
+ interface DamlLedgerProps {
166
+ ledger: Ledger;
167
+ party: string;
168
+ children: ReactNode;
169
+ }
170
+ ```
171
+
172
+ ### Hooks
173
+
174
+ #### `useQuery(templateId, options?)`
175
+
176
+ Query active contracts for a template.
177
+
178
+ ```tsx
179
+ function useQuery<T>(
180
+ templateId: string,
181
+ options?: QueryOptions
182
+ ): QueryResult<T>;
183
+
184
+ interface QueryOptions {
185
+ autoReload?: boolean;
186
+ }
187
+
188
+ interface QueryResult<T> {
189
+ contracts: T[];
190
+ loading: boolean;
191
+ error: Error | null;
192
+ reload: () => void;
193
+ }
194
+ ```
195
+
196
+ #### `useLedger()`
197
+
198
+ Get the ledger instance.
199
+
200
+ ```tsx
201
+ function useLedger(): Ledger;
202
+ ```
203
+
204
+ #### `useUser()`
205
+
206
+ Get the current user information from the token.
207
+
208
+ ```tsx
209
+ function useUser(): UserResult;
210
+
211
+ interface UserResult {
212
+ user: User | null;
213
+ loading: boolean;
214
+ error: Error | null;
215
+ }
216
+ ```
217
+
218
+ #### `useStreamQuery(templateId, options?)`
219
+
220
+ Stream active contracts with real-time updates.
221
+
222
+ ```tsx
223
+ function useStreamQuery<T>(
224
+ templateId: string,
225
+ options?: QueryOptions
226
+ ): StreamQueryResult<T>;
227
+
228
+ interface StreamQueryResult<T> extends QueryResult<T> {
229
+ connected: boolean;
230
+ }
231
+ ```
232
+
233
+ #### `useStreamQueries(templateIds, options?)`
234
+
235
+ Stream multiple templates simultaneously.
236
+
237
+ ```tsx
238
+ function useStreamQueries<T>(
239
+ templateIds: string[],
240
+ options?: QueryOptions
241
+ ): StreamQueryResult<T>;
242
+ ```
243
+
244
+ #### `useReload()`
245
+
246
+ Get a function to reload all queries.
247
+
248
+ ```tsx
249
+ function useReload(): () => void;
250
+ ```
251
+
252
+ ## Migration from @daml/react
253
+
254
+ This package provides a compatible API with `@daml/react` but uses the newer Canton JSON API v2:
255
+
256
+ | @daml/react | @c7-digital/react | Notes |
257
+ | ------------------ | ------------------ | ------------------------------ |
258
+ | `DamlLedger` | `DamlLedger` | Same API, different backend |
259
+ | `useParty()` | `useParty()` | Identical |
260
+ | `useLedger()` | `useLedger()` | Enhanced with typed operations |
261
+ | `useQuery()` | `useQuery()` | Improved filtering and caching |
262
+ | `useStreamQuery()` | `useStreamQuery()` | Real-time WebSocket updates |
263
+
264
+ ## License
265
+
266
+ Apache-2.0
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@c7-digital/react",
3
+ "version": "0.0.2",
4
+ "description": "React hooks for Daml ledger integration using Canton JSON API v2",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "dev": "tsc --watch",
13
+ "clean": "rm -rf dist",
14
+ "prepare": "tsc",
15
+ "prepublishOnly": "pnpm run clean && pnpm run build"
16
+ },
17
+ "keywords": [
18
+ "daml",
19
+ "canton",
20
+ "react",
21
+ "hooks",
22
+ "ledger",
23
+ "blockchain"
24
+ ],
25
+ "author": "",
26
+ "license": "Apache-2.0",
27
+ "peerDependencies": {
28
+ "@c7-digital/ledger": "^0.0.2",
29
+ "react": "^18.0.0"
30
+ },
31
+ "dependencies": {
32
+ "@daml/types": "3.4.8"
33
+ },
34
+ "devDependencies": {
35
+ "@c7-digital/ledger": "file:../ledger",
36
+ "@types/react": "^18.3.12",
37
+ "react": "^18.3.1",
38
+ "typescript": "^5.8.3"
39
+ },
40
+ "engines": {
41
+ "node": ">=18.0.0",
42
+ "pnpm": ">=8.0.0"
43
+ }
44
+ }