CoinbaseProAdapters { public static Order adaptOrder(CoinbaseProOrder order) { OrderType type = order.getSide().equals("buy") ? OrderType.BID : OrderType.ASK; CurrencyPair currencyPair = new CurrencyPair(order.getProductId().replace('-', '/')); Order.Builder builder = null; if (order.getType().equals("market")) { builder = new MarketOrder.Builder(type, currencyPair); } else if (order.getType().equals("limit")) { if (order.getStop() == null) { builder = new LimitOrder.Builder(type, currencyPair).limitPrice(order.getPrice()); } else { builder = new StopOrder.Builder(type, currencyPair).stopPrice(order.getStopPrice()); } } if (builder == null) { return null; } builder .orderStatus(adaptOrderStatus(order)) .originalAmount(order.getSize()) .id(order.getId()) .timestamp(parseDate(order.getCreatedAt())) .cumulativeAmount(order.getFilledSize()) .fee(order.getFillFees()); BigDecimal averagePrice; if (order.getFilledSize().signum() != 0 && order.getExecutedvalue().signum() != 0) { averagePrice = order.getExecutedvalue().divide(order.getFilledSize(), MathContext.DECIMAL32); } else { averagePrice = BigDecimal.ZERO; } builder.averagePrice(averagePrice); return builder.build(); } private  CoinbaseProAdapters(); static CurrencyPair toCurrencyPair(final String productId); static Ticker adaptTicker(
      CoinbaseProProductTicker ticker, CoinbaseProProductStats stats, CurrencyPair currencyPair); static OrderBook adaptOrderBook(
      CoinbaseProProductBook book, CurrencyPair currencyPair, Date date); static OrderBook adaptOrderBook(CoinbaseProProductBook book, CurrencyPair currencyPair); static Wallet adaptAccountInfo(CoinbaseProAccount[] coinbaseProAccounts); @SuppressWarnings("unchecked") static OpenOrders adaptOpenOrders(CoinbaseProOrder[] coinbaseExOpenOrders); static Order adaptOrder(CoinbaseProOrder order); static OrderStatus[] adaptOrderStatuses(CoinbaseProOrder[] orders); static OrderStatus adaptOrderStatus(CoinbaseProOrder order); static Trades adaptTrades(
      List<CoinbaseProTrade> coinbaseProTradesList, CurrencyPair currencyPair); static UserTrades adaptTradeHistory(CoinbaseProFill[] coinbaseExFills); static Trades adaptTrades(CoinbaseProTrade[] coinbaseExTrades, CurrencyPair currencyPair); static CurrencyPair adaptCurrencyPair(CoinbaseProProduct product); static ExchangeMetaData adaptToExchangeMetaData(
      ExchangeMetaData exchangeMetaData,
      CoinbaseProProduct[] products,
      CoinbaseProCurrency[] cbCurrencies); static String adaptProductID(CurrencyPair currencyPair); static CoinbaseProPlaceOrder.Side adaptSide(OrderType orderType); static CoinbaseProPlaceOrder.Stop adaptStop(OrderType orderType); static CoinbaseProPlaceLimitOrder adaptCoinbaseProPlaceLimitOrder(LimitOrder limitOrder); static CoinbaseProPlaceMarketOrder adaptCoinbaseProPlaceMarketOrder(
      MarketOrder marketOrder); static CoinbaseProPlaceOrder adaptCoinbaseProStopOrder(StopOrder stopOrder); static FundingRecord adaptFundingRecord(
      Currency currency, CoinbaseProTransfer coinbaseProTransfer);  }
@Test public void testOrderStatusMarketOrderFilled() throws IOException { JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); ObjectMapper mapper = factory.createObjectMapper(); InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-market-order-filled.json"); CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.FILLED); assertThat(order.getId()).isEqualTo(coinbaseProOrder.getId()); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_USD)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("1.00000000")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(new BigDecimal("0.01291771")); assertThat(order.getRemainingAmount()) .isEqualByComparingTo(new BigDecimal("1.0").subtract(new BigDecimal("0.01291771"))); assertThat(order.getFee()).isEqualTo(new BigDecimal("0.0249376391550000")); assertThat(MarketOrder.class.isAssignableFrom(order.getClass())).isTrue(); assertThat(order.getType()).isEqualTo(OrderType.BID); assertThat(order.getTimestamp()).isEqualTo(new Date(1481227745508L)); assertThat(order.getAveragePrice()) .isEqualByComparingTo( new BigDecimal("9.9750556620000000") .divide(new BigDecimal("0.01291771"), new MathContext(8))); }
@Test public void testOrderStatusLimitOrderFilled() throws IOException { JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); ObjectMapper mapper = factory.createObjectMapper(); InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-limit-order-filled.json"); CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.FILLED); assertThat(order.getId()).isEqualTo("b2cdd7fe-1f4a-495e-8b96-7a4be368f43c"); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_USD)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getRemainingAmount()).isEqualByComparingTo(new BigDecimal("0.00000000")); assertThat(order.getFee()).isEqualTo(new BigDecimal("2.6256545174247500")); assertThat(LimitOrder.class.isAssignableFrom(order.getClass())).isTrue(); assertThat(order.getType()).isEqualTo(OrderType.ASK); assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L)); assertThat(order.getAveragePrice()) .isEqualByComparingTo( new BigDecimal("1050.2618069699000000") .divide(new BigDecimal("0.07060351"), new MathContext(8))); }
@Test public void testOrderStatusLimitOrderSettled() throws IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-limit-order-settled.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.FILLED); assertThat(order.getId()).isEqualTo("b2cdd7fe-1f4a-495e-8b96-7a4be368f43c"); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_USD)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getRemainingAmount()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(LimitOrder.class.isAssignableFrom(order.getClass())).isTrue(); assertThat(order.getType()).isEqualTo(OrderType.ASK); assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L)); assertThat(order.getAveragePrice()) .isEqualByComparingTo( new BigDecimal("1050.2618069699000000") .divide(new BigDecimal("0.07060351"), new MathContext(8))); }
@Test public void testOrderStatusLimitOrderNew() throws IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-limit-order-new.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.NEW); assertThat(order.getId()).isEqualTo("b2cdd7fe-1f4a-495e-8b96-7a4be368f43c"); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_USD)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(order.getRemainingAmount()).isEqualByComparingTo(new BigDecimal("0.07060351")); assertThat(order.getType()).isEqualTo(OrderType.ASK); assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L)); assertThat(order.getAveragePrice()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(LimitOrder.class.isAssignableFrom(order.getClass())).isTrue(); LimitOrder limitOrder = (LimitOrder) order; assertThat(limitOrder.getLimitPrice()).isEqualByComparingTo(new BigDecimal("14839.76")); }
@Test public void testOrderStatusStopOrderNew() throws JsonParseException, JsonMappingException, IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-stop-order-new.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.NEW); assertThat(order.getId()).isEqualTo("853a9989-7dd9-40f8-9392-64237a9eccc4"); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_EUR)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.01")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(order.getRemainingAmount()).isEqualByComparingTo(new BigDecimal("0.01")); assertThat(order.getType()).isEqualTo(OrderType.ASK); assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L)); assertThat(order.getAveragePrice()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(StopOrder.class.isAssignableFrom(order.getClass())).isTrue(); StopOrder stop = (StopOrder) order; assertThat(stop.getStopPrice()).isEqualByComparingTo("6364.31"); }
@Test public void testOrderStatusStopOrderStopped() throws JsonParseException, JsonMappingException, IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-stop-order-stopped.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.STOPPED); assertThat(order.getId()).isEqualTo("853a9989-7dd9-40f8-9392-64237a9eccc4"); assertThat(order.getCurrencyPair()).isEqualTo((CurrencyPair.BTC_EUR)); assertThat(order.getOriginalAmount()).isEqualByComparingTo(new BigDecimal("0.01")); assertThat(order.getCumulativeAmount()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(order.getRemainingAmount()).isEqualByComparingTo(new BigDecimal("0.01")); assertThat(order.getType()).isEqualTo(OrderType.ASK); assertThat(order.getTimestamp()).isEqualTo(new Date(1515434144454L)); assertThat(order.getAveragePrice()).isEqualByComparingTo(BigDecimal.ZERO); assertThat(StopOrder.class.isAssignableFrom(order.getClass())).isTrue(); StopOrder stop = (StopOrder) order; assertThat(stop.getStopPrice()).isEqualByComparingTo("6364.31"); }
@Test public void testOrderStatusStopOrderFilled() throws JsonParseException, JsonMappingException, IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-stop-order-filled.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertStopOrderFilled(order); }
@Test public void testOrderStatusLimitOrderPending() throws IOException { final JacksonObjectMapperFactory factory = new DefaultJacksonObjectMapperFactory(); final ObjectMapper mapper = factory.createObjectMapper(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/coinbasepro/dto/order/example-limit-order-pending.json"); final CoinbaseProOrder coinbaseProOrder = mapper.readValue(is, CoinbaseProOrder.class); final Order order = CoinbaseProAdapters.adaptOrder(coinbaseProOrder); assertLimitOrderPending(order); }