@Test public void testRequiredBase(){ String serializedOrderBook = "{\"id\": 1,\"result\": [{\"id\": \"1.7.37284933\",\"expiration\": \"2018-11-17T00:03:49\",\"seller\": \"1.2.132823\",\"for_sale\": 10,\"sell_price\": {\"base\": {\"amount\": 1,\"asset_id\": \"1.3.121\"},\"quote\": {\"amount\": 10,\"asset_id\": \"1.3.0\"}},\"deferred_fee\": 0},{\"id\": \"1.7.37284933\",\"expiration\": \"2018-11-17T00:03:49\",\"seller\": \"1.2.132823\",\"for_sale\": 20,\"sell_price\": {\"base\": {\"amount\": 1,\"asset_id\": \"1.3.121\"},\"quote\": {\"amount\": 20,\"asset_id\": \"1.3.0\"}},\"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 _quote = new Asset("1.3.121", "USD", 4); final Asset _base = new Asset("1.3.0", "BTS", 5); long _totalQuote = 14; UnsignedLong _totalBase = orderBook.calculateRequiredBase(new AssetAmount(UnsignedLong.valueOf(_totalQuote), _quote)); Assert.assertEquals("Should buy 10 at 10 and 4 at 20, which sums up to 180",180, _totalBase.longValue()); System.out.println(String.format("Base: %s, Quote: %s", _base.getObjectId(), _quote.getObjectId())); System.out.println(String.format("_totalQuote: %d, _totalBase: %d", _totalQuote, _totalBase.longValue())); }
public UnsignedLong calculateRequiredBase(AssetAmount quoteAmount){ UnsignedLong myQuote = quoteAmount.getAmount(); UnsignedLong obtainedBase = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myQuote.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().base.getAsset().equals(quoteAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong forSale = order.getForSale(); if(forSale.compareTo(myQuote) > 0){ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (myQuote.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = UnsignedLong.ZERO; }else{ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (forSale.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = myQuote.minus(forSale); } } } return obtainedBase; }
OrderBook { public UnsignedLong calculateRequiredBase(AssetAmount quoteAmount){ UnsignedLong myQuote = quoteAmount.getAmount(); UnsignedLong obtainedBase = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myQuote.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().base.getAsset().equals(quoteAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong forSale = order.getForSale(); if(forSale.compareTo(myQuote) > 0){ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (myQuote.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = UnsignedLong.ZERO; }else{ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (forSale.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = myQuote.minus(forSale); } } } return obtainedBase; } }
OrderBook { public UnsignedLong calculateRequiredBase(AssetAmount quoteAmount){ UnsignedLong myQuote = quoteAmount.getAmount(); UnsignedLong obtainedBase = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myQuote.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().base.getAsset().equals(quoteAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong forSale = order.getForSale(); if(forSale.compareTo(myQuote) > 0){ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (myQuote.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = UnsignedLong.ZERO; }else{ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (forSale.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = myQuote.minus(forSale); } } } return obtainedBase; } OrderBook(List<LimitOrder> limitOrders); }
OrderBook { public UnsignedLong calculateRequiredBase(AssetAmount quoteAmount){ UnsignedLong myQuote = quoteAmount.getAmount(); UnsignedLong obtainedBase = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myQuote.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().base.getAsset().equals(quoteAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong forSale = order.getForSale(); if(forSale.compareTo(myQuote) > 0){ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (myQuote.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = UnsignedLong.ZERO; }else{ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (forSale.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = myQuote.minus(forSale); } } } return obtainedBase; } 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 calculateRequiredBase(AssetAmount quoteAmount){ UnsignedLong myQuote = quoteAmount.getAmount(); UnsignedLong obtainedBase = UnsignedLong.ZERO; for(int i = 0; i < limitOrders.size() && myQuote.compareTo(UnsignedLong.ZERO) > 0; i++){ LimitOrder order = limitOrders.get(i); if(order.getSellPrice().base.getAsset().equals(quoteAmount.getAsset())){ UnsignedLong orderBase = order.getSellPrice().base.getAmount(); UnsignedLong orderQuote = order.getSellPrice().quote.getAmount(); UnsignedLong forSale = order.getForSale(); if(forSale.compareTo(myQuote) > 0){ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (myQuote.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = UnsignedLong.ZERO; }else{ obtainedBase = obtainedBase.plus(UnsignedLong.valueOf((long) (forSale.doubleValue() * orderQuote.doubleValue() / orderBase.doubleValue()))); myQuote = myQuote.minus(forSale); } } } return obtainedBase; } 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();  }