Skip to content

Commit 610e15c

Browse files
nour/indic-quotes-sender-v2 (#1667)
* nour/indic-quotes-sender-v2 * prettify
1 parent 7d5e094 commit 610e15c

File tree

2 files changed

+188
-65
lines changed

2 files changed

+188
-65
lines changed

sdk/src/indicative-quotes/indicativeQuotesSender.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { decodeUTF8 } from 'tweetnacl-util';
55
import WebSocket from 'ws';
66

77
const SEND_INTERVAL = 500;
8-
const MAX_BUFFERED_AMOUNT = 10 * 1024; // 10 KB as worst case scenario
8+
const MAX_BUFFERED_AMOUNT = 20 * 1024; // 20 KB as worst case scenario
99

1010
type Quote = {
1111
bidPrice: BN;
@@ -25,7 +25,7 @@ export class IndicativeQuotesSender {
2525
private ws: WebSocket | null = null;
2626
private connected = false;
2727

28-
private quotes: Map<number, Quote> = new Map();
28+
private quotes: Map<number, Quote[]> = new Map();
2929

3030
constructor(
3131
private endpoint: string,
@@ -87,15 +87,19 @@ export class IndicativeQuotesSender {
8787
) {
8888
this.sendQuotesInterval = setInterval(() => {
8989
if (this.connected) {
90-
for (const [marketIndex, quote] of this.quotes.entries()) {
90+
for (const [marketIndex, quotes] of this.quotes.entries()) {
9191
const message = {
92-
market_type: 'perp',
9392
market_index: marketIndex,
94-
bid_price: quote.bidPrice.toString(),
95-
ask_price: quote.askPrice.toString(),
96-
bid_size: quote.bidBaseAssetAmount.toString(),
97-
ask_size: quote.askBaseAssetAmount.toString(),
98-
is_oracle_offset: quote.isOracleOffset,
93+
market_type: 'perp',
94+
quotes: quotes.map((quote) => {
95+
return {
96+
bid_price: quote.bidPrice.toString(),
97+
ask_price: quote.askPrice.toString(),
98+
bid_size: quote.bidBaseAssetAmount.toString(),
99+
ask_size: quote.askBaseAssetAmount.toString(),
100+
is_oracle_offset: quote.isOracleOffset,
101+
};
102+
}),
99103
};
100104
try {
101105
if (
@@ -156,27 +160,37 @@ export class IndicativeQuotesSender {
156160
}, this.heartbeatIntervalMs);
157161
}
158162

159-
setQuote(quote: Quote): void {
163+
setQuote(newQuotes: Quote | Quote[]): void {
160164
if (!this.connected) {
161165
console.warn('Setting quote before connected to the server, ignoring');
162166
}
163-
if (
164-
quote.marketIndex == null ||
165-
quote.bidPrice == null ||
166-
quote.askPrice == null ||
167-
quote.bidBaseAssetAmount == null ||
168-
quote.askBaseAssetAmount == null
169-
) {
170-
console.warn(
171-
'Received incomplete quote, ignoring and deleting old quote',
172-
quote
173-
);
174-
if (quote.marketIndex != null) {
175-
this.quotes.delete(quote.marketIndex);
167+
const quotes = Array.isArray(newQuotes) ? newQuotes : [newQuotes];
168+
const newQuoteMap = new Map<number, Quote[]>();
169+
for (const quote of quotes) {
170+
if (
171+
quote.marketIndex == null ||
172+
quote.bidPrice == null ||
173+
quote.askPrice == null ||
174+
quote.bidBaseAssetAmount == null ||
175+
quote.askBaseAssetAmount == null
176+
) {
177+
console.warn(
178+
'Received incomplete quote, ignoring and deleting old quote',
179+
quote
180+
);
181+
if (quote.marketIndex != null) {
182+
this.quotes.delete(quote.marketIndex);
183+
}
184+
return;
176185
}
177-
return;
186+
if (!newQuoteMap.has(quote.marketIndex)) {
187+
newQuoteMap.set(quote.marketIndex, []);
188+
}
189+
newQuoteMap.get(quote.marketIndex)?.push(quote);
190+
}
191+
for (const marketIndex of newQuoteMap.keys()) {
192+
this.quotes.set(marketIndex, newQuoteMap.get(marketIndex));
178193
}
179-
this.quotes.set(quote.marketIndex, quote);
180194
}
181195

182196
private reconnect() {

0 commit comments

Comments
 (0)