Skip to content

Commit 987a855

Browse files
authored
Merge pull request #2233 from Flemingjp/impl-coinegg
[CoinEgg] Corrected the mix-up between trades and the orderbook
2 parents 3988437 + 5421f7f commit 987a855

15 files changed

+120
-130
lines changed

xchange-coinegg/src/main/java/org/xchange/coinegg/CoinEgg.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import javax.ws.rs.Produces;
99
import javax.ws.rs.core.MediaType;
1010

11-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder;
11+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade;
1212
import org.xchange.coinegg.dto.marketdata.CoinEggTicker;
13-
import org.xchange.coinegg.dto.marketdata.CoinEggTrades;
13+
import org.xchange.coinegg.dto.marketdata.CoinEggOrders;
1414

1515
@Path("api/v1")
1616
@Produces(MediaType.APPLICATION_JSON)
@@ -21,11 +21,11 @@ public interface CoinEgg {
2121
CoinEggTicker getTicker(@PathParam("symbol") String symbol) throws IOException;
2222

2323
@GET
24-
@Path("depth?coin={symbol}")
25-
CoinEggTrades getTrades(@PathParam("symbol") String symbol) throws IOException;
24+
@Path("orders?coin={symbol}")
25+
CoinEggTrade[] getTrades(@PathParam("symbol") String symbol) throws IOException;
2626

2727
@GET
28-
@Path("orders?coin={symbol}")
29-
CoinEggOrder[] getOrders(@PathParam("symbol") String symbol) throws IOException;
28+
@Path("depth?coin={symbol}")
29+
CoinEggOrders getOrders(@PathParam("symbol") String symbol) throws IOException;
3030

3131
}

xchange-coinegg/src/main/java/org/xchange/coinegg/CoinEggAdapters.java

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.math.BigDecimal;
44
import java.util.ArrayList;
5-
import java.util.Date;
65
import java.util.HashSet;
76
import java.util.List;
87
import java.util.Set;
@@ -23,13 +22,12 @@
2322
import org.knowm.xchange.dto.trade.LimitOrder;
2423
import org.knowm.xchange.dto.trade.UserTrade;
2524
import org.knowm.xchange.dto.trade.UserTrades;
26-
import org.knowm.xchange.utils.DateUtils;
2725
import org.xchange.coinegg.dto.accounts.CoinEggBalance;
28-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder;
26+
import org.xchange.coinegg.dto.marketdata.CoinEggOrders;
27+
import org.xchange.coinegg.dto.marketdata.CoinEggOrders.CoinEggOrder;
2928
import org.xchange.coinegg.dto.marketdata.CoinEggTicker;
30-
import org.xchange.coinegg.dto.marketdata.CoinEggTrades;
31-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder.Type;
32-
import org.xchange.coinegg.dto.marketdata.CoinEggTrades.CoinEggTrade;
29+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade;
30+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade.Type;
3331
import org.xchange.coinegg.dto.trade.CoinEggTradeAdd;
3432
import org.xchange.coinegg.dto.trade.CoinEggTradeCancel;
3533
import org.xchange.coinegg.dto.trade.CoinEggTradeView;
@@ -57,58 +55,46 @@ public static Ticker adaptTicker(CoinEggTicker coinEggTicker, CurrencyPair curre
5755
return ticker;
5856
}
5957

60-
public static LimitOrder adaptOrder(CoinEggOrder coinEggOrder, CurrencyPair currencyPair) {
61-
62-
OrderType type = coinEggOrder.getType() == Type.BUY ? OrderType.ASK : OrderType.BID;
63-
Date timestamp = DateUtils.fromMillisUtc(coinEggOrder.getTimestamp());
64-
String id = String.valueOf(coinEggOrder.getTimestamp());
65-
BigDecimal amount = coinEggOrder.getAmount();
66-
BigDecimal price = coinEggOrder.getPrice();
58+
public static LimitOrder adaptOrder(CoinEggOrder order, OrderType type, CurrencyPair currencyPair) {
59+
BigDecimal quantity = order.getQuantity();
60+
BigDecimal price = order.getPrice();
6761

68-
LimitOrder order = new LimitOrder.Builder(type, currencyPair)
69-
.id(id)
70-
.timestamp(timestamp)
71-
.originalAmount(amount)
62+
LimitOrder limitOrder = new LimitOrder.Builder(type, currencyPair)
63+
.originalAmount(quantity)
7264
.limitPrice(price)
7365
.build();
7466

75-
return order;
67+
return limitOrder;
7668
}
7769

78-
public static OrderBook adaptOrders(CoinEggOrder[] coinEggOrders, CurrencyPair currencyPair) {
79-
List<LimitOrder> asks = Stream.of(coinEggOrders)
80-
.filter(o -> o.getType() == Type.BUY)
81-
.map(o -> adaptOrder(o, currencyPair))
70+
public static OrderBook adaptOrders(CoinEggOrders coinEggOrders, CurrencyPair currencyPair) {
71+
72+
73+
List<LimitOrder> asks = Stream.of(coinEggOrders.getAsks())
74+
.map(order -> adaptOrder(order, OrderType.ASK, currencyPair))
8275
.collect(Collectors.toList());
83-
84-
List<LimitOrder> bids = Stream.of(coinEggOrders)
85-
.filter(o -> o.getType() == Type.SELL)
86-
.map(o -> adaptOrder(o, currencyPair))
76+
77+
List<LimitOrder> bids = Stream.of(coinEggOrders.getBids())
78+
.map(order -> adaptOrder(order, OrderType.BID, currencyPair))
8779
.collect(Collectors.toList());
88-
80+
8981
return new OrderBook(null, asks, bids);
9082
}
9183

92-
public static Trade adaptTrade(CoinEggTrade coinEggTrade, OrderType type, CurrencyPair currencyPair) {
84+
public static Trade adaptTrade(CoinEggTrade coinEggTrade, CurrencyPair currencyPair) {
9385
return new Trade.Builder()
9486
.currencyPair(currencyPair)
95-
.id(String.valueOf(coinEggTrade.hashCode()))
96-
.type(type)
87+
.id(String.valueOf(coinEggTrade.getTransactionID()))
88+
.type(coinEggTrade.getOrderType())
9789
.price(coinEggTrade.getPrice())
98-
.originalAmount(coinEggTrade.getQuantity())
90+
.originalAmount(coinEggTrade.getAmount())
9991
.build();
10092
}
10193

102-
public static Trades adaptTrades(CoinEggTrades coinEggTrades, CurrencyPair currencyPair) {
103-
List<Trade> trades = new ArrayList<Trade>();
104-
105-
trades.addAll(Stream.of(coinEggTrades.getAsks())
106-
.map(t -> adaptTrade(t, OrderType.ASK, currencyPair))
107-
.collect(Collectors.toList()));
108-
109-
trades.addAll(Stream.of(coinEggTrades.getBids())
110-
.map(t -> adaptTrade(t, OrderType.BID, currencyPair))
111-
.collect(Collectors.toList()));
94+
public static Trades adaptTrades(CoinEggTrade[] coinEggTrades, CurrencyPair currencyPair) {
95+
List<Trade> trades = Stream.of(coinEggTrades)
96+
.map(t -> adaptTrade(t, currencyPair))
97+
.collect(Collectors.toList());
11298

11399
return new Trades(trades);
114100
}
@@ -146,8 +132,7 @@ public static UserTrades adaptTradeHistory(CoinEggTradeView coinEggTradeView) {
146132

147133
return new UserTrades(trades, null);
148134
}
149-
150-
135+
151136
public static String adaptTradeAdd(CoinEggTradeAdd coinEggTradeAdd) {
152137
return String.valueOf(coinEggTradeAdd.getID());
153138
}

xchange-coinegg/src/main/java/org/xchange/coinegg/CoinEggExchange.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public ExchangeSpecification getDefaultExchangeSpecification() {
3535
exchangeSpecification.setPort(80);
3636
exchangeSpecification.setExchangeName("CoinEgg");
3737
exchangeSpecification.setExchangeDescription("CoinEgg is a Bitcoin exchange based in the United Kingdom.");
38+
exchangeSpecification.setApiKey("");
39+
exchangeSpecification.setSecretKey("");
40+
exchangeSpecification.setPassword("");
3841

3942
return exchangeSpecification;
4043
}

xchange-coinegg/src/main/java/org/xchange/coinegg/dto/marketdata/CoinEggTrades.java renamed to xchange-coinegg/src/main/java/org/xchange/coinegg/dto/marketdata/CoinEggOrders.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import com.fasterxml.jackson.annotation.JsonFormat;
66
import com.fasterxml.jackson.annotation.JsonProperty;
77

8-
public class CoinEggTrades {
8+
public class CoinEggOrders {
99

1010
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
11-
public static class CoinEggTrade {
11+
public static class CoinEggOrder {
1212

1313
@JsonProperty() private BigDecimal price;
1414
@JsonProperty() private BigDecimal quantity;
@@ -22,19 +22,19 @@ public final BigDecimal getQuantity() {
2222
}
2323
}
2424

25-
private final CoinEggTrade[] asks;
26-
private final CoinEggTrade[] bids;
25+
private final CoinEggOrder[] asks;
26+
private final CoinEggOrder[] bids;
2727

28-
public CoinEggTrades(@JsonProperty("asks") CoinEggTrade[] asks, @JsonProperty("bids") CoinEggTrade[] bids) {
28+
public CoinEggOrders(@JsonProperty("asks") CoinEggOrder[] asks, @JsonProperty("bids") CoinEggOrder[] bids) {
2929
this.asks = asks;
3030
this.bids = bids;
3131
}
3232

33-
public CoinEggTrade[] getAsks() {
33+
public CoinEggOrder[] getAsks() {
3434
return asks;
3535
}
3636

37-
public CoinEggTrade[] getBids() {
37+
public CoinEggOrder[] getBids() {
3838
return bids;
3939
}
4040
}

xchange-coinegg/src/main/java/org/xchange/coinegg/dto/marketdata/CoinEggOrder.java renamed to xchange-coinegg/src/main/java/org/xchange/coinegg/dto/marketdata/CoinEggTrade.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.math.BigDecimal;
44

5+
import org.knowm.xchange.dto.Order.OrderType;
6+
57
import com.fasterxml.jackson.annotation.JsonCreator;
68
import com.fasterxml.jackson.annotation.JsonProperty;
79

8-
public class CoinEggOrder {
10+
public class CoinEggTrade {
911

1012
public enum Type {
1113
BUY,
@@ -23,7 +25,7 @@ public static Type forValue(String value) {
2325
private final long timestamp;
2426
private final int tid;
2527

26-
public CoinEggOrder(@JsonProperty("date") long timestamp, @JsonProperty("price") BigDecimal price,
28+
public CoinEggTrade(@JsonProperty("date") long timestamp, @JsonProperty("price") BigDecimal price,
2729
@JsonProperty("amount") BigDecimal amount, @JsonProperty("tid") int tid, @JsonProperty("type") Type type) {
2830

2931
this.timestamp = timestamp;
@@ -46,6 +48,10 @@ public Type getType() {
4648
return type;
4749
}
4850

51+
public OrderType getOrderType() {
52+
return type == Type.BUY ? OrderType.BID : OrderType.ASK;
53+
}
54+
4955
public int getTransactionID() {
5056
return tid;
5157
}

xchange-coinegg/src/main/java/org/xchange/coinegg/dto/trade/CoinEggTradeList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Date;
55

66
import org.knowm.xchange.utils.DateUtils;
7-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder.Type;
7+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade.Type;
88

99
import com.fasterxml.jackson.annotation.JsonProperty;
1010
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

xchange-coinegg/src/main/java/org/xchange/coinegg/dto/trade/CoinEggTradeView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.math.BigDecimal;
44

5-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder.Type;
5+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade.Type;
66

77
import com.fasterxml.jackson.annotation.JsonProperty;
88

xchange-coinegg/src/main/java/org/xchange/coinegg/service/CoinEggMarketDataServiceRaw.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.io.IOException;
44

55
import org.knowm.xchange.Exchange;
6-
import org.xchange.coinegg.dto.marketdata.CoinEggOrder;
6+
import org.xchange.coinegg.dto.marketdata.CoinEggTrade;
77
import org.xchange.coinegg.dto.marketdata.CoinEggTicker;
8-
import org.xchange.coinegg.dto.marketdata.CoinEggTrades;
8+
import org.xchange.coinegg.dto.marketdata.CoinEggOrders;
99

1010
public class CoinEggMarketDataServiceRaw extends CoinEggBaseService {
1111

@@ -19,12 +19,12 @@ public CoinEggTicker getCoinEggTicker(String coin) throws IOException {
1919
}
2020

2121
// TODO: Exception Handling - See Bitfinex
22-
public CoinEggTrades getCoinEggTrades(String coin) throws IOException {
22+
public CoinEggTrade[] getCoinEggTrades(String coin) throws IOException {
2323
return coinEgg.getTrades(coin);
2424
}
2525

2626
// TODO: Exception Handling - See Bitfinex
27-
public CoinEggOrder[] getCoinEggOrders(String coin) throws IOException {
27+
public CoinEggOrders getCoinEggOrders(String coin) throws IOException {
2828
return coinEgg.getOrders(coin);
2929
}
3030
}

xchange-coinegg/src/test/java/org/xchange/coinegg/dto/marketdata/CoinEggOrderJSONTest.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.xchange.coinegg.dto.marketdata;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.math.BigDecimal;
8+
9+
import org.junit.Test;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
public class CoinEggOrdersJSONTest {
14+
15+
@Test
16+
public void testUnmarshal() throws IOException {
17+
18+
// Read in the JSON from the example resources
19+
InputStream is = CoinEggOrdersJSONTest.class.getResourceAsStream("/marketdata/example-depth-data.json");
20+
21+
// Parse JSON Example Using Jackson
22+
ObjectMapper mapper = new ObjectMapper();
23+
CoinEggOrders coinEggOrders = mapper.readValue(is, CoinEggOrders.class);
24+
25+
// Verify The Trades Unmarshalls Correctly
26+
assertThat(coinEggOrders).isNotNull();
27+
28+
assertThat(coinEggOrders.getAsks()).isNotNull();
29+
assertThat(coinEggOrders.getAsks()).isNotEmpty();
30+
assertThat(coinEggOrders.getAsks().length).isEqualTo(31);
31+
32+
assertThat(coinEggOrders.getAsks()[0].getPrice()).isEqualTo(new BigDecimal("70000"));
33+
assertThat(coinEggOrders.getAsks()[0].getQuantity()).isEqualTo(new BigDecimal("5"));
34+
35+
assertThat(coinEggOrders.getBids()).isNotNull();
36+
assertThat(coinEggOrders.getBids()).isNotEmpty();
37+
assertThat(coinEggOrders.getBids().length).isEqualTo(32);
38+
39+
assertThat(coinEggOrders.getBids()[0].getPrice()).isEqualTo(new BigDecimal("38300"));
40+
assertThat(coinEggOrders.getBids()[0].getQuantity()).isEqualTo(new BigDecimal("1.879"));
41+
}
42+
}

0 commit comments

Comments
 (0)