@vocdoni/davinci-sdk 0.0.1 → 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.
package/README.md CHANGED
@@ -195,7 +195,7 @@ await sdk.init();
195
195
 
196
196
  ### Process Management
197
197
 
198
- #### Creating a Process
198
+ #### Creating a Process (Simple)
199
199
 
200
200
  ```typescript
201
201
  const processResult = await sdk.createProcess({
@@ -244,6 +244,51 @@ const processResult = await sdk.createProcess({
244
244
  console.log('Process created:', processResult.processId);
245
245
  ```
246
246
 
247
+ #### Creating a Process with Real-Time Status (Stream)
248
+
249
+ For applications that need to show real-time transaction progress to users, use `createProcessStream()`:
250
+
251
+ ```typescript
252
+ import { TxStatus } from '@vocdoni/davinci-sdk';
253
+
254
+ const stream = sdk.createProcessStream({
255
+ title: "Election Title",
256
+ // ... same configuration as above
257
+ });
258
+
259
+ // Monitor transaction status in real-time
260
+ for await (const event of stream) {
261
+ switch (event.status) {
262
+ case TxStatus.Pending:
263
+ console.log("📝 Transaction submitted:", event.hash);
264
+ // Update UI to show pending state
265
+ break;
266
+
267
+ case TxStatus.Completed:
268
+ console.log("✅ Process created:", event.response.processId);
269
+ console.log(" Transaction:", event.response.transactionHash);
270
+ // Update UI to show success
271
+ break;
272
+
273
+ case TxStatus.Failed:
274
+ console.error("❌ Transaction failed:", event.error);
275
+ // Update UI to show error
276
+ break;
277
+
278
+ case TxStatus.Reverted:
279
+ console.error("⚠️ Transaction reverted:", event.reason);
280
+ // Update UI to show revert reason
281
+ break;
282
+ }
283
+ }
284
+ ```
285
+
286
+ **When to use each method:**
287
+
288
+ - Use `createProcess()` for simple scripts and when you don't need transaction progress updates
289
+ - Use `createProcessStream()` for UI applications where users need real-time feedback during transaction processing
290
+
291
+
247
292
  #### Retrieving Process Information
248
293
 
249
294
  ```typescript
@@ -1,4 +1,4 @@
1
- import { ContractTransactionResponse, ContractRunner } from 'ethers';
1
+ import { ContractTransactionResponse, BaseContract, ContractEventName, EventFilter, ContractRunner } from 'ethers';
2
2
  import * as _vocdoni_davinci_contracts_dist_src_ProcessRegistry from '@vocdoni/davinci-contracts/dist/src/ProcessRegistry';
3
3
 
4
4
  /**
@@ -83,9 +83,14 @@ type TxStatusEvent<T = any> = {
83
83
  };
84
84
  /**
85
85
  * Abstract base class providing common functionality for smart contract interactions.
86
- * Implements transaction handling, status monitoring, and event normalization.
86
+ * Implements transaction handling, status monitoring, event normalization, and
87
+ * event listener management with automatic fallback for RPCs that don't support eth_newFilter.
87
88
  */
88
89
  declare abstract class SmartContractService {
90
+ /** Active polling intervals for event listeners using fallback mode */
91
+ private pollingIntervals;
92
+ /** Default polling interval in milliseconds for event listener fallback */
93
+ protected eventPollingInterval: number;
89
94
  /**
90
95
  * Sends a transaction and yields status events during its lifecycle.
91
96
  * This method handles the complete transaction flow from submission to completion,
@@ -156,6 +161,56 @@ declare abstract class SmartContractService {
156
161
  * ```
157
162
  */
158
163
  protected normalizeListener<Args extends any[]>(callback: (...args: Args) => void): (...listenerArgs: any[]) => void;
164
+ /**
165
+ * Sets up an event listener with automatic fallback for RPCs that don't support eth_newFilter.
166
+ * First attempts to use contract.on() which relies on eth_newFilter. If the RPC doesn't support
167
+ * this method (error code -32601), automatically falls back to polling with queryFilter.
168
+ *
169
+ * @template Args - Tuple type representing the event arguments
170
+ * @param contract - The contract instance to listen to
171
+ * @param eventFilter - The event filter to listen for
172
+ * @param callback - The callback function to invoke when the event occurs
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * this.setupEventListener(
177
+ * this.contract,
178
+ * this.contract.filters.Transfer(),
179
+ * (from: string, to: string, amount: bigint) => {
180
+ * console.log(`Transfer: ${from} -> ${to}: ${amount}`);
181
+ * }
182
+ * );
183
+ * ```
184
+ */
185
+ protected setupEventListener<Args extends any[]>(contract: BaseContract, eventFilter: ContractEventName | EventFilter, callback: (...args: Args) => void): Promise<void>;
186
+ /**
187
+ * Checks if an error indicates that the RPC method is unsupported (eth_newFilter).
188
+ *
189
+ * @param error - The error to check
190
+ * @returns true if the error indicates unsupported method
191
+ */
192
+ private isUnsupportedMethodError;
193
+ /**
194
+ * Sets up a polling-based event listener as fallback when eth_newFilter is not supported.
195
+ * Periodically queries for new events and invokes the callback for each new event found.
196
+ *
197
+ * @template Args - Tuple type representing the event arguments
198
+ * @param contract - The contract instance to poll
199
+ * @param eventFilter - The event filter to poll for
200
+ * @param callback - The callback function to invoke for each event
201
+ */
202
+ private setupPollingListener;
203
+ /**
204
+ * Clears all active polling intervals.
205
+ * Should be called when removing all listeners or cleaning up the service.
206
+ */
207
+ protected clearPollingIntervals(): void;
208
+ /**
209
+ * Sets the polling interval for event listeners using the fallback mechanism.
210
+ *
211
+ * @param intervalMs - Polling interval in milliseconds
212
+ */
213
+ setEventPollingInterval(intervalMs: number): void;
159
214
  }
160
215
 
161
216
  /**