@miradorlabs/parallax-web 1.0.6 → 1.0.8

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.
@@ -1,306 +0,0 @@
1
- # ParallaxService Usage Guide
2
-
3
- The `ParallaxService` provides a simplified, high-level interface for tracking transactions in web applications. It automatically manages trace lifecycle, client metadata collection, and blockchain correlation.
4
-
5
- ## Features
6
-
7
- - **Automatic Root Span Creation**: When you create a trace, a root span is automatically created (no need to call `startSpan` separately)
8
- - **Transaction Lifecycle Management**: Track transactions from initiation to completion
9
- - **Automatic Client Metadata**: Optionally includes browser, OS, IP, and other client details
10
- - **Blockchain Integration**: Associate transaction hashes with traces for correlation
11
- - **Event Tracking**: Add events at any point in the transaction lifecycle
12
- - **Error Tracking**: Proper error handling with stack traces
13
-
14
- ## Quick Start
15
-
16
- ### Using the Singleton Instance
17
-
18
- ```typescript
19
- import { parallaxService } from '@miradorlabs/parallax-web';
20
-
21
- // Start tracking a transaction
22
- const { traceId, rootSpanId, txId } = await parallaxService.startTransactionTrace(
23
- {
24
- from: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
25
- to: '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199',
26
- value: '0.1',
27
- network: 'ethereum',
28
- walletAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
29
- },
30
- 'SendTransaction', // trace name
31
- {
32
- includeClientMeta: true, // include browser, OS, IP, etc.
33
- apiKey: 'your-api-key', // optional
34
- gatewayUrl: 'https://gateway-parallax-dev.mirador.org', // optional
35
- }
36
- );
37
-
38
- // Later, when you get the transaction hash
39
- await parallaxService.associateTransactionHash(txId, '0xabc123...', 1);
40
-
41
- // Add events
42
- await parallaxService.addTransactionEvent(txId, 'wallet_signed', {
43
- timestamp: new Date().toISOString(),
44
- });
45
-
46
- // Track errors
47
- try {
48
- // transaction logic
49
- } catch (error) {
50
- await parallaxService.addTransactionError(txId, error, 'TransactionError');
51
- }
52
-
53
- // Finish the trace
54
- await parallaxService.finishTransactionTrace(txId, {
55
- success: true,
56
- });
57
- ```
58
-
59
- ### Creating Your Own Instance
60
-
61
- ```typescript
62
- import { ParallaxService } from '@miradorlabs/parallax-web';
63
-
64
- const myService = new ParallaxService();
65
- // Use the same API as above
66
- ```
67
-
68
- ## API Reference
69
-
70
- ### `startTransactionTrace(txData, name, options)`
71
-
72
- Starts a new transaction trace with automatic root span creation.
73
-
74
- **Parameters:**
75
- - `txData`: Object with transaction details
76
- - `from`: Sender address (required)
77
- - `to`: Recipient address (required)
78
- - `value`: Transaction value (required)
79
- - `network`: Network name (optional)
80
- - `walletAddress`: Wallet address (optional)
81
- - `additionalData`: Any extra data (optional)
82
- - `name`: Trace name (default: 'WalletTransaction')
83
- - `options`: Optional configuration
84
- - `apiKey`: API key for authentication
85
- - `gatewayUrl`: Custom gateway URL
86
- - `includeClientMeta`: Include browser/OS metadata (default: true)
87
-
88
- **Returns:**
89
- ```typescript
90
- Promise<{
91
- traceId: string;
92
- rootSpanId: string; // This is the automatically created root span
93
- txId: string; // Internal transaction identifier
94
- }>
95
- ```
96
-
97
- ### `associateTransactionHash(txId, txHash, chainId)`
98
-
99
- Associates a blockchain transaction hash with the trace for correlation.
100
-
101
- **Parameters:**
102
- - `txId`: Transaction identifier from `startTransactionTrace`
103
- - `txHash`: Blockchain transaction hash
104
- - `chainId`: Chain ID (e.g., 1 for Ethereum mainnet)
105
-
106
- ### `addTransactionEvent(txId, eventName, attributes)`
107
-
108
- Adds an event to the transaction trace.
109
-
110
- **Parameters:**
111
- - `txId`: Transaction identifier
112
- - `eventName`: Event name (e.g., 'wallet_signed', 'sent_to_network')
113
- - `attributes`: Event attributes (object)
114
-
115
- ### `addTransactionError(txId, error, errorType)`
116
-
117
- Adds an error to the transaction trace with stack trace support.
118
-
119
- **Parameters:**
120
- - `txId`: Transaction identifier
121
- - `error`: Error object or message
122
- - `errorType`: Error category (default: 'TransactionError')
123
-
124
- ### `finishTransactionTrace(txId, options)`
125
-
126
- Completes the transaction trace.
127
-
128
- **Parameters:**
129
- - `txId`: Transaction identifier
130
- - `options`: Finish options
131
- - `success`: Whether transaction succeeded (required)
132
- - `error`: Error message if failed (optional)
133
-
134
- ### `getTransactionInfo(txId)`
135
-
136
- Gets information about an active transaction.
137
-
138
- **Returns:** Transaction info object or null
139
-
140
- ### `getAllActiveTransactions()`
141
-
142
- Gets all currently active transactions.
143
-
144
- **Returns:** Array of transaction info objects
145
-
146
- ### `getClient(apiKey?, gatewayUrl?)`
147
-
148
- Gets the underlying `ParallaxClient` for advanced usage.
149
-
150
- ## Important Note: Automatic Root Span Creation
151
-
152
- **The key difference** from previous versions: When you call `createTrace`, a root span is **automatically created** on the backend. You receive a `rootSpanId` in the response.
153
-
154
- You **do NOT need** to call `startSpan` after creating a trace anymore. The `rootSpanId` serves as the parent span for any child spans you want to create.
155
-
156
- ### Example of Creating Child Spans (Optional)
157
-
158
- If you need child spans for more detailed tracking:
159
-
160
- ```typescript
161
- // Start transaction (creates trace + root span automatically)
162
- const { traceId, rootSpanId, txId } = await parallaxService.startTransactionTrace(
163
- txData,
164
- 'ComplexTransaction'
165
- );
166
-
167
- // Access the underlying client for advanced usage
168
- const client = parallaxService.getClient();
169
-
170
- // Create a child span if needed
171
- const childSpanReq = await client.createStartSpanRequest({
172
- traceId: traceId,
173
- name: 'SigningPhase',
174
- parentSpanId: rootSpanId, // Use the root span as parent
175
- attr: { phase: 'signing' },
176
- });
177
-
178
- const childSpanResponse = await client.startSpan(childSpanReq);
179
- const childSpanId = childSpanResponse.getSpanId();
180
-
181
- // Later, finish the child span
182
- const finishReq = client.createFinishSpanRequest({
183
- traceId: traceId,
184
- spanId: childSpanId,
185
- status: { success: true, errorMessage: '' },
186
- });
187
-
188
- await client.finishSpan(finishReq);
189
- ```
190
-
191
- ## Client Metadata
192
-
193
- When `includeClientMeta: true`, the following metadata is automatically collected:
194
- - Browser (Chrome, Firefox, Safari, Edge)
195
- - Operating System (Windows, macOS, Linux, Android, iOS)
196
- - User Agent
197
- - Platform
198
- - Language
199
- - Screen dimensions
200
- - Viewport dimensions
201
- - Timezone
202
- - Page URL
203
- - Referrer
204
- - IP address (if not blocked by CSP)
205
-
206
- All metadata is prefixed with `client.` in attributes.
207
-
208
- ## Environment Detection
209
-
210
- The service automatically detects the environment:
211
- - **Localhost/127.0.0.1**: Uses proxy endpoint `${window.location.protocol}//${window.location.host}/parallax-gateway`
212
- - **Production**: Uses `https://gateway-parallax-dev.mirador.org`
213
-
214
- You can override this with the `gatewayUrl` option.
215
-
216
- ## Example: Complete Transaction Flow
217
-
218
- ```typescript
219
- import { parallaxService } from '@miradorlabs/parallax-web';
220
-
221
- async function handleTransaction() {
222
- let txId: string;
223
-
224
- try {
225
- // 1. Start the trace
226
- const result = await parallaxService.startTransactionTrace(
227
- {
228
- from: userAddress,
229
- to: recipientAddress,
230
- value: amount,
231
- network: 'ethereum',
232
- walletAddress: userAddress,
233
- additionalData: {
234
- gasPrice: gasPrice,
235
- gasLimit: gasLimit,
236
- },
237
- },
238
- 'SendETH',
239
- { includeClientMeta: true }
240
- );
241
-
242
- txId = result.txId;
243
-
244
- // 2. User signs the transaction
245
- await parallaxService.addTransactionEvent(txId, 'user_signing', {});
246
-
247
- const signedTx = await wallet.signTransaction(txData);
248
-
249
- await parallaxService.addTransactionEvent(txId, 'transaction_signed', {});
250
-
251
- // 3. Send to network
252
- await parallaxService.addTransactionEvent(txId, 'sending_to_network', {});
253
-
254
- const txReceipt = await provider.sendTransaction(signedTx);
255
-
256
- // 4. Associate the transaction hash
257
- await parallaxService.associateTransactionHash(
258
- txId,
259
- txReceipt.hash,
260
- 1 // Ethereum mainnet
261
- );
262
-
263
- // 5. Wait for confirmation
264
- await parallaxService.addTransactionEvent(txId, 'waiting_confirmation', {
265
- txHash: txReceipt.hash,
266
- });
267
-
268
- await txReceipt.wait();
269
-
270
- // 6. Success!
271
- await parallaxService.finishTransactionTrace(txId, { success: true });
272
-
273
- } catch (error) {
274
- // Track the error
275
- if (txId) {
276
- await parallaxService.addTransactionError(txId, error, 'TransactionError');
277
- await parallaxService.finishTransactionTrace(txId, {
278
- success: false,
279
- error: error.message,
280
- });
281
- }
282
- throw error;
283
- }
284
- }
285
- ```
286
-
287
- ## TypeScript Support
288
-
289
- Full TypeScript support with exported types:
290
-
291
- ```typescript
292
- import type {
293
- CreateTraceRequest,
294
- CreateTraceResponse,
295
- StartSpanRequest,
296
- StartSpanResponse,
297
- FinishSpanRequest,
298
- FinishSpanResponse,
299
- AddSpanEventRequest,
300
- AddSpanEventResponse,
301
- AddSpanErrorRequest,
302
- AddSpanErrorResponse,
303
- AddSpanHintRequest,
304
- AddSpanHintResponse,
305
- } from '@miradorlabs/parallax-web';
306
- ```
package/SETUP.md DELETED
@@ -1,220 +0,0 @@
1
- # Web Parallax Client Setup Summary
2
-
3
- This document summarizes the complete configuration and structure of the web-parallax-client SDK.
4
-
5
- ## Directory Structure
6
-
7
- ```
8
- web-parallax-client/
9
- ├── dist/ # Build output (generated)
10
- │ ├── index.esm.js # ES Module bundle
11
- │ ├── index.esm.js.map # ESM source map
12
- │ ├── index.umd.js # UMD bundle (browser global)
13
- │ ├── index.umd.js.map # UMD source map
14
- │ └── index.d.ts # TypeScript declarations
15
- ├── src/
16
- │ ├── grpc/
17
- │ │ └── index.ts # GrpcWebRpc adapter (uses Fetch API)
18
- │ ├── parallax/
19
- │ │ └── index.ts # ParallaxClient main class
20
- │ └── helpers/
21
- │ └── index.ts # Serialization helpers
22
- ├── tests/
23
- │ └── parallax.test.ts # Jest unit tests
24
- ├── index.ts # Main entry point
25
- ├── package.json # NPM package configuration
26
- ├── tsconfig.json # TypeScript config (dev)
27
- ├── tsconfig.build.json # TypeScript config (build)
28
- ├── rollup.config.mjs # Rollup bundler configuration
29
- ├── jest.config.ts # Jest test configuration
30
- ├── eslint.config.js # ESLint configuration
31
- ├── .prettierrc # Prettier configuration
32
- ├── .nvmrc # Node version (22.13.0)
33
- ├── .gitignore # Git ignore rules
34
- └── README.md # User documentation
35
- ```
36
-
37
- ## Key Configuration Files
38
-
39
- ### package.json
40
-
41
- - **Name**: `@miradorlabs/parallax-web`
42
- - **Main Entry Points**:
43
- - `main`: `dist/index.umd.js` (UMD for Node.js/CommonJS)
44
- - `module`: `dist/index.esm.js` (ES Module for bundlers)
45
- - `browser`: `dist/index.umd.js` (Browser global)
46
- - `types`: `dist/index.d.ts` (TypeScript definitions)
47
- - **Dependencies**:
48
- - `google-protobuf`: ^4.0.1
49
- - `mirador-gateway-parallax-web`: grpc-web package from GCS
50
- - `rxjs`: ^7.8.2
51
- - **Scripts**:
52
- - `build`: Build all bundles with Rollup
53
- - `test`: Run Jest tests
54
- - `test:watch`: Run Jest in watch mode
55
- - `test:coverage`: Generate coverage report
56
-
57
- ### rollup.config.mjs
58
-
59
- Generates three outputs:
60
-
61
- 1. **ESM Bundle** (`dist/index.esm.js`)
62
- - Format: ES Module
63
- - For modern bundlers (Webpack, Vite, etc.)
64
-
65
- 2. **UMD Bundle** (`dist/index.umd.js`)
66
- - Format: UMD
67
- - Global name: `ParallaxWeb`
68
- - For browser `<script>` tags
69
-
70
- 3. **Type Definitions** (`dist/index.d.ts`)
71
- - TypeScript declarations
72
-
73
- All bundles treat external dependencies as external:
74
- - `google-protobuf`
75
- - `mirador-gateway-parallax-web/*`
76
- - `rxjs/*`
77
-
78
- ### tsconfig.json
79
-
80
- - Target: ES2020
81
- - Module: ESNext (for browser)
82
- - Lib: ES2020, DOM, DOM.Iterable
83
- - Strict mode enabled
84
- - Module resolution: bundler
85
-
86
- ### jest.config.ts
87
-
88
- - Preset: ts-jest
89
- - Test environment: **jsdom** (browser simulation)
90
- - Test match: `**/*.test.ts`
91
- - Coverage from `src/**/*.ts`
92
-
93
- ## Key Differences from nodejs-parallax-client
94
-
95
- | Feature | nodejs-parallax-client | web-parallax-client |
96
- |---------|------------------------|---------------------|
97
- | gRPC Library | `@grpc/grpc-js` | Fetch API (gRPC-Web compatible) |
98
- | Transport | Native gRPC | HTTP/HTTPS with gRPC-Web protocol |
99
- | Environment | Node.js | Browser |
100
- | Module System | CommonJS | ESM + UMD |
101
- | Test Environment | Node | jsdom (browser) |
102
- | Connection | Can use insecure | Always uses HTTPS |
103
- | Default URL | localhost:50053 | https://gateway-parallax-dev... |
104
-
105
- ## Source Files
106
-
107
- ### src/grpc/index.ts - GrpcWebRpc
108
-
109
- Implements the gRPC-Web RPC adapter using browser Fetch API:
110
-
111
- - **Unary requests**: Standard HTTP POST with protobuf binary body
112
- - **Server streaming**: Uses ReadableStream from Response.body
113
- - **Headers**: Includes `Content-Type: application/grpc-web+proto`
114
- - **API Key**: Sent as `x-api-key` header
115
- - **Type casting**: `data.buffer as ArrayBuffer` for Fetch compatibility
116
-
117
- ### src/parallax/index.ts - ParallaxClient
118
-
119
- Main SDK client with methods:
120
- - `createTrace()`: Create a new trace
121
- - `startSpan()`: Start a span within a trace
122
- - `finishSpan()`: Finish a span
123
- - `addSpanAttributes()`: Add attributes to a span
124
- - `addSpanEvent()`: Add an event to a span
125
- - `addSpanError()`: Add an error to a span
126
- - `addSpanHint()`: Add a blockchain transaction hint
127
-
128
- ### tests/parallax.test.ts
129
-
130
- Comprehensive test suite covering:
131
- - Client instantiation
132
- - All SDK methods (success and error cases)
133
- - Integration scenarios
134
- - API key handling
135
- - Mock implementation of GrpcWebRpc
136
-
137
- ## Build Process
138
-
139
- ```bash
140
- npm run build
141
- ```
142
-
143
- 1. Clears `dist/` directory
144
- 2. Runs Rollup with three configurations in parallel:
145
- - ESM bundle with source maps
146
- - UMD bundle with source maps
147
- - TypeScript declarations
148
-
149
- Build warnings about `mirador-gateway-parallax-web` imports are **expected** - these are external dependencies resolved at runtime.
150
-
151
- ## Testing
152
-
153
- ```bash
154
- npm test # Run all tests
155
- npm run test:watch # Watch mode
156
- npm run test:coverage # Generate coverage report
157
- ```
158
-
159
- Tests use:
160
- - **jsdom**: Simulates browser environment
161
- - **ts-jest**: TypeScript transformation
162
- - **Mocking**: GrpcWebRpc is mocked for unit tests
163
-
164
- ## Publishing
165
-
166
- ```bash
167
- npm run release:patch # Bump patch version (1.0.0 -> 1.0.1)
168
- npm run release:minor # Bump minor version (1.0.0 -> 1.1.0)
169
- npm run release:major # Bump major version (1.0.0 -> 2.0.0)
170
- ```
171
-
172
- Versioning automatically:
173
- 1. Updates `package.json` version
174
- 2. Creates git tag
175
- 3. Pushes with `--follow-tags`
176
-
177
- ## Browser Compatibility
178
-
179
- Requires modern browsers supporting:
180
- - ES2020 features
181
- - Fetch API
182
- - Promises
183
- - ReadableStream
184
- - Uint8Array
185
-
186
- For older browsers, polyfills may be needed.
187
-
188
- ## Usage in Browser
189
-
190
- ### Via NPM/Bundler (Recommended)
191
-
192
- ```typescript
193
- import { ParallaxClient } from '@miradorlabs/parallax-web';
194
-
195
- const client = new ParallaxClient('your-api-key');
196
- ```
197
-
198
- ### Via UMD Script Tag
199
-
200
- ```html
201
- <script src="node_modules/@miradorlabs/parallax-web/dist/index.umd.js"></script>
202
- <script>
203
- const client = new ParallaxWeb.ParallaxClient('your-api-key');
204
- </script>
205
- ```
206
-
207
- ## Next Steps
208
-
209
- 1. **Install dependencies**: Already done (`npm install`)
210
- 2. **Build**: Already done (`npm run build`)
211
- 3. **Test**: Run `npm test` to verify all tests pass
212
- 4. **Customize**: Update `GRPC_GATEWAY_API_URL` in `src/parallax/index.ts` if needed
213
- 5. **Publish**: When ready, use `npm run release:patch` to version and publish
214
-
215
- ## Notes
216
-
217
- - The package uses the grpc-web package from GCS: `mirador-gateway-parallax-grpc-web-1.0.9.tgz`
218
- - Type warnings during build about missing type declarations are expected for external packages
219
- - All gRPC-Web requests use the browser's Fetch API, making this SDK compatible with modern web applications
220
- - The SDK maintains the same API surface as the Node.js client for consistency