Skip to content

Commit

Permalink
Make amount optional (defaults to 1) for conversion rate command line…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
sjanel committed Nov 22, 2024
1 parent 1d25e51 commit 49866ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/engine/src/coincenter-commands-processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ TransferableCommandResultVector CoincenterCommandsProcessor::processGroupedComma
_queryResultPrinter.printConversion(startAmountsPerExchangePos, firstCmd.cur1(), conversionPerExchange);
FillConversionTransferableCommandResults(conversionPerExchange, transferableResults);
} else {
for (const auto &exchangeName : firstCmd.exchangeNames()) {
ExchangeNameEnum exchangeNameEnum = exchangeName.exchangeNameEnum();
if (std::ranges::find(exchangeNameEnumVector, exchangeNameEnum) == exchangeNameEnumVector.end()) {
exchangeNameEnumVector.push_back(exchangeNameEnum);
}
}
const auto conversionPerExchange =
_coincenter.getConversion(firstCmd.amount(), firstCmd.cur1(), exchangeNameEnumVector);
_queryResultPrinter.printConversion(firstCmd.amount(), firstCmd.cur1(), conversionPerExchange);
Expand Down
14 changes: 12 additions & 2 deletions src/engine/src/coincentercommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,23 @@ void CoincenterCommands::addOption(const CoincenterCmdLineOptions &cmdLineOption
if (!cmdLineOptions.conversion.empty()) {
optionParser = StringOptionParser(cmdLineOptions.conversion);

const auto [amount, amountType] = optionParser.parseNonZeroAmount(StringOptionParser::FieldIs::kOptional);
auto [amount, amountType] = optionParser.parseNonZeroAmount(StringOptionParser::FieldIs::kOptional);
if (amountType == StringOptionParser::AmountType::kPercentage) {
throw invalid_argument("conversion should start with an absolute amount");
}

CurrencyCode to;
if (amountType == StringOptionParser::AmountType::kNotPresent) {
Market market = optionParser.parseMarket();
amount = MonetaryAmount(1, market.base());
to = market.quote();
} else {
to = optionParser.parseCurrency();
}

_commands.emplace_back(CoincenterCommandType::Conversion)
.setAmount(amount)
.setCur1(optionParser.parseCurrency())
.setCur1(to)
.setExchangeNames(optionParser.parseExchanges());
}

Expand Down
4 changes: 3 additions & 1 deletion src/schema/include/exchange-tradefees-config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ struct ExchangeTradeFeesConfig {
/// Apply the general maker fee defined for this exchange trade fees config on given MonetaryAmount.
/// In other words, convert a gross amount into a net amount with maker fees
MonetaryAmount applyFee(MA ma, FeeType feeType) const {
return ma * (feeType == FeeType::Maker ? maker : taker);
return (ma * (MonetaryAmount(100) - fee(feeType))) / 100;
}

auto fee(FeeType feeType) const { return feeType == FeeType::Maker ? maker : taker; }

optional_or_t<MonetaryAmount, Optional> maker;
optional_or_t<MonetaryAmount, Optional> taker;
};
Expand Down

0 comments on commit 49866ea

Please sign in to comment.