RippleAdapters { public static UserTrade adaptTrade( final IRippleTradeTransaction trade, final TradeHistoryParams params, final ITransferFeeSource transferFeeSource, final int scale) throws IOException { final List<RippleAmount> balanceChanges = trade.getBalanceChanges(); final Iterator<RippleAmount> iterator = balanceChanges.iterator(); while (iterator.hasNext()) { final RippleAmount amount = iterator.next(); if (amount.getCurrency().equals("XRP") && trade.getFee().equals(amount.getValue().negate())) { iterator.remove(); } } if (balanceChanges.size() != 2) { logger.warn( "for hash[{}] of type[{}] balance changes section should contains 2 currency amounts but found {}", trade.getHash(), trade.getClass().getSimpleName(), balanceChanges); return null; } final Collection<Currency> preferredBase, preferredCounter; if (params instanceof RippleTradeHistoryPreferredCurrencies) { final RippleTradeHistoryPreferredCurrencies rippleParams = (RippleTradeHistoryPreferredCurrencies) params; preferredBase = rippleParams.getPreferredBaseCurrency(); preferredCounter = rippleParams.getPreferredCounterCurrency(); } else { preferredBase = preferredCounter = EMPTY_CURRENCY_SET; } final RippleAmount base, counter; if (preferredBase.contains(Currency.getInstance(balanceChanges.get(0).getCurrency()))) { base = balanceChanges.get(0); counter = balanceChanges.get(1); } else if (preferredBase.contains(Currency.getInstance(balanceChanges.get(1).getCurrency()))) { base = balanceChanges.get(1); counter = balanceChanges.get(0); } else if (preferredCounter.contains( Currency.getInstance(balanceChanges.get(0).getCurrency()))) { counter = balanceChanges.get(0); base = balanceChanges.get(1); } else if (preferredCounter.contains( Currency.getInstance(balanceChanges.get(1).getCurrency()))) { counter = balanceChanges.get(1); base = balanceChanges.get(0); } else if ((params instanceof TradeHistoryParamCurrencyPair) && (((TradeHistoryParamCurrencyPair) params).getCurrencyPair() != null)) { final CurrencyPair pair = ((TradeHistoryParamCurrencyPair) params).getCurrencyPair(); if (pair.base.getCurrencyCode().equals(balanceChanges.get(0).getCurrency()) && pair.counter.getCurrencyCode().equals(balanceChanges.get(1).getCurrency())) { base = balanceChanges.get(0); counter = balanceChanges.get(1); } else if (pair.base.getCurrencyCode().equals(balanceChanges.get(1).getCurrency()) && pair.counter.getCurrencyCode().equals(balanceChanges.get(0).getCurrency())) { base = balanceChanges.get(1); counter = balanceChanges.get(0); } else { throw new IllegalStateException( String.format( "trade history param currency filter specified %s but trade query returned %s and %s", pair, balanceChanges.get(0).getCurrency(), balanceChanges.get(1).getCurrency())); } } else { base = balanceChanges.get(0); counter = balanceChanges.get(1); } final OrderType type; if (base.getValue().signum() == 1) { type = OrderType.BID; } else { type = OrderType.ASK; } final String currencyPairString = base.getCurrency() + "/" + counter.getCurrency(); final CurrencyPair currencyPair = CurrencyPairDeserializer.getCurrencyPairFromString(currencyPairString); final BigDecimal baseTransferFee = RippleTradeServiceRaw.getExpectedTransferFee( transferFeeSource, base.getCounterparty(), base.getCurrency(), base.getValue(), type); final BigDecimal baseValue = base.getValue().abs().subtract(baseTransferFee); final OrderType counterDirection; if (type == OrderType.BID) { counterDirection = OrderType.ASK; } else { counterDirection = OrderType.BID; } final BigDecimal counterTransferFee = RippleTradeServiceRaw.getExpectedTransferFee( transferFeeSource, counter.getCounterparty(), counter.getCurrency(), counter.getValue(), counterDirection); final BigDecimal counterValue = counter.getValue().abs().subtract(counterTransferFee); final BigDecimal transactionFee = trade.getFee(); final BigDecimal quantity; if (base.getCurrency().equals("XRP")) { if (type == OrderType.BID) { quantity = baseValue.add(transactionFee); } else { quantity = baseValue.subtract(transactionFee); } } else { quantity = baseValue; } final BigDecimal counterAmount; if (counter.getCurrency().equals("XRP")) { if (type == OrderType.ASK) { counterAmount = counterValue.add(transactionFee); } else { counterAmount = counterValue.subtract(transactionFee); } } else { counterAmount = counterValue; } final BigDecimal price = counterAmount.divide(quantity, scale, RoundingMode.HALF_UP); final String orderId = Long.toString(trade.getOrderId()); final RippleUserTrade.Builder builder = (RippleUserTrade.Builder) new RippleUserTrade.Builder() .currencyPair(currencyPair) .feeAmount(transactionFee) .feeCurrency(Currency.XRP) .id(trade.getHash()) .orderId(orderId) .price(price.stripTrailingZeros()) .timestamp(trade.getTimestamp()) .originalAmount(quantity.stripTrailingZeros()) .type(type); builder.baseTransferFee(baseTransferFee.abs()); builder.counterTransferFee(counterTransferFee.abs()); if (base.getCounterparty().length() > 0) { builder.baseCounterparty(base.getCounterparty()); } if (counter.getCounterparty().length() > 0) { builder.counterCounterparty(counter.getCounterparty()); } return builder.build(); } private  RippleAdapters(); static AccountInfo adaptAccountInfo(
      final RippleAccountBalances account, final String username); static OrderBook adaptOrderBook(
      final RippleOrderBook rippleOrderBook,
      final RippleMarketDataParams params,
      final CurrencyPair currencyPair); static List<LimitOrder> createOrders(
      final CurrencyPair currencyPair,
      final OrderType orderType,
      final List<RippleOrder> orders,
      final String baseCounterparty,
      final String counterCounterparty); static OpenOrders adaptOpenOrders(
      final RippleAccountOrders rippleOrders, final int scale); static UserTrade adaptTrade(
      final IRippleTradeTransaction trade,
      final TradeHistoryParams params,
      final ITransferFeeSource transferFeeSource,
      final int scale); static UserTrades adaptTrades(
      final Collection<IRippleTradeTransaction> tradesForAccount,
      final TradeHistoryParams params,
      final RippleAccountService accountService,
      final int roundingScale);  }
@Test public void adaptTrade_BuyXRP_SellBTC() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-trade-buyXRP-sellBTC.json"); final ObjectMapper mapper = new ObjectMapper(); final RippleOrderTransaction response = mapper.readValue(is, RippleOrderTransaction.class); final RippleTradeHistoryParams params = new RippleTradeHistoryParams(); params.addPreferredCounterCurrency(Currency.BTC); final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair()).isEqualTo(CurrencyPair.XRP_BTC); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("0000000000000000000000000000000000000000000000000000000000000000"); assertThat(trade.getOrderId()).isEqualTo("1010"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("0.000029250906985924") .setScale(roundingScale, RoundingMode.HALF_UP) .stripTrailingZeros()); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2000-00-00T00:00:00.000Z")); assertThat(trade.getOriginalAmount()).isEqualTo("1"); assertThat(trade.getType()).isEqualTo(OrderType.BID); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEmpty(); assertThat(ripple.getBaseTransferFee()).isZero(); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.XRP); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getCounterTransferFee()).isEqualTo("0.000000058619052076"); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); }
@Test public void adaptTrade_SellBTC_BuyXRP() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-trade-buyXRP-sellBTC.json"); final ObjectMapper mapper = new ObjectMapper(); final RippleOrderTransaction response = mapper.readValue(is, RippleOrderTransaction.class); final RippleTradeHistoryParams params = new RippleTradeHistoryParams(); params.addPreferredBaseCurrency(Currency.BTC); final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair()).isEqualTo(CurrencyPair.BTC_XRP); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("0000000000000000000000000000000000000000000000000000000000000000"); assertThat(trade.getOrderId()).isEqualTo("1010"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("34186.97411609205306550363511634115030681332485583111528") .setScale(roundingScale, RoundingMode.HALF_UP)); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2000-00-00T00:00:00.000Z")); assertThat(trade.getOriginalAmount()).isEqualTo("0.000029250906985924"); assertThat(trade.getType()).isEqualTo(OrderType.ASK); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getBaseTransferFee()).isEqualTo("0.000000058619052076"); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEmpty(); assertThat(ripple.getCounterTransferFee()).isZero(); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.XRP); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); }
@Test public void adaptTrade_SellXRP_BuyBTC() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-trade-sellXRP-buyBTC.json"); final ObjectMapper mapper = new ObjectMapper(); final IRippleTradeTransaction response = mapper.readValue(is, RippleOrderTransaction.class); final RippleTradeHistoryParams params = new RippleTradeHistoryParams(); params.setCurrencyPair(CurrencyPair.XRP_BTC); final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair()).isEqualTo(CurrencyPair.XRP_BTC); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("1111111111111111111111111111111111111111111111111111111111111111"); assertThat(trade.getOrderId()).isEqualTo("1111"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("0.000028572057152") .setScale(roundingScale, RoundingMode.HALF_UP) .stripTrailingZeros()); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2011-11-11T11:11:11.111Z")); assertThat(trade.getOriginalAmount()).isEqualTo("1"); assertThat(trade.getType()).isEqualTo(OrderType.ASK); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEmpty(); assertThat(ripple.getBaseTransferFee()).isZero(); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.XRP); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getCounterTransferFee()).isZero(); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); final UserTrade trade2 = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade2.getCurrencyPair()).isEqualTo(trade.getCurrencyPair()); assertThat(trade2.getFeeAmount()).isEqualTo(trade.getFeeAmount()); assertThat(trade2.getFeeCurrency()).isEqualTo(trade.getFeeCurrency()); assertThat(trade2.getId()).isEqualTo(trade.getId()); assertThat(trade2.getOrderId()).isEqualTo(trade.getOrderId()); assertThat(trade2.getPrice()).isEqualTo(trade.getPrice()); assertThat(trade2.getTimestamp()).isEqualTo(trade.getTimestamp()); assertThat(trade2.getOriginalAmount()).isEqualTo(trade.getOriginalAmount()); assertThat(trade2.getType()).isEqualTo(trade.getType()); }
@Test public void adaptTrade_BuyBTC_SellXRP() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-trade-sellXRP-buyBTC.json"); final ObjectMapper mapper = new ObjectMapper(); final RippleOrderTransaction response = mapper.readValue(is, RippleOrderTransaction.class); final RippleTradeHistoryParams params = new RippleTradeHistoryParams(); params.addPreferredBaseCurrency(Currency.BTC); final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair()).isEqualTo(CurrencyPair.BTC_XRP); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("1111111111111111111111111111111111111111111111111111111111111111"); assertThat(trade.getOrderId()).isEqualTo("1111"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("34999.23000574012011552062010939099496310569328655387396") .setScale(roundingScale, RoundingMode.HALF_UP) .stripTrailingZeros()); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2011-11-11T11:11:11.111Z")); assertThat(trade.getOriginalAmount()).isEqualTo("0.000028572057152"); assertThat(trade.getType()).isEqualTo(OrderType.BID); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getBaseTransferFee()).isZero(); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEmpty(); assertThat(ripple.getCounterTransferFee()).isZero(); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.XRP); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); }
@Test public void adaptTrade_BuyBTC_SellBTC() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-trade-buyBTC-sellBTC.json"); final ObjectMapper mapper = new ObjectMapper(); final RippleOrderTransaction response = mapper.readValue(is, RippleOrderTransaction.class); final TradeHistoryParams params = new TradeHistoryParams() {}; final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair().base).isEqualTo(Currency.BTC); assertThat(trade.getCurrencyPair().counter).isEqualTo(Currency.BTC); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("2222222222222222222222222222222222222222222222222222222222222222"); assertThat(trade.getOrderId()).isEqualTo("2222"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("0.99698837430165008596385145696065600512973847422746") .setScale(roundingScale, RoundingMode.HALF_UP)); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2022-22-22T22:22:22.222Z")); assertThat(trade.getOriginalAmount()).isEqualTo("0.50150835545121407952"); assertThat(trade.getType()).isEqualTo(OrderType.BID); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getBaseTransferFee()).isZero(); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEqualTo("rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"); assertThat(ripple.getCounterTransferFee()).isEqualTo("0.001002"); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); }
@Test public void adaptTrade_PaymentPassthrough() throws JsonParseException, JsonMappingException, IOException, ParseException { final RippleExchange exchange = new RippleExchange(); final int roundingScale = exchange.getRoundingScale(); final InputStream is = getClass() .getResourceAsStream( "/org/knowm/xchange/ripple/dto/trade/example-payment-passthrough.json"); final ObjectMapper mapper = new ObjectMapper(); final RipplePaymentTransaction response = mapper.readValue(is, RipplePaymentTransaction.class); final TradeHistoryParams params = new TradeHistoryParams() {}; final UserTrade trade = RippleAdapters.adaptTrade(response, params, this, roundingScale); assertThat(trade.getCurrencyPair().base).isEqualTo(Currency.XRP); assertThat(trade.getCurrencyPair().counter).isEqualTo(Currency.BTC); assertThat(trade.getFeeAmount()).isEqualTo("0.012"); assertThat(trade.getFeeCurrency()).isEqualTo(Currency.XRP); assertThat(trade.getId()) .isEqualTo("GHRE072948B95345396B2D9A364363GDE521HRT67QQRGGRTHYTRUP0RRB631107"); assertThat(trade.getOrderId()).isEqualTo("9338"); assertThat(trade.getPrice()) .isEqualTo( new BigDecimal("0.00002844097635229638527900589254299967193321026478") .setScale(roundingScale, RoundingMode.HALF_UP)); assertThat(trade.getTimestamp()).isEqualTo(RippleExchange.ToDate("2015-08-07T03:58:10.000Z")); assertThat(trade.getOriginalAmount()).isEqualTo("349.547725"); assertThat(trade.getType()).isEqualTo(OrderType.ASK); assertThat(trade).isInstanceOf(RippleUserTrade.class); final RippleUserTrade ripple = (RippleUserTrade) trade; assertThat(ripple.getBaseCounterparty()).isEqualTo(""); assertThat(ripple.getBaseTransferFee()).isZero(); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(Currency.XRP); assertThat(ripple.getBaseTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().base); assertThat(ripple.getCounterCounterparty()).isEqualTo("rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"); assertThat(ripple.getCounterTransferFee()).isEqualTo("0"); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(Currency.BTC); assertThat(ripple.getCounterTransferFeeCurrency()).isEqualTo(trade.getCurrencyPair().counter); }