@runesx/api-client 0.4.0 → 0.5.1

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.
@@ -3,6 +3,7 @@ import { coinStore, getCoins } from './store/coinStore.mjs';
3
3
  import { chainStore, getChains } from './store/chainStore.mjs';
4
4
  import { walletStore, getWallets } from './store/walletStore.mjs';
5
5
  import { userSharesStore, getUserShares } from './store/userSharesStore.mjs';
6
+ import { orderbookStore, getAllOrderBooks } from './store/orderbookStore.mjs';
6
7
 
7
8
  export function waitForStores(socket) {
8
9
  // Function to wait for poolStore to be populated
@@ -18,7 +19,7 @@ export function waitForStores(socket) {
18
19
  reject(new Error('Timeout waiting for initial pool data'));
19
20
  }, 30000); // 30-second timeout
20
21
 
21
- socket.on('pools_updated', ({ isInitial, pools }) => {
22
+ socket.on('pools_updated', ({ isInitial }) => {
22
23
  if (isInitial) {
23
24
  const pools = getPools();
24
25
  clearTimeout(timeout);
@@ -53,7 +54,7 @@ export function waitForStores(socket) {
53
54
  reject(new Error('Timeout waiting for initial coin data'));
54
55
  }, 30000); // 30-second timeout
55
56
 
56
- socket.on('coins_updated', ({ isInitial, coins }) => {
57
+ socket.on('coins_updated', ({ isInitial }) => {
57
58
  if (isInitial) {
58
59
  const coins = getCoins();
59
60
  clearTimeout(timeout);
@@ -123,7 +124,7 @@ export function waitForStores(socket) {
123
124
  reject(new Error('Timeout waiting for initial wallet data'));
124
125
  }, 30000); // 30-second timeout
125
126
 
126
- socket.on('wallets_updated', ({ isInitial, wallets }) => {
127
+ socket.on('wallets_updated', ({ isInitial }) => {
127
128
  if (isInitial) {
128
129
  const wallets = getWallets();
129
130
  clearTimeout(timeout);
@@ -158,7 +159,7 @@ export function waitForStores(socket) {
158
159
  reject(new Error('Timeout waiting for initial user shares data'));
159
160
  }, 30000); // 30-second timeout
160
161
 
161
- socket.on('user_shares_updated', ({ isInitial, userShares }) => {
162
+ socket.on('user_shares_updated', ({ isInitial }) => {
162
163
  if (isInitial) {
163
164
  const userShares = getUserShares();
164
165
  clearTimeout(timeout);
@@ -180,13 +181,47 @@ export function waitForStores(socket) {
180
181
  });
181
182
  };
182
183
 
184
+ // Function to wait for orderbookStore to be populated
185
+ const waitForOrderBooks = () => {
186
+ return new Promise((resolve, reject) => {
187
+ if (orderbookStore.isInitialReceived) {
188
+ resolve(getAllOrderBooks());
189
+ return;
190
+ }
191
+
192
+ const timeout = setTimeout(() => {
193
+ reject(new Error('Timeout waiting for initial orderbook data'));
194
+ }, 30000);
195
+
196
+ socket.on('orderbooks_initial', ({ isInitial }) => {
197
+ if (isInitial) {
198
+ clearTimeout(timeout);
199
+ resolve(getAllOrderBooks());
200
+ }
201
+ });
202
+
203
+ socket.on('connect_error', (err) => {
204
+ console.error('Socket connect error:', err.message);
205
+ clearTimeout(timeout);
206
+ reject(new Error('Socket connection failed'));
207
+ });
208
+
209
+ socket.on('disconnect', (reason) => {
210
+ console.error('Socket disconnected:', reason);
211
+ clearTimeout(timeout);
212
+ reject(new Error('Socket disconnected before receiving initial orderbook data'));
213
+ });
214
+ });
215
+ };
216
+
183
217
  // Return a promise that resolves when all stores are populated
184
- return Promise.all([waitForPools(), waitForCoins(), waitForChains(), waitForWallets(), waitForUserShares()])
185
- .then(([pools, coins, chains, wallets, userShares]) => ({
218
+ return Promise.all([waitForPools(), waitForCoins(), waitForChains(), waitForWallets(), waitForUserShares(), waitForOrderBooks()])
219
+ .then(([pools, coins, chains, wallets, userShares, orderbooks]) => ({
186
220
  pools,
187
221
  coins,
188
222
  chains,
189
223
  wallets,
190
224
  userShares,
225
+ orderbooks,
191
226
  }));
192
227
  }
@@ -4,9 +4,9 @@ import { parentPort } from 'worker_threads';
4
4
  import { estimateSwap } from '../utils/swapUtils.mjs';
5
5
 
6
6
  parentPort.on('message', async (data) => {
7
- const { inputCoin, outputCoin, amountIn, pools, coins, maxHops, algorithm } = data;
7
+ const { inputCoin, outputCoin, amountIn, pools, coins, maxHops, algorithm, orderbooks, userOrders, clobFees, marketsLookup } = data;
8
8
  try {
9
- const result = await estimateSwap(inputCoin, outputCoin, amountIn, pools, coins, maxHops, algorithm);
9
+ const result = await estimateSwap(inputCoin, outputCoin, amountIn, pools, coins, maxHops, algorithm, orderbooks, userOrders, clobFees, marketsLookup);
10
10
  parentPort.postMessage({ result });
11
11
  } catch (err) {
12
12
  parentPort.postMessage({ error: err.message });