From 61dc72724e7183afaf54a920069126a346bf38de Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 12 Oct 2017 18:22:29 -0500 Subject: [PATCH] Fixed a bug in the OrderBook.calculateRequiredBase method that made the calculation of the required asset amount to fail some times --- gradle.properties | 2 +- graphenej/build.gradle | 2 +- .../java/cy/agorise/graphenej/OrderBook.java | 8 ++- .../cy/agorise/graphenej/api/BaseApiTest.java | 6 +-- .../graphenej/api/GetLimitOrdersTest.java | 54 ++++++++++++++++++- 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index 80c0e44..bfea545 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME=0.4.5 +VERSION_NAME=0.4.5-SNAPSHOT VERSION_CODE=7 GROUP=com.github.bilthon diff --git a/graphenej/build.gradle b/graphenej/build.gradle index 14e9c8c..e0168b7 100644 --- a/graphenej/build.gradle +++ b/graphenej/build.gradle @@ -22,7 +22,7 @@ android { minSdkVersion 9 targetSdkVersion 24 versionCode 7 - versionName "0.4.5" + versionName "0.4.5-SNAPSHOT" vectorDrawables.useSupportLibrary = true } diff --git a/graphenej/src/main/java/cy/agorise/graphenej/OrderBook.java b/graphenej/src/main/java/cy/agorise/graphenej/OrderBook.java index 61b5729..2fceb6a 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/OrderBook.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/OrderBook.java @@ -89,7 +89,13 @@ public class OrderBook { }else{ // If the offered amount is less than what we need, we exchange the whole order totalBought += orderAmount; - totalSold += order.getSellPrice().quote.getAmount().longValue(); + + // The amount specified in the price ratio is not always all for sale. So in order to calculate + // the amount actually sold we have to do: + // actually_sold = for_sale * quote / base + double sellRatio = ((double) orderAmount) / ((double) order.getSellPrice().base.getAmount().longValue()); + + totalSold += Math.floor(order.getSellPrice().quote.getAmount().doubleValue() * sellRatio); } } } diff --git a/graphenej/src/test/java/cy/agorise/graphenej/api/BaseApiTest.java b/graphenej/src/test/java/cy/agorise/graphenej/api/BaseApiTest.java index d2ec0fc..7a3e76a 100644 --- a/graphenej/src/test/java/cy/agorise/graphenej/api/BaseApiTest.java +++ b/graphenej/src/test/java/cy/agorise/graphenej/api/BaseApiTest.java @@ -10,11 +10,11 @@ import javax.net.ssl.SSLContext; import cy.agorise.graphenej.test.NaiveSSLContext; /** - * Created by nelson on 4/14/17. + * Base class that every test that involves any communication with the API must extend */ public class BaseApiTest { - protected String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); + protected String NODE_URL = System.getenv("NODE_URL"); protected SSLContext context; protected WebSocket mWebSocket; @@ -27,7 +27,7 @@ public class BaseApiTest { // Set the custom SSL context. factory.setSSLContext(context); - mWebSocket = factory.createSocket(BLOCK_PAY_DE); + mWebSocket = factory.createSocket(NODE_URL); } } diff --git a/graphenej/src/test/java/cy/agorise/graphenej/api/GetLimitOrdersTest.java b/graphenej/src/test/java/cy/agorise/graphenej/api/GetLimitOrdersTest.java index 524d4c7..d0fd134 100644 --- a/graphenej/src/test/java/cy/agorise/graphenej/api/GetLimitOrdersTest.java +++ b/graphenej/src/test/java/cy/agorise/graphenej/api/GetLimitOrdersTest.java @@ -5,6 +5,7 @@ import com.neovisionaries.ws.client.WebSocketException; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.List; @@ -23,13 +24,18 @@ import cy.agorise.graphenej.operations.LimitOrderCreateOperation; import static org.hamcrest.CoreMatchers.is; /** - * Created by nelson on 3/24/17. + * Class used to test the 'get_limit_order' API wrapper */ public class GetLimitOrdersTest extends BaseApiTest { private UserAccount seller = new UserAccount("1.2.143563"); private final Asset base = new Asset("1.3.121", "USD", 4); private final Asset quote = new Asset("1.3.0", "BTS", 5); + @Before + public void setup(){ + System.out.println("Connecting to node: "+NODE_URL); + } + @Test public void testGetLimitOrders(){ try { @@ -104,7 +110,7 @@ public class GetLimitOrdersTest extends BaseApiTest { 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 + // Testing the successful 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); @@ -135,6 +141,50 @@ public class GetLimitOrdersTest extends BaseApiTest { } } + @Test + public void testRequiredBase(){ + try { + final Asset _quote = new Asset("1.3.121", "USD", 4); + final Asset _base = new Asset("1.3.0", "BTS", 5); + mWebSocket.addListener(new GetLimitOrders(_base.getObjectId(), _quote.getObjectId(), 100, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + List orders = (List) response.result; + OrderBook orderBook = new OrderBook(orders); + + long _totalQuote = 1000; + long _totalBase = orderBook.calculateRequiredBase(new AssetAmount(UnsignedLong.valueOf(_totalQuote), _quote)); + + System.out.println(String.format("Base: %s, Quote: %s", _base.getObjectId(), _quote.getObjectId())); + System.out.println(String.format("_totalQuote: %d, _totalBase: %d", _totalQuote, _totalBase)); + + synchronized (GetLimitOrdersTest.this){ + GetLimitOrdersTest.this.notifyAll(); + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError. Msg: "+error.message); + synchronized (GetLimitOrdersTest.this){ + GetLimitOrdersTest.this.notifyAll(); + } + } + })); + + mWebSocket.connect(); + + synchronized (this){ + wait(); + } + + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (InterruptedException e) { + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } + @After public void tearDown() throws Exception { mWebSocket.disconnect();