2525import org .tron .common .crypto .Hash ;
2626import org .tron .common .utils .ByteArray ;
2727import org .tron .common .utils .ByteUtil ;
28+ import org .tron .common .utils .MarketComparator ;
2829import org .tron .core .capsule .AccountCapsule ;
2930import org .tron .core .capsule .MarketAccountOrderCapsule ;
3031import org .tron .core .capsule .MarketOrderCapsule ;
@@ -227,29 +228,6 @@ public static byte[] createPairKey(byte[] sellTokenId, byte[] buyTokenId) {
227228 return result ;
228229 }
229230
230- /**
231- * Note: the params should be the same token pair, or you should change the order.
232- * All the quantity should be bigger than 0.
233- * */
234- public static int comparePrice (long price1SellQuantity , long price1BuyQuantity ,
235- long price2SellQuantity , long price2BuyQuantity ) {
236- try {
237- return Long .compare (multiplyExact (price1BuyQuantity , price2SellQuantity , true ),
238- multiplyExact (price2BuyQuantity , price1SellQuantity , true ));
239-
240- } catch (ArithmeticException ex ) {
241- // do nothing here, because we will use BigInteger to compute again
242- }
243-
244- BigInteger price1BuyQuantityBI = BigInteger .valueOf (price1BuyQuantity );
245- BigInteger price1SellQuantityBI = BigInteger .valueOf (price1SellQuantity );
246- BigInteger price2BuyQuantityBI = BigInteger .valueOf (price2BuyQuantity );
247- BigInteger price2SellQuantityBI = BigInteger .valueOf (price2SellQuantity );
248-
249- return price1BuyQuantityBI .multiply (price2SellQuantityBI )
250- .compareTo (price2BuyQuantityBI .multiply (price1SellQuantityBI ));
251- }
252-
253231 /**
254232 * if takerPrice >= makerPrice, return True
255233 * note: here are two different token pairs
@@ -265,7 +243,8 @@ public static boolean priceMatch(MarketPrice takerPrice, MarketPrice makerPrice)
265243 // ==> Price_TRX * sellQuantity_taker/buyQuantity_taker >= Price_TRX * buyQuantity_maker/sellQuantity_maker
266244 // ==> sellQuantity_taker * sellQuantity_maker > buyQuantity_taker * buyQuantity_maker
267245
268- return comparePrice (takerPrice .getBuyTokenQuantity (), takerPrice .getSellTokenQuantity (),
246+ return MarketComparator .comparePrice (takerPrice .getBuyTokenQuantity (),
247+ takerPrice .getSellTokenQuantity (),
269248 makerPrice .getSellTokenQuantity (), makerPrice .getBuyTokenQuantity ()) >= 0 ;
270249 }
271250
@@ -316,57 +295,7 @@ public static void returnSellTokenRemain(MarketOrderCapsule orderCapsule,
316295 }
317296
318297 public static int comparePriceKey (byte [] o1 , byte [] o2 ) {
319- //compare pair
320- byte [] pair1 = new byte [TOKEN_ID_LENGTH * 2 ];
321- byte [] pair2 = new byte [TOKEN_ID_LENGTH * 2 ];
322-
323- System .arraycopy (o1 , 0 , pair1 , 0 , TOKEN_ID_LENGTH * 2 );
324- System .arraycopy (o2 , 0 , pair2 , 0 , TOKEN_ID_LENGTH * 2 );
325-
326- int pairResult = org .bouncycastle .util .Arrays .compareUnsigned (pair1 , pair2 );
327- if (pairResult != 0 ) {
328- return pairResult ;
329- }
330-
331- //compare price
332- byte [] getSellTokenQuantity1 = new byte [8 ];
333- byte [] getBuyTokenQuantity1 = new byte [8 ];
334-
335- byte [] getSellTokenQuantity2 = new byte [8 ];
336- byte [] getBuyTokenQuantity2 = new byte [8 ];
337-
338- int longByteNum = 8 ;
339-
340- System .arraycopy (o1 , TOKEN_ID_LENGTH + TOKEN_ID_LENGTH ,
341- getSellTokenQuantity1 , 0 , longByteNum );
342- System .arraycopy (o1 , TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum ,
343- getBuyTokenQuantity1 , 0 , longByteNum );
344-
345- System .arraycopy (o2 , TOKEN_ID_LENGTH + TOKEN_ID_LENGTH ,
346- getSellTokenQuantity2 , 0 , longByteNum );
347- System .arraycopy (o2 , TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum ,
348- getBuyTokenQuantity2 , 0 , longByteNum );
349-
350- long sellTokenQuantity1 = ByteArray .toLong (getSellTokenQuantity1 );
351- long buyTokenQuantity1 = ByteArray .toLong (getBuyTokenQuantity1 );
352- long sellTokenQuantity2 = ByteArray .toLong (getSellTokenQuantity2 );
353- long buyTokenQuantity2 = ByteArray .toLong (getBuyTokenQuantity2 );
354-
355- if ((sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0 )
356- && (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0 )) {
357- return 0 ;
358- }
359-
360- if (sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0 ) {
361- return -1 ;
362- }
363-
364- if (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0 ) {
365- return 1 ;
366- }
367-
368- return comparePrice (sellTokenQuantity1 , buyTokenQuantity1 ,
369- sellTokenQuantity2 , buyTokenQuantity2 );
298+ return MarketComparator .comparePriceKey (o1 , o2 );
370299
371300 }
372301
0 commit comments