Skip to content

Commit

Permalink
Implement CoinbaseEx (GDAX) remote init
Browse files Browse the repository at this point in the history
  • Loading branch information
mmazi committed Sep 28, 2016
1 parent b77df53 commit d36258a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.knowm.xchange.coinbaseex.dto.account.CoinbaseExAccount;
Expand All @@ -27,6 +29,10 @@
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.marketdata.Trades.TradeSortType;
import org.knowm.xchange.dto.meta.CurrencyMetaData;
import org.knowm.xchange.dto.meta.CurrencyPairMetaData;
import org.knowm.xchange.dto.meta.ExchangeMetaData;
import org.knowm.xchange.dto.meta.RateLimit;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.dto.trade.UserTrade;
Expand Down Expand Up @@ -151,12 +157,23 @@ public static Trades adaptTrades(CoinbaseExTrade[] coinbaseExTrades, CurrencyPai
return new Trades(trades, TradeSortType.SortByID);
}

public static List<CurrencyPair> adaptProductsToSupportedExchangeSymbols(List<CoinbaseExProduct> products) {
List<CurrencyPair> result = new ArrayList<CurrencyPair>();
public static CurrencyPair adaptCurrencyPair(CoinbaseExProduct product) {
return new CurrencyPair(product.getBaseCurrency(), product.getTargetCurrency());
}

public static ExchangeMetaData adaptToExchangeMetaData(List<CoinbaseExProduct> products) {
Map<CurrencyPair, CurrencyPairMetaData> currencyPairs = new HashMap<>();
Map<Currency, CurrencyMetaData> currencies = new HashMap<>();
for (CoinbaseExProduct product : products) {
result.add(new CurrencyPair(product.getBaseCurrency(), product.getTargetCurrency()));
BigDecimal minSize = product.getBaseMinSize().setScale(product.getQuoteIncrement().scale(), BigDecimal.ROUND_UNNECESSARY);
BigDecimal maxSize = product.getBaseMaxSize().setScale(product.getQuoteIncrement().scale(), BigDecimal.ROUND_UNNECESSARY);
CurrencyPairMetaData cpmd = new CurrencyPairMetaData(null, minSize, maxSize, 8); // todo: 8 is a wild guess
CurrencyPair pair = adaptCurrencyPair(product);
currencyPairs.put(pair, cpmd);
currencies.put(pair.base, null);
currencies.put(pair.counter, null);
}

return result;
return new ExchangeMetaData(currencyPairs, currencies, new RateLimit[0], new RateLimit[0], true);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.knowm.xchange.coinbaseex;

import java.io.IOException;
import java.util.List;

import org.knowm.xchange.BaseExchange;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeSpecification;
import org.knowm.xchange.coinbaseex.dto.marketdata.CoinbaseExProduct;
import org.knowm.xchange.coinbaseex.service.polling.CoinbaseExAccountService;
import org.knowm.xchange.coinbaseex.service.polling.CoinbaseExMarketDataService;
import org.knowm.xchange.coinbaseex.service.polling.CoinbaseExMarketDataServiceRaw;
import org.knowm.xchange.coinbaseex.service.polling.CoinbaseExTradeService;
import org.knowm.xchange.utils.nonce.CurrentTimeNonceFactory;

Expand Down Expand Up @@ -43,14 +46,7 @@ public SynchronizedValueFactory<Long> getNonceFactory() {

@Override
public void remoteInit() throws IOException {

// TODO Implement this.
// List<CoinbaseExProduct> products = ((CoinbaseExMarketDataServiceRaw) pollingMarketDataService). getConbaseExProducts()();
// other endpoints?
// hard-coded meta data from json file not available at an endpoint?
// TODO take all the info gathered above and create a `ExchangeMetaData` object via a new method in `*Adapters` class
// exchangeMetaData = *Adapters.adaptToExchangeMetaData(blah, blah);

super.remoteInit();
List<CoinbaseExProduct> products = ((CoinbaseExMarketDataServiceRaw) pollingMarketDataService).getConbaseExProducts();
exchangeMetaData = CoinbaseExAdapters.adaptToExchangeMetaData(products);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public CoinbaseExTrade[] getCoinbaseExTrades(CurrencyPair currencyPair, int limi

}

private boolean checkProductExists(CurrencyPair currencyPair) throws IOException {
public boolean checkProductExists(CurrencyPair currencyPair) throws IOException {

boolean currencyPairSupported = false;
for (CurrencyPair cp : exchange.getExchangeSymbols()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package org.knowm.xchange.coinbaseex;

import org.junit.Assert;
import org.junit.Test;

import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.coinbaseex.service.polling.CoinbaseExMarketDataServiceRaw;
import org.knowm.xchange.currency.CurrencyPair;

public class CoinbaseExExchangeTest {

@Test
public void coinbaseShouldBeInstantiatedWithoutAnExceptionWhenUsingDefaultSpecification() {
ExchangeFactory.INSTANCE.createExchange(CoinbaseExExchange.class.getCanonicalName());
}

@Test
public void shouldSupportEthUsdByRemoteInit() throws Exception {
Exchange ex = ExchangeFactory.INSTANCE.createExchange(CoinbaseExExchange.class.getCanonicalName());
ex.remoteInit();
Assert.assertTrue(((CoinbaseExMarketDataServiceRaw) ex.getPollingMarketDataService()).checkProductExists(new CurrencyPair("ETH", "USD")));
}
}

0 comments on commit d36258a

Please sign in to comment.