From 80358bc08626ffead72faeb8a140eb8305bff404 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 6 Apr 2017 15:34:23 -0500 Subject: [PATCH] Fixed a rounding error in the OrderBook class' exchange method --- .../java/de/bitsharesmunich/graphenej/OrderBook.java | 7 ++++++- .../graphenej/api/GetLimitOrdersTest.java | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java index aec67b7..3615b0b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java @@ -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; diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java index d16356e..ffa2665 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java @@ -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 orders = (List) 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(); }