@rainlanguage/ui-components 0.0.1-alpha.76 → 0.0.1-alpha.77

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.
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @module awaitTransactionIndexing
3
+ * @description Utilities for waiting for transactions to be indexed by a subgraph
4
+ */
5
+ import type { SgAddOrderWithOrder, SgRemoveOrderWithOrder, SgTransaction } from '@rainlanguage/orderbook';
6
+ /**
7
+ * Error message when subgraph indexing times out
8
+ */
9
+ export declare const TIMEOUT_ERROR = "The subgraph took too long to respond. Your transaction may still be processing.";
10
+ /**
11
+ * Result of a subgraph indexing operation
12
+ * @template T The type of data returned by the subgraph
13
+ */
14
+ export type IndexingResult<T> = {
15
+ /**
16
+ * The successful result of the indexing operation
17
+ */
18
+ value?: {
19
+ /**
20
+ * The transaction hash
21
+ */
22
+ txHash: string;
23
+ /**
24
+ * Message to display on successful indexing
25
+ */
26
+ successMessage: string;
27
+ /**
28
+ * Optional order hash if available
29
+ */
30
+ orderHash?: string;
31
+ /**
32
+ * Optional network key
33
+ */
34
+ network?: string;
35
+ /**
36
+ * Optional data returned from the subgraph
37
+ */
38
+ data?: T;
39
+ };
40
+ /**
41
+ * Error message if indexing failed
42
+ */
43
+ error?: string;
44
+ };
45
+ /**
46
+ * Generic function to handle waiting for subgraph indexing
47
+ * Returns a promise that resolves with an object containing either value or error
48
+ *
49
+ * @template T The type of data returned by the subgraph
50
+ * @param options Configuration options for the indexing operation
51
+ * @returns Promise resolving to an IndexingResult
52
+ */
53
+ export declare const awaitSubgraphIndexing: <T>(options: {
54
+ /**
55
+ * URL of the subgraph to query
56
+ */
57
+ subgraphUrl: string;
58
+ /**
59
+ * Transaction hash to check for indexing
60
+ */
61
+ txHash: string;
62
+ /**
63
+ * Message to display on successful indexing
64
+ */
65
+ successMessage: string;
66
+ /**
67
+ * Maximum number of attempts before timing out
68
+ */
69
+ maxAttempts?: number;
70
+ /**
71
+ * Interval between attempts in milliseconds
72
+ */
73
+ interval?: number;
74
+ /**
75
+ * Optional network identifier
76
+ */
77
+ network?: string;
78
+ /**
79
+ * Function to fetch data from the subgraph
80
+ * @param subgraphUrl URL of the subgraph
81
+ * @param txHash Transaction hash to query
82
+ */
83
+ fetchData: (subgraphUrl: string, txHash: string) => Promise<T | null | undefined>;
84
+ /**
85
+ * Function to determine if the fetched data indicates success
86
+ * @param data The data returned from the subgraph
87
+ */
88
+ isSuccess: (data: T) => boolean;
89
+ }) => Promise<IndexingResult<T>>;
90
+ /**
91
+ * Configuration for transaction indexing
92
+ * @template T The type of data returned by the subgraph
93
+ */
94
+ export interface TransactionConfig<T> {
95
+ /**
96
+ * URL of the subgraph to query
97
+ */
98
+ subgraphUrl: string;
99
+ /**
100
+ * Transaction hash to check for indexing
101
+ */
102
+ txHash: string;
103
+ /**
104
+ * Message to display on successful indexing
105
+ */
106
+ successMessage: string;
107
+ /**
108
+ * Optional network key
109
+ */
110
+ network?: string;
111
+ /**
112
+ * Maximum number of attempts before timing out
113
+ */
114
+ maxAttempts?: number;
115
+ /**
116
+ * Interval between attempts in milliseconds
117
+ */
118
+ interval?: number;
119
+ /**
120
+ * Function to fetch data from the subgraph
121
+ */
122
+ fetchData: (subgraphUrl: string, txHash: string) => Promise<T>;
123
+ /**
124
+ * Function to determine if the fetched data indicates success
125
+ */
126
+ isSuccess: (data: T) => boolean;
127
+ }
128
+ /**
129
+ * Creates a configuration for checking general transaction indexing
130
+ *
131
+ * @param subgraphUrl URL of the subgraph to query
132
+ * @param txHash Transaction hash to check for indexing
133
+ * @param successMessage Message to display on successful indexing
134
+ * @param network Optional network key
135
+ * @param maxAttempts Maximum number of attempts before timing out
136
+ * @param interval Interval between attempts in milliseconds
137
+ * @returns Configuration for transaction indexing
138
+ */
139
+ export declare const getTransactionConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgTransaction>;
140
+ /**
141
+ * Creates a configuration for checking new order indexing
142
+ *
143
+ * @param subgraphUrl URL of the subgraph to query
144
+ * @param txHash Transaction hash to check for indexing
145
+ * @param successMessage Message to display on successful indexing
146
+ * @param network Optional network key
147
+ * @param maxAttempts Maximum number of attempts before timing out
148
+ * @param interval Interval between attempts in milliseconds
149
+ * @returns Configuration for new order indexing
150
+ */
151
+ export declare const getNewOrderConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgAddOrderWithOrder[]>;
152
+ /**
153
+ * Creates a configuration for checking order removal indexing
154
+ *
155
+ * @param subgraphUrl URL of the subgraph to query
156
+ * @param txHash Transaction hash to check for indexing
157
+ * @param successMessage Message to display on successful indexing
158
+ * @param network Optional network key
159
+ * @param maxAttempts Maximum number of attempts before timing out
160
+ * @param interval Interval between attempts in milliseconds
161
+ * @returns Configuration for order removal indexing
162
+ */
163
+ export declare const getRemoveOrderConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgRemoveOrderWithOrder[]>;
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @module awaitTransactionIndexing
3
+ * @description Utilities for waiting for transactions to be indexed by a subgraph
4
+ */
5
+ import { getTransaction, getTransactionAddOrders, getTransactionRemoveOrders } from '@rainlanguage/orderbook';
6
+ /**
7
+ * Error message when subgraph indexing times out
8
+ */
9
+ export const TIMEOUT_ERROR = 'The subgraph took too long to respond. Your transaction may still be processing.';
10
+ /**
11
+ * Generic function to handle waiting for subgraph indexing
12
+ * Returns a promise that resolves with an object containing either value or error
13
+ *
14
+ * @template T The type of data returned by the subgraph
15
+ * @param options Configuration options for the indexing operation
16
+ * @returns Promise resolving to an IndexingResult
17
+ */
18
+ export const awaitSubgraphIndexing = async (options) => {
19
+ const { subgraphUrl, txHash, successMessage, maxAttempts = 10, interval = 1000, network, fetchData, isSuccess } = options;
20
+ return new Promise((resolve) => {
21
+ let attempts = 0;
22
+ const checkInterval = setInterval(async () => {
23
+ attempts++;
24
+ try {
25
+ const data = await fetchData(subgraphUrl, txHash);
26
+ if (data && isSuccess(data)) {
27
+ clearInterval(checkInterval);
28
+ let orderHash;
29
+ // Extract orderHash from order data if it exists in the expected format
30
+ if (Array.isArray(data) && data.length > 0 && data[0]?.order?.orderHash) {
31
+ orderHash = data[0].order.orderHash;
32
+ }
33
+ resolve({
34
+ value: {
35
+ txHash,
36
+ successMessage,
37
+ orderHash,
38
+ network,
39
+ data
40
+ }
41
+ });
42
+ return;
43
+ }
44
+ }
45
+ catch {
46
+ // Continue with the next attempt
47
+ }
48
+ if (attempts >= maxAttempts) {
49
+ clearInterval(checkInterval);
50
+ resolve({
51
+ error: TIMEOUT_ERROR
52
+ });
53
+ return;
54
+ }
55
+ }, interval);
56
+ });
57
+ };
58
+ /**
59
+ * Creates a configuration for checking general transaction indexing
60
+ *
61
+ * @param subgraphUrl URL of the subgraph to query
62
+ * @param txHash Transaction hash to check for indexing
63
+ * @param successMessage Message to display on successful indexing
64
+ * @param network Optional network key
65
+ * @param maxAttempts Maximum number of attempts before timing out
66
+ * @param interval Interval between attempts in milliseconds
67
+ * @returns Configuration for transaction indexing
68
+ */
69
+ export const getTransactionConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
70
+ subgraphUrl,
71
+ txHash,
72
+ successMessage,
73
+ network,
74
+ maxAttempts,
75
+ interval,
76
+ fetchData: getTransaction,
77
+ isSuccess: (tx) => !!tx
78
+ });
79
+ /**
80
+ * Creates a configuration for checking new order indexing
81
+ *
82
+ * @param subgraphUrl URL of the subgraph to query
83
+ * @param txHash Transaction hash to check for indexing
84
+ * @param successMessage Message to display on successful indexing
85
+ * @param network Optional network key
86
+ * @param maxAttempts Maximum number of attempts before timing out
87
+ * @param interval Interval between attempts in milliseconds
88
+ * @returns Configuration for new order indexing
89
+ */
90
+ export const getNewOrderConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
91
+ subgraphUrl,
92
+ txHash,
93
+ successMessage,
94
+ network,
95
+ maxAttempts,
96
+ interval,
97
+ fetchData: getTransactionAddOrders,
98
+ isSuccess: (addOrders) => addOrders?.length > 0
99
+ });
100
+ /**
101
+ * Creates a configuration for checking order removal indexing
102
+ *
103
+ * @param subgraphUrl URL of the subgraph to query
104
+ * @param txHash Transaction hash to check for indexing
105
+ * @param successMessage Message to display on successful indexing
106
+ * @param network Optional network key
107
+ * @param maxAttempts Maximum number of attempts before timing out
108
+ * @param interval Interval between attempts in milliseconds
109
+ * @returns Configuration for order removal indexing
110
+ */
111
+ export const getRemoveOrderConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
112
+ subgraphUrl,
113
+ txHash,
114
+ successMessage,
115
+ network,
116
+ maxAttempts,
117
+ interval,
118
+ fetchData: getTransactionRemoveOrders,
119
+ isSuccess: (removeOrders) => removeOrders?.length > 0
120
+ });
@@ -1,7 +1,7 @@
1
1
  import { writable } from 'svelte/store';
2
2
  import { sendTransaction, switchChain, waitForTransactionReceipt } from '@wagmi/core';
3
- import { getTransaction, getTransactionAddOrders, getTransactionRemoveOrders } from '@rainlanguage/orderbook';
4
3
  import { getExplorerLink } from '../services/getExplorerLink';
4
+ import { awaitSubgraphIndexing, getNewOrderConfig, getRemoveOrderConfig, getTransactionConfig } from '../services/awaitTransactionIndexing';
5
5
  export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
6
6
  export const ONE = BigInt('1000000000000000000');
7
7
  export var TransactionStatus;
@@ -52,24 +52,13 @@ const transactionStore = () => {
52
52
  status: TransactionStatus.PENDING_SUBGRAPH,
53
53
  message: 'Waiting for transaction to be indexed...'
54
54
  }));
55
- let attempts = 0;
56
- let newTx;
57
- const interval = setInterval(async () => {
58
- attempts++;
59
- try {
60
- newTx = await getTransaction(subgraphUrl, txHash);
61
- if (newTx) {
62
- clearInterval(interval);
63
- transactionSuccess(txHash, successMessage);
64
- }
65
- }
66
- catch {
67
- if (attempts >= 10) {
68
- clearInterval(interval);
69
- return transactionError(TransactionErrorMessage.TIMEOUT);
70
- }
71
- }
72
- }, 1000);
55
+ const result = await awaitSubgraphIndexing(getTransactionConfig(subgraphUrl, txHash, successMessage));
56
+ if (result.error) {
57
+ return transactionError(TransactionErrorMessage.TIMEOUT);
58
+ }
59
+ if (result.value) {
60
+ return transactionSuccess(result.value.txHash, result.value.successMessage);
61
+ }
73
62
  };
74
63
  const awaitNewOrderIndexing = async (subgraphUrl, txHash, network) => {
75
64
  update((state) => ({
@@ -77,23 +66,13 @@ const transactionStore = () => {
77
66
  status: TransactionStatus.PENDING_SUBGRAPH,
78
67
  message: 'Waiting for new order to be indexed...'
79
68
  }));
80
- let attempts = 0;
81
- const interval = setInterval(async () => {
82
- attempts++;
83
- try {
84
- const addOrders = await getTransactionAddOrders(subgraphUrl, txHash);
85
- if (addOrders?.length > 0) {
86
- clearInterval(interval);
87
- return transactionSuccess(txHash, '', addOrders[0].order.orderHash, network);
88
- }
89
- }
90
- catch {
91
- if (attempts >= 10) {
92
- clearInterval(interval);
93
- return transactionError(TransactionErrorMessage.TIMEOUT);
94
- }
95
- }
96
- }, 1000);
69
+ const result = await awaitSubgraphIndexing(getNewOrderConfig(subgraphUrl, txHash, '', network));
70
+ if (result.error) {
71
+ return transactionError(TransactionErrorMessage.TIMEOUT);
72
+ }
73
+ if (result.value) {
74
+ return transactionSuccess(result.value.txHash, result.value.successMessage, result.value.orderHash, result.value.network);
75
+ }
97
76
  };
98
77
  const awaitRemoveOrderIndexing = async (subgraphUrl, txHash) => {
99
78
  update((state) => ({
@@ -101,23 +80,13 @@ const transactionStore = () => {
101
80
  status: TransactionStatus.PENDING_SUBGRAPH,
102
81
  message: 'Waiting for order removal to be indexed...'
103
82
  }));
104
- let attempts = 0;
105
- const interval = setInterval(async () => {
106
- attempts++;
107
- try {
108
- const removeOrders = await getTransactionRemoveOrders(subgraphUrl, txHash);
109
- if (removeOrders?.length > 0) {
110
- clearInterval(interval);
111
- return transactionSuccess(txHash, 'Order removed successfully');
112
- }
113
- }
114
- catch {
115
- if (attempts >= 10) {
116
- clearInterval(interval);
117
- return transactionError(TransactionErrorMessage.TIMEOUT);
118
- }
119
- }
120
- }, 1000);
83
+ const result = await awaitSubgraphIndexing(getRemoveOrderConfig(subgraphUrl, txHash, 'Order removed successfully'));
84
+ if (result.error) {
85
+ return transactionError(TransactionErrorMessage.TIMEOUT);
86
+ }
87
+ if (result.value) {
88
+ return transactionSuccess(result.value.txHash, result.value.successMessage);
89
+ }
121
90
  };
122
91
  const checkingWalletAllowance = (message) => update((state) => ({
123
92
  ...state,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rainlanguage/ui-components",
3
- "version": "0.0.1-alpha.76",
3
+ "version": "0.0.1-alpha.77",
4
4
  "description": "A component library for building Svelte applications to be used with Raindex.",
5
5
  "license": "LicenseRef-DCL-1.0",
6
6
  "author": "Rain Open Source Software Ltd",
@@ -53,7 +53,7 @@
53
53
  "@fontsource/dm-sans": "5.1.0",
54
54
  "@imask/svelte": "7.6.1",
55
55
  "@observablehq/plot": "0.6.16",
56
- "@rainlanguage/orderbook": "0.0.1-alpha.76",
56
+ "@rainlanguage/orderbook": "0.0.1-alpha.77",
57
57
  "@reown/appkit": "1.6.4",
58
58
  "@reown/appkit-adapter-wagmi": "1.6.4",
59
59
  "@sentry/sveltekit": "7.120.0",