Adjustments in the OrderBook class and adding getters & setters to the LimitOrderCreateOperation class

This commit is contained in:
Nelson R. Perez 2017-03-29 22:22:35 -05:00
parent 8bd9f293a4
commit 5f08eafa4c
4 changed files with 67 additions and 20 deletions

View file

@ -17,7 +17,7 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true # org.gradle.parallel=true
VERSION_NAME=0.4.1 VERSION_NAME=0.4.1-SNAPSHOT
VERSION_CODE=3 VERSION_CODE=3
GROUP=com.github.kenCode-de GROUP=com.github.kenCode-de

View file

@ -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 * 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. * 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 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 myBaseAsset: The asset the user is willing to give away
* @param myQuoteAmount: The amount of a given asset the user wants * @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. * @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 totalBought = 0;
long totalSold = 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); LimitOrder order = this.limitOrders.get(i);
// If the base asset is the same as our quote asset, we have a match // 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 // My quote amount, is the order's base amount
long orderAmount = order.getSellPrice().base.getAmount().longValue(); long orderAmount = order.getSellPrice().base.getAmount().longValue();
// The amount of the quote asset we still need // 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 the offered amount is greater than what we still need, we exchange just what we need
if(orderAmount >= stillNeed) { if(orderAmount >= stillNeed) {
@ -72,10 +88,6 @@ public class OrderBook {
} }
} }
} }
AssetAmount toSell = new AssetAmount(UnsignedLong.valueOf(totalSold), myBaseAsset); return totalSold;
AssetAmount toReceive = myQuoteAmount;
LimitOrderCreateOperation buyOrder = new LimitOrderCreateOperation(seller, toSell, toReceive, true);
return buyOrder;
} }
} }

View file

@ -47,14 +47,6 @@ public class LimitOrderCreateOperation extends BaseOperation {
private int expiration; private int expiration;
private boolean fillOrKill; 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 seller: Id of the seller
* @param toSell: Id of the asset to sell * @param toSell: Id of the asset to sell
@ -101,6 +93,48 @@ public class LimitOrderCreateOperation extends BaseOperation {
this.fee = assetAmount; 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 @Override
public byte[] toBytes() { public byte[] toBytes() {
byte[] feeBytes = this.fee.toBytes(); byte[] feeBytes = this.fee.toBytes();

View file

@ -122,7 +122,8 @@ public class GetLimitOrdersTest {
OrderBook orderBook = new OrderBook(orders); OrderBook orderBook = new OrderBook(orders);
AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(9850000), quote); 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 // Testing the successfull creation of a limit order create operation
Assert.assertTrue(operation != null); Assert.assertTrue(operation != null);