@@ -5,7 +5,7 @@ import { decodeUTF8 } from 'tweetnacl-util';
5
5
import WebSocket from 'ws' ;
6
6
7
7
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
9
9
10
10
type Quote = {
11
11
bidPrice : BN ;
@@ -25,7 +25,7 @@ export class IndicativeQuotesSender {
25
25
private ws : WebSocket | null = null ;
26
26
private connected = false ;
27
27
28
- private quotes : Map < number , Quote > = new Map ( ) ;
28
+ private quotes : Map < number , Quote [ ] > = new Map ( ) ;
29
29
30
30
constructor (
31
31
private endpoint : string ,
@@ -87,15 +87,19 @@ export class IndicativeQuotesSender {
87
87
) {
88
88
this . sendQuotesInterval = setInterval ( ( ) => {
89
89
if ( this . connected ) {
90
- for ( const [ marketIndex , quote ] of this . quotes . entries ( ) ) {
90
+ for ( const [ marketIndex , quotes ] of this . quotes . entries ( ) ) {
91
91
const message = {
92
- market_type : 'perp' ,
93
92
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
+ } ) ,
99
103
} ;
100
104
try {
101
105
if (
@@ -156,27 +160,37 @@ export class IndicativeQuotesSender {
156
160
} , this . heartbeatIntervalMs ) ;
157
161
}
158
162
159
- setQuote ( quote : Quote ) : void {
163
+ setQuote ( newQuotes : Quote | Quote [ ] ) : void {
160
164
if ( ! this . connected ) {
161
165
console . warn ( 'Setting quote before connected to the server, ignoring' ) ;
162
166
}
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 ;
176
185
}
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 ) ) ;
178
193
}
179
- this . quotes . set ( quote . marketIndex , quote ) ;
180
194
}
181
195
182
196
private reconnect ( ) {
0 commit comments