From 5f08eafa4cca436c398cd077faebfc187b7ebfac Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Wed, 29 Mar 2017 22:22:35 -0500 Subject: [PATCH] Adjustments in the OrderBook class and adding getters & setters to the LimitOrderCreateOperation class --- gradle.properties | 2 +- .../bitsharesmunich/graphenej/OrderBook.java | 32 ++++++++---- .../operations/LimitOrderCreateOperation.java | 50 ++++++++++++++++--- .../graphenej/api/GetLimitOrdersTest.java | 3 +- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/gradle.properties b/gradle.properties index b568a9b..2dc6a3e 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.1 +VERSION_NAME=0.4.1-SNAPSHOT VERSION_CODE=3 GROUP=com.github.kenCode-de diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java index e82e9be..2d66b06 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/OrderBook.java @@ -36,7 +36,8 @@ public class OrderBook { } /** - * Method used to exchange a specific amount of an asset (The base) for another one (The quote) at market value. + * High level method used to exchange a specific amount of an asset (The base) for another + * one (The quote) at market value. * * It should analyze the order book and figure out the optimal amount of the base asset to give * away in order to obtain the desired amount of the quote asset. @@ -44,22 +45,37 @@ public class OrderBook { * @param seller: User account of the seller, used to build the limit order create operation * @param myBaseAsset: The asset the user is willing to give away * @param myQuoteAmount: The amount of a given asset the user wants + * @param expiration: The expiration time of the limit order * * @return An instance of the LimitOrderCreateOperation class, which is ready to be broadcasted. */ - public LimitOrderCreateOperation exchange(UserAccount seller, Asset myBaseAsset, AssetAmount myQuoteAmount){ + public LimitOrderCreateOperation exchange(UserAccount seller, Asset myBaseAsset, AssetAmount myQuoteAmount, int expiration){ + AssetAmount toSell = new AssetAmount(UnsignedLong.valueOf(calculateRequiredBase(myQuoteAmount)), myBaseAsset); + AssetAmount toReceive = myQuoteAmount; + LimitOrderCreateOperation buyOrder = new LimitOrderCreateOperation(seller, toSell, toReceive, expiration, true); + + return buyOrder; + } + + /** + * Given a specific amount of a desired asset, this method will calculate how much of the corresponding + * asset we need to offer to perform a successful transaction with the current order book. + * @param quoteAmount: The amount of the desired asset. + * @return: The minimum amount of the base asset that we need to give away + */ + public long calculateRequiredBase(AssetAmount quoteAmount){ long totalBought = 0; long totalSold = 0; - for(int i = 0; i < this.limitOrders.size() && totalBought < myQuoteAmount.getAmount().longValue(); i++){ + for(int i = 0; i < this.limitOrders.size() && totalBought < quoteAmount.getAmount().longValue(); i++){ LimitOrder order = this.limitOrders.get(i); // If the base asset is the same as our quote asset, we have a match - if(order.getSellPrice().base.getAsset().getObjectId().equals(myQuoteAmount.getAsset().getObjectId())){ + if(order.getSellPrice().base.getAsset().getObjectId().equals(quoteAmount.getAsset().getObjectId())){ // My quote amount, is the order's base amount long orderAmount = order.getSellPrice().base.getAmount().longValue(); // The amount of the quote asset we still need - long stillNeed = myQuoteAmount.getAmount().longValue() - totalBought; + long stillNeed = quoteAmount.getAmount().longValue() - totalBought; // If the offered amount is greater than what we still need, we exchange just what we need if(orderAmount >= stillNeed) { @@ -72,10 +88,6 @@ public class OrderBook { } } } - AssetAmount toSell = new AssetAmount(UnsignedLong.valueOf(totalSold), myBaseAsset); - AssetAmount toReceive = myQuoteAmount; - LimitOrderCreateOperation buyOrder = new LimitOrderCreateOperation(seller, toSell, toReceive, true); - - return buyOrder; + return totalSold; } } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/LimitOrderCreateOperation.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/LimitOrderCreateOperation.java index 96a1403..c98ed0b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/LimitOrderCreateOperation.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/LimitOrderCreateOperation.java @@ -47,14 +47,6 @@ public class LimitOrderCreateOperation extends BaseOperation { private int expiration; private boolean fillOrKill; - public LimitOrderCreateOperation(UserAccount seller, AssetAmount toSell, AssetAmount minToReceive, boolean fillOrKill){ - super(OperationType.LIMIT_ORDER_CREATE_OPERATION); - this.seller = seller; - this.amountToSell = toSell; - this.minToReceive = minToReceive; - this.fillOrKill = fillOrKill; - } - /** * @param seller: Id of the seller * @param toSell: Id of the asset to sell @@ -101,6 +93,48 @@ public class LimitOrderCreateOperation extends BaseOperation { this.fee = assetAmount; } + public AssetAmount getFee(){ return this.fee; } + + public UserAccount getSeller() { + return seller; + } + + public void setSeller(UserAccount seller) { + this.seller = seller; + } + + public AssetAmount getAmountToSell() { + return amountToSell; + } + + public void setAmountToSell(AssetAmount amountToSell) { + this.amountToSell = amountToSell; + } + + public AssetAmount getMinToReceive() { + return minToReceive; + } + + public void setMinToReceive(AssetAmount minToReceive) { + this.minToReceive = minToReceive; + } + + public int getExpiration() { + return expiration; + } + + public void setExpiration(int expiration) { + this.expiration = expiration; + } + + public boolean isFillOrKill() { + return fillOrKill; + } + + public void setFillOrKill(boolean fillOrKill) { + this.fillOrKill = fillOrKill; + } + @Override public byte[] toBytes() { byte[] feeBytes = this.fee.toBytes(); 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 2474f0c..78ccfd9 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java @@ -122,7 +122,8 @@ public class GetLimitOrdersTest { OrderBook orderBook = new OrderBook(orders); AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(9850000), quote); - LimitOrderCreateOperation operation = orderBook.exchange(seller, base, toBuy); + 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);