@covalenthq/client-sdk 2.2.6 → 2.3.0

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
@@ -46,7 +46,7 @@ The GoldRush SDK is the fastest way to integrate the GoldRush API for working wi
46
46
  const balanceResp =
47
47
  await client.BalanceService.getTokenBalancesForWalletAddress(
48
48
  "eth-mainnet",
49
- "demo.eth"
49
+ "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de"
50
50
  );
51
51
 
52
52
  if (balanceResp.error) {
@@ -60,7 +60,7 @@ The GoldRush SDK is the fastest way to integrate the GoldRush API for working wi
60
60
  };
61
61
  ```
62
62
 
63
- The `GoldRushClient` can be configured with a second object argument, `settings`. The settings offered are
63
+ The `GoldRushClient` can be configured with a second object argument, `settings`, and a third argument for `streamingConfig`. The settings offered are
64
64
 
65
65
  1. **debug**: A boolean that enables server logs of the API calls, enhanced with the request URL, response time and code, and other settings. It is set as `false` by default.
66
66
  2. **threadCount**: A number that sets the number of concurrent requests allowed. It is set as `2` by default.
@@ -69,6 +69,15 @@ The GoldRush SDK is the fastest way to integrate the GoldRush API for working wi
69
69
  5. **retryDelay**: A number that sets the delay in `ms` for retrying the API endpoint in case of `429 - rate limit` error. To be in effect, it requires `enableRetry` to be enabled. It is set as `2` by default.
70
70
  6. **source**: A string that defines the `source` of the request of better analytics.
71
71
 
72
+ The `streamingConfig` is optional and configures the real-time streaming service. The available options are:
73
+
74
+ 1. **shouldRetry**: A function that determines whether to retry connection based on retry count. Defaults to retry up to 5 times.
75
+ 2. **maxReconnectAttempts**: Maximum number of reconnection attempts. Defaults to `5`.
76
+ 3. **onConnecting**: Callback function triggered when connecting to the streaming service.
77
+ 4. **onOpened**: Callback function triggered when connection is successfully established.
78
+ 5. **onClosed**: Callback function triggered when connection is closed.
79
+ 6. **onError**: Callback function triggered when an error occurs.
80
+
72
81
  ## Features
73
82
 
74
83
  ### 1. Name Resolution
@@ -89,7 +98,7 @@ For example, the following sets the `quoteCurrency` query parameter to `CAD` and
89
98
  ```ts
90
99
  const resp = await client.BalanceService.getTokenBalancesForWalletAddress(
91
100
  "eth-mainnet",
92
- "demo.eth",
101
+ "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de",
93
102
  {
94
103
  quoteCurrency: "CAD",
95
104
  nft: true,
@@ -112,7 +121,7 @@ import {
112
121
 
113
122
  const resp = await client.BalanceService.getTokenBalancesForWalletAddress(
114
123
  "eth-mainnet",
115
- "demo.eth",
124
+ "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de",
116
125
  {
117
126
  quoteCurrency: "CAD",
118
127
  nft: true,
@@ -136,7 +145,7 @@ The SDK supports the following formats for the chain input:
136
145
 
137
146
  For paginated responses, the GoldRush API can at max support 100 items per page. However, the Covalent SDK leverages generators to _seamlessly fetch all items without the user having to deal with pagination_.
138
147
 
139
- For example, the following fetches ALL transactions for `demo.eth` on Ethereum:
148
+ For example, the following fetches ALL transactions for `0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de` on Ethereum:
140
149
 
141
150
  ```ts
142
151
  import { GoldRushClient } from "@covalenthq/client-sdk";
@@ -146,7 +155,7 @@ const ApiServices = async () => {
146
155
  try {
147
156
  for await (const tx of client.TransactionService.getAllTransactionsForAddress(
148
157
  "eth-mainnet",
149
- "demo.eth"
158
+ "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de"
150
159
  )) {
151
160
  console.log("tx", tx);
152
161
  }
@@ -166,7 +175,7 @@ import { GoldRushClient } from "@covalenthq/client-sdk";
166
175
  const client = new GoldRushClient("<YOUR_API_KEY>");
167
176
  const resp = await client.TransactionService.getAllTransactionsForAddressByPage(
168
177
  "eth-mainnet",
169
- "demo.eth"
178
+ "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de"
170
179
  ); // assuming resp.data.current_page is 10
171
180
  if (resp.data !== null) {
172
181
  const prevPage = await resp.data.prev(); // will retrieve page 9
@@ -216,7 +225,219 @@ if (resp.data !== null) {
216
225
 
217
226
  You can explore more examples [here](./test/unit/timestamp-parser.test.ts)
218
227
 
219
- ### 8. Standardized (Error) Responses
228
+ ### 8. Real-time Streaming
229
+
230
+ The GoldRush SDK now supports real-time streaming of blockchain data via WebSocket connections. This allows you to subscribe to live data feeds for OHLCV (Open, High, Low, Close, Volume) data for trading pairs and tokens, new DEX pair creations, wallet activity, and more.
231
+
232
+ ```ts
233
+ import {
234
+ GoldRushClient,
235
+ StreamingChain,
236
+ StreamingInterval,
237
+ StreamingTimeframe,
238
+ StreamingProtocol,
239
+ } from "@covalenthq/client-sdk";
240
+
241
+ const client = new GoldRushClient(
242
+ "<YOUR_API_KEY>",
243
+ {},
244
+ {
245
+ onConnecting: () => console.log("Connecting to streaming service..."),
246
+ onOpened: () => console.log("Connected to streaming service!"),
247
+ onClosed: () => console.log("Disconnected from streaming service"),
248
+ onError: (error) => console.error("Streaming error:", error),
249
+ }
250
+ );
251
+
252
+ // Subscribe to OHLCV data for trading pairs
253
+ const unsubscribePairs = client.StreamingService.subscribeToOHLCVPairs(
254
+ {
255
+ chain_name: StreamingChain.BASE_MAINNET,
256
+ pair_addresses: ["0x9c087Eb773291e50CF6c6a90ef0F4500e349B903"],
257
+ interval: StreamingInterval.ONE_MINUTE,
258
+ timeframe: StreamingTimeframe.ONE_HOUR,
259
+ },
260
+ {
261
+ next: (data) => {
262
+ console.log("Received OHLCV pair data:", data);
263
+ },
264
+ error: (error) => {
265
+ console.error("Streaming error:", error);
266
+ },
267
+ complete: () => {
268
+ console.log("Stream completed");
269
+ },
270
+ }
271
+ );
272
+
273
+ // Unsubscribe when done
274
+ // unsubscribePairs();
275
+
276
+ // Disconnect from streaming service when finished
277
+ // await client.StreamingService.disconnect();
278
+ ```
279
+
280
+ #### Multiple Subscriptions
281
+
282
+ The SDK uses a singleton WebSocket client internally, allowing you to create multiple subscriptions through the same `GoldRushClient`.
283
+
284
+ ```ts
285
+ // Create a single client
286
+ const client = new GoldRushClient("<YOUR_API_KEY>");
287
+
288
+ // Create multiple subscriptions through the same connection
289
+ const unsubscribePairs = client.StreamingService.subscribeToOHLCVPairs(
290
+ {
291
+ chain_name: StreamingChain.BASE_MAINNET,
292
+ pair_addresses: ["0x9c087Eb773291e50CF6c6a90ef0F4500e349B903"],
293
+ interval: StreamingInterval.ONE_MINUTE,
294
+ timeframe: StreamingTimeframe.ONE_HOUR,
295
+ },
296
+ {
297
+ next: (data) => console.log("OHLCV Pairs:", data),
298
+ }
299
+ );
300
+
301
+ const unsubscribeTokens = client.StreamingService.subscribeToOHLCVTokens(
302
+ {
303
+ chain_name: StreamingChain.BASE_MAINNET,
304
+ token_addresses: ["0x4B6104755AfB5Da4581B81C552DA3A25608c73B8"],
305
+ interval: StreamingInterval.ONE_MINUTE,
306
+ timeframe: StreamingTimeframe.ONE_HOUR,
307
+ },
308
+ {
309
+ next: (data) => console.log("OHLCV Tokens:", data),
310
+ }
311
+ );
312
+
313
+ const unsubscribeWallet = client.StreamingService.subscribeToWalletActivity(
314
+ {
315
+ chain_name: StreamingChain.BASE_MAINNET,
316
+ wallet_addresses: ["0xbaed383ede0e5d9d72430661f3285daa77e9439f"],
317
+ },
318
+ {
319
+ next: (data) => console.log("Wallet Activity:", data),
320
+ }
321
+ );
322
+
323
+ // Unsubscribe from individual streams when needed
324
+ // unsubscribePairs();
325
+ // unsubscribeTokens();
326
+ // unsubscribeWallet();
327
+
328
+ // Or disconnect from all streams at once
329
+ // await client.StreamingService.disconnect();
330
+ ```
331
+
332
+ #### React Integration
333
+
334
+ For React applications, use the `useEffect` hook to properly manage subscription lifecycle:
335
+
336
+ ```tsx
337
+ useEffect(() => {
338
+ const unsubscribe = client.StreamingService.rawQuery(
339
+ `subscription {
340
+ ohlcvCandlesForPair(
341
+ chain_name: BASE_MAINNET
342
+ pair_addresses: ["0x9c087Eb773291e50CF6c6a90ef0F4500e349B903"],
343
+ interval: StreamingInterval.ONE_MINUTE,
344
+ timeframe: StreamingTimeframe.ONE_HOUR,
345
+ ) {
346
+ open
347
+ high
348
+ low
349
+ close
350
+ volume
351
+ price_usd
352
+ volume_usd
353
+ chain_name
354
+ pair_address
355
+ interval
356
+ timeframe
357
+ timestamp
358
+ }
359
+ }`,
360
+ {},
361
+ {
362
+ next: (data) => console.log("Received streaming data:", data),
363
+ error: (err) => console.error("Subscription error:", err),
364
+ complete: () => console.info("Subscription completed"),
365
+ }
366
+ );
367
+
368
+ // Cleanup function - unsubscribe when component unmounts
369
+ return () => {
370
+ unsubscribe();
371
+ };
372
+ }, []);
373
+ ```
374
+
375
+ #### Raw Queries
376
+
377
+ You can also use raw GraphQL queries for more streamlined and selective data scenarios:
378
+
379
+ ```ts
380
+ const unsubscribeNewPairs = client.StreamingService.rawQuery(
381
+ `subscription {
382
+ newPairs(
383
+ chain_name: BASE_MAINNET,
384
+ protocols: [UNISWAP_V2, UNISWAP_V3]
385
+ ) {
386
+ chain_name
387
+ protocol
388
+ pair_address
389
+ tx_hash
390
+ liquidity
391
+ base_token_metadata {
392
+ contract_name
393
+ contract_ticker_symbol
394
+ }
395
+ quote_token_metadata {
396
+ contract_name
397
+ contract_ticker_symbol
398
+ }
399
+ }
400
+ }`,
401
+ {},
402
+ {
403
+ next: (data) => console.log("Raw new pairs data:", data),
404
+ error: (error) => console.error("Error:", error),
405
+ }
406
+ );
407
+
408
+ // Raw query for token OHLCV data
409
+ const unsubscribeTokenOHLCV = client.StreamingService.rawQuery(
410
+ `subscription {
411
+ ohlcvCandlesForToken(
412
+ chain_name: BASE_MAINNET
413
+ token_addresses: ["0x4B6104755AfB5Da4581B81C552DA3A25608c73B8"]
414
+ interval: ONE_MINUTE
415
+ timeframe: ONE_HOUR
416
+ ) {
417
+ open
418
+ high
419
+ low
420
+ close
421
+ volume
422
+ volume_usd
423
+ quote_rate
424
+ quote_rate_usd
425
+ timestamp
426
+ base_token {
427
+ contract_name
428
+ contract_ticker_symbol
429
+ }
430
+ }
431
+ }`,
432
+ {},
433
+ {
434
+ next: (data) => console.log("Raw token OHLCV data:", data),
435
+ error: (error) => console.error("Error:", error),
436
+ }
437
+ );
438
+ ```
439
+
440
+ ### 9. Standardized (Error) Responses
220
441
 
221
442
  All the responses are of the same standardized format
222
443
 
@@ -343,6 +564,23 @@ The Covalent SDK provides comprehensive support for all Class A, Class B, and Pr
343
564
  - `getEarliestTransactionsForAddress()`: Fetch and render the earliest transactions involving an address. Frequently seen in wallet applications.
344
565
  </details>
345
566
 
567
+ <details>
568
+ <summary>
569
+ 9. <strong>Streaming Service</strong>: Real-time blockchain data streaming via WebSocket connections
570
+ </summary>
571
+
572
+ - `getClient()`: Get the underlying GraphQL WebSocket client for direct access.
573
+ - `isConnected`: Check the connection status of the streaming service.
574
+ - `subscribeToOHLCVPairs()`: Subscribe to real-time OHLCV (Open, High, Low, Close, Volume) data for specific trading pairs. Supports multiple chains and configurable intervals and timeframes.
575
+ - `subscribeToOHLCVTokens()`: Subscribe to real-time OHLCV (Open, High, Low, Close, Volume) data for specific tokens. Tracks token prices across their primary DEX pools with configurable intervals and timeframes.
576
+ - `subscribeToTokenBalances()`: Subscribe to real-time token balance updates for a specific wallet address. Tracks balance changes across native and ERC-20 tokens with USD valuations.
577
+ - `subscribeToWalletActivity()`: Subscribe to real-time wallet activity including transactions, token transfers, and smart contract interactions. Provides decoded transaction details for swaps, transfers, bridges, deposits, withdrawals, and approvals.
578
+ - `subscribeToNewPairs()`: Subscribe to real-time notifications when new liquidity pairs are created on supported decentralized exchanges (DEXes). Supports Uniswap V2/V3 across Base, Ethereum, and BSC networks.
579
+ - `rawQuery()`: Execute custom GraphQL subscription queries for advanced streaming scenarios and future extensibility.
580
+ - `disconnect()`: Properly disconnect from the streaming service.
581
+
582
+ </details>
583
+
346
584
  ## Contributing
347
585
 
348
586
  Contributions, issues and feature requests are welcome!
@@ -12,4 +12,5 @@ export * from "./src/utils/types/Generic.types";
12
12
  export * from "./src/utils/types/NftService.types";
13
13
  export * from "./src/utils/types/PricingService.types";
14
14
  export * from "./src/utils/types/SecurityService.types";
15
+ export * from "./src/utils/types/StreamingService.types";
15
16
  export * from "./src/utils/types/TransactionService.types";