Merge remote-tracking branch 'origin/master' into BATM-410

This commit is contained in:
Radovan Panak 2018-02-02 11:32:18 +01:00
commit d8d9908ca2
10 changed files with 594 additions and 0 deletions

View File

@ -23,6 +23,7 @@ public interface ICurrencies {
public static final String BCH = "BCH"; //Bitcoin Cash
public static final String ETH = "ETH";
public static final String LTC = "LTC";
public static final String VIA = "VIA";
public static final String DEX = "DEX";
public static final String DASH = "DASH";
public static final String DOGE = "DOGE";

View File

@ -0,0 +1,52 @@
/*************************************************************************************
* Copyright (C) 2014-2017 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.viacoin;
import com.generalbytes.batm.server.coinutil.AddressFormatException;
import com.generalbytes.batm.server.coinutil.Base58;
import com.generalbytes.batm.server.extensions.ExtensionsUtil;
import com.generalbytes.batm.server.extensions.ICryptoAddressValidator;
public class ViacoinAddressValidator implements ICryptoAddressValidator {
@Override
public boolean isAddressValid(String address) {
if (address.startsWith("V") || address.startsWith("E")) {
try {
Base58.decodeToBigInteger(address);
Base58.decodeChecked(address);
} catch (AddressFormatException e) {
e.printStackTrace();
return false;
}
return true;
}
return false;
}
@Override
public boolean isPaperWalletSupported() {
return false;
}
@Override
public boolean mustBeBase58Address() {
return true;
}
}

View File

@ -0,0 +1,135 @@
/*************************************************************************************
* Copyright (C) 2014-2017 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.viacoin;
import com.generalbytes.batm.server.extensions.*;
import com.generalbytes.batm.server.extensions.extra.viacoin.sources.FixPriceRateSource;
import com.generalbytes.batm.server.extensions.extra.viacoin.sources.poloniex.PoloniexRateSource;
import com.generalbytes.batm.server.extensions.extra.viacoin.wallets.viacoind.ViacoindRPCWallet;
import com.generalbytes.batm.server.extensions.watchlist.IWatchList;
import java.math.BigDecimal;
import java.util.*;
public class ViacoinExtension implements IExtension{
@Override
public String getName(){
return "BATM Viacoin extension";
}
@Override
public IExchange createExchange(String exchangeLogin){
return null;
}
@Override
public IWallet createWallet(String walletLogin){
if(walletLogin != null && !walletLogin.trim().isEmpty()){
StringTokenizer st = new StringTokenizer(walletLogin,":");
String walletType = st.nextToken();
if("viacoind".equalsIgnoreCase(walletType)){
//"viacoind::protocol:user:password:ip:port:accountname"
String protocol = st.nextToken();
String username = st.nextToken();
String password = st.nextToken();
String hostname = st.nextToken();
String port = st.nextToken();
String accountname = "";
if(st.hasMoreTokens()){
accountname = st.nextToken();
}
if (protocol != null && username != null && password != null && hostname !=null && port != null && accountname != null) {
String rpcURL = protocol +"://" + username +":" + password + "@" + hostname +":" + port;
return new ViacoindRPCWallet(rpcURL,accountname);
}
}
}
return null;
}
@Override
public ICryptoAddressValidator createAddressValidator(String cryptoCurrency){
if(ICurrencies.VIA.equalsIgnoreCase(cryptoCurrency)){
return new ViacoinAddressValidator();
}
return null;
}
@Override
public IPaperWalletGenerator createPaperWalletGenerator(String cryptoCurrency) {
return null;
}
@Override
public IPaymentProcessor createPaymentProcessor(String paymentProcessorLogin){
return null;
}
@Override
public IRateSource createRateSource(String sourceLogin) {
if (sourceLogin != null && !sourceLogin.trim().isEmpty()) {
StringTokenizer st = new StringTokenizer(sourceLogin,":");
String rsType = st.nextToken();
if ("viafix".equalsIgnoreCase(rsType)) {
BigDecimal rate = BigDecimal.ZERO;
if (st.hasMoreTokens()) {
try {
rate = new BigDecimal(st.nextToken());
} catch (Throwable e) {
}
}
String preferedFiatCurrency = ICurrencies.USD;
if (st.hasMoreTokens()) {
preferedFiatCurrency = st.nextToken().toUpperCase();
}
return new FixPriceRateSource(rate,preferedFiatCurrency);
}else if ("poloniexrs".equalsIgnoreCase(rsType)) {
String preferredFiatCurrency = ICurrencies.USD;
if (st.hasMoreTokens()) {
preferredFiatCurrency = st.nextToken();
}
return new PoloniexRateSource(preferredFiatCurrency);
}
}
return null;
}
@Override
public Set<String> getSupportedCryptoCurrencies(){
Set<String> result = new HashSet<String>();
result.add(ICurrencies.VIA);
return result;
}
@Override
public Set<String> getSupportedWatchListsNames(){
return null;
}
@Override
public IWatchList getWatchList(String name){
return null;
}
}

View File

@ -0,0 +1,56 @@
package com.generalbytes.batm.server.extensions.extra.viacoin.sources;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IRateSource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
/**
* Classes implementing this interface are used by the server to obtain price of specified cryptocurrency in fiat currency
*/
public class FixPriceRateSource implements IRateSource {
private BigDecimal rate = BigDecimal.ZERO;
private String preferedFiatCurrency = ICurrencies.USD;
public FixPriceRateSource(BigDecimal rate, String preferedFiatCurrency)
{
this.rate = rate;
if(ICurrencies.EUR.equalsIgnoreCase(preferedFiatCurrency)){
this.preferedFiatCurrency = ICurrencies.EUR;
}
if(ICurrencies.USD.equalsIgnoreCase(preferedFiatCurrency)){
this.preferedFiatCurrency = ICurrencies.USD;
}
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.VIA);
return result;
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency){
if(ICurrencies.VIA.equalsIgnoreCase(cryptoCurrency)){
return rate;
}
return null;
}
@Override
public Set<String> getFiatCurrencies(){
Set<String> result = new HashSet<String>();
result.add(ICurrencies.USD);
result.add(ICurrencies.EUR);
return result;
}
@Override
public String getPreferredFiatCurrency(){
return preferedFiatCurrency;
}
}

View File

@ -0,0 +1,33 @@
/*************************************************************************************
* Copyright (C) 2015-2016 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.viacoin.sources.poloniex;
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("/")
@Produces(MediaType.APPLICATION_JSON)
public interface IPoloniexAPI {
@GET
@Path("/public")
public OrderBookResponse returnOrderBook(@QueryParam("command") String command, @QueryParam("currencyPair") String currencyPair, @QueryParam("depth") long depth);
}

View File

@ -0,0 +1,37 @@
package com.generalbytes.batm.server.extensions.extra.viacoin.sources.poloniex;
import java.math.BigDecimal;
/**
* Created by b00lean on 3/8/15.
*/
public class OrderBookResponse {
private BigDecimal[][] asks;
private BigDecimal[][] bids;
private String isFrozen;
public BigDecimal[][] getAsks() {
return asks;
}
public void setAsks(BigDecimal[][] asks) {
this.asks = asks;
}
public BigDecimal[][] getBids() {
return bids;
}
public void setBids(BigDecimal[][] bids) {
this.bids = bids;
}
public String getIsFrozen() {
return isFrozen;
}
public void setIsFrozen(String isFrozen) {
this.isFrozen = isFrozen;
}
}

View File

@ -0,0 +1,140 @@
/*************************************************************************************
* Copyright (C) 2015-2017 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.viacoin.sources.poloniex;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IRateSource;
import com.generalbytes.batm.server.extensions.extra.bitcoin.exchanges.bitfinex.BitfinexExchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import si.mazi.rescu.RestProxyFactory;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class PoloniexRateSource implements IRateSource{
private static final Logger log = LoggerFactory.getLogger(PoloniexRateSource.class);
private BitfinexExchange btcRs;
private String preferedFiatCurrency;
private IPoloniexAPI 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 PoloniexRateSource(String preferedFiatCurrency) {
if (preferedFiatCurrency == null) {
preferedFiatCurrency = ICurrencies.USD;
}
this.preferedFiatCurrency = preferedFiatCurrency;
btcRs = new BitfinexExchange("***","***");
api = RestProxyFactory.createProxy(IPoloniexAPI.class, "https://poloniex.com");
}
@Override
public Set<String> getFiatCurrencies() {
return btcRs.getFiatCurrencies();
}
@Override
public String getPreferredFiatCurrency() {
return btcRs.getPreferredFiatCurrency();
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.VIA);
return result;
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
if (!ICurrencies.VIA.equalsIgnoreCase(cryptoCurrency)) {
return null;
}
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 bitcoinaverage 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 bitcoinaverage 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 (!ICurrencies.VIA.equalsIgnoreCase(cryptoCurrency)) {
return null; //unsupported currency
}
OrderBookResponse orderBookResponse = api.returnOrderBook("returnOrderBook", "BTC_VIA", 10000);
if (orderBookResponse != null) {
BigDecimal[][] asks = orderBookResponse.getAsks();
BigDecimal asksTotal = BigDecimal.ZERO;
BigDecimal targetAmount = new BigDecimal(100000); //calculate price based on this amount of VIA
BigDecimal tradableLimit = BigDecimal.ZERO;
for (int i = 0; i < asks.length; i++) {
BigDecimal[] ask = asks[i];
//log.debug("ask = " + ask);
asksTotal = asksTotal.add(ask[1]);
if (targetAmount.compareTo(asksTotal) <= 0) {
tradableLimit = ask[0];
break;
}
}
//System.out.println("tradableLimit = " + tradableLimit);;
if (tradableLimit != null) {
BigDecimal btcRate = btcRs.getExchangeRateLast(ICurrencies.BTC, fiatCurrency);
if (btcRate != null) {
return btcRate.multiply(tradableLimit);
}
}
return null;
}
return null;
}
public static void main(String[] args) {
PoloniexRateSource rs = new PoloniexRateSource(ICurrencies.USD);
System.out.println("rs = " + rs.getExchangeRateLast(ICurrencies.VIA,ICurrencies.USD));
}
}

View File

@ -0,0 +1,120 @@
/*************************************************************************************
* Copyright (C) 2014-2017 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.viacoin.wallets.viacoind;
import com.azazar.bitcoin.jsonrpcclient.BitcoinException;
import com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ViacoindRPCWallet implements IWallet{
private static final Logger log = LoggerFactory.getLogger(ViacoindRPCWallet.class);
private static final String CRYPTO_CURRENCY = ICurrencies.VIA;
public ViacoindRPCWallet(String rpcURL, String accountName){
this.rpcURL = rpcURL;
this.accountName = accountName;
}
private String rpcURL;
private String accountName;
@Override
public Set<String> getCryptoCurrencies(){
Set<String> result = new HashSet<String>();
result.add(CRYPTO_CURRENCY);
return result;
}
@Override
public String getPreferredCryptoCurrency(){
return CRYPTO_CURRENCY;
}
@Override
public String sendCoins(String destinationAddress, BigDecimal amount, String cryptoCurrency, String description){
if(!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)){
log.error("Viacoind wallet error: unknown cryptocurrency");
return null;
}
log.info("Viacoind sending coins from " + accountName + " to: " + destinationAddress + " " + amount);
try {
String result = getClient(rpcURL).sendFrom(accountName, destinationAddress, amount.doubleValue());
log.debug("result=" + result);
return result;
}catch(BitcoinException e){
e.printStackTrace();
return null;
}
}
@Override
public String getCryptoAddress(String cryptoCurrency) {
if (!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)) {
log.error("Viacoind wallet error: unknown cryptocurrency.");
return null;
}
try {
List<String> addressesByAccount = getClient(rpcURL).getAddressesByAccount(accountName);
if (addressesByAccount == null || addressesByAccount.size() == 0) {
return null;
}else{
return addressesByAccount.get(0);
}
} catch (BitcoinException e) {
e.printStackTrace();
return null;
}
}
@Override
public BigDecimal getCryptoBalance(String cryptoCurrency){
if(!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)){
log.error("Viacoind wallet error: unknown cryptocurrency" + cryptoCurrency);
return null;
}
try{
double balance = getClient(rpcURL).getBalance(accountName);
return BigDecimal.valueOf(balance);
}catch(BitcoinException e){
e.printStackTrace();
return null;
}
}
private BitcoinJSONRPCClient getClient(String rpcURL){
try{
return new BitcoinJSONRPCClient(rpcURL);
}catch(MalformedURLException e){
e.printStackTrace();
}
return null;
}
}

View File

@ -137,6 +137,26 @@
</ratesource>
<cryptologo cryptocurrency="LTC" file="ltc.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.viacoin.ViacoinExtension" >
<wallet prefix="viacoind" name="Viacoind">
<param name="protocol" />
<param name="user" />
<param name="password" />
<param name="host" />
<param name="port" />
<param name="accountname" />
<cryptocurrency>VIA</cryptocurrency>
</wallet>
<ratesource prefix="viafix" name ="Fix Price" >
<param name="price" />
<cryptocurrency>VIA</cryptocurrency>
</ratesource>
<ratesource prefix="poloniexrs" name ="Poloniex.com vs bitcoinaverage.com" >
<param name="fiatcurrency" />
<cryptocurrency>VIA</cryptocurrency>
</ratesource>
<cryptologo cryptocurrency="VIA" file="via.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.worldcoin.WorldcoinExtension" >
<wallet prefix="worldcoind" name="Worldcoind">
<param name="protocol" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB