* fix_add_order_of_order_book
* fix order book
* opt code
Co-authored-by: Paladz <yzhu101@uottawa.ca>
-// Cmp compares x and y and returns -1 if x < y, 0 if x == y, +1 if x > y
-func (o *Order) Cmp(other *Order) int {
+// cmpRate compares rate of x and y and returns -1 if x < y, 0 if x == y, +1 if x > y
+func (o *Order) cmpRate(other *Order) int {
rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
otherRate := big.NewRat(other.RatioNumerator, other.RatioDenominator)
return rate.Cmp(otherRate)
}
rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
otherRate := big.NewRat(other.RatioNumerator, other.RatioDenominator)
return rate.Cmp(otherRate)
}
+// Cmp first compare the rate, if rate is equals, then compare the utxo hash
+func (o *Order) Cmp(other *Order) int {
+ cmp := o.cmpRate(other)
+ if cmp == 0 {
+ if hex.EncodeToString(o.UTXOHash().Bytes()) < hex.EncodeToString(other.UTXOHash().Bytes()) {
+ return -1
+ }
+ return 1
+ }
+ return cmp
+}
+
// OrderSlice is define for order's sort
type OrderSlice []*Order
func (o OrderSlice) Len() int { return len(o) }
func (o OrderSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o OrderSlice) Less(i, j int) bool {
// OrderSlice is define for order's sort
type OrderSlice []*Order
func (o OrderSlice) Len() int { return len(o) }
func (o OrderSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o OrderSlice) Less(i, j int) bool {
- cmp := o[i].Cmp(o[j])
- if cmp == 0 {
- return hex.EncodeToString(o[i].UTXOHash().Bytes()) < hex.EncodeToString(o[j].UTXOHash().Bytes())
- }
- return cmp < 0
+ return o[i].Cmp(o[j]) < 0
}
// NewOrderFromOutput convert txinput to order
}
// NewOrderFromOutput convert txinput to order