Skip to content

Commit 98a54d1

Browse files
committed
feat(Market): Unify comparePrice
1 parent 658e373 commit 98a54d1

File tree

2 files changed

+4
-155
lines changed

2 files changed

+4
-155
lines changed

chainbase/src/main/java/org/tron/core/capsule/utils/MarketUtils.java

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tron.common.crypto.Hash;
2626
import org.tron.common.utils.ByteArray;
2727
import org.tron.common.utils.ByteUtil;
28+
import org.tron.common.utils.MarketComparator;
2829
import org.tron.core.capsule.AccountCapsule;
2930
import org.tron.core.capsule.MarketAccountOrderCapsule;
3031
import 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

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.tron.plugins.utils;
22

3-
import java.math.BigInteger;
4-
import org.tron.plugins.utils.ByteArray;
5-
63
public class MarketUtils {
74

85
public static final int TOKEN_ID_LENGTH = ByteArray
@@ -68,81 +65,4 @@ private static byte[] doCreatePairPriceKey(byte[] sellTokenId, byte[] buyTokenId
6865
}
6966

7067

71-
public static int comparePriceKey(byte[] o1, byte[] o2) {
72-
//compare pair
73-
byte[] pair1 = new byte[TOKEN_ID_LENGTH * 2];
74-
byte[] pair2 = new byte[TOKEN_ID_LENGTH * 2];
75-
76-
System.arraycopy(o1, 0, pair1, 0, TOKEN_ID_LENGTH * 2);
77-
System.arraycopy(o2, 0, pair2, 0, TOKEN_ID_LENGTH * 2);
78-
79-
int pairResult = ByteArray.compareUnsigned(pair1, pair2);
80-
if (pairResult != 0) {
81-
return pairResult;
82-
}
83-
84-
//compare price
85-
byte[] getSellTokenQuantity1 = new byte[8];
86-
byte[] getBuyTokenQuantity1 = new byte[8];
87-
88-
byte[] getSellTokenQuantity2 = new byte[8];
89-
byte[] getBuyTokenQuantity2 = new byte[8];
90-
91-
int longByteNum = 8;
92-
93-
System.arraycopy(o1, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH,
94-
getSellTokenQuantity1, 0, longByteNum);
95-
System.arraycopy(o1, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum,
96-
getBuyTokenQuantity1, 0, longByteNum);
97-
98-
System.arraycopy(o2, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH,
99-
getSellTokenQuantity2, 0, longByteNum);
100-
System.arraycopy(o2, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum,
101-
getBuyTokenQuantity2, 0, longByteNum);
102-
103-
long sellTokenQuantity1 = ByteArray.toLong(getSellTokenQuantity1);
104-
long buyTokenQuantity1 = ByteArray.toLong(getBuyTokenQuantity1);
105-
long sellTokenQuantity2 = ByteArray.toLong(getSellTokenQuantity2);
106-
long buyTokenQuantity2 = ByteArray.toLong(getBuyTokenQuantity2);
107-
108-
if ((sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0)
109-
&& (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0)) {
110-
return 0;
111-
}
112-
113-
if (sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0) {
114-
return -1;
115-
}
116-
117-
if (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0) {
118-
return 1;
119-
}
120-
121-
return comparePrice(sellTokenQuantity1, buyTokenQuantity1,
122-
sellTokenQuantity2, buyTokenQuantity2);
123-
124-
}
125-
126-
/**
127-
* Note: the params should be the same token pair, or you should change the order.
128-
* All the quantity should be bigger than 0.
129-
* */
130-
public static int comparePrice(long price1SellQuantity, long price1BuyQuantity,
131-
long price2SellQuantity, long price2BuyQuantity) {
132-
try {
133-
return Long.compare(StrictMath.multiplyExact(price1BuyQuantity, price2SellQuantity),
134-
StrictMath.multiplyExact(price2BuyQuantity, price1SellQuantity));
135-
136-
} catch (ArithmeticException ex) {
137-
// do nothing here, because we will use BigInteger to compute again
138-
}
139-
140-
BigInteger price1BuyQuantityBI = BigInteger.valueOf(price1BuyQuantity);
141-
BigInteger price1SellQuantityBI = BigInteger.valueOf(price1SellQuantity);
142-
BigInteger price2BuyQuantityBI = BigInteger.valueOf(price2BuyQuantity);
143-
BigInteger price2SellQuantityBI = BigInteger.valueOf(price2SellQuantity);
144-
145-
return price1BuyQuantityBI.multiply(price2SellQuantityBI)
146-
.compareTo(price2BuyQuantityBI.multiply(price1SellQuantityBI));
147-
}
14868
}

0 commit comments

Comments
 (0)