Skip to content

Commit

Permalink
Query result - migrate to glaze json
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Dec 23, 2024
1 parent fea1440 commit c35cfec
Show file tree
Hide file tree
Showing 94 changed files with 1,774 additions and 1,313 deletions.
12 changes: 0 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,6 @@ if(CCT_ENABLE_TESTS)
enable_testing()
endif()

# nlohmann_json - json container library
find_package(nlohmann_json CONFIG)
if(NOT nlohmann_json_FOUND)
FetchContent_Declare(
nlohmann_json
URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.3.tar.gz
URL_HASH SHA256=0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406
)

list(APPEND fetchContentPackagesToMakeAvailable nlohmann_json)
endif()

# Glaze - fast json serialization library
find_package(glaze CONFIG)
if(NOT glaze)
Expand Down
1 change: 0 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ If you are building frequently `coincenter` you can install them to speed up its
| -------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------- |
| [amc](https://github.com/AmadeusITGroup/amc.git) | High performance C++ containers (maintained by me) | MIT |
| [googletest](https://github.com/google/googletest.git) | Google Testing and Mocking Framework | BSD-3-Clause License |
| [json container](https://github.com/nlohmann/json) | JSON for Modern C++ | MIT |
| [json serialization](https://github.com/stephenberry/glaze) | Extremely fast, in memory, JSON and interface library for modern C++ | MIT |
| [spdlog](https://github.com/gabime/spdlog.git) | Fast C++ logging library | MIT |
| [prometheus-cpp](https://github.com/jupp0r/prometheus-cpp.git) | Prometheus Client Library for Modern C++ | MIT |
Expand Down
3 changes: 0 additions & 3 deletions src/api-objects/include/closed-order.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "cct_string.hpp"
#include "monetaryamount.hpp"
#include "order.hpp"
#include "orderid.hpp"
Expand All @@ -16,8 +15,6 @@ class ClosedOrder : public Order {

TimePoint matchedTime() const { return _matchedTime; }

string matchedTimeStr() const;

/// Compute the resulting merged closed order from *this and given one.
/// Given closed order should be of same ID, TradeSide and Market.
[[nodiscard]] ClosedOrder mergeWith(const ClosedOrder &closedOrder) const;
Expand Down
9 changes: 2 additions & 7 deletions src/api-objects/include/order.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include <compare>
#include <string_view>

#include "cct_string.hpp"
#include "cct_type_traits.hpp"
#include "market.hpp"
#include "monetaryamount.hpp"
Expand All @@ -25,18 +23,15 @@ class Order {

TradeSide side() const { return _side; }

std::string_view sideStr() const;

string placedTimeStr() const;

Market market() const { return Market(_matchedVolume.currencyCode(), _price.currencyCode()); }

std::strong_ordering operator<=>(const Order &) const noexcept = default;

using trivially_relocatable = is_trivially_relocatable<OrderId>::type;

protected:
Order(OrderId id, MonetaryAmount matchedVolume, MonetaryAmount price, TimePoint placedTime, TradeSide side);
Order(OrderId id, MonetaryAmount matchedVolume, MonetaryAmount price, TimePoint placedTime, TradeSide side)
: _placedTime(placedTime), _id(std::move(id)), _matchedVolume(matchedVolume), _price(price), _side(side) {}

private:
TimePoint _placedTime;
Expand Down
4 changes: 2 additions & 2 deletions src/api-objects/include/tradeinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace cct::api {
using UserRefInt = int32_t;

struct TradeContext {
CurrencyCode fromCur() const { return side == TradeSide::kSell ? market.base() : market.quote(); }
CurrencyCode toCur() const { return side == TradeSide::kBuy ? market.base() : market.quote(); }
CurrencyCode fromCur() const { return side == TradeSide::sell ? market.base() : market.quote(); }
CurrencyCode toCur() const { return side == TradeSide::buy ? market.base() : market.quote(); }

Market market;
TradeSide side{};
Expand Down
14 changes: 8 additions & 6 deletions src/api-objects/include/tradeoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TradeOptions {
TradeOptions(const PriceOptions &priceOptions, TradeTimeoutAction timeoutAction, TradeMode tradeMode,
Duration maxTradeTime, Duration minTimeBetweenPriceUpdates = kUndefinedDuration,
TradeTypePolicy tradeTypePolicy = TradeTypePolicy::kDefault,
TradeSyncPolicy tradeSyncPolicy = TradeSyncPolicy::kSynchronous);
TradeSyncPolicy tradeSyncPolicy = TradeSyncPolicy::synchronous);

/// Constructs a new TradeOptions based on 'rhs' with unspecified options overriden from exchange config values
TradeOptions(const TradeOptions &rhs, const schema::ExchangeQueryTradeConfig &exchangeTradeConfig);
Expand All @@ -53,13 +53,15 @@ class TradeOptions {
return _priceOptions.isTakerStrategy() && (!isSimulation() || !placeRealOrderInSimulationMode);
}

constexpr bool isSimulation() const { return _tradeMode == TradeMode::kSimulation; }
constexpr bool isSimulation() const { return _tradeMode == TradeMode::simulation; }

constexpr bool isFixedPrice() const { return _priceOptions.isFixedPrice(); }

constexpr bool isRelativePrice() const { return _priceOptions.isRelativePrice(); }

constexpr bool placeMarketOrderAtTimeout() const { return _timeoutAction == TradeTimeoutAction::kMatch; }
constexpr TradeTimeoutAction timeoutAction() const { return _timeoutAction; }

constexpr bool placeMarketOrderAtTimeout() const { return _timeoutAction == TradeTimeoutAction::match; }

constexpr void switchToTakerStrategy() { _priceOptions.switchToTakerStrategy(); }

Expand All @@ -75,10 +77,10 @@ class TradeOptions {
Duration _maxTradeTime = kUndefinedDuration;
Duration _minTimeBetweenPriceUpdates = kUndefinedDuration;
PriceOptions _priceOptions;
TradeTimeoutAction _timeoutAction = TradeTimeoutAction::kDefault;
TradeMode _tradeMode = TradeMode::kReal;
TradeTimeoutAction _timeoutAction = TradeTimeoutAction::exchange_default;
TradeMode _tradeMode = TradeMode::real;
TradeTypePolicy _tradeTypePolicy = TradeTypePolicy::kDefault;
TradeSyncPolicy _tradeSyncPolicy = TradeSyncPolicy::kSynchronous;
TradeSyncPolicy _tradeSyncPolicy = TradeSyncPolicy::synchronous;
};

} // namespace cct
2 changes: 1 addition & 1 deletion src/api-objects/include/withdrawinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SentWithdrawInfo {
private:
MonetaryAmount _netEmittedAmount;
MonetaryAmount _fee;
Withdraw::Status _withdrawStatus = Withdraw::Status::kInitial;
Withdraw::Status _withdrawStatus = Withdraw::Status::initial;
};

class ReceivedWithdrawInfo {
Expand Down
16 changes: 12 additions & 4 deletions src/api-objects/include/withdrawoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <cstdint>
#include <string_view>

#include "cct_json.hpp"
#include "timedef.hpp"

namespace cct {

enum class WithdrawSyncPolicy : int8_t {
kSynchronous, // Follow lifetime of the withdraw until funds are received at destination
kAsynchronous // Only trigger withdraw and exit withdraw process directly
synchronous, // Follow lifetime of the withdraw until funds are received at destination
asynchronous // Only trigger withdraw and exit withdraw process directly
};

class WithdrawOptions {
Expand All @@ -36,7 +37,14 @@ class WithdrawOptions {
static constexpr auto kWithdrawRefreshTime = seconds(5);

Duration _withdrawRefreshTime = kWithdrawRefreshTime;
WithdrawSyncPolicy _withdrawSyncPolicy = WithdrawSyncPolicy::kSynchronous;
WithdrawSyncPolicy _withdrawSyncPolicy = WithdrawSyncPolicy::synchronous;
Mode _mode = Mode::kReal;
};
} // namespace cct
} // namespace cct

template <>
struct glz::meta<cct::WithdrawSyncPolicy> {
using enum cct::WithdrawSyncPolicy;

static constexpr auto value = enumerate(synchronous, asynchronous);
};
15 changes: 11 additions & 4 deletions src/api-objects/include/withdrawordeposit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <compare>
#include <string_view>

#include "cct_json.hpp"
#include "cct_string.hpp"
#include "monetaryamount.hpp"
#include "timedef.hpp"
Expand All @@ -11,10 +12,10 @@ namespace cct {
class WithdrawOrDeposit {
public:
enum class Status : int8_t {
kInitial,
kSuccess,
kProcessing,
kFailureOrRejected,
initial,
success,
processing,
failed,
};

template <class StringType>
Expand Down Expand Up @@ -46,3 +47,9 @@ class WithdrawOrDeposit {
};

} // namespace cct

template <>
struct glz::meta<::cct::WithdrawOrDeposit::Status> {
using enum ::cct::WithdrawOrDeposit::Status;
static constexpr auto value = enumerate(initial, success, processing, failed);
};
3 changes: 0 additions & 3 deletions src/api-objects/src/closed-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <utility>

#include "cct_string.hpp"
#include "monetaryamount.hpp"
#include "order.hpp"
#include "orderid.hpp"
Expand All @@ -16,8 +15,6 @@ ClosedOrder::ClosedOrder(OrderId id, MonetaryAmount matchedVolume, MonetaryAmoun
TimePoint matchedTime, TradeSide side)
: Order(std::move(id), matchedVolume, price, placedTime, side), _matchedTime(matchedTime) {}

string ClosedOrder::matchedTimeStr() const { return TimeToString(_matchedTime); }

ClosedOrder ClosedOrder::mergeWith(const ClosedOrder &closedOrder) const {
const MonetaryAmount totalMatchedVolume = closedOrder.matchedVolume() + matchedVolume();
const auto previousMatchedTs = TimestampToMillisecondsSinceEpoch(matchedTime());
Expand Down
20 changes: 0 additions & 20 deletions src/api-objects/src/order.cpp

This file was deleted.

14 changes: 7 additions & 7 deletions src/api-objects/src/tradeoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace cct {

namespace {
auto ComputeTimeoutAction(TradeTimeoutAction tradeTimeoutAction, bool timeoutMatch) {
if (tradeTimeoutAction != TradeTimeoutAction::kDefault) {
if (tradeTimeoutAction != TradeTimeoutAction::exchange_default) {
return tradeTimeoutAction;
}
return timeoutMatch ? TradeTimeoutAction::kMatch : TradeTimeoutAction::kCancel;
return timeoutMatch ? TradeTimeoutAction::match : TradeTimeoutAction::cancel;
}
} // namespace

Expand Down Expand Up @@ -58,12 +58,12 @@ bool TradeOptions::isMultiTradeAllowed(bool multiTradeAllowedByDefault) const {

std::string_view TradeOptions::timeoutActionStr() const {
switch (_timeoutAction) {
case TradeTimeoutAction::kDefault:
case TradeTimeoutAction::exchange_default:
// Default will behave the same as cancel - this field is not publicly exposed
[[fallthrough]];
case TradeTimeoutAction::kCancel:
case TradeTimeoutAction::cancel:
return "cancel";
case TradeTimeoutAction::kMatch:
case TradeTimeoutAction::match:
return "match";
default:
unreachable();
Expand All @@ -72,9 +72,9 @@ std::string_view TradeOptions::timeoutActionStr() const {

std::string_view TradeOptions::tradeSyncPolicyStr() const {
switch (_tradeSyncPolicy) {
case TradeSyncPolicy::kSynchronous:
case TradeSyncPolicy::synchronous:
return "synchronous";
case TradeSyncPolicy::kAsynchronous:
case TradeSyncPolicy::asynchronous:
return "asynchronous";
default:
unreachable();
Expand Down
4 changes: 2 additions & 2 deletions src/api-objects/src/withdrawoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ WithdrawOptions::WithdrawOptions(Duration withdrawRefreshTime, WithdrawSyncPolic

std::string_view WithdrawOptions::withdrawSyncPolicyStr() const {
switch (_withdrawSyncPolicy) {
case WithdrawSyncPolicy::kSynchronous:
case WithdrawSyncPolicy::synchronous:
return "synchronous";
case WithdrawSyncPolicy::kAsynchronous:
case WithdrawSyncPolicy::asynchronous:
return "asynchronous";
default:
unreachable();
Expand Down
8 changes: 4 additions & 4 deletions src/api-objects/src/withdrawordeposit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace cct {
std::string_view WithdrawOrDeposit::statusStr() const {
switch (_status) {
case Status::kInitial:
case Status::initial:
return "initial";
case Status::kProcessing:
case Status::processing:
return "processing";
case Status::kFailureOrRejected:
case Status::failed:
return "failed";
case Status::kSuccess:
case Status::success:
return "success";
default:
unreachable();
Expand Down
4 changes: 2 additions & 2 deletions src/api-objects/test/closed-order_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ClosedOrderTest : public ::testing::Test {
TimePoint tp2{milliseconds{std::numeric_limits<int64_t>::max() / 9900000}};
TimePoint tp3{milliseconds{std::numeric_limits<int64_t>::max() / 9800000}};

ClosedOrder closedOrder1{"1", MonetaryAmount(15, "BTC", 1), MonetaryAmount(35000, "USDT"), tp1, tp1, TradeSide::kBuy};
ClosedOrder closedOrder2{"2", MonetaryAmount(25, "BTC", 1), MonetaryAmount(45000, "USDT"), tp2, tp3, TradeSide::kBuy};
ClosedOrder closedOrder1{"1", MonetaryAmount(15, "BTC", 1), MonetaryAmount(35000, "USDT"), tp1, tp1, TradeSide::buy};
ClosedOrder closedOrder2{"2", MonetaryAmount(25, "BTC", 1), MonetaryAmount(45000, "USDT"), tp2, tp3, TradeSide::buy};
};

TEST_F(ClosedOrderTest, SelfMerge) {
Expand Down
10 changes: 5 additions & 5 deletions src/api-objects/test/deposit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class DepositTest : public ::testing::Test {
TimePoint tp3{milliseconds{std::numeric_limits<int64_t>::max() / 8000000}};
TimePoint tp4{milliseconds{std::numeric_limits<int64_t>::max() / 7000000}};

Deposit deposit1{"id1", tp2, MonetaryAmount("0.045", "BTC"), Deposit::Status::kSuccess};
Deposit deposit2{"id2", tp4, MonetaryAmount(37, "XRP"), Deposit::Status::kSuccess};
Deposit deposit3{"id3", tp1, MonetaryAmount("15020.67", "EUR"), Deposit::Status::kFailureOrRejected};
Deposit deposit4{"id4", tp4, MonetaryAmount("1.31", "ETH"), Deposit::Status::kProcessing};
Deposit deposit5{"id5", tp3, MonetaryAmount("69204866.9", "DOGE"), Deposit::Status::kSuccess};
Deposit deposit1{"id1", tp2, MonetaryAmount("0.045", "BTC"), Deposit::Status::success};
Deposit deposit2{"id2", tp4, MonetaryAmount(37, "XRP"), Deposit::Status::success};
Deposit deposit3{"id3", tp1, MonetaryAmount("15020.67", "EUR"), Deposit::Status::failed};
Deposit deposit4{"id4", tp4, MonetaryAmount("1.31", "ETH"), Deposit::Status::processing};
Deposit deposit5{"id5", tp3, MonetaryAmount("69204866.9", "DOGE"), Deposit::Status::success};

vector<Deposit> deposits{deposit1, deposit2, deposit3, deposit4, deposit5};
};
Expand Down
11 changes: 5 additions & 6 deletions src/api-objects/test/withdraw_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ class WithdrawTest : public ::testing::Test {
TimePoint tp3{milliseconds{std::numeric_limits<int64_t>::max() / 8000000}};
TimePoint tp4{milliseconds{std::numeric_limits<int64_t>::max() / 7000000}};

Withdraw withdraw1{"id1", tp2, MonetaryAmount("0.045", "BTC"), Withdraw::Status::kSuccess,
Withdraw withdraw1{"id1", tp2, MonetaryAmount("0.045", "BTC"), Withdraw::Status::success,
MonetaryAmount("0.001", "BTC")};
Withdraw withdraw2{"id2", tp4, MonetaryAmount(37, "XRP"), Withdraw::Status::kSuccess, MonetaryAmount("0.02 XRP")};
Withdraw withdraw3{"id3", tp1, MonetaryAmount("15020.67", "EUR"), Withdraw::Status::kFailureOrRejected,
MonetaryAmount("0 EUR")};
Withdraw withdraw4{"id4", tp4, MonetaryAmount("1.31", "ETH"), Withdraw::Status::kProcessing,
Withdraw withdraw2{"id2", tp4, MonetaryAmount(37, "XRP"), Withdraw::Status::success, MonetaryAmount("0.02 XRP")};
Withdraw withdraw3{"id3", tp1, MonetaryAmount("15020.67", "EUR"), Withdraw::Status::failed, MonetaryAmount("0 EUR")};
Withdraw withdraw4{"id4", tp4, MonetaryAmount("1.31", "ETH"), Withdraw::Status::processing,
MonetaryAmount("0.001", "ETH")};
Withdraw withdraw5{"id5", tp3, MonetaryAmount("69204866.9", "DOGE"), Withdraw::Status::kSuccess,
Withdraw withdraw5{"id5", tp3, MonetaryAmount("69204866.9", "DOGE"), Withdraw::Status::success,
MonetaryAmount("1 DOGE")};

vector<Withdraw> withdraws{withdraw1, withdraw2, withdraw3, withdraw4, withdraw5};
Expand Down
2 changes: 1 addition & 1 deletion src/api/common/src/commonapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "cachedresult.hpp"
#include "cct_const.hpp"
#include "cct_json-serialization.hpp"
#include "cct_json.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "cct_vector.hpp"
Expand Down
Loading

0 comments on commit c35cfec

Please sign in to comment.