Skip to content

Commit

Permalink
[core] [refactor] Make Currency into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
baffo32 committed Nov 28, 2015
1 parent a8066a2 commit 67e8809
Show file tree
Hide file tree
Showing 234 changed files with 1,764 additions and 1,247 deletions.
4 changes: 2 additions & 2 deletions xchange-anx/src/main/java/com/xeiam/xchange/anx/ANXUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.net.URLEncoder;
import java.util.List;

import com.xeiam.xchange.currency.Currencies;
import com.xeiam.xchange.currency.Currency;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.trade.LimitOrder;

Expand Down Expand Up @@ -51,7 +51,7 @@ public static boolean findLimitOrder(List<LimitOrder> orders, LimitOrder order,

public static int getMaxPriceScale(CurrencyPair currencyPair) {

if (currencyPair.baseSymbol.equalsIgnoreCase(Currencies.BTC.toString()) || currencyPair.baseSymbol.equalsIgnoreCase(Currencies.LTC.toString())) {
if (currencyPair.base.equals(Currency.BTC) || currencyPair.base.equals(Currency.LTC)) {
return 5;
} else {
return 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private static UserTrade adaptUserTrade(ANXTradeResult aNXTradeResult, ANXMetaDa
OrderType type = adaptSide(aNXTradeResult.getSide());
// for fees, getWalletHistory should be used.
return new UserTrade(type, tradedCurrencyFillAmount, currencyPair, price, aNXTradeResult.getTimestamp(), aNXTradeResult.getTradeId(),
aNXTradeResult.getOrderId(), null, null);
aNXTradeResult.getOrderId());
}

private static CurrencyPair adaptCurrencyPair(String currencyPairRaw) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.xeiam.xchange.currency.Currency;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.meta.CurrencyMetaData;
import com.xeiam.xchange.dto.meta.ExchangeMetaData;
Expand All @@ -16,7 +17,7 @@ public class ANXMetaData extends ExchangeMetaData {
public BigDecimal takerTradingFee;

public ANXMetaData(@JsonProperty("currencyPair") Map<CurrencyPair, ANXMarketMetaData> currencyPairs,
@JsonProperty("currency") Map<String, CurrencyMetaData> currency, @JsonProperty("publicRateLimits") Set<RateLimit> publicRateLimits,
@JsonProperty("currency") Map<Currency, CurrencyMetaData> currency, @JsonProperty("publicRateLimits") Set<RateLimit> publicRateLimits,
@JsonProperty("privateRateLimits") Set<RateLimit> privateRateLimits, @JsonProperty("shareRateLimits") Boolean shareRateLimits,
@JsonProperty("makerTradingFee") BigDecimal makerTradingFee, @JsonProperty("takerTradingFee") BigDecimal takerTradingFee) {
super((Map) currencyPairs, currency, publicRateLimits, privateRateLimits, shareRateLimits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.xeiam.xchange.BaseExchange;
import com.xeiam.xchange.anx.ANXUtils;
import com.xeiam.xchange.anx.v2.ANXAdapters;
import com.xeiam.xchange.currency.Currency;
import com.xeiam.xchange.dto.account.AccountInfo;
import com.xeiam.xchange.service.polling.account.PollingAccountService;

Expand Down Expand Up @@ -34,7 +35,7 @@ public AccountInfo getAccountInfo() throws IOException {
}

@Override
public String withdrawFunds(String currency, BigDecimal amount, String address) throws IOException {
public String withdrawFunds(Currency currency, BigDecimal amount, String address) throws IOException {

if (amount.scale() > ANXUtils.VOLUME_AND_AMOUNT_MAX_SCALE) {
throw new IllegalArgumentException("Amount scale exceed " + ANXUtils.VOLUME_AND_AMOUNT_MAX_SCALE);
Expand All @@ -44,14 +45,14 @@ public String withdrawFunds(String currency, BigDecimal amount, String address)
throw new IllegalArgumentException("Amount cannot be null");
}

anxWithdrawFunds(currency, amount, address);
anxWithdrawFunds(currency.toString(), amount, address);
return "success";
}

@Override
public String requestDepositAddress(String currency, String... args) throws IOException {
public String requestDepositAddress(Currency currency, String... args) throws IOException {

return anxRequestDepositAddress(currency).getAddress();
return anxRequestDepositAddress(currency.toString()).getAddress();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws
}

// Adapt to XChange DTOs
List<LimitOrder> asks = ANXAdapters.adaptOrders(anxDepthWrapper.getAnxDepth().getAsks(), currencyPair.baseSymbol, currencyPair.counterSymbol,
List<LimitOrder> asks = ANXAdapters.adaptOrders(anxDepthWrapper.getAnxDepth().getAsks(), currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode(),
"ask", "");
List<LimitOrder> bids = ANXAdapters.adaptOrders(anxDepthWrapper.getAnxDepth().getBids(), currencyPair.baseSymbol, currencyPair.counterSymbol,
List<LimitOrder> bids = ANXAdapters.adaptOrders(anxDepthWrapper.getAnxDepth().getBids(), currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode(),
"bid", "");
Date date = new Date(anxDepthWrapper.getAnxDepth().getMicroTime() / 1000);
return new OrderBook(date, asks, bids);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected ANXMarketDataServiceRaw(Exchange exchange) {
public ANXTicker getANXTicker(CurrencyPair currencyPair) throws IOException {

try {
ANXTickerWrapper anxTickerWrapper = anxV2.getTicker(currencyPair.baseSymbol, currencyPair.counterSymbol);
ANXTickerWrapper anxTickerWrapper = anxV2.getTicker(currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode());
return anxTickerWrapper.getAnxTicker();
} catch (ANXException e) {
throw handleError(e);
Expand All @@ -60,7 +60,7 @@ public Map<String, ANXTicker> getANXTickers(Collection<CurrencyPair> currencyPai
if (i++ == currencyPairs.size()) {
pathCurrencyPair = currencyPair;
} else {
extraCurrencyPairs.append(currencyPair.baseSymbol).append(currencyPair.counterSymbol).append(",");
extraCurrencyPairs.append(currencyPair.base.getCurrencyCode()).append(currencyPair.counter.getCurrencyCode()).append(",");
}
}
if (pathCurrencyPair == null) {
Expand All @@ -70,10 +70,10 @@ public Map<String, ANXTicker> getANXTickers(Collection<CurrencyPair> currencyPai
if (i == 2) {
ANXTicker anxTicker = getANXTicker(pathCurrencyPair);
Map<String, ANXTicker> ticker = new HashMap<String, ANXTicker>();
ticker.put(pathCurrencyPair.baseSymbol + pathCurrencyPair.counterSymbol, anxTicker);
ticker.put(pathCurrencyPair.base.getCurrencyCode() + pathCurrencyPair.counter.getCurrencyCode(), anxTicker);
return ticker;
}
ANXTickersWrapper anxTickerWrapper = anxV2.getTickers(pathCurrencyPair.baseSymbol, pathCurrencyPair.counterSymbol,
ANXTickersWrapper anxTickerWrapper = anxV2.getTickers(pathCurrencyPair.base.getCurrencyCode(), pathCurrencyPair.counter.getCurrencyCode(),
extraCurrencyPairs.toString());
return anxTickerWrapper.getAnxTickers();
} catch (ANXException e) {
Expand All @@ -86,7 +86,7 @@ public Map<String, ANXTicker> getANXTickers(Collection<CurrencyPair> currencyPai
public ANXDepthWrapper getANXFullOrderBook(CurrencyPair currencyPair) throws IOException {

try {
ANXDepthWrapper anxDepthWrapper = anxV2.getFullDepth(currencyPair.baseSymbol, currencyPair.counterSymbol);
ANXDepthWrapper anxDepthWrapper = anxV2.getFullDepth(currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode());
return anxDepthWrapper;
} catch (ANXException e) {
throw handleError(e);
Expand All @@ -104,7 +104,7 @@ public Map<String, ANXDepth> getANXFullOrderBooks(Collection<CurrencyPair> curre
if (i++ == currencyPairs.size()) {
pathCurrencyPair = currencyPair;
} else {
extraCurrencyPairs.append(currencyPair.baseSymbol).append(currencyPair.counterSymbol).append(",");
extraCurrencyPairs.append(currencyPair.base.getCurrencyCode()).append(currencyPair.counter.getCurrencyCode()).append(",");
}
}
if (pathCurrencyPair == null) {
Expand All @@ -114,10 +114,10 @@ public Map<String, ANXDepth> getANXFullOrderBooks(Collection<CurrencyPair> curre
if (i == 2) {
ANXDepthWrapper anxDepthWrapper = getANXFullOrderBook(pathCurrencyPair);
Map<String, ANXDepth> book = new HashMap<String, ANXDepth>();
book.put(pathCurrencyPair.baseSymbol + pathCurrencyPair.counterSymbol, anxDepthWrapper.getAnxDepth());
book.put(pathCurrencyPair.base.getCurrencyCode() + pathCurrencyPair.counter.getCurrencyCode(), anxDepthWrapper.getAnxDepth());
return book;
}
ANXDepthsWrapper anxDepthWrapper = anxV2.getFullDepths(pathCurrencyPair.baseSymbol, pathCurrencyPair.counterSymbol,
ANXDepthsWrapper anxDepthWrapper = anxV2.getFullDepths(pathCurrencyPair.base.getCurrencyCode(), pathCurrencyPair.counter.getCurrencyCode(),
extraCurrencyPairs.toString());
return anxDepthWrapper.getAnxDepths();
} catch (ANXException e) {
Expand All @@ -130,7 +130,7 @@ public Map<String, ANXDepth> getANXFullOrderBooks(Collection<CurrencyPair> curre
public ANXDepthWrapper getANXPartialOrderBook(CurrencyPair currencyPair) throws IOException {

try {
ANXDepthWrapper anxDepthWrapper = anxV2.getPartialDepth(currencyPair.baseSymbol, currencyPair.counterSymbol);
ANXDepthWrapper anxDepthWrapper = anxV2.getPartialDepth(currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode());
return anxDepthWrapper;
} catch (ANXException e) {
throw handleError(e);
Expand All @@ -142,7 +142,7 @@ public ANXDepthWrapper getANXPartialOrderBook(CurrencyPair currencyPair) throws
public List<ANXTrade> getANXTrades(CurrencyPair currencyPair, Long sinceTimeStamp) throws IOException {

try {
return anxV2.getTrades(currencyPair.baseSymbol, currencyPair.counterSymbol, sinceTimeStamp).getANXTrades();
return anxV2.getTrades(currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode(), sinceTimeStamp).getANXTrades();
} catch (ANXException e) {
throw handleError(e);
} catch (HttpStatusIOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ANXGenericResponse placeANXMarketOrder(MarketOrder marketOrder) throws IO

try {
ANXGenericResponse anxGenericResponse = anxV2.placeOrder(exchange.getExchangeSpecification().getApiKey(), signatureCreator,
exchange.getNonceFactory(), marketOrder.getCurrencyPair().baseSymbol, marketOrder.getCurrencyPair().counterSymbol,
exchange.getNonceFactory(), marketOrder.getCurrencyPair().base.getCurrencyCode(), marketOrder.getCurrencyPair().counter.getCurrencyCode(),
marketOrder.getType().equals(Order.OrderType.BID) ? "bid" : "ask", marketOrder.getTradableAmount(), null);
return anxGenericResponse;
} catch (ANXException e) {
Expand All @@ -84,7 +84,7 @@ public ANXGenericResponse placeANXLimitOrder(CurrencyPair currencyPair, String t

try {
ANXGenericResponse anxGenericResponse = anxV2.placeOrder(exchange.getExchangeSpecification().getApiKey(), signatureCreator,
exchange.getNonceFactory(), currencyPair.baseSymbol, currencyPair.counterSymbol, type, amount, price);
exchange.getNonceFactory(), currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode(), type, amount, price);

return anxGenericResponse;
} catch (ANXException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.List;

import com.xeiam.xchange.currency.Currency;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -24,7 +25,6 @@
import com.xeiam.xchange.anx.v2.dto.marketdata.TickerJSONTest;
import com.xeiam.xchange.anx.v2.dto.meta.ANXMetaData;
import com.xeiam.xchange.anx.v2.dto.trade.polling.ANXOpenOrder;
import com.xeiam.xchange.currency.Currencies;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.Order.OrderType;
import com.xeiam.xchange.dto.account.AccountInfo;
Expand Down Expand Up @@ -61,9 +61,9 @@ public void testAccountInfoAdapter() throws IOException {
AccountInfo accountInfo = ANXAdapters.adaptAccountInfo(anxAccountInfo);
assertThat(accountInfo.getUsername()).isEqualTo("[email protected]");

assertThat(accountInfo.getWallet(Currencies.DOGE).getBalance()).isEqualTo(new BigDecimal("9999781.09457936"));
assertThat(accountInfo.getWallet(Currencies.DOGE).getAvailable()).isEqualTo(new BigDecimal("9914833.52608521"));
assertThat(accountInfo.getWallet(Currencies.DOGE).getFrozen()).isEqualTo(new BigDecimal("84947.56849415"));
assertThat(accountInfo.getWallet(Currency.DOGE).getBalance()).isEqualTo(new BigDecimal("9999781.09457936"));
assertThat(accountInfo.getWallet(Currency.DOGE).getAvailable()).isEqualTo(new BigDecimal("9914833.52608521"));
assertThat(accountInfo.getWallet(Currency.DOGE).getFrozen()).isEqualTo(new BigDecimal("84947.56849415"));
}

@Test
Expand All @@ -88,8 +88,8 @@ public void testOrderAdapterWithOpenOrders() throws IOException {
Assert.assertEquals(new BigDecimal("412.34567"), openorders.get(0).getLimitPrice());
Assert.assertEquals(new BigDecimal("10.00000000"), openorders.get(0).getTradableAmount());

Assert.assertEquals("BTC", openorders.get(0).getCurrencyPair().baseSymbol);
Assert.assertEquals("HKD", openorders.get(0).getCurrencyPair().counterSymbol);
Assert.assertEquals("BTC", openorders.get(0).getCurrencyPair().base.getCurrencyCode());
Assert.assertEquals("HKD", openorders.get(0).getCurrencyPair().counter.getCurrencyCode());

Assert.assertEquals(new Date(1393411075000L), openorders.get(0).getTimestamp());
}
Expand All @@ -114,8 +114,8 @@ public void testOrderAdapterWithDepth() throws IOException {
Assert.assertEquals(new BigDecimal("16.00000000"), asks.get(0).getTradableAmount());
Assert.assertEquals(new BigDecimal("3260.40000"), asks.get(0).getLimitPrice());

Assert.assertEquals("BTC", asks.get(0).getCurrencyPair().baseSymbol);
Assert.assertEquals("USD", asks.get(0).getCurrencyPair().counterSymbol);
Assert.assertEquals("BTC", asks.get(0).getCurrencyPair().base.getCurrencyCode());
Assert.assertEquals("USD", asks.get(0).getCurrencyPair().counter.getCurrencyCode());
}

@Test
Expand Down Expand Up @@ -160,9 +160,9 @@ public void testWalletAdapter() throws IOException {
List<Wallet> wallets = ANXAdapters.adaptWallets(anxAccountInfo.getWallets());
Assert.assertEquals(21, wallets.size());

Assert.assertTrue(wallets.contains(new Wallet(Currencies.CAD, new BigDecimal("100000.00000"), new BigDecimal("100000.00000"))));
Assert.assertTrue(wallets.contains(new Wallet(Currencies.BTC, new BigDecimal("100000.01988000"), new BigDecimal("100000.01988000"))));
Assert.assertTrue(wallets.contains(new Wallet(Currencies.DOGE, new BigDecimal("9999781.09457936"), new BigDecimal("9914833.52608521"))));
Assert.assertTrue(wallets.contains(new Wallet(Currency.CAD, new BigDecimal("100000.00000"), new BigDecimal("100000.00000"))));
Assert.assertTrue(wallets.contains(new Wallet(Currency.BTC, new BigDecimal("100000.01988000"), new BigDecimal("100000.01988000"))));
Assert.assertTrue(wallets.contains(new Wallet(Currency.DOGE, new BigDecimal("9999781.09457936"), new BigDecimal("9914833.52608521"))));
}

@Test
Expand Down
Loading

0 comments on commit 67e8809

Please sign in to comment.