Skip to content

Commit

Permalink
Merge pull request #57 from charvam/ET-767-Poloniex_fix_fee
Browse files Browse the repository at this point in the history
ET-767 Fixed Poloniex CSV parser BUY transaction fee cacluclation.
  • Loading branch information
generalbytes authored Jul 14, 2021
2 parents 0ae050d + fef012b commit 1d1ea08
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projectVersion=3.5.0
projectVersion=3.5.1
pf4jVersion=3.4.0
xchangeVersion=5.0.4
requiredEverytradeVersion=>=20210427
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class PoloniexBeanV2 extends ExchangeBean {
public static final String UNSUPPORTED_CATEGORY = "Unsupported category ";
public static final String EXCHANGE_CATEGORY = "Exchange";
public static final String UNSUPPORTED_FEE_CURRENCY = "Unsupported fee currency ";
private Instant date;
private Currency marketBase;
private Currency marketQuote;
Expand Down Expand Up @@ -94,20 +95,37 @@ public TransactionCluster toTransactionCluster() {
validateCurrencyPair(marketBase, marketQuote);
validatePositivity(amount,total);

final BigDecimal feeValue;
if (feeCurrency.equals(marketBase)) {
feeValue = amount.subtract(quoteTotalLessFee.abs());
} else if (feeCurrency.equals(marketQuote)) {
feeValue = total.subtract(baseTotalLessFee.abs());
} else {
throw new DataValidationException(String.format(
"Fee currency '%s' differs from base currency '%s' and quote currency '%s'.",
feeCurrency,
marketBase,
marketQuote
final BigDecimal totalPrice;
final BigDecimal fee;
if (TransactionType.BUY.equals(type)) {
totalPrice = total;
if (feeCurrency.equals(marketBase)) {
fee = amount.subtract(quoteTotalLessFee.abs());
} else {
throw new DataValidationException(String.format(
UNSUPPORTED_FEE_CURRENCY + "'%s' for BUY transaction on pair '%s/%s'.",
feeCurrency,
marketBase,
marketQuote
));
}
} else if (TransactionType.SELL.equals(type)) {
totalPrice = baseTotalLessFee.abs();
if (feeCurrency.equals(marketQuote)) {
fee = total.subtract(baseTotalLessFee.abs());
} else {
throw new DataValidationException(String.format(
UNSUPPORTED_FEE_CURRENCY + "'%s' for SELL transaction on pair '%s/%s'.",
feeCurrency,
marketBase,
marketQuote
));
}
} else {
throw new DataValidationException(String.format("Unsupported transaction type '%s'.", type));
}
validatePositivity(feeValue);

validatePositivity(totalPrice, fee);

return new TransactionCluster(
new BuySellImportedTransactionBean(
Expand All @@ -116,8 +134,8 @@ public TransactionCluster toTransactionCluster() {
marketBase, //base
marketQuote, //quote
type, //action
quoteTotalLessFee.abs(), //base quantity
evalUnitPrice(baseTotalLessFee.abs(), quoteTotalLessFee.abs()) //unit price
amount, //base quantity
evalUnitPrice(totalPrice, amount) //unit price
),
List.of(
new FeeRebateImportedTransactionBean(
Expand All @@ -126,7 +144,7 @@ public TransactionCluster toTransactionCluster() {
marketBase,
marketQuote,
TransactionType.FEE,
feeValue,
fee,
feeCurrency
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.ExchangeBean.ILLEGAL_NEGATIVE_VALUES;
import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.ExchangeBean.UNSUPPORTED_CURRENCY_PAIR;
import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.bean.PoloniexBeanV1.UNSUPPORTED_CATEGORY;
import static io.everytrade.server.plugin.impl.everytrade.parser.exchange.bean.PoloniexBeanV2.UNSUPPORTED_FEE_CURRENCY;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -46,8 +47,8 @@ void testCorrectParsingRawTransactionBuy() {
Currency.BTC,
Currency.USDT,
TransactionType.BUY,
new BigDecimal("0.56673303"),
new BigDecimal("9360.0977267727")
new BigDecimal("0.5671584"),
new BigDecimal("9353.0776336734")
),
List.of(
new FeeRebateImportedTransactionBean(
Expand Down Expand Up @@ -122,4 +123,13 @@ void testNegativeValues() {
final String error = parsingProblem.getMessage();
assertTrue(error.contains(ILLEGAL_NEGATIVE_VALUES.concat("[0, 1]")));
}

@Test
void testNotAllowedFeeCurrency() {
final String row = "2020-01-27 20:39:14,BTC/USDT,Exchange,Sell,8955,0.32647158,2923.5529989,0.075%," +
"436733958991,2921.36033415,-0.32647158,BTC,2.19266474\n";
final ParsingProblem parsingProblem = ParserTestUtils.getParsingProblem(HEADER_CORRECT + row);
final String error = parsingProblem.getMessage();
assertTrue(error.contains(UNSUPPORTED_FEE_CURRENCY.concat("'BTC'")));
}
}

0 comments on commit 1d1ea08

Please sign in to comment.