@settlemint/sdk-portal 2.3.2-pr8227fae7 → 2.3.2-pr8d084a79

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
@@ -30,6 +30,7 @@
30
30
  - [Examples](#examples)
31
31
  - [Deploy contract](#deploy-contract)
32
32
  - [Get pending transactions](#get-pending-transactions)
33
+ - [Monitoring alerting](#monitoring-alerting)
33
34
  - [Send transaction using hd wallet](#send-transaction-using-hd-wallet)
34
35
  - [API Reference](#api-reference)
35
36
  - [Functions](#functions)
@@ -40,6 +41,8 @@
40
41
  - [Interfaces](#interfaces)
41
42
  - [HandleWalletVerificationChallengeOptions\<Setup\>](#handlewalletverificationchallengeoptionssetup)
42
43
  - [Transaction](#transaction)
44
+ - [TransactionEvent](#transactionevent)
45
+ - [TransactionReceipt](#transactionreceipt)
43
46
  - [WaitForTransactionReceiptOptions](#waitfortransactionreceiptoptions)
44
47
  - [WebsocketClientOptions](#websocketclientoptions)
45
48
  - [Type Aliases](#type-aliases)
@@ -219,6 +222,236 @@ const query = portalGraphql(`
219
222
  const result = await portalClient.request(query);
220
223
  console.log(`There are ${result.getPendingTransactions?.count} pending transactions`);
221
224
 
225
+ ```
226
+ ### Monitoring alerting
227
+
228
+ ```ts
229
+ /**
230
+ * This example demonstrates how to implement real-time transaction monitoring and alerting.
231
+ *
232
+ * The process involves:
233
+ * 1. Creating a WebSocket subscription to monitor all blockchain transactions
234
+ * 2. Setting up custom handlers for different monitoring scenarios
235
+ * 3. Processing transactions in real-time as they are confirmed
236
+ * 4. Implementing specific monitoring functions for addresses, events, and failures
237
+ * 5. Triggering alerts based on predefined conditions
238
+ *
239
+ * This pattern is useful for applications that need to:
240
+ * - Detect suspicious activities for security purposes
241
+ * - Track high-value transfers or specific contract interactions
242
+ * - Monitor for failed transactions that require attention
243
+ * - Implement compliance reporting and audit trails
244
+ * - Build automated workflows that respond to on-chain events
245
+ * - Provide real-time notifications to stakeholders
246
+ */
247
+
248
+ import type { FormattedExecutionResult } from "graphql";
249
+ import { type Transaction, type WebsocketClientOptions, getWebsocketClient } from "../portal.js"; // Replace this path with "@settlemint/sdk-portal"
250
+
251
+ /**
252
+ * Handlers for different monitoring scenarios
253
+ * You can implement your own handlers
254
+ */
255
+ export type AlertHandlers = {
256
+ onAddressActivity: (transaction: Transaction, addresses: string[]) => void;
257
+ onEvent: (transaction: Transaction, eventNames: string[]) => void;
258
+ onFailure: (transaction: Transaction) => void;
259
+ };
260
+
261
+ /**
262
+ * Monitors all blockchain transactions by subscribing to transaction updates via GraphQL.
263
+ * This function continuously logs all transaction receipts as they are received.
264
+ *
265
+ * @param options - Configuration options for connecting to the Portal API
266
+ * @param handlers - Optional handlers for different monitoring scenarios
267
+ * @throws Error if the subscription fails
268
+ *
269
+ * @example
270
+ * import { monitorAllTransactions } from "@settlemint/sdk-portal";
271
+ *
272
+ * monitorAllTransactions({
273
+ * portalGraphqlEndpoint: "https://example.settlemint.com/graphql",
274
+ * accessToken: "your-access-token"
275
+ * }, {
276
+ * onAddressActivity: (tx, address) => {
277
+ * console.log(`Address ${address} was involved in transaction ${tx.transactionHash}`);
278
+ * },
279
+ * onEvent: (tx, eventName) => {
280
+ * console.log(`Event ${eventName} detected in transaction ${tx.transactionHash}`);
281
+ * },
282
+ * onFailure: (tx, reason) => {
283
+ * console.log(`Transaction ${tx.transactionHash} failed: ${reason}`);
284
+ * }
285
+ * });
286
+ */
287
+ export function monitorAllTransactions(options: WebsocketClientOptions, handlers: AlertHandlers) {
288
+ const wsClient = getWebsocketClient(options);
289
+
290
+ const subscription = wsClient.iterate<{
291
+ getProcessedTransactions: {
292
+ records: Transaction[];
293
+ };
294
+ }>({
295
+ query: `subscription getProcessedTransactions {
296
+ getProcessedTransactions(pageSize: 1) {
297
+ records {
298
+ receipt {
299
+ transactionHash
300
+ to
301
+ status
302
+ from
303
+ type
304
+ revertReason
305
+ revertReasonDecoded
306
+ logs
307
+ events
308
+ contractAddress
309
+ }
310
+ transactionHash
311
+ from
312
+ createdAt
313
+ address
314
+ functionName
315
+ isContract
316
+ }
317
+ }
318
+ }`,
319
+ });
320
+
321
+ // Start the monitoring process
322
+ processSubscription(subscription, handlers);
323
+
324
+ return subscription;
325
+ }
326
+
327
+ /**
328
+ * Internal helper to process the subscription stream
329
+ */
330
+ async function processSubscription(
331
+ subscription: AsyncIterable<
332
+ FormattedExecutionResult<
333
+ {
334
+ getProcessedTransactions: {
335
+ records: Transaction[];
336
+ };
337
+ },
338
+ unknown
339
+ >
340
+ >,
341
+ handlers: AlertHandlers,
342
+ ) {
343
+ (async () => {
344
+ for await (const result of subscription) {
345
+ if (result?.data?.getProcessedTransactions?.records) {
346
+ const records = result.data.getProcessedTransactions.records;
347
+ const transaction = records.at(-1);
348
+
349
+ if (transaction) {
350
+ processTransaction(transaction, handlers);
351
+ }
352
+ }
353
+ }
354
+ })();
355
+ }
356
+
357
+ /**
358
+ * Process a single transaction with the configured handlers
359
+ */
360
+ function processTransaction(transaction: Transaction, handlers: AlertHandlers) {
361
+ // Monitor specific addresses (example addresses)
362
+ handlers.onAddressActivity(transaction, ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e"]);
363
+
364
+ // Monitor for specific events
365
+ handlers.onEvent(transaction, ["Transfer", "Approval"]);
366
+
367
+ // Monitor for failed transactions
368
+ handlers.onFailure(transaction);
369
+ }
370
+
371
+ /**
372
+ * Monitors transactions from or to specific addresses.
373
+ *
374
+ * @param transaction - The transaction to check
375
+ * @param addresses - The addresses to monitor
376
+ *
377
+ * @example
378
+ * import { monitorSpecificAddresses } from "@settlemint/sdk-portal";
379
+ *
380
+ * monitorSpecificAddresses(transaction, ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e"]);
381
+ */
382
+ export function monitorSpecificAddresses(transaction: Transaction, addresses: string[]) {
383
+ const { from, address } = transaction;
384
+ const { to } = transaction.receipt;
385
+ const isInvolved = addresses.some((address) => [from, to].includes(address));
386
+
387
+ if (isInvolved) {
388
+ notify(`[ADDRESS] Address ${address} was involved in transaction ${transaction.transactionHash}`);
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Monitors transactions for specific contract events.
394
+ *
395
+ * @param transaction - The transaction to check
396
+ * @param eventNames - The event names to monitor
397
+ *
398
+ * @example
399
+ * import { monitorContractEvents } from "@settlemint/sdk-portal";
400
+ *
401
+ * monitorContractEvents(transaction, ["Transfer", "Approval"]);
402
+ */
403
+ export function monitorContractEvents(transaction: Transaction, eventNames: string[]) {
404
+ const events = transaction.receipt.events;
405
+
406
+ const eventDetected = events.find((event) => eventNames.includes(event.eventName));
407
+ if (eventDetected) {
408
+ notify(`[EVENT] Event ${eventDetected.eventName} detected in transaction ${transaction.transactionHash}`);
409
+ }
410
+ }
411
+
412
+ /**
413
+ * Monitors for failed transactions that require attention.
414
+ *
415
+ * @param transaction - The transaction to check
416
+ *
417
+ * @example
418
+ * import { monitorFailedTransactions } from "@settlemint/sdk-portal";
419
+ *
420
+ * monitorFailedTransactions(transaction, "Unknown reason");
421
+ */
422
+ export function monitorFailedTransactions(transaction: Transaction) {
423
+ const status = transaction.receipt?.status;
424
+
425
+ if (status === "Reverted") {
426
+ const reason = transaction.receipt.revertReasonDecoded;
427
+ notify(`[FAILED] Transaction ${transaction.transactionHash} failed: ${reason}`);
428
+ }
429
+ }
430
+
431
+ const notify = (message: string) => {
432
+ console.log(message);
433
+ };
434
+
435
+ /**
436
+ * Example usage - monitoring specific on-chain activity
437
+ */
438
+ export function runMonitoringExample() {
439
+ // Basic usage
440
+ monitorAllTransactions(
441
+ {
442
+ portalGraphqlEndpoint: "https://example.settlemint.com/graphql",
443
+ accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,
444
+ },
445
+ {
446
+ onAddressActivity: monitorSpecificAddresses,
447
+ onEvent: monitorContractEvents,
448
+ onFailure: monitorFailedTransactions,
449
+ },
450
+ );
451
+ }
452
+
453
+ runMonitoringExample();
454
+
222
455
  ```
223
456
  ### Send transaction using hd wallet
224
457
 
@@ -449,7 +682,7 @@ const result = await portalClient.request(query);
449
682
 
450
683
  > **getWebsocketClient**(`options`): `Client`
451
684
 
452
- Defined in: [sdk/portal/src/utils/websocket-client.ts:23](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L23)
685
+ Defined in: [sdk/portal/src/utils/websocket-client.ts:30](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L30)
453
686
 
454
687
  Creates a GraphQL WebSocket client for the Portal API
455
688
 
@@ -465,13 +698,24 @@ Creates a GraphQL WebSocket client for the Portal API
465
698
 
466
699
  The GraphQL WebSocket client
467
700
 
701
+ ##### Example
702
+
703
+ ```ts
704
+ import { getWebsocketClient } from "@settlemint/sdk-portal";
705
+
706
+ const client = getWebsocketClient({
707
+ portalGraphqlEndpoint: "https://portal.settlemint.com/graphql",
708
+ accessToken: "your-access-token",
709
+ });
710
+ ```
711
+
468
712
  ***
469
713
 
470
714
  #### handleWalletVerificationChallenge()
471
715
 
472
716
  > **handleWalletVerificationChallenge**\<`Setup`\>(`options`): `Promise`\<\{ `challengeResponse`: `string`; `verificationId?`: `string`; \}\>
473
717
 
474
- Defined in: [sdk/portal/src/utils/wallet-verification-challenge.ts:106](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L106)
718
+ Defined in: [sdk/portal/src/utils/wallet-verification-challenge.ts:103](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L103)
475
719
 
476
720
  Handles a wallet verification challenge by generating an appropriate response
477
721
 
@@ -524,7 +768,7 @@ const result = await handleWalletVerificationChallenge({
524
768
 
525
769
  > **waitForTransactionReceipt**(`transactionHash`, `options`): `Promise`\<[`Transaction`](#transaction)\>
526
770
 
527
- Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:79](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L79)
771
+ Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:83](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L83)
528
772
 
529
773
  Waits for a blockchain transaction receipt by subscribing to transaction updates via GraphQL.
530
774
  This function polls until the transaction is confirmed or the timeout is reached.
@@ -562,29 +806,80 @@ const transaction = await waitForTransactionReceipt("0x123...", {
562
806
 
563
807
  #### HandleWalletVerificationChallengeOptions\<Setup\>
564
808
 
565
- Defined in: [sdk/portal/src/utils/wallet-verification-challenge.ts:73](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L73)
809
+ Defined in: [sdk/portal/src/utils/wallet-verification-challenge.ts:64](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L64)
566
810
 
567
811
  Options for handling a wallet verification challenge
568
812
 
569
813
  ##### Type Parameters
570
814
 
571
- | Type Parameter | Description |
572
- | ------ | ------ |
573
- | `Setup` *extends* `AbstractSetupSchema` | The GraphQL schema setup type |
815
+ | Type Parameter |
816
+ | ------ |
817
+ | `Setup` *extends* `AbstractSetupSchema` |
818
+
819
+ ##### Properties
820
+
821
+ | Property | Type | Description | Defined in |
822
+ | ------ | ------ | ------ | ------ |
823
+ | <a id="code"></a> `code` | `string` \| `number` | The verification code provided by the user | [sdk/portal/src/utils/wallet-verification-challenge.ts:74](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L74) |
824
+ | <a id="portalclient"></a> `portalClient` | `GraphQLClient` | The portal client instance | [sdk/portal/src/utils/wallet-verification-challenge.ts:66](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L66) |
825
+ | <a id="portalgraphql"></a> `portalGraphql` | `initGraphQLTada`\<`Setup`\> | The GraphQL query builder | [sdk/portal/src/utils/wallet-verification-challenge.ts:68](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L68) |
826
+ | <a id="userwalletaddress"></a> `userWalletAddress` | `` `0x${string}` `` | The wallet address to verify | [sdk/portal/src/utils/wallet-verification-challenge.ts:72](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L72) |
827
+ | <a id="verificationid"></a> `verificationId` | `string` | The ID of the verification challenge | [sdk/portal/src/utils/wallet-verification-challenge.ts:70](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L70) |
828
+ | <a id="verificationtype"></a> `verificationType` | `"otp"` \| `"secret-code"` \| `"pincode"` | The type of verification being performed | [sdk/portal/src/utils/wallet-verification-challenge.ts:76](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wallet-verification-challenge.ts#L76) |
574
829
 
575
830
  ***
576
831
 
577
832
  #### Transaction
578
833
 
579
- Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:26](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L26)
834
+ Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:37](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L37)
580
835
 
581
836
  Represents the structure of a blockchain transaction with its receipt
582
837
 
838
+ ##### Properties
839
+
840
+ | Property | Type | Description | Defined in |
841
+ | ------ | ------ | ------ | ------ |
842
+ | <a id="address"></a> `address` | `string` | The contract address involved in the transaction | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:46](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L46) |
843
+ | <a id="createdat"></a> `createdAt` | `string` | Timestamp when the transaction was created | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:44](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L44) |
844
+ | <a id="from"></a> `from` | `string` | The sender address (duplicate of receipt.from) | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:42](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L42) |
845
+ | <a id="functionname"></a> `functionName` | `string` | The name of the function called in the transaction | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:48](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L48) |
846
+ | <a id="iscontract"></a> `isContract` | `boolean` | Whether the transaction is a contract deployment | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:50](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L50) |
847
+ | <a id="transactionhash"></a> `transactionHash` | `string` | The hash of the transaction (duplicate of receipt.transactionHash) | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:40](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L40) |
848
+
849
+ ***
850
+
851
+ #### TransactionEvent
852
+
853
+ Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:14](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L14)
854
+
855
+ Represents an event emitted during a transaction execution
856
+
857
+ ***
858
+
859
+ #### TransactionReceipt
860
+
861
+ Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:23](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L23)
862
+
863
+ Represents the structure of a blockchain transaction receipt
864
+
865
+ ##### Extends
866
+
867
+ - `TransactionReceipt`\<`string`, `number`, `"Success"` \| `"Reverted"`\>
868
+
869
+ ##### Properties
870
+
871
+ | Property | Type | Description | Overrides | Defined in |
872
+ | ------ | ------ | ------ | ------ | ------ |
873
+ | <a id="contractaddress"></a> `contractAddress` | `` `0x${string}` `` | The address of the contract deployed in the transaction | `TransactionReceiptViem.contractAddress` | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:31](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L31) |
874
+ | <a id="events"></a> `events` | [`TransactionEvent`](#transactionevent)[] | Array of events emitted during the transaction | - | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:29](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L29) |
875
+ | <a id="revertreason"></a> `revertReason` | `string` | The raw reason for transaction reversion, if applicable | - | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:25](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L25) |
876
+ | <a id="revertreasondecoded"></a> `revertReasonDecoded` | `string` | Human-readable version of the revert reason | - | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:27](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L27) |
877
+
583
878
  ***
584
879
 
585
880
  #### WaitForTransactionReceiptOptions
586
881
 
587
- Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:57](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L57)
882
+ Defined in: [sdk/portal/src/utils/wait-for-transaction-receipt.ts:60](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L60)
588
883
 
589
884
  Options for waiting for a transaction receipt
590
885
 
@@ -592,11 +887,19 @@ Options for waiting for a transaction receipt
592
887
 
593
888
  - [`WebsocketClientOptions`](#websocketclientoptions)
594
889
 
890
+ ##### Properties
891
+
892
+ | Property | Type | Description | Inherited from | Defined in |
893
+ | ------ | ------ | ------ | ------ | ------ |
894
+ | <a id="accesstoken"></a> `accessToken` | `string` | The access token for authentication with the Portal API | [`WebsocketClientOptions`](#websocketclientoptions).[`accessToken`](#accesstoken-1) | [sdk/portal/src/utils/websocket-client.ts:14](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L14) |
895
+ | <a id="portalgraphqlendpoint"></a> `portalGraphqlEndpoint` | `string` | The GraphQL endpoint URL for the Portal API | [`WebsocketClientOptions`](#websocketclientoptions).[`portalGraphqlEndpoint`](#portalgraphqlendpoint-1) | [sdk/portal/src/utils/websocket-client.ts:10](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L10) |
896
+ | <a id="timeout"></a> `timeout?` | `number` | Optional timeout in milliseconds before the operation fails | - | [sdk/portal/src/utils/wait-for-transaction-receipt.ts:62](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/wait-for-transaction-receipt.ts#L62) |
897
+
595
898
  ***
596
899
 
597
900
  #### WebsocketClientOptions
598
901
 
599
- Defined in: [sdk/portal/src/utils/websocket-client.ts:10](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L10)
902
+ Defined in: [sdk/portal/src/utils/websocket-client.ts:6](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L6)
600
903
 
601
904
  Options for the GraphQL WebSocket client
602
905
 
@@ -604,6 +907,13 @@ Options for the GraphQL WebSocket client
604
907
 
605
908
  - [`WaitForTransactionReceiptOptions`](#waitfortransactionreceiptoptions)
606
909
 
910
+ ##### Properties
911
+
912
+ | Property | Type | Description | Defined in |
913
+ | ------ | ------ | ------ | ------ |
914
+ | <a id="accesstoken-1"></a> `accessToken` | `string` | The access token for authentication with the Portal API | [sdk/portal/src/utils/websocket-client.ts:14](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L14) |
915
+ | <a id="portalgraphqlendpoint-1"></a> `portalGraphqlEndpoint` | `string` | The GraphQL endpoint URL for the Portal API | [sdk/portal/src/utils/websocket-client.ts:10](https://github.com/settlemint/sdk/blob/v2.3.2/sdk/portal/src/utils/websocket-client.ts#L10) |
916
+
607
917
  ### Type Aliases
608
918
 
609
919
  #### ClientOptions
package/dist/portal.cjs CHANGED
@@ -33,6 +33,81 @@ var import_validation = require("@settlemint/sdk-utils/validation");
33
33
  var import_gql = require("gql.tada");
34
34
  var import_graphql_request = require("graphql-request");
35
35
  var import_v4 = require("zod/v4");
36
+ var import_gql2 = require("gql.tada");
37
+
38
+ // src/utils/websocket-client.ts
39
+ var import_graphql_ws = require("graphql-ws");
40
+ function getWebsocketClient({ portalGraphqlEndpoint, accessToken }) {
41
+ if (!portalGraphqlEndpoint) {
42
+ throw new Error("portalGraphqlEndpoint is required");
43
+ }
44
+ if (!accessToken) {
45
+ throw new Error("accessToken is required");
46
+ }
47
+ const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));
48
+ return (0, import_graphql_ws.createClient)({
49
+ url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}`
50
+ });
51
+ }
52
+ function setWsProtocol(url) {
53
+ if (url.protocol === "ws:" || url.protocol === "wss:") {
54
+ return url;
55
+ }
56
+ if (url.protocol === "http:") {
57
+ url.protocol = "ws:";
58
+ } else {
59
+ url.protocol = "wss:";
60
+ }
61
+ return url;
62
+ }
63
+
64
+ // src/utils/wait-for-transaction-receipt.ts
65
+ async function waitForTransactionReceipt(transactionHash, options) {
66
+ const wsClient = getWebsocketClient(options);
67
+ const subscription = wsClient.iterate({
68
+ query: `subscription getTransaction($transactionHash: String!) {
69
+ getTransaction(transactionHash: $transactionHash) {
70
+ receipt {
71
+ transactionHash
72
+ to
73
+ status
74
+ from
75
+ type
76
+ revertReason
77
+ revertReasonDecoded
78
+ logs
79
+ events
80
+ contractAddress
81
+ }
82
+ transactionHash
83
+ from
84
+ createdAt
85
+ address
86
+ functionName
87
+ isContract
88
+ }
89
+ }`,
90
+ variables: { transactionHash }
91
+ });
92
+ const promises = [getTransactionFromSubscription(subscription)];
93
+ if (options.timeout) {
94
+ promises.push(createTimeoutPromise(options.timeout));
95
+ }
96
+ return Promise.race(promises);
97
+ }
98
+ function createTimeoutPromise(timeout) {
99
+ return new Promise((_, reject) => {
100
+ setTimeout(() => reject(new Error("Transaction receipt not found")), timeout);
101
+ });
102
+ }
103
+ async function getTransactionFromSubscription(subscription) {
104
+ for await (const result of subscription) {
105
+ if (result?.data?.getTransaction?.receipt) {
106
+ return result.data.getTransaction;
107
+ }
108
+ }
109
+ throw new Error("No transaction found");
110
+ }
36
111
 
37
112
  // src/utils/wallet-verification-challenge.ts
38
113
  var import_node_crypto = require("crypto");
@@ -112,82 +187,7 @@ async function handleWalletVerificationChallenge({
112
187
  }
113
188
  }
114
189
 
115
- // src/utils/websocket-client.ts
116
- var import_graphql_ws = require("graphql-ws");
117
- function getWebsocketClient({ portalGraphqlEndpoint, accessToken }) {
118
- if (!portalGraphqlEndpoint) {
119
- throw new Error("portalGraphqlEndpoint is required");
120
- }
121
- if (!accessToken) {
122
- throw new Error("accessToken is required");
123
- }
124
- const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));
125
- return (0, import_graphql_ws.createClient)({
126
- url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}`
127
- });
128
- }
129
- function setWsProtocol(url) {
130
- if (url.protocol === "ws:" || url.protocol === "wss:") {
131
- return url;
132
- }
133
- if (url.protocol === "http:") {
134
- url.protocol = "ws:";
135
- } else {
136
- url.protocol = "wss:";
137
- }
138
- return url;
139
- }
140
-
141
- // src/utils/wait-for-transaction-receipt.ts
142
- async function waitForTransactionReceipt(transactionHash, options) {
143
- const wsClient = getWebsocketClient(options);
144
- const subscription = wsClient.iterate({
145
- query: `subscription getTransaction($transactionHash: String!) {
146
- getTransaction(transactionHash: $transactionHash) {
147
- receipt {
148
- transactionHash
149
- to
150
- status
151
- from
152
- type
153
- revertReason
154
- revertReasonDecoded
155
- logs
156
- events
157
- contractAddress
158
- }
159
- transactionHash
160
- from
161
- createdAt
162
- address
163
- functionName
164
- isContract
165
- }
166
- }`,
167
- variables: { transactionHash }
168
- });
169
- const promises = [getTransactionFromSubscription(subscription)];
170
- if (options.timeout) {
171
- promises.push(createTimeoutPromise(options.timeout));
172
- }
173
- return Promise.race(promises);
174
- }
175
- function createTimeoutPromise(timeout) {
176
- return new Promise((_, reject) => {
177
- setTimeout(() => reject(new Error("Transaction receipt not found")), timeout);
178
- });
179
- }
180
- async function getTransactionFromSubscription(subscription) {
181
- for await (const result of subscription) {
182
- if (result?.data?.getTransaction?.receipt) {
183
- return result.data.getTransaction;
184
- }
185
- }
186
- throw new Error("No transaction found");
187
- }
188
-
189
190
  // src/portal.ts
190
- var import_gql2 = require("gql.tada");
191
191
  var ClientOptionsSchema = import_v4.z.object({
192
192
  instance: import_validation.UrlOrPathSchema,
193
193
  accessToken: import_validation.ApplicationAccessTokenSchema,