OSDN Git Service

fix match
authorshenao78 <shenao.78@163.com>
Tue, 5 Nov 2019 06:11:33 +0000 (14:11 +0800)
committershenao78 <shenao.78@163.com>
Tue, 5 Nov 2019 06:11:33 +0000 (14:11 +0800)
application/mov/match/match.go

index 336269a..12a14a2 100644 (file)
@@ -35,12 +35,7 @@ func (e *Engine) HasMatchedTx(tradePairs ...*common.TradePair) bool {
                return false
        }
 
-       for i, order := range orders {
-               if canNotBeMatched(order, orders[getOppositeIndex(len(orders), i)]) {
-                       return false
-               }
-       }
-       return true
+       return isMatched(orders)
 }
 
 // NextMatchedTx return the next matchable transaction by the specified trade pairs
@@ -56,6 +51,10 @@ func (e *Engine) NextMatchedTx(tradePairs ...*common.TradePair) (*types.Tx, erro
                return nil, errors.New("no order for the specified trade pair in the order table")
        }
 
+       if !isMatched(orders) {
+               return nil, errors.New("the specified trade pairs can not be matched")
+       }
+
        tx, err := e.buildMatchTx(orders)
        if err != nil {
                return nil, err
@@ -98,9 +97,14 @@ func validateTradePairs(tradePairs []*common.TradePair) error {
        return nil
 }
 
-func canNotBeMatched(order, oppositeOrder *common.Order) bool {
-       rate := 1 / order.Rate
-       return rate < oppositeOrder.Rate
+func isMatched(orders []*common.Order) bool {
+       for i, order := range orders {
+               opposisteOrder := orders[getOppositeIndex(len(orders), i)]
+               if 1 / order.Rate < opposisteOrder.Rate {
+                       return false
+               }
+       }
+       return true
 }
 
 func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, error) {