View Javadoc
1   package org.oxerr.peatio.rest;
2   
3   import static java.util.stream.Collectors.toList;
4   
5   import java.math.BigDecimal;
6   import java.util.Arrays;
7   import java.util.List;
8   import java.util.Map;
9   
10  import org.oxerr.peatio.rest.dto.Depth;
11  import org.oxerr.peatio.rest.dto.Market;
12  import org.oxerr.peatio.rest.dto.MarketTicker;
13  import org.oxerr.peatio.rest.dto.Member;
14  import org.oxerr.peatio.rest.dto.Order;
15  
16  import com.xeiam.xchange.currency.CurrencyPair;
17  import com.xeiam.xchange.dto.Order.OrderType;
18  import com.xeiam.xchange.dto.account.AccountInfo;
19  import com.xeiam.xchange.dto.marketdata.OrderBook;
20  import com.xeiam.xchange.dto.marketdata.Ticker;
21  import com.xeiam.xchange.dto.marketdata.Trade;
22  import com.xeiam.xchange.dto.marketdata.Trades;
23  import com.xeiam.xchange.dto.marketdata.Trades.TradeSortType;
24  import com.xeiam.xchange.dto.trade.LimitOrder;
25  import com.xeiam.xchange.dto.trade.OpenOrders;
26  import com.xeiam.xchange.dto.trade.UserTrade;
27  import com.xeiam.xchange.dto.trade.UserTrades;
28  import com.xeiam.xchange.dto.trade.Wallet;
29  
30  /**
31   * Various adapters for converting from Peatio DTOs to XChange DTOs.
32   */
33  public final class PeatioAdapters {
34  
35  	private PeatioAdapters() {
36  	}
37  
38  	public static List<CurrencyPair> adaptCurrencyPairs(Market[] markets) {
39  		return Arrays.stream(markets).map(market -> adaptCurrencyPair(market)).collect(toList());
40  	}
41  
42  	public static CurrencyPair adaptCurrencyPair(Market market) {
43  		String[] parts = market.getName().split("/");
44  		return new CurrencyPair(parts[0], parts[1]);
45  	}
46  
47  	public static CurrencyPair adaptCurrencyPair(String marketId) {
48  		int l = marketId.length();
49  		String counterSymbol = marketId.substring(l - 3, l);
50  		String baseSymbol = marketId.substring(0, l - 3);
51  		return new CurrencyPair(baseSymbol.toUpperCase(), counterSymbol.toUpperCase());
52  	}
53  
54  	public static String adaptMarketId(CurrencyPair currencyPair) {
55  		return (currencyPair.baseSymbol + currencyPair.counterSymbol).toLowerCase();
56  	}
57  
58  	public static Ticker adaptTicker(CurrencyPair currencyPair, MarketTicker ticker) {
59  		return new Ticker.Builder()
60  			.currencyPair(currencyPair)
61  			.timestamp(ticker.getAt())
62  			.bid(ticker.getTicker().getBuy())
63  			.ask(ticker.getTicker().getSell())
64  			.low(ticker.getTicker().getLow())
65  			.high(ticker.getTicker().getHigh())
66  			.last(ticker.getTicker().getLast())
67  			.volume(ticker.getTicker().getVol())
68  			.build();
69  	}
70  
71  	/**
72  	 * Adapt market depth to generic order book.
73  	 *
74  	 * @param currencyPair the currency pair.
75  	 * @param depth the raw depth.
76  	 * @return the generic order book.
77  	 */
78  	public static OrderBook adaptOrderBook(CurrencyPair currencyPair, Depth depth) {
79  		List<LimitOrder> asks = adaptLimitOrders(currencyPair, OrderType.ASK, depth.getAsks());
80  		List<LimitOrder> bids = adaptLimitOrders(currencyPair, OrderType.BID, depth.getBids());
81  		return new OrderBook(depth.getTimestamp(), asks, bids);
82  	}
83  
84  	/**
85  	 * Adapt depth items to limit order list.
86  	 *
87  	 * @param currencyPair the currency pair.
88  	 * @param orderType the order type, bid or ask.
89  	 * @param items the depth items.
90  	 * @return the sorted limit order list.
91  	 */
92  	public static List<LimitOrder> adaptLimitOrders(CurrencyPair currencyPair,
93  			OrderType orderType, BigDecimal[][] items) {
94  		return Arrays
95  			.stream(items)
96  			.map(
97  				item -> new LimitOrder.Builder(orderType, currencyPair)
98  					.limitPrice(item[0])
99  					.tradableAmount(item[1])
100 					.build()
101 			)
102 			.sorted()
103 			.collect(toList());
104 	}
105 
106 	public static List<LimitOrder> adaptLimitOrders(Order[] orders) {
107 		return Arrays.stream(orders).map(order -> adaptLimitOrder(order)).collect(toList());
108 	}
109 
110 	public static List<LimitOrder> adaptLimitOrders(Market market, Order[] orders) {
111 		CurrencyPair currencyPair = adaptCurrencyPair(market);
112 		return adaptLimitOrders(currencyPair, orders);
113 	}
114 
115 	public static List<LimitOrder> adaptLimitOrders(CurrencyPair currencyPair, Order[] orders) {
116 		return Arrays.stream(orders).map(order -> adaptLimitOrder(currencyPair, order)).collect(toList());
117 	}
118 
119 	public static LimitOrder adaptLimitOrder(Order order) {
120 		return adaptLimitOrder(adaptCurrencyPair(order.getMarket()), order);
121 	}
122 
123 	public static LimitOrder adaptLimitOrder(CurrencyPair currencyPair, Order order) {
124 		return new LimitOrder.Builder(
125 				adaptOrderType(order.getSide()), currencyPair)
126 			.id(String.valueOf(order.getId()))
127 			.limitPrice(order.getPrice())
128 			.timestamp(order.getCreatedAt())
129 			.tradableAmount(order.getRemainingVolume())
130 			.build();
131 	}
132 
133 	public static OrderType adaptOrderType(String side) {
134 		if (side == null) {
135 			return null;
136 		}
137 
138 		OrderType orderType;
139 		switch (side) {
140 		case "sell":
141 		case "ask":
142 			orderType = OrderType.ASK;
143 			break;
144 		case "buy":
145 		case "bid":
146 			orderType = OrderType.BID;
147 			break;
148 		default:
149 			throw new IllegalArgumentException("Unknow order side: " + side);
150 		}
151 		return orderType;
152 	}
153 
154 	public static String adaptSide(OrderType orderType) {
155 		return orderType == OrderType.ASK ? "sell" : "buy";
156 	}
157 
158 	public static Trades adaptTrades(
159 			org.oxerr.peatio.rest.dto.Trade[] tradeArray) {
160 		List<Trade> tradeList = Arrays.stream(tradeArray)
161 				.map(trade -> adaptTrade(trade)).collect(toList());
162 		return new Trades(tradeList,
163 				tradeList.size() > 0 ? Long.valueOf(tradeList.get(0).getId()) : null,
164 				TradeSortType.SortByID);
165 	}
166 
167 	public static Trade adaptTrade(org.oxerr.peatio.rest.dto.Trade trade) {
168 		return new Trade.Builder()
169 			.id(String.valueOf(trade.getId()))
170 			.price(trade.getPrice())
171 			.tradableAmount(trade.getVolume())
172 			.currencyPair(adaptCurrencyPair(trade.getMarket()))
173 			.timestamp(trade.getCreatedAt())
174 			.type(adaptOrderType(trade.getSide()))
175 			.build();
176 	}
177 
178 	public static AccountInfo adaptMember(Member member) {
179 		List<Wallet> wallets = Arrays
180 				.stream(member.getAccounts())
181 				.map(account -> new Wallet(account.getCurrency().toUpperCase(),
182 						account.getBalance())).collect(toList());
183 		AccountInfo accountInfo = new AccountInfo(member.getEmail(), wallets);
184 		return accountInfo;
185 	}
186 
187 	public static OpenOrders adaptOpenOrders(Map<Market, Order[]> ordersMap) {
188 		List<LimitOrder> openOrders = ordersMap
189 				.entrySet()
190 				.stream()
191 				.map(e -> adaptLimitOrders(e.getKey(), e.getValue()))
192 				.flatMap(x -> x.stream())
193 				.collect(toList());
194 		return new OpenOrders(openOrders);
195 	}
196 
197 	public static UserTrade adaptUserTrade(CurrencyPair currencyPair,
198 			org.oxerr.peatio.rest.dto.Trade trade) {
199 		return new UserTrade.Builder()
200 			.id(String.valueOf(trade.getId()))
201 			.price(trade.getPrice())
202 			.tradableAmount(trade.getVolume())
203 			.currencyPair(currencyPair)
204 			.timestamp(trade.getCreatedAt())
205 			.type(adaptOrderType(trade.getSide()))
206 			.orderId(String.valueOf(trade.getOrderId()))
207 			.build();
208 	}
209 
210 	public static List<UserTrade> adaptUserTradeList(Market market,
211 			org.oxerr.peatio.rest.dto.Trade[] trade) {
212 		return adaptUserTradeList(adaptCurrencyPair(market), trade);
213 	}
214 
215 	public static List<UserTrade> adaptUserTradeList(CurrencyPair currencyPair,
216 			org.oxerr.peatio.rest.dto.Trade[] trades) {
217 		return Arrays.stream(trades)
218 				.map(trade -> adaptUserTrade(currencyPair, trade))
219 				.collect(toList());
220 	}
221 
222 	public static UserTrades adaptUserTrades(CurrencyPair currencyPair,
223 			org.oxerr.peatio.rest.dto.Trade[] trades) {
224 		long lastID = trades.length > 0 ? trades[trades.length - 1].getId() : 0;
225 		return new UserTrades(adaptUserTradeList(currencyPair, trades), lastID,
226 				TradeSortType.SortByID);
227 	}
228 
229 	public static UserTrades adaptUserTrades(
230 			Map<Market, org.oxerr.peatio.rest.dto.Trade[]> tradesMap) {
231 		List<UserTrade> trades = tradesMap
232 				.entrySet()
233 				.stream()
234 				.map(e -> adaptUserTradeList(e.getKey(), e.getValue()))
235 				.flatMap(x -> x.stream())
236 				.collect(toList());
237 		int size = trades.size();
238 		long lastID = size > 0 ? Long.parseLong(trades.get(size - 1).getId()) : 0;
239 		return new UserTrades(trades, lastID, TradeSortType.SortByID);
240 	}
241 
242 }