2017-11-29 09:51:45 -08:00
|
|
|
package com.generalbytes.batm.server.extensions.extra.dash.sources.coinmarketcap;
|
|
|
|
|
2018-04-24 08:22:12 -07:00
|
|
|
import com.generalbytes.batm.server.extensions.Currencies;
|
2017-11-29 09:51:45 -08:00
|
|
|
import com.generalbytes.batm.server.extensions.IRateSource;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2018-06-03 09:09:38 -07:00
|
|
|
import java.util.HashMap;
|
2017-11-29 09:51:45 -08:00
|
|
|
import si.mazi.rescu.RestProxyFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by kkyovsky on 11/29/17.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class CoinmarketcapRateSource implements IRateSource {
|
|
|
|
private ICoinmarketcapAPI api;
|
2018-05-18 22:39:03 -07:00
|
|
|
private static HashMap<String,Integer> coinIDs = new HashMap<String, Integer>();
|
2018-04-24 08:22:12 -07:00
|
|
|
private String preferredFiatCurrency = Currencies.USD;
|
2018-03-23 03:45:07 -07:00
|
|
|
|
|
|
|
public CoinmarketcapRateSource(String preferedFiatCurrency) {
|
|
|
|
this();
|
2018-04-24 08:22:12 -07:00
|
|
|
if (Currencies.EUR.equalsIgnoreCase(preferedFiatCurrency)) {
|
|
|
|
this.preferredFiatCurrency = Currencies.EUR;
|
2018-03-23 03:45:07 -07:00
|
|
|
}
|
2018-04-24 08:22:12 -07:00
|
|
|
if (Currencies.USD.equalsIgnoreCase(preferedFiatCurrency)) {
|
|
|
|
this.preferredFiatCurrency = Currencies.USD;
|
2018-03-23 03:45:07 -07:00
|
|
|
}
|
2018-05-18 22:22:28 -07:00
|
|
|
if (Currencies.CAD.equalsIgnoreCase(preferedFiatCurrency)) {
|
|
|
|
this.preferredFiatCurrency = Currencies.CAD;
|
|
|
|
}
|
2018-05-18 22:41:10 -07:00
|
|
|
coinIDs.put(Currencies.BTC, 1);
|
|
|
|
coinIDs.put(Currencies.SYS, 541);
|
|
|
|
coinIDs.put(Currencies.BCH, 1831);
|
|
|
|
coinIDs.put(Currencies.BTX, 1654);
|
|
|
|
coinIDs.put(Currencies.LTC, 2);
|
|
|
|
coinIDs.put(Currencies.ETH, 1027);
|
|
|
|
coinIDs.put(Currencies.DASH, 131);
|
|
|
|
coinIDs.put(Currencies.XMR, 328);
|
|
|
|
coinIDs.put(Currencies.POT, 122);
|
|
|
|
coinIDs.put(Currencies.FLASH, 1755);
|
2018-03-23 03:45:07 -07:00
|
|
|
}
|
|
|
|
|
2017-11-29 09:51:45 -08:00
|
|
|
public CoinmarketcapRateSource() {
|
|
|
|
api = RestProxyFactory.createProxy(ICoinmarketcapAPI.class, "https://api.coinmarketcap.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<String> getCryptoCurrencies() {
|
|
|
|
Set<String> result = new HashSet<String>();
|
2018-04-24 08:22:12 -07:00
|
|
|
result.add(Currencies.BTC);
|
2018-05-18 22:22:28 -07:00
|
|
|
result.add(Currencies.SYS);
|
2018-04-24 08:22:12 -07:00
|
|
|
result.add(Currencies.BCH);
|
|
|
|
result.add(Currencies.BTX);
|
|
|
|
result.add(Currencies.LTC);
|
|
|
|
result.add(Currencies.ETH);
|
|
|
|
result.add(Currencies.DASH);
|
|
|
|
result.add(Currencies.XMR);
|
|
|
|
result.add(Currencies.POT);
|
|
|
|
result.add(Currencies.FLASH);
|
2018-04-16 13:19:05 -07:00
|
|
|
|
2017-11-29 09:51:45 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<String> getFiatCurrencies() {
|
|
|
|
Set<String> result = new HashSet<String>();
|
2018-04-24 08:22:12 -07:00
|
|
|
result.add(Currencies.USD);
|
2018-05-18 22:22:28 -07:00
|
|
|
result.add(Currencies.CAD);
|
2018-04-24 08:22:12 -07:00
|
|
|
result.add(Currencies.EUR);
|
2017-11-29 09:51:45 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPreferredFiatCurrency() {
|
2018-04-24 08:22:12 -07:00
|
|
|
return Currencies.USD;
|
2017-11-29 09:51:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
|
|
|
|
if (!getFiatCurrencies().contains(fiatCurrency)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-18 22:41:10 -07:00
|
|
|
Integer cryptoId = coinIDs.get(cryptoCurrency);
|
|
|
|
if(cryptoId == null){
|
2018-05-18 22:22:28 -07:00
|
|
|
return null;
|
2018-05-18 22:41:10 -07:00
|
|
|
}
|
2018-04-16 13:19:05 -07:00
|
|
|
|
2018-06-03 16:56:41 -07:00
|
|
|
Map<String, String> ticker = api.getTickers(cryptoId, fiatCurrency);
|
|
|
|
JSONObject json = new JSONObject(ticker);
|
|
|
|
System.out.printf( "JSON: %s", json.toString() );
|
2018-06-03 11:31:55 -07:00
|
|
|
/*if (cryptoCurrency.equalsIgnoreCase(ticker.getSymbol())) {
|
|
|
|
if (Currencies.EUR.equalsIgnoreCase(fiatCurrency)) {
|
|
|
|
return ticker.getPrice_eur();
|
|
|
|
}else if (Currencies.CAD.equalsIgnoreCase(fiatCurrency)) {
|
|
|
|
return ticker.getPrice_cad();
|
2017-11-29 09:51:45 -08:00
|
|
|
}
|
2018-06-03 11:31:55 -07:00
|
|
|
else{
|
|
|
|
return ticker.getPrice_usd();
|
|
|
|
}
|
|
|
|
}*/
|
2018-06-03 16:56:41 -07:00
|
|
|
return 0;
|
2017-11-29 09:51:45 -08:00
|
|
|
}
|
|
|
|
}
|