Skip to content

Commit

Permalink
[Huobi public] - migrate to glaze
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Dec 17, 2024
1 parent 27be8a2 commit 131f874
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 179 deletions.
8 changes: 3 additions & 5 deletions src/api/exchanges/include/binance-schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
#include "cct_vector.hpp"
#include "monetaryamount.hpp"

namespace cct::api {
template <class T>
using has_msg_t = decltype(std::declval<T>().msg);
namespace cct::schema::binance {

template <class T>
using has_code_t = decltype(std::declval<T>().code);
} // namespace cct::api

namespace cct::schema::binance {
template <class T>
using has_msg_t = decltype(std::declval<T>().msg);

// PUBLIC

Expand Down
193 changes: 193 additions & 0 deletions src/api/exchanges/include/huobi-schema.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#pragma once

#include <cstdint>
#include <glaze/glaze.hpp> // IWYU pragma: export

#include "cct_string.hpp"
#include "cct_type_traits.hpp"
#include "cct_vector.hpp"
#include "monetaryamount.hpp"

namespace cct::schema::huobi {

template <class T>
using has_code_t = decltype(std::declval<T>().code);

template <class T>
using has_status_t = decltype(std::declval<T>().status);

// PUBLIC

// https://huobiapi.github.io/docs/spot/v1/en/#get-system-status

struct V2SystemStatus {
struct Status {
string description;
};

struct Incidents {
auto operator<=>(const Incidents&) const = default;
};

vector<Incidents> incidents;

Status status;
};

// https://huobiapi.github.io/docs/spot/v1/en/#apiv2-currency-amp-chains

struct V2ReferenceCurrencyDetails {
string currency;
string instStatus;

struct Chain {
string chain;
string displayName;
string depositStatus;
string withdrawStatus;
string withdrawFeeType;
string transactFeeWithdraw;

MonetaryAmount minWithdrawAmt;
MonetaryAmount maxWithdrawAmt;
int8_t withdrawPrecision;

using trivially_relocatable = is_trivially_relocatable<string>::type;

auto operator<=>(const Chain&) const = default;
};

vector<Chain> chains;

using trivially_relocatable = is_trivially_relocatable<string>::type;

auto operator<=>(const V2ReferenceCurrencyDetails&) const = default;
};

struct V2ReferenceCurrency {
int code;
vector<V2ReferenceCurrencyDetails> data;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-all-supported-trading-symbol-v2

struct V1SettingsCommonMarketSymbol {
string bc; // base currency
string qc; // quote currency
string state;
string at;
int8_t ap;
int8_t pp;
double minov;
double maxov;
double lominoa;
double lomaxoa;
double smminoa;
double smmaxoa;
double bmmaxov;

using trivially_relocatable = is_trivially_relocatable<string>::type;

auto operator<=>(const V1SettingsCommonMarketSymbol&) const = default;
};

struct V1SettingsCommonMarketSymbols {
string status;
vector<V1SettingsCommonMarketSymbol> data;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-latest-tickers-for-all-pairs

struct MarketTickers {
string status;

struct Ticker {
string symbol;
double ask;
double bid;
double askSize;
double bidSize;

using trivially_relocatable = is_trivially_relocatable<string>::type;

auto operator<=>(const Ticker&) const = default;
};

vector<Ticker> data;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-market-depth

struct MarketDepth {
string status;

struct Tick {
using PriceQuantityPair = std::array<double, 2>;

vector<PriceQuantityPair> asks;
vector<PriceQuantityPair> bids;
};

Tick tick;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-latest-aggregated-ticker

struct MarketDetailMerged {
string status;

struct Tick {
double amount;
};

Tick tick;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-the-most-recent-trades

struct MarketHistoryTrade {
string status;

struct Trade {
struct TradeData {
double amount;
double price;
int64_t ts;

enum class Direction : int8_t { buy, sell };

Direction direction;

auto operator<=>(const TradeData&) const = default;
};

vector<TradeData> data;

auto operator<=>(const Trade&) const = default;
};

vector<Trade> data;
};

// https://huobiapi.github.io/docs/spot/v1/en/#get-the-last-trade

struct MarketTrade {
string status;
struct Tick {
struct Data {
double price;

auto operator<=>(const Data&) const = default;
};
vector<Data> data;
};
Tick tick;
};

} // namespace cct::schema::huobi

template <>
struct glz::meta<::cct::schema::huobi::MarketHistoryTrade::Trade::TradeData::Direction> {
using enum ::cct::schema::huobi::MarketHistoryTrade::Trade::TradeData::Direction;
static constexpr auto value = enumerate(buy, sell);
};
6 changes: 3 additions & 3 deletions src/api/exchanges/include/huobipublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <unordered_map>

#include "cachedresult.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
#include "exchange-asset-config.hpp"
#include "exchangepublicapi.hpp"
#include "exchangepublicapitypes.hpp"
#include "huobi-schema.hpp"
#include "public-trade-vector.hpp"
#include "volumeandpricenbdecimals.hpp"

Expand Down Expand Up @@ -74,7 +74,7 @@ class HuobiPublic : public ExchangePublic {
friend class HuobiPrivate;

struct TradableCurrenciesFunc {
json::container operator()();
schema::huobi::V2ReferenceCurrency operator()();

CurlHandle& _curlHandle;
};
Expand Down Expand Up @@ -136,7 +136,7 @@ class HuobiPublic : public ExchangePublic {

WithdrawParams getWithdrawParams(CurrencyCode cur);

static bool ShouldDiscardChain(CurrencyCode cur, const json::container& chainDetail);
static bool ShouldDiscardChain(CurrencyCode cur, const schema::huobi::V2ReferenceCurrencyDetails::Chain& chainDetail);

CurlHandle _curlHandle;
CurlHandle _healthCheckCurlHandle;
Expand Down
6 changes: 3 additions & 3 deletions src/api/exchanges/src/binanceprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bool CheckErrorDoRetry(int statusCode, const T& ret, QueryDelayDir& queryDelayDi
Duration& queryDelay) {
switch (statusCode) {
case kInvalidTimestamp: {
if constexpr (amc::is_detected<has_msg_t, T>::value) {
if constexpr (amc::is_detected<schema::binance::has_msg_t, T>::value) {
if (ret.msg) {
return CheckErrorMsg(*ret.msg, queryDelayDir, sleepingTime, queryDelay);
}
Expand Down Expand Up @@ -213,7 +213,7 @@ T PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType req
continue;
}

if constexpr (amc::is_detected<has_code_t, T>::value) {
if constexpr (amc::is_detected<schema::binance::has_code_t, T>::value) {
if (!ret.code || *ret.code == 0) {
return ret;
}
Expand All @@ -233,7 +233,7 @@ T PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType req
if (throwIfError) {
std::string_view errorMsg;
string jsonStr = WriteMiniJsonOrThrow(ret);
if constexpr (amc::is_detected<has_msg_t, T>::value) {
if constexpr (amc::is_detected<schema::binance::has_msg_t, T>::value) {
if (ret.msg) {
errorMsg = *ret.msg;
}
Expand Down
3 changes: 2 additions & 1 deletion src/api/exchanges/src/binancepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ T PublicQuery(CurlHandle& curlHandle, std::string_view method, const CurlPostDat

return requestRetry.query<T, json::opts{.error_on_unknown_keys = false, .minified = true, .raw_string = true}>(
endpoint, [](const T& response) {
if constexpr (amc::is_detected<has_code_t, T>::value && amc::is_detected<has_msg_t, T>::value) {
if constexpr (amc::is_detected<schema::binance::has_code_t, T>::value &&
amc::is_detected<schema::binance::has_msg_t, T>::value) {
if (response.code && response.msg) {
const int statusCode = *response.code; // "1100" for instance
log::warn("Binance error ({}), msg: '{}'", statusCode, *response.msg);
Expand Down
8 changes: 3 additions & 5 deletions src/api/exchanges/src/bithumb-schema.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <glaze/glaze.hpp> // IWYU pragma: export
#include <unordered_map>
#include <variant>

Expand All @@ -9,14 +10,11 @@
#include "market.hpp"
#include "timepoint-schema.hpp"

namespace cct::api {
namespace cct::schema::bithumb {

template <class T>
using has_status_t = decltype(std::declval<T>().status);

} // namespace cct::api

namespace cct::schema::bithumb {

// PUBLIC

// https://apidocs.bithumb.com/reference/%EB%A7%88%EC%BC%93%EC%BD%94%EB%93%9C-%EC%A1%B0%ED%9A%8C
Expand Down
2 changes: 1 addition & 1 deletion src/api/exchanges/src/bithumbpublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ T PublicQuery(CurlHandle& curlHandle, std::string_view method, CurrencyCode base

return requestRetry.query<T, json::opts{.error_on_unknown_keys = false, .minified = true, .raw_string = true}>(
ComputeMethodUrl(method, base, quote, urlOpts), [](const T& response) {
if constexpr (amc::is_detected<has_status_t, T>::value) {
if constexpr (amc::is_detected<schema::bithumb::has_status_t, T>::value) {
if (!response.status.empty()) {
auto statusCode = StringToIntegral<int64_t>(response.status);
if (statusCode != BithumbPublic::kStatusOK) {
Expand Down
Loading

0 comments on commit 131f874

Please sign in to comment.