From 845ebdba1d10ebd74d287a0245ef748d5df69ceb Mon Sep 17 00:00:00 2001 From: xdustinface Date: Sun, 29 Apr 2018 11:27:48 +0200 Subject: [PATCH] Created SmartCash API rate source extension. --- server_extensions_extra/build.gradle | 4 +- .../extra/smartcash/SmartcashExtension.java | 7 + .../sources/smartcash/APIResponse.java | 67 +++++++++ .../sources/smartcash/ISmartCashAPI.java | 41 ++++++ .../smartcash/SmartCashRateSource.java | 131 ++++++++++++++++++ .../src/main/resources/batm-extensions.xml | 6 +- 6 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/APIResponse.java create mode 100644 server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/ISmartCashAPI.java create mode 100644 server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/SmartCashRateSource.java diff --git a/server_extensions_extra/build.gradle b/server_extensions_extra/build.gradle index 7da0690..081ed6e 100644 --- a/server_extensions_extra/build.gradle +++ b/server_extensions_extra/build.gradle @@ -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 } } -} \ No newline at end of file +} diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/SmartcashExtension.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/SmartcashExtension.java index c80590b..5fc47dc 100644 --- a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/SmartcashExtension.java +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/SmartcashExtension.java @@ -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); } } diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/APIResponse.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/APIResponse.java new file mode 100644 index 0000000..c7df692 --- /dev/null +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/APIResponse.java @@ -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; + } +} diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/ISmartCashAPI.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/ISmartCashAPI.java new file mode 100644 index 0000000..4d2a2a9 --- /dev/null +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/ISmartCashAPI.java @@ -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(); +} diff --git a/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/SmartCashRateSource.java b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/SmartCashRateSource.java new file mode 100644 index 0000000..dde433f --- /dev/null +++ b/server_extensions_extra/src/main/java/com/generalbytes/batm/server/extensions/extra/smartcash/sources/smartcash/SmartCashRateSource.java @@ -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 rateAmounts = new HashMap(); + private static HashMap rateTimes = new HashMap(); + 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 getFiatCurrencies() { + Set fiatCurrencies = new HashSet(); + fiatCurrencies.add(Currencies.USD); + fiatCurrencies.add(Currencies.EUR); + fiatCurrencies.add(Currencies.CHF); + return fiatCurrencies; + } + + @Override + public String getPreferredFiatCurrency() { + return this.preferedFiatCurrency; + } + + @Override + public Set getCryptoCurrencies() { + Set result = new HashSet(); + 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; + } + +} diff --git a/server_extensions_extra/src/main/resources/batm-extensions.xml b/server_extensions_extra/src/main/resources/batm-extensions.xml index 7146710..964ee56 100644 --- a/server_extensions_extra/src/main/resources/batm-extensions.xml +++ b/server_extensions_extra/src/main/resources/batm-extensions.xml @@ -327,6 +327,10 @@ SMART + + + SMART + @@ -432,6 +436,6 @@ BTX - +