Skip to content

Commit

Permalink
support returnOrderStatus/withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
cody824 authored and guodong committed Nov 5, 2018
1 parent b75b131 commit a44ac81
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 32 deletions.
15 changes: 6 additions & 9 deletions src/main/java/com/cf/ExchangeService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.cf;

import com.cf.data.model.poloniex.PoloniexActiveLoanTypes;
import com.cf.data.model.poloniex.PoloniexChartData;
import com.cf.data.model.poloniex.PoloniexCompleteBalance;
import com.cf.data.model.poloniex.PoloniexFeeInfo;
import com.cf.data.model.poloniex.PoloniexOpenOrder;
import com.cf.data.model.poloniex.PoloniexOrderResult;
import com.cf.data.model.poloniex.PoloniexOrderTrade;
import com.cf.data.model.poloniex.PoloniexTicker;
import com.cf.data.model.poloniex.PoloniexTradeHistory;
import com.cf.data.model.poloniex.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -60,4 +53,8 @@ public interface ExchangeService {

public PoloniexActiveLoanTypes returnActiveLoans();

public PoloniexOrderStatus returnOrderStatus(String orderNumber);

public PoloniexWithdrawResult withdraw(String currency, BigDecimal amount, String address, String paymentId);

}
4 changes: 4 additions & 0 deletions src/main/java/com/cf/TradingAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface TradingAPIClient

public String returnOrderTrades(String orderNumber);

public String returnOrderStatus(String orderNumber);

public String cancelOrder(String orderNumber);

public String moveOrder(String orderNumber, BigDecimal rate);
Expand All @@ -28,6 +30,8 @@ public interface TradingAPIClient

public String buy(String currencyPair, BigDecimal buyPrice, BigDecimal amount, boolean fillOrKill, boolean immediateOrCancel, boolean postOnly);

public String withdraw(String currency, BigDecimal amount, String address, String paymentId);

// Lending APIs
public String returnActiveLoans();

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/cf/client/poloniex/PoloniexExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,59 @@ public PoloniexActiveLoanTypes returnActiveLoans() {
return activeLoanTypes;
}

/**
* Returns order status for a given orderNumber
*
* @param orderNumber
* @return PoloniexOrderStatus
*/
@Override
public PoloniexOrderStatus returnOrderStatus(String orderNumber) {
long start = System.currentTimeMillis();
PoloniexOrderStatus orderStatus = null;
PoloniexOrderStatusCheck orderStatusCheck;
try {
String orderStatusStr = tradingClient.returnOrderStatus(orderNumber);
orderStatusCheck = mapper.mapOrderStatusCheck(orderStatusStr);
if (orderStatusCheck.success == 1) {
orderStatus = mapper.mapOrderStatus(orderStatusStr);
} else {
PoloniexOrderStatusError error = mapper.mapOrderStatusError(orderStatusStr);
orderStatus = new PoloniexOrderStatus(0, error.result.get("error"), null);
}
LOG.trace("Retrieved and mapped {} {} order status in {} ms", orderStatusStr, orderNumber, System.currentTimeMillis() - start);
return orderStatus;
} catch (Exception ex) {
LOG.error("Error retrieving order status for {} - {}", orderNumber, ex.getMessage());
}

return orderStatus;
}

/**
* Places a withdraw order
*
* @param currency Examples: USDT ETH
* @param amount the amount to withdraw
* @param address the address of currency
* @param paymentId For XMR withdrawals, you may optionally specify "paymentId".
* @return PoloniexWithdrawResult
*/
@Override
public PoloniexWithdrawResult withdraw(String currency, BigDecimal amount, String address, String paymentId) {
long start = System.currentTimeMillis();
PoloniexWithdrawResult withdrawResult = null;
try {
String withdrawResultStr = tradingClient.withdraw(currency, amount, address, paymentId);
withdrawResult = mapper.mapWithdrawResult(withdrawResultStr);
LOG.trace("Retrieved and mapped {} {} withdraw in {} ms", withdrawResultStr, currency, System.currentTimeMillis() - start);
return withdrawResult;
} catch (Exception ex) {
LOG.error("Error retrieving withdraw for {} - {}", currency, ex.getMessage());
}
return withdrawResult;
}

/**
* *
* Returns your open orders for a given currency pair
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/cf/client/poloniex/PoloniexTradingAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public String returnOrderTrades(String orderNumber) {
return returnTradingAPICommandResults("returnOrderTrades", additionalPostParams);
}

@Override
public String returnOrderStatus(String orderNumber) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
additionalPostParams.add(new BasicNameValuePair("orderNumber", orderNumber));
return returnTradingAPICommandResults("returnOrderStatus", additionalPostParams);
}

@Override
public String cancelOrder(String orderNumber) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
Expand Down Expand Up @@ -116,6 +123,18 @@ public String buy(String currencyPair, BigDecimal buyPrice, BigDecimal amount, b
return trade("buy", currencyPair, buyPrice, amount, fillOrKill, immediateOrCancel, postOnly);
}

@Override
public String withdraw(String currency, BigDecimal amount, String address, String paymentId) {
List<NameValuePair> additionalPostParams = new ArrayList<>();
additionalPostParams.add(new BasicNameValuePair("currency", currency));
additionalPostParams.add(new BasicNameValuePair("amount", amount.toPlainString()));
additionalPostParams.add(new BasicNameValuePair("address", address));
if (paymentId != null) {
additionalPostParams.add(new BasicNameValuePair("paymentId", paymentId));
}
return returnTradingAPICommandResults("withdraw", additionalPostParams);
}

// Lending APIs
@Override
public String returnActiveLoans() {
Expand Down
48 changes: 25 additions & 23 deletions src/main/java/com/cf/data/map/poloniex/PoloniexDataMapper.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
package com.cf.data.map.poloniex;

import com.cf.data.model.poloniex.PoloniexActiveLoanTypes;
import com.cf.data.model.poloniex.PoloniexChartData;
import com.cf.data.model.poloniex.PoloniexCompleteBalance;
import com.cf.data.model.poloniex.PoloniexFeeInfo;
import com.cf.data.model.poloniex.PoloniexLendingHistory;
import com.cf.data.model.poloniex.PoloniexLendingResult;
import com.cf.data.model.poloniex.PoloniexLoanOffer;
import com.cf.data.model.poloniex.PoloniexOpenOrder;
import com.cf.data.model.poloniex.PoloniexOrderResult;
import com.cf.data.model.poloniex.PoloniexOrderTrade;
import com.cf.data.model.poloniex.PoloniexTicker;
import com.cf.data.model.poloniex.PoloniexTradeHistory;
import com.cf.data.model.poloniex.*;
import com.cf.data.model.poloniex.deserialize.PoloniexChartDataDeserializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.time.ZoneOffset;
Expand All @@ -31,9 +16,6 @@
import java.util.*;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
*
* @author David
Expand Down Expand Up @@ -175,4 +157,24 @@ public PoloniexLendingResult mapLendingResult(String result) {
return plr;
}

public PoloniexOrderStatus mapOrderStatus(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatus>() {
}.getType());
}

public PoloniexOrderStatusCheck mapOrderStatusCheck(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatusCheck>() {
}.getType());
}

public PoloniexOrderStatusError mapOrderStatusError(String result) {
return gson.fromJson(result, new TypeToken<PoloniexOrderStatusError>() {
}.getType());
}

public PoloniexWithdrawResult mapWithdrawResult(String result) {
return gson.fromJson(result, new TypeToken<PoloniexWithdrawResult>() {
}.getType());
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/cf/data/model/poloniex/PoloniexOrderStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cf.data.model.poloniex;

import com.google.gson.Gson;

import java.util.Map;

/**
* @author guodong
*/
public class PoloniexOrderStatus {
public final Integer success;
public final String error;
public final Map<String, PoloniexOrderStatusDetail> result;

public PoloniexOrderStatus(Integer success, String error, Map<String, PoloniexOrderStatusDetail> result) {
this.success = success;
this.error = error;
this.result = result;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cf.data.model.poloniex;

import com.google.gson.Gson;

/**
* @author guodong
*/
public class PoloniexOrderStatusCheck {
public final Integer success;
public final Object result;

public PoloniexOrderStatusCheck(Integer success, Object result) {
this.success = success;
this.result = result;
}


@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cf.data.model.poloniex;

import com.google.gson.Gson;

/**
* @author guodong
*/
public class PoloniexOrderStatusDetail {
public final String status;
public final String rate;
public final String amount;
public final String currencyPair;
public final String date;
public final String total;
public final String type;
public final String startingAmount;

public PoloniexOrderStatusDetail(String status, String rate, String amount, String currencyPair, String date, String total, String type, String startingAmount) {
this.status = status;
this.rate = rate;
this.amount = amount;
this.currencyPair = currencyPair;
this.date = date;
this.total = total;
this.type = type;
this.startingAmount = startingAmount;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cf.data.model.poloniex;

import com.google.gson.Gson;

import java.util.Map;

/**
* @author guodong
*/
public class PoloniexOrderStatusError {
public final Integer success;
public final Map<String, String> result;

public PoloniexOrderStatusError(Integer success, Map<String, String> result) {
this.success = success;
this.result = result;
}


@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cf.data.model.poloniex;

import com.google.gson.Gson;

/**
* @author guodong
*/
public class PoloniexWithdrawResult {
public final String response;
public final String error;

public PoloniexWithdrawResult(String response, String error) {
this.response = response;
this.error = error;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}

0 comments on commit a44ac81

Please sign in to comment.