@Test public void testRequiredQuote(){ String serializedOrderBook = "{\"id\":1,\"result\":[{\"id\":\"1.7.37284933\",\"expiration\":\"2018-11-17T00:03:49\",\"seller\":\"1.2.132823\",\"for_sale\":20,\"sell_price\":{\"base\":{\"amount\":20,\"asset_id\":\"1.3.0\"},\"quote\":{\"amount\":1,\"asset_id\":\"1.3.121\"}},\"deferred_fee\":0},{\"id\":\"1.7.37284933\",\"expiration\":\"2018-11-17T00:03:49\",\"seller\":\"1.2.132823\",\"for_sale\":100,\"sell_price\":{\"base\":{\"amount\":10,\"asset_id\":\"1.3.0\"},\"quote\":{\"amount\":1,\"asset_id\":\"1.3.121\"}},\"deferred_fee\":0}]}"; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); builder.registerTypeAdapter(LimitOrder.class, new LimitOrder.LimitOrderDeserializer()); Type GetLimitOrdersResponse = new TypeToken<WitnessResponse<List<LimitOrder>>>() {}.getType(); WitnessResponse<List<LimitOrder>> witnessResponse = builder.create().fromJson(serializedOrderBook, GetLimitOrdersResponse); List<LimitOrder> orders = witnessResponse.result; OrderBook orderBook = new OrderBook(orders); final Asset _base = new Asset("1.3.121", "USD", 4); final Asset _quote = new Asset("1.3.0", "BTS", 5); long _totalBase = 2; UnsignedLong _totalQuote = orderBook.calculateObtainedQuote(new AssetAmount(UnsignedLong.valueOf(_totalBase), _base)); Assert.assertEquals("Should be able to buy 20 on the first order and 10 on the second, totalling 30",30, _totalQuote.longValue()); System.out.println(String.format("Base: %s, Quote: %s", _base.getObjectId(), _quote.getObjectId())); System.out.println(String.format("_totalQuote: %d, _totalBase: %d", _totalQuote.longValue(), _totalQuote.longValue())); }
public UnsignedLong calculateObtainedQuote(AssetAmount baseAmount){ UnsignedLong myBase = baseAmount.getAmount(); UnsignedLong obtainedQuote = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myBase.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().quote.getAsset().equals(baseAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong availableBase = order.getForSale(); UnsignedLong myQuote = UnsignedLong.valueOf((long)(myBase.times(orderBase).doubleValue() / (orderQuote.doubleValue()))); if(myQuote.compareTo(availableBase) > 0){ myBase = myBase.minus(availableBase.times(orderQuote).dividedBy(orderBase)); UnsignedLong thisBatch = UnsignedLong.valueOf((long)(orderBase.times(availableBase).doubleValue() / orderQuote.times(orderBase).doubleValue())); obtainedQuote = obtainedQuote.plus(thisBatch); }else{ obtainedQuote = obtainedQuote.plus(myBase.times(orderBase).dividedBy(orderQuote)); myBase = UnsignedLong.ZERO; } } } return obtainedQuote; }
OrderBook { public UnsignedLong calculateObtainedQuote(AssetAmount baseAmount){ UnsignedLong myBase = baseAmount.getAmount(); UnsignedLong obtainedQuote = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myBase.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().quote.getAsset().equals(baseAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong availableBase = order.getForSale(); UnsignedLong myQuote = UnsignedLong.valueOf((long)(myBase.times(orderBase).doubleValue() / (orderQuote.doubleValue()))); if(myQuote.compareTo(availableBase) > 0){ myBase = myBase.minus(availableBase.times(orderQuote).dividedBy(orderBase)); UnsignedLong thisBatch = UnsignedLong.valueOf((long)(orderBase.times(availableBase).doubleValue() / orderQuote.times(orderBase).doubleValue())); obtainedQuote = obtainedQuote.plus(thisBatch); }else{ obtainedQuote = obtainedQuote.plus(myBase.times(orderBase).dividedBy(orderQuote)); myBase = UnsignedLong.ZERO; } } } return obtainedQuote; } }
OrderBook { public UnsignedLong calculateObtainedQuote(AssetAmount baseAmount){ UnsignedLong myBase = baseAmount.getAmount(); UnsignedLong obtainedQuote = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myBase.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().quote.getAsset().equals(baseAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong availableBase = order.getForSale(); UnsignedLong myQuote = UnsignedLong.valueOf((long)(myBase.times(orderBase).doubleValue() / (orderQuote.doubleValue()))); if(myQuote.compareTo(availableBase) > 0){ myBase = myBase.minus(availableBase.times(orderQuote).dividedBy(orderBase)); UnsignedLong thisBatch = UnsignedLong.valueOf((long)(orderBase.times(availableBase).doubleValue() / orderQuote.times(orderBase).doubleValue())); obtainedQuote = obtainedQuote.plus(thisBatch); }else{ obtainedQuote = obtainedQuote.plus(myBase.times(orderBase).dividedBy(orderQuote)); myBase = UnsignedLong.ZERO; } } } return obtainedQuote; } OrderBook(List<LimitOrder> limitOrders); }
OrderBook { public UnsignedLong calculateObtainedQuote(AssetAmount baseAmount){ UnsignedLong myBase = baseAmount.getAmount(); UnsignedLong obtainedQuote = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myBase.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().quote.getAsset().equals(baseAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong availableBase = order.getForSale(); UnsignedLong myQuote = UnsignedLong.valueOf((long)(myBase.times(orderBase).doubleValue() / (orderQuote.doubleValue()))); if(myQuote.compareTo(availableBase) > 0){ myBase = myBase.minus(availableBase.times(orderQuote).dividedBy(orderBase)); UnsignedLong thisBatch = UnsignedLong.valueOf((long)(orderBase.times(availableBase).doubleValue() / orderQuote.times(orderBase).doubleValue())); obtainedQuote = obtainedQuote.plus(thisBatch); }else{ obtainedQuote = obtainedQuote.plus(myBase.times(orderBase).dividedBy(orderQuote)); myBase = UnsignedLong.ZERO; } } } return obtainedQuote; } OrderBook(List<LimitOrder> limitOrders); void update(List<LimitOrder> limitOrders); void update(LimitOrder limitOrder); LimitOrderCreateOperation exchange(UserAccount seller, Asset myBaseAsset, AssetAmount myQuoteAmount, int expiration); LimitOrderCreateOperation exchange(UserAccount seller, AssetAmount baseAmount, Asset quoteAsset, int expiration); UnsignedLong calculateObtainedQuote(AssetAmount baseAmount); UnsignedLong calculateRequiredBase(AssetAmount quoteAmount); List<LimitOrder> getLimitOrders(); }
OrderBook { public UnsignedLong calculateObtainedQuote(AssetAmount baseAmount){ UnsignedLong myBase = baseAmount.getAmount(); UnsignedLong obtainedQuote = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myBase.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().quote.getAsset().equals(baseAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong availableBase = order.getForSale(); UnsignedLong myQuote = UnsignedLong.valueOf((long)(myBase.times(orderBase).doubleValue() / (orderQuote.doubleValue()))); if(myQuote.compareTo(availableBase) > 0){ myBase = myBase.minus(availableBase.times(orderQuote).dividedBy(orderBase)); UnsignedLong thisBatch = UnsignedLong.valueOf((long)(orderBase.times(availableBase).doubleValue() / orderQuote.times(orderBase).doubleValue())); obtainedQuote = obtainedQuote.plus(thisBatch); }else{ obtainedQuote = obtainedQuote.plus(myBase.times(orderBase).dividedBy(orderQuote)); myBase = UnsignedLong.ZERO; } } } return obtainedQuote; } OrderBook(List<LimitOrder> limitOrders); void update(List<LimitOrder> limitOrders); void update(LimitOrder limitOrder); LimitOrderCreateOperation exchange(UserAccount seller, Asset myBaseAsset, AssetAmount myQuoteAmount, int expiration); LimitOrderCreateOperation exchange(UserAccount seller, AssetAmount baseAmount, Asset quoteAsset, int expiration); UnsignedLong calculateObtainedQuote(AssetAmount baseAmount); UnsignedLong calculateRequiredBase(AssetAmount quoteAmount); List<LimitOrder> getLimitOrders();  }