Fixed a rounding error in the OrderBook class' exchange method

develop
Nelson R. Perez 2017-04-06 15:34:23 -05:00
parent aa35925718
commit 80358bc086
2 changed files with 11 additions and 5 deletions

View File

@ -1,7 +1,9 @@
package de.bitsharesmunich.graphenej;
import com.google.common.math.DoubleMath;
import com.google.common.primitives.UnsignedLong;
import java.math.RoundingMode;
import java.util.List;
import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation;
@ -80,7 +82,10 @@ public class OrderBook {
// If the offered amount is greater than what we still need, we exchange just what we need
if(orderAmount >= stillNeed) {
totalBought += stillNeed;
totalSold += (order.getSellPrice().quote.getAmount().longValue() * stillNeed) / order.getSellPrice().base.getAmount().longValue();;
double additionalRatio = (double) stillNeed / (double) order.getSellPrice().base.getAmount().longValue();
double additionalAmount = order.getSellPrice().quote.getAmount().longValue() * additionalRatio;
long longAdditional = DoubleMath.roundToLong(additionalAmount, RoundingMode.HALF_UP);
totalSold += longAdditional;
}else{
// If the offered amount is less than what we need, we exchange the whole order
totalBought += orderAmount;

View File

@ -34,8 +34,8 @@ import static org.hamcrest.CoreMatchers.is;
public class GetLimitOrdersTest {
private String BLOCK_PAY_DE = System.getenv("BLOCKPAY_DE");
private UserAccount seller = new UserAccount("1.2.143563");
private final Asset base = new Asset("1.3.0", "BTS", 5);
private final Asset quote = new Asset("1.3.120", "EUR", 4);
private final Asset base = new Asset("1.3.121", "USD", 4);
private final Asset quote = new Asset("1.3.0", "BTS", 5);
private SSLContext context;
private WebSocket mWebSocket;
@ -121,13 +121,14 @@ public class GetLimitOrdersTest {
List<LimitOrder> orders = (List<LimitOrder>) response.result;
OrderBook orderBook = new OrderBook(orders);
AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(9850000), quote);
AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(100000), quote);
int expiration = (int) ((System.currentTimeMillis() + 60000) / 1000);
LimitOrderCreateOperation operation = orderBook.exchange(seller, base, toBuy, expiration);
// Testing the successfull creation of a limit order create operation
Assert.assertTrue(operation != null);
double price = (double) Math.pow(10, base.getPrecision() - quote.getPrecision()) * operation.getMinToReceive().getAmount().longValue() / operation.getAmountToSell().getAmount().longValue();
System.out.println("price: "+price);
synchronized (GetLimitOrdersTest.this){
GetLimitOrdersTest.this.notifyAll();
}