@runesx/api-client 0.3.0 → 0.4.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/src/socket.mjs CHANGED
@@ -18,12 +18,30 @@ export function setupSocket(config) {
18
18
  reconnectionDelayMax: 5000,
19
19
  });
20
20
 
21
- const errorCount = { count: 0 }; // Track connection errors
21
+ const errorCount = { count: 0 };
22
+
23
+ // User-registered event callbacks
24
+ const callbacks = {};
25
+
26
+ function emit(event, data) {
27
+ if (callbacks[event]) {
28
+ callbacks[event].forEach((cb) => {
29
+ try { cb(data); } catch (e) { console.error(`Error in ${event} callback:`, e.message); }
30
+ });
31
+ }
32
+ }
33
+
34
+ const heartbeatInterval = setInterval(() => {
35
+ if (socket.connected) {
36
+ socket.emit('ping');
37
+ }
38
+ }, 30000);
22
39
 
23
40
  socket.on('connect', () => {
24
41
  socket.emit('join_public');
25
42
  socket.emit('join_private');
26
43
  errorCount.count = 0;
44
+ emit('connect', null);
27
45
  });
28
46
 
29
47
  socket.on('connect_error', (err) => {
@@ -36,32 +54,30 @@ export function setupSocket(config) {
36
54
  resetWallets();
37
55
  resetUserShares();
38
56
  }
57
+ emit('connect_error', err);
39
58
  });
40
59
 
41
60
  socket.on('disconnect', (reason) => {
42
61
  console.log('Disconnected from Socket.IO server:', reason);
62
+ clearInterval(heartbeatInterval);
43
63
  resetPools();
44
64
  resetCoins();
45
65
  resetChains();
46
66
  resetWallets();
47
67
  resetUserShares();
68
+ emit('disconnect', reason);
48
69
  });
49
70
 
50
- socket.on('reconnect_attempt', (attempt) => {
71
+ socket.io.on('reconnect_attempt', (attempt) => {
51
72
  console.log(`Reconnect attempt #${attempt}`);
73
+ emit('reconnect_attempt', attempt);
52
74
  });
53
75
 
54
- socket.on('reconnect', () => {
55
- socket.emit('join_public');
56
- socket.emit('join_private');
57
- resetPools();
58
- resetCoins();
59
- resetChains();
60
- resetWallets();
61
- resetUserShares();
76
+ socket.io.on('reconnect', () => {
77
+ emit('reconnect', null);
62
78
  });
63
79
 
64
- socket.on('reconnect_error', (err) => {
80
+ socket.io.on('reconnect_error', (err) => {
65
81
  console.log('Reconnect error:', err.message);
66
82
  errorCount.count += 1;
67
83
  if (errorCount.count >= 3) {
@@ -71,187 +87,207 @@ export function setupSocket(config) {
71
87
  resetWallets();
72
88
  resetUserShares();
73
89
  }
90
+ emit('reconnect_error', err);
74
91
  });
75
92
 
76
93
  socket.on('error', (err) => {
77
94
  console.log('Socket error:', err.message);
95
+ emit('error', err);
78
96
  });
79
97
 
98
+ // ---- Public room events ----
99
+
80
100
  socket.on('pools_updated', ({ pools, isInitial }) => {
81
- // console.log('Received pools_updated:', { pools, isInitial });
82
101
  if (isInitial) {
83
102
  setInitialPools(pools);
84
103
  } else {
85
104
  pools.forEach(pool => updatePool(pool));
86
105
  }
106
+ emit('pools_updated', { pools, isInitial });
87
107
  });
88
108
 
89
109
  socket.on('coins_updated', ({ coins, isInitial }) => {
90
- // console.log('Received coins_updated:', { coins, isInitial });
91
110
  if (isInitial) {
92
111
  setInitialCoins(coins);
93
112
  } else {
94
113
  coins.forEach(coin => updateCoin(coin));
95
114
  }
115
+ emit('coins_updated', { coins, isInitial });
96
116
  });
97
117
 
98
118
  socket.on('chains_updated', ({ chains, isInitial }) => {
99
- // console.log('Received chains_updated:', { chains, isInitial });
100
119
  if (isInitial) {
101
120
  setInitialChains(chains);
102
121
  } else {
103
122
  chains.forEach(chain => updateChain(chain));
104
123
  }
124
+ emit('chains_updated', { chains, isInitial });
125
+ });
126
+
127
+ socket.on('buckets_updated', (data) => {
128
+ emit('buckets_updated', data);
129
+ });
130
+
131
+ socket.on('operations_updated', (operations) => {
132
+ emit('operations_updated', operations);
133
+ });
134
+
135
+ socket.on('recent_yard_messages', (data) => {
136
+ emit('recent_yard_messages', data);
137
+ });
138
+
139
+ socket.on('status_updated', (data) => {
140
+ emit('status_updated', data);
141
+ });
142
+
143
+ socket.on('volumeUpdate', (data) => {
144
+ emit('volumeUpdate', data);
105
145
  });
106
146
 
147
+ socket.on('operationUpdate', (operation) => {
148
+ emit('operationUpdate', operation);
149
+ });
150
+
151
+ socket.on('candlestick_updated', (data) => {
152
+ emit('candlestick_updated', data);
153
+ });
154
+
155
+ // ---- Private room events ----
156
+
107
157
  socket.on('wallets_updated', ({ wallets, isInitial }) => {
108
- // console.log('Received wallets_updated:', { wallets, isInitial });
109
158
  if (isInitial) {
110
159
  setInitialWallets(wallets);
111
160
  } else {
112
161
  wallets.forEach(wallet => updateWallet(wallet));
113
162
  }
163
+ emit('wallets_updated', { wallets, isInitial });
114
164
  });
115
165
 
116
166
  socket.on('user_shares_updated', ({ userShares, isInitial }) => {
117
- // console.log('Received user_shares_updated:', { userShares, isInitial });
118
167
  if (isInitial) {
119
168
  setInitialUserShares(userShares);
120
169
  } else {
121
170
  userShares.forEach(share => updateUserShare(share));
122
171
  }
172
+ emit('user_shares_updated', { userShares, isInitial });
123
173
  });
124
174
 
125
- socket.on('volumeUpdate', ({ type, poolId, timestamp, volume }) => {
126
- // console.log('Volume update:', { type, poolId, timestamp, volume });
127
- });
128
-
129
- socket.on('operationUpdate', (operation) => {
130
- // console.log('Operation update:', operation);
131
- // Assuming operation includes liquidity deposit/withdraw results
132
- // if (operation.type === 'liquidity_deposit' || operation.type === 'liquidity_withdraw') {
133
- // console.log(`Liquidity operation ${operation.type} completed:`, {
134
- // uid: operation.uid,
135
- // coinA: operation.coinA,
136
- // coinB: operation.coinB,
137
- // amountA: operation.amountA,
138
- // amountB: operation.amountB,
139
- // shares: operation.shares,
140
- // status: operation.status,
141
- // });
142
- // }
143
- });
144
- socket.on('status_updated', (data) => {
145
- // console.log('Status update:', data);
146
- });
147
-
148
- socket.on('deposit_address_generated', ({ requestId, chainName, address, memo }) => {
149
- // console.log('Deposit address generated:', { requestId, chainName, address, memo });
175
+ socket.on('deposit_address_generated', (data) => {
176
+ emit('deposit_address_generated', data);
150
177
  });
151
178
 
152
179
  socket.on('deposit_processed', (data) => {
153
- const { amount, coin, chain, confirmations, status, credited } = data;
154
- const message =
155
- status === 'confirmed' && credited
156
- ? `Deposit of ${amount} ${coin.ticker} confirmed!`
157
- : `Deposit of ${amount} ${coin.ticker} (confirming) [${confirmations}/${chain.requiredConfirmations}]`;
158
- console.log('Deposit processed:', { message, data });
180
+ emit('deposit_processed', data);
159
181
  });
160
182
 
161
183
  socket.on('withdrawal_processed', (data) => {
162
- const { amount, coin, chain, confirmations, status, credited, createdAt } = data;
163
- let message;
164
- if (!createdAt) {
165
- message = `Withdrawal of ${amount} ${coin.ticker} is stalled due to network congestion.`;
166
- } else if (status === 'confirmed' && credited) {
167
- message = `Withdrawal of ${amount} ${coin.ticker} confirmed!`;
168
- } else {
169
- message = `Withdrawal of ${amount} ${coin.ticker} (confirming) [${confirmations}/${chain.requiredConfirmations}]`;
170
- }
171
- console.log('Withdrawal processed:', { message, data });
184
+ emit('withdrawal_processed', data);
172
185
  });
173
186
 
174
187
  socket.on('withdrawal_initiated', (data) => {
175
- console.log('Withdrawal initiated:', data);
188
+ emit('withdrawal_initiated', data);
189
+ });
190
+
191
+ socket.on('withdrawal_pin_generated', (data) => {
192
+ emit('withdrawal_pin_generated', data);
176
193
  });
177
194
 
178
- socket.on('withdrawal_pin_generated', ({ pinImage, ticker, amount, pendingWithdrawalId, expiresAt, dp, fee, memoRequired }) => {
179
- console.log('Withdrawal pin generated:', {
180
- ticker,
181
- amount,
182
- pinImage,
183
- pendingWithdrawalId,
184
- expiresAt,
185
- dp,
186
- fee,
187
- memoRequired,
188
- });
195
+ socket.on('withdrawal_queued', (data) => {
196
+ emit('withdrawal_queued', data);
189
197
  });
190
198
 
191
- socket.on('withdrawal_queued', ({ pendingWithdrawalId, ticker }) => {
192
- console.log('Withdrawal queued:', { pendingWithdrawalId, ticker });
199
+ socket.on('withdrawal_canceled', (data) => {
200
+ emit('withdrawal_canceled', data);
193
201
  });
194
202
 
195
- socket.on('withdrawal_canceled', ({ ticker, amount }) => {
196
- console.log('Withdrawal canceled:', { ticker, amount });
203
+ socket.on('withdrawal_updated', (data) => {
204
+ emit('withdrawal_updated', data);
197
205
  });
198
206
 
199
- // socket.on('recent_yard_messages', (initialMessages) => {
200
- // console.log('Recent yard messages:', initialMessages);
201
- // });
207
+ socket.on('withdrawal_expired', (data) => {
208
+ emit('withdrawal_expired', data);
209
+ });
202
210
 
203
211
  socket.on('yard_message', (message) => {
204
- // console.log('New chat message:', {
205
- // username: message.username,
206
- // text: message.text,
207
- // role: message.role,
208
- // timestamp: new Date(message.timestamp).toLocaleString(),
209
- // });
212
+ emit('yard_message', message);
210
213
  });
211
214
 
212
- socket.on('message_deleted', ({ messageId }) => {
213
- // console.log('Message deleted:', { messageId });
215
+ socket.on('message_deleted', (data) => {
216
+ emit('message_deleted', data);
214
217
  });
215
218
 
216
- socket.on('banned', ({ reason, bannedUntil }) => {
217
- // console.log('Banned from yard:', {
218
- // reason,
219
- // bannedUntil: new Date(bannedUntil).toLocaleString(),
220
- // });
219
+ socket.on('banned', (data) => {
220
+ emit('banned', data);
221
221
  });
222
222
 
223
- socket.on('withdrawal_updated', ({ pendingWithdrawalId, expiresAt, stage }) => {
224
- // console.log('Withdrawal updated:', { pendingWithdrawalId, expiresAt, stage });
223
+ socket.on('yard_read_marked', (data) => {
224
+ emit('yard_read_marked', data);
225
225
  });
226
226
 
227
- socket.on('withdrawal_expired', ({ pendingWithdrawalId, ticker, amount }) => {
228
- // console.log('Withdrawal expired:', { pendingWithdrawalId, ticker, amount });
227
+ socket.on('session_expired', (data) => {
228
+ emit('session_expired', data);
229
229
  });
230
230
 
231
231
  socket.on('pong', () => {
232
- console.log('Received pong from server');
232
+ emit('pong', null);
233
233
  });
234
234
 
235
- const heartbeatInterval = setInterval(() => {
236
- if (socket.connected) {
237
- socket.emit('ping');
238
- }
239
- }, 30000);
240
-
241
- socket.on('close', () => {
242
- clearInterval(heartbeatInterval);
243
- resetPools();
244
- resetCoins();
245
- resetChains();
246
- resetWallets();
247
- resetUserShares();
248
- });
235
+ // ---- Convenience methods ----
249
236
 
250
- socket.on('connect', () => {
251
- // socket.emit('yard_message', {
252
- // text: 'Hello from RunesX API Key Example Bot!!',
253
- // });
254
- });
237
+ function on(event, callback) {
238
+ if (!callbacks[event]) {
239
+ callbacks[event] = [];
240
+ }
241
+ callbacks[event].push(callback);
242
+ }
255
243
 
256
- return { socket };
257
- }
244
+ function off(event, callback) {
245
+ if (!callbacks[event]) { return; }
246
+ if (callback) {
247
+ callbacks[event] = callbacks[event].filter((cb) => cb !== callback);
248
+ } else {
249
+ delete callbacks[event];
250
+ }
251
+ }
252
+
253
+ function joinCandlesticks(poolId, timeframe) {
254
+ socket.emit('join_candlesticks', { poolId, timeframe });
255
+ }
256
+
257
+ function leaveCandlesticks(poolId, timeframe) {
258
+ socket.emit('leave_candlesticks', { poolId, timeframe });
259
+ }
260
+
261
+ function sendYardMessage(text) {
262
+ socket.emit('yard_message', { text });
263
+ }
264
+
265
+ function deleteMessage(messageId) {
266
+ socket.emit('delete_message', { messageId });
267
+ }
268
+
269
+ function markYardRead() {
270
+ socket.emit('mark_yard_read');
271
+ }
272
+
273
+ function leavePublic() {
274
+ socket.leave('public');
275
+ }
276
+
277
+ function leavePrivate() {
278
+ socket.leave('private');
279
+ }
280
+
281
+ return {
282
+ socket,
283
+ on,
284
+ off,
285
+ joinCandlesticks,
286
+ leaveCandlesticks,
287
+ sendYardMessage,
288
+ deleteMessage,
289
+ markYardRead,
290
+ leavePublic,
291
+ leavePrivate,
292
+ };
293
+ }