@@ -6,6 +6,7 @@ import { DEFAULT_DECIMALS } from "@src/constants";
6
6
import { SpotMarketOrder } from "@src/entity" ;
7
7
import useVM from "@src/hooks/useVM" ;
8
8
import BN from "@src/utils/BN" ;
9
+ import { groupOrders } from "@src/utils/groupOrders" ;
9
10
import { IntervalUpdater } from "@src/utils/IntervalUpdater" ;
10
11
import { RootStore , useStores } from "@stores" ;
11
12
@@ -26,8 +27,8 @@ export const SpotOrderbookVMProvider: React.FC<IProps> = ({ children }) => {
26
27
export const useSpotOrderbookVM = ( ) => useVM ( ctx ) ;
27
28
28
29
type TOrderbookData = {
29
- buy : Array < SpotMarketOrder > ;
30
- sell : Array < SpotMarketOrder > ;
30
+ buy : SpotMarketOrder [ ] ;
31
+ sell : SpotMarketOrder [ ] ;
31
32
spreadPercent : string ;
32
33
spreadPrice : string ;
33
34
} ;
@@ -37,13 +38,16 @@ const UPDATE_INTERVAL = 2 * 1000;
37
38
class SpotOrderbookVM {
38
39
rootStore : RootStore ;
39
40
41
+ allBuyOrders : SpotMarketOrder [ ] = [ ] ;
42
+ allSellOrders : SpotMarketOrder [ ] = [ ] ;
43
+
40
44
orderbook : TOrderbookData = {
41
45
buy : [ ] ,
42
46
sell : [ ] ,
43
47
spreadPercent : "0.00" ,
44
48
spreadPrice : "" ,
45
49
} ;
46
- decimalKey : string = "0" ;
50
+ decimalGroup = 2 ;
47
51
orderFilter : SPOT_ORDER_FILTER = SPOT_ORDER_FILTER . SELL_AND_BUY ;
48
52
amountOfOrders : number = 0 ;
49
53
@@ -115,7 +119,17 @@ class SpotOrderbookVM {
115
119
116
120
setAmountOfOrders = ( value : number ) => ( this . amountOfOrders = value ) ;
117
121
118
- setDecimalKey = ( value : string ) => ( this . decimalKey = value ) ;
122
+ setDecimalGroup = ( value : number ) => {
123
+ this . decimalGroup = value ;
124
+
125
+ const buyOrdersCombinedByDecimal = groupOrders ( this . allBuyOrders , value ) ;
126
+ const sellOrdersCombinedByDecimal = groupOrders ( this . allSellOrders , value ) ;
127
+
128
+ this . setOrderbook ( {
129
+ buy : buyOrdersCombinedByDecimal ,
130
+ sell : sellOrdersCombinedByDecimal ,
131
+ } ) ;
132
+ } ;
119
133
120
134
setOrderFilter = ( value : SPOT_ORDER_FILTER ) => ( this . orderFilter = value ) ;
121
135
@@ -127,7 +141,7 @@ class SpotOrderbookVM {
127
141
if ( ! this . rootStore . initialized || ! market ) return ;
128
142
129
143
const bcNetwork = FuelNetwork . getInstance ( ) ;
130
- const limit = 100 ;
144
+ const limit = 200 ;
131
145
132
146
this . isOrderBookLoading = true ;
133
147
@@ -136,28 +150,46 @@ class SpotOrderbookVM {
136
150
bcNetwork ! . fetchSpotOrders ( { baseToken : market . baseToken . assetId , type : "SELL" , limit } ) ,
137
151
] ) ;
138
152
139
- //bid = max of buy
140
- const buyPrices = buy . map ( ( order ) => order . price ) ;
141
- const maxBuyPrice = buyPrices . reduce ( ( max , current ) => ( current . gt ( max ) ? current : max ) , buyPrices [ 0 ] ) ;
153
+ this . allBuyOrders = buy ;
154
+ this . allSellOrders = sell ;
142
155
143
- //ask = min of sell
144
- const sellPrices = sell . map ( ( order ) => order . price ) ;
145
- const minSellPrice = sellPrices . reduce ( ( min , current ) => ( current . lt ( min ) ? current : min ) , sellPrices [ 0 ] ) ;
156
+ const buyOrdersCombinedByDecimal = groupOrders ( this . allBuyOrders , this . decimalGroup ) ;
157
+ const sellOrdersCombinedByDecimal = groupOrders ( this . allSellOrders , this . decimalGroup ) ;
158
+
159
+ const getPrice = ( orders : SpotMarketOrder [ ] , priceType : "max" | "min" ) : BN => {
160
+ const compareType = priceType === "max" ? "gt" : "lt" ;
161
+ return orders . reduce ( ( value , order ) => ( order . price [ compareType ] ( value ) ? order . price : value ) , orders [ 0 ] . price ) ;
162
+ } ;
163
+
164
+ const maxBuyPrice = getPrice ( this . allBuyOrders , "max" ) ;
165
+ const minSellPrice = getPrice ( this . allSellOrders , "min" ) ;
146
166
147
167
if ( maxBuyPrice && minSellPrice ) {
148
168
// spread = ask - bid
149
169
const spread = minSellPrice . minus ( maxBuyPrice ) ;
150
170
const formattedSpread = BN . formatUnits ( spread , DEFAULT_DECIMALS ) . toSignificant ( 2 ) ;
151
-
152
- const spreadPercent = minSellPrice . minus ( maxBuyPrice ) . div ( maxBuyPrice ) . times ( 100 ) ;
153
- this . setOrderbook ( { buy, sell, spreadPercent : spreadPercent . toFormat ( 2 ) , spreadPrice : formattedSpread } ) ;
171
+ const spreadPercent = spread . div ( maxBuyPrice ) . times ( 100 ) ;
172
+
173
+ this . setOrderbook ( {
174
+ buy : buyOrdersCombinedByDecimal ,
175
+ sell : sellOrdersCombinedByDecimal ,
176
+ spreadPercent : spreadPercent . toFormat ( 2 ) ,
177
+ spreadPrice : formattedSpread ,
178
+ } ) ;
154
179
this . isOrderBookLoading = false ;
155
180
return ;
156
181
}
157
182
158
- this . setOrderbook ( { buy, sell, spreadPercent : "0.00" , spreadPrice : "0.00" } ) ;
183
+ this . setOrderbook ( {
184
+ buy : buyOrdersCombinedByDecimal ,
185
+ sell : sellOrdersCombinedByDecimal ,
186
+ spreadPercent : "0.00" ,
187
+ spreadPrice : "0.00" ,
188
+ } ) ;
159
189
this . isOrderBookLoading = false ;
160
190
} ;
161
191
162
- private setOrderbook = ( orderbook : TOrderbookData ) => ( this . orderbook = orderbook ) ;
192
+ private setOrderbook = ( orderbook : Partial < TOrderbookData > ) => {
193
+ this . orderbook = { ...this . orderbook , ...orderbook } ;
194
+ } ;
163
195
}
0 commit comments