Skip to content

Commit

Permalink
Improve returned trade result information
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Nov 11, 2023
1 parent 841c489 commit 54f60b2
Show file tree
Hide file tree
Showing 33 changed files with 1,375 additions and 613 deletions.
238 changes: 208 additions & 30 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/api-objects/include/withdrawoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class WithdrawOptions {

std::string_view withdrawSyncPolicyStr() const;

bool operator==(const WithdrawOptions &) const noexcept = default;

private:
/// The waiting time between each query of withdraw info to check withdraw status from an exchange.
/// A very small value is not relevant as withdraw time order of magnitude are minutes or hours
Expand Down
2 changes: 2 additions & 0 deletions src/api-objects/include/withdrawsordepositsconstraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class WithdrawsOrDepositsConstraints {
}
bool isIdOnlyDependent() const { return _currencyIdTimeConstraintsBmp.isDepositIdOnlyDependent(); }

bool operator==(const WithdrawsOrDepositsConstraints &) const noexcept = default;

using trivially_relocatable = is_trivially_relocatable<IdSet>::type;

private:
Expand Down
7 changes: 7 additions & 0 deletions src/api/interface/include/exchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Exchange {
std::string_view name() const { return _exchangePublic.name(); }
std::string_view keyName() const { return apiPrivate().keyName(); }

ExchangeName createExchangeName() const {
if (hasPrivateAPI()) {
return {name(), keyName()};
}
return ExchangeName(name());
}

api::ExchangePublic &apiPublic() { return _exchangePublic; }
const api::ExchangePublic &apiPublic() const { return _exchangePublic; }

Expand Down
14 changes: 14 additions & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ target_link_libraries(coincenter_engine PUBLIC coincenter_api-interface)
target_link_libraries(coincenter_engine PUBLIC coincenter_objects)
target_include_directories(coincenter_engine PUBLIC include)

add_unit_test(
coincentercommandfactory_test
test/coincentercommandfactory_test.cpp
LIBRARIES
coincenter_engine
)

add_unit_test(
coincenteroptions_test
test/coincenteroptions_test.cpp
Expand Down Expand Up @@ -65,3 +72,10 @@ add_unit_test(
LIBRARIES
coincenter_engine
)

add_unit_test(
transferablecommandresult_test
test/transferablecommandresult_test.cpp
LIBRARIES
coincenter_engine
)
8 changes: 3 additions & 5 deletions src/engine/include/coincenter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

#include <optional>
#include <span>
#include <string_view>

#include "apikeysprovider.hpp"
#include "coincenterinfo.hpp"
#include "commonapi.hpp"
#include "exchange.hpp"
#include "exchangename.hpp"
#include "exchangepool.hpp"
#include "exchangesorchestrator.hpp"
#include "fiatconverter.hpp"
#include "metricsexporter.hpp"
#include "monitoringinfo.hpp"
#include "ordersconstraints.hpp"
#include "queryresultprinter.hpp"
#include "queryresulttypes.hpp"
#include "tradedamounts.hpp"
#include "transferablecommandresult.hpp"

namespace cct {

Expand Down Expand Up @@ -132,7 +129,8 @@ class Coincenter {
const FiatConverter &fiatConverter() const { return _fiatConverter; }

private:
void processCommand(const CoincenterCommand &cmd);
TransferableCommandResultVector processCommand(
const CoincenterCommand &cmd, std::span<const TransferableCommandResult> previousTransferableResults);

const CoincenterInfo &_coincenterInfo;
api::CommonAPI _commonAPI;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/include/coincentercommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class CoincenterCommand {
bool isPercentageAmount() const { return _isPercentageAmount; }
bool withBalanceInUse() const { return _withBalanceInUse; }

bool operator==(const CoincenterCommand&) const noexcept = default;

using trivially_relocatable = std::integral_constant<bool, is_trivially_relocatable_v<ExchangeNames> &&
is_trivially_relocatable_v<OrdersConstraints>>::type;

Expand Down
29 changes: 29 additions & 0 deletions src/engine/include/coincentercommandfactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "coincentercommand.hpp"
#include "coincentercommandtype.hpp"
#include "coincenteroptions.hpp"

namespace cct {
class StringOptionParser;

class CoincenterCommandFactory {
public:
CoincenterCommandFactory(const CoincenterCmdLineOptions &cmdLineOptions, const CoincenterCommand *pPreviousCommand)
: _cmdLineOptions(cmdLineOptions), _pPreviousCommand(pPreviousCommand) {}

static CoincenterCommand CreateMarketCommand(StringOptionParser &optionParser);

CoincenterCommand createOrderCommand(CoincenterCommandType type, StringOptionParser &optionParser);

CoincenterCommand createTradeCommand(CoincenterCommandType type, StringOptionParser &optionParser);

CoincenterCommand createWithdrawApplyCommand(StringOptionParser &optionParser);

CoincenterCommand createWithdrawApplyAllCommand(StringOptionParser &optionParser);

private:
const CoincenterCmdLineOptions &_cmdLineOptions;
const CoincenterCommand *_pPreviousCommand;
};
} // namespace cct
9 changes: 1 addition & 8 deletions src/engine/include/coincentercommands.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#include <span>
#include <string_view>

#include "cct_vector.hpp"
#include "coincentercommand.hpp"
#include "coincenteroptions.hpp"
#include "monitoringinfo.hpp"
#include "timedef.hpp"

namespace cct {
Expand All @@ -16,18 +14,13 @@ class CoincenterCommands {
// Builds a CoincenterCommands without any commands.
CoincenterCommands() noexcept = default;

// Builds a CoincenterCommands and add commands from given command line options.
explicit CoincenterCommands(const CoincenterCmdLineOptions &cmdLineOptions)
: CoincenterCommands(std::span<const CoincenterCmdLineOptions>{&cmdLineOptions, 1U}) {}

// Builds a CoincenterCommands and add commands from given command line options span.
explicit CoincenterCommands(std::span<const CoincenterCmdLineOptions> cmdLineOptionsSpan);

static vector<CoincenterCmdLineOptions> ParseOptions(int argc, const char *argv[]);

/// @brief Set this CoincenterCommands from given command line options.
/// @return false if only help or version is asked, true otherwise
bool addOption(const CoincenterCmdLineOptions &cmdLineOptions);
void addOption(const CoincenterCmdLineOptions &cmdLineOptions, const CoincenterCommand *pPreviousCommand);

std::span<const CoincenterCommand> commands() const { return _commands; }

Expand Down
3 changes: 2 additions & 1 deletion src/engine/include/staticcommandlineoptioncheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>

#include "commandlineoption.hpp"
Expand Down Expand Up @@ -81,4 +82,4 @@ consteval auto ComputeAllCommandLineOptions(std::array<T, N>... ar) {
return all;
}

} // namespace cct
} // namespace cct
57 changes: 20 additions & 37 deletions src/engine/include/stringoptionparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <tuple>
#include <utility>

#include "cct_string.hpp"
Expand All @@ -16,52 +15,36 @@
namespace cct {
class StringOptionParser {
public:
using MarketExchanges = std::pair<Market, ExchangeNames>;
using CurrenciesPrivateExchanges = std::tuple<CurrencyCode, CurrencyCode, ExchangeNames>;
using CurrencyPrivateExchanges = std::pair<CurrencyCode, ExchangeNames>;
using MonetaryAmountCurrencyPrivateExchanges = std::tuple<MonetaryAmount, bool, CurrencyCode, ExchangeNames>;
using CurrencyFromToPrivateExchange = std::pair<CurrencyCode, ExchangeNames>;
using MonetaryAmountFromToPrivateExchange = std::tuple<MonetaryAmount, bool, ExchangeNames>;
using MonetaryAmountFromToPublicExchangeToCurrency = std::tuple<MonetaryAmount, ExchangeNames, CurrencyCode>;
using CurrencyPublicExchanges = std::pair<CurrencyCode, ExchangeNames>;
using CurrenciesPublicExchanges = std::tuple<CurrencyCode, CurrencyCode, ExchangeNames>;
enum class AmountType : int8_t { kAbsolute, kPercentage, kNotPresent };
enum class FieldIs : int8_t { kMandatory, kOptional };

enum class CurrencyIs : int8_t { kMandatory, kOptional };
StringOptionParser() noexcept = default;

explicit StringOptionParser(std::string_view optFullStr) : _opt(optFullStr) {}

ExchangeNames getExchanges() const;
/// If FieldIs is kOptional and there is no currency, default currency code will be returned.
/// otherwise exception invalid_argument will be raised
CurrencyCode parseCurrency(FieldIs fieldIs = FieldIs::kMandatory);

MarketExchanges getMarketExchanges() const;
/// If FieldIs is kOptional and there is no market, default market will be returned.
/// otherwise exception invalid_argument will be raised
Market parseMarket(FieldIs fieldIs = FieldIs::kMandatory);

CurrencyPrivateExchanges getCurrencyPrivateExchanges(CurrencyIs currencyIs) const;
/// If FieldIs is kOptional and there is no amount, AmountType kNotPresent will be returned
/// otherwise exception invalid_argument will be raised
std::pair<MonetaryAmount, AmountType> parseNonZeroAmount(FieldIs fieldIs = FieldIs::kMandatory);

auto getMonetaryAmountPrivateExchanges() const {
auto ret = getMonetaryAmountCurrencyPrivateExchanges(false);
return std::make_tuple(std::move(std::get<0>(ret)), std::move(std::get<1>(ret)), std::move(std::get<3>(ret)));
}
/// Parse the remaining option string with CSV string values.
vector<string> getCSVValues();

CurrenciesPrivateExchanges getCurrenciesPrivateExchanges(bool currenciesShouldBeSet = true) const;
/// Parse exchanges.
/// Exception will be raised for any invalid exchange name - but an empty list of exchanges is accepted.
ExchangeNames parseExchanges(char sep = ',');

MonetaryAmountCurrencyPrivateExchanges getMonetaryAmountCurrencyPrivateExchanges() const {
return getMonetaryAmountCurrencyPrivateExchanges(true);
}

CurrencyFromToPrivateExchange getCurrencyFromToPrivateExchange() const;

MonetaryAmountFromToPrivateExchange getMonetaryAmountFromToPrivateExchange() const;

CurrencyPublicExchanges getCurrencyPublicExchanges() const;

CurrenciesPublicExchanges getCurrenciesPublicExchanges() const;

vector<string> getCSVValues() const;

protected:
std::size_t getNextCommaPos(std::size_t startPos = 0, bool throwIfNone = true) const;

MonetaryAmountCurrencyPrivateExchanges getMonetaryAmountCurrencyPrivateExchanges(bool withCurrency) const;
void checkEndParsing() const;

private:
std::string_view _opt;
std::size_t _pos{};
};
} // namespace cct
38 changes: 38 additions & 0 deletions src/engine/include/transferablecommandresult.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <span>
#include <utility>

#include "cct_smallvector.hpp"
#include "cct_type_traits.hpp"
#include "exchangename.hpp"
#include "monetaryamount.hpp"

namespace cct {
class TransferableCommandResult {
public:
TransferableCommandResult(ExchangeName targetedExchange, MonetaryAmount resultedAmount)
: _targetedExchange(std::move(targetedExchange)), _resultedAmount(resultedAmount) {}

const ExchangeName &targetedExchange() const { return _targetedExchange; }
MonetaryAmount resultedAmount() const { return _resultedAmount; }

using trivially_relocatable = is_trivially_relocatable<ExchangeName>::type;

bool operator==(const TransferableCommandResult &) const noexcept = default;

private:
ExchangeName _targetedExchange;
MonetaryAmount _resultedAmount;
};

using TransferableCommandResultVector = SmallVector<TransferableCommandResult, 1>;

class CoincenterCommand;

std::pair<MonetaryAmount, ExchangeNames> ComputeTradeAmountAndExchanges(
const CoincenterCommand &cmd, std::span<const TransferableCommandResult> previousTransferableResults);

std::pair<MonetaryAmount, ExchangeName> ComputeWithdrawAmount(
const CoincenterCommand &cmd, std::span<const TransferableCommandResult> previousTransferableResults);
} // namespace cct
Loading

0 comments on commit 54f60b2

Please sign in to comment.