Created SmartCash API rate source extension.

This commit is contained in:
xdustinface 2018-04-29 11:27:48 +02:00
parent 8350ec5ee5
commit 845ebdba1d
6 changed files with 254 additions and 2 deletions

View File

@ -29,6 +29,8 @@ dependencies {
compile(group: 'com.github.mmazi', name: 'rescu', version: '1.9.1')
compile(group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0.1')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.0')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.2')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.2')
compile(group: 'org.knowm.xchange', name: 'xchange-core', version: '4.2.1')
compile(group: 'com.google.guava', name: 'guava', version: '18.0')
compile(group: 'com.google.zxing', name: 'core', version: '2.3.0')
@ -65,4 +67,4 @@ publishing {
from components.java
}
}
}
}

View File

@ -19,6 +19,7 @@ package com.generalbytes.batm.server.extensions.extra.smartcash;
import com.generalbytes.batm.server.extensions.*;
import com.generalbytes.batm.server.extensions.extra.smartcash.sources.FixPriceRateSource;
import com.generalbytes.batm.server.extensions.extra.smartcash.sources.smartcash.SmartCashRateSource;
import com.generalbytes.batm.server.extensions.extra.smartcash.wallets.smartcashd.SmartcashRPCWallet;
import com.generalbytes.batm.server.extensions.watchlist.IWatchList;
@ -99,6 +100,12 @@ public class SmartcashExtension implements IExtension{
preferedFiatCurrency = st.nextToken().toUpperCase();
}
return new FixPriceRateSource(rate,preferedFiatCurrency);
}else if ("smartapi".equalsIgnoreCase(exchangeType)) {
String preferredFiatCurrency = Currencies.USD;
if (st.hasMoreTokens()) {
preferredFiatCurrency = st.nextToken();
}
return new SmartCashRateSource(preferredFiatCurrency);
}
}

View File

@ -0,0 +1,67 @@
/* ##
# Part of the SmartCash API price extension
#
# Copyright 2018 dustinface
# Created 29.04.2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## */
package com.generalbytes.batm.server.extensions.extra.smartcash.sources.smartcash;
import java.math.BigDecimal;
public class APIResponse {
public BigDecimal count;
public Item[] items;
public Last last;
public String resource;
public BigDecimal status;
public BigDecimal execution;
public static class Item {
public String updated;
public Currencies currencies;
}
public static class Currencies {
public BigDecimal USD;
public BigDecimal EUR;
public BigDecimal CHF;
}
public class Last {
public String id;
public String created;
}
public BigDecimal getPrice(String fiatCurrency) {
if (fiatCurrency.equalsIgnoreCase("USD")) {
return this.items[0].currencies.USD;
}else if (fiatCurrency.equalsIgnoreCase("EUR")) {
return this.items[0].currencies.EUR;
}else if (fiatCurrency.equalsIgnoreCase("CHF")) {
return this.items[0].currencies.CHF;
}
return null;
}
}

View File

@ -0,0 +1,41 @@
/* ##
# Part of the SmartCash API price extension
#
# Copyright 2018 dustinface
# Created 29.04.2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## */
package com.generalbytes.batm.server.extensions.extra.smartcash.sources.smartcash;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/v1/")
@Produces(MediaType.APPLICATION_JSON)
public interface ISmartCashAPI {
@GET
@Path("exchange/currencies?limit=2")
public APIResponse returnResponse();
}

View File

@ -0,0 +1,131 @@
/* ##
# Part of the SmartCash API price extension
#
# Copyright 2018 dustinface
# Created 29.04.2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## */
package com.generalbytes.batm.server.extensions.extra.smartcash.sources.smartcash;
import com.generalbytes.batm.server.extensions.Currencies;
import com.generalbytes.batm.server.extensions.Currencies;
import com.generalbytes.batm.server.extensions.IRateSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import si.mazi.rescu.ClientConfig;
import si.mazi.rescu.RestProxyFactory;
import si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory;
import si.mazi.rescu.serialization.jackson.JacksonObjectMapperFactory;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class SmartCashRateSource implements IRateSource{
private static final Logger log = LoggerFactory.getLogger(SmartCashRateSource.class);
private String preferedFiatCurrency;
private ISmartCashAPI api;
private static HashMap<String,BigDecimal> rateAmounts = new HashMap<String, BigDecimal>();
private static HashMap<String,Long> rateTimes = new HashMap<String, Long>();
private static final long MAXIMUM_ALLOWED_TIME_OFFSET = 30 * 1000; //30sec
public SmartCashRateSource(String preferedFiatCurrency) {
if (preferedFiatCurrency == null) {
preferedFiatCurrency = Currencies.USD;
}
this.preferedFiatCurrency = preferedFiatCurrency;
api = RestProxyFactory.createProxy(ISmartCashAPI.class, "https://api.smartcash.cc");
}
@Override
public Set<String> getFiatCurrencies() {
Set<String> fiatCurrencies = new HashSet<String>();
fiatCurrencies.add(Currencies.USD);
fiatCurrencies.add(Currencies.EUR);
fiatCurrencies.add(Currencies.CHF);
return fiatCurrencies;
}
@Override
public String getPreferredFiatCurrency() {
return this.preferedFiatCurrency;
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(Currencies.SMART);
return result;
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
String key = cryptoCurrency +"_" + fiatCurrency;
synchronized (rateAmounts) {
long now = System.currentTimeMillis();
BigDecimal amount = rateAmounts.get(key);
if (amount == null) {
BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency);
log.debug("Called smartcash exchange for rate: " + key + " = " + result);
rateAmounts.put(key,result);
rateTimes.put(key,now+MAXIMUM_ALLOWED_TIME_OFFSET);
return result;
}else {
Long expirationTime = rateTimes.get(key);
if (expirationTime > now) {
return rateAmounts.get(key);
}else{
//do the job;
BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency);
log.debug("Called smartcash exchange for rate: " + key + " = " + result);
rateAmounts.put(key,result);
rateTimes.put(key,now+MAXIMUM_ALLOWED_TIME_OFFSET);
return result;
}
}
}
}
private BigDecimal getExchangeRateLastSync(String cryptoCurrency, String fiatCurrency) {
if (!Currencies.SMART.equalsIgnoreCase(cryptoCurrency)) {
return null; //unsupported currency
}
APIResponse response = api.returnResponse();
if (response != null) {
return response.getPrice(fiatCurrency);
}
return null;
}
}

View File

@ -327,6 +327,10 @@
<param name="price" />
<cryptocurrency>SMART</cryptocurrency>
</ratesource>
<ratesource prefix="smartapi" name ="SmartCash API" >
<param name="price" />
<cryptocurrency>SMART</cryptocurrency>
</ratesource>
<cryptologo cryptocurrency="SMART" file="smart.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.startcoin.StartcoinExtension" >
@ -432,6 +436,6 @@
<cryptocurrency>BTX</cryptocurrency>
</ratesource>
<cryptologo cryptocurrency="BTX" file="btx.png"/>
</extension>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.watchlists.BasicWatchlistsExtension" />
</extensions>