diff --git a/dex/src/main/scala/com/wavesplatform/dex/api/ws/state/WsOrderBookState.scala b/dex/src/main/scala/com/wavesplatform/dex/api/ws/state/WsOrderBookState.scala index 4d7570b8c7..12ca66a952 100644 --- a/dex/src/main/scala/com/wavesplatform/dex/api/ws/state/WsOrderBookState.scala +++ b/dex/src/main/scala/com/wavesplatform/dex/api/ws/state/WsOrderBookState.scala @@ -86,13 +86,21 @@ case class WsOrderBookState( def updatePrevTickState(asks: Map[Price, Amount], bids: Map[Price, Amount], lt: Option[LastTrade], lc: Option[Double]): WsState = prevTickState.copy( - asks = applyLevelChanges(prevTickState.asks, asks), - bids = applyLevelChanges(prevTickState.bids, bids), + asks = (prevTickState.asks ++ asks).filter(_._2 != 0), + bids = (prevTickState.bids ++ bids).filter(_._2 != 0), lastTrade = if (lt.isEmpty) prevTickState.lastTrade else lt, tickSize = if (lc.isEmpty) prevTickState.tickSize else lc ) - def applyLevelChanges(m1: TreeMap[Price, Amount], m2: Map[Price, Amount]): TreeMap[Price, Amount] = (m1 ++ m2.map { + def updatePrevTickStateFromDiff(asks: Map[Price, Amount], bids: Map[Price, Amount], lt: Option[LastTrade], lc: Option[Double]): WsState = + prevTickState.copy( + asks = applyLevelDiff(prevTickState.asks, asks), + bids = applyLevelDiff(prevTickState.bids, bids), + lastTrade = if (lt.isEmpty) prevTickState.lastTrade else lt, + tickSize = if (lc.isEmpty) prevTickState.tickSize else lc + ) + + def applyLevelDiff(m1: TreeMap[Price, Amount], m2: Map[Price, Amount]): TreeMap[Price, Amount] = (m1 ++ m2.map { case (pr, am) => (pr, m1.get(pr).fold(am)(_ + am)) }).filter(_._2 != 0) @@ -123,7 +131,7 @@ case class WsOrderBookState( )(this) else copy( - prevTickState = updatePrevTickState(lc.asks, lc.bids, lt, ts) + prevTickState = updatePrevTickStateFromDiff(lc.asks, lc.bids, lt, ts) ) }