Viacoin: Poloniex orders

This commit is contained in:
romanornr 2017-11-08 15:59:46 +01:00
parent 96277f8038
commit 7ffd8f6ff8
No known key found for this signature in database
GPG Key ID: 3F92368F0D21A206
5 changed files with 259 additions and 2 deletions

View File

@ -19,7 +19,7 @@ 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.btce.BTCeRateSource;
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;
@ -83,9 +83,36 @@ public class ViacoinExtentsion implements IExtension{
public IPaymentProcessors createPaymentProcessor(String paymentProcessorLogin){
return null;
}
@Override
public IRateSource createRateSource(String sourceLogin) {
// to do
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

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.via.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.via.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.via.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

@ -124,6 +124,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" />