Skip to content

Commit

Permalink
Use glaze for binance public api
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Nov 24, 2024
1 parent c124e10 commit 2f08e3d
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,126 @@
#include "cct_vector.hpp"
#include "monetaryamount.hpp"

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

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

namespace cct::schema::binance {

// PUBLIC

// https://binance-docs.github.io/apidocs/spot/en/#exchange-information

struct V3ExchangeInfo {
struct Symbol {
string baseAsset;
string quoteAsset;
string status;
int8_t baseAssetPrecision;
int8_t quoteAssetPrecision;

struct Filter {
string filterType;
MonetaryAmount maxPrice;
MonetaryAmount minPrice;
MonetaryAmount tickSize;
MonetaryAmount minNotional;
MonetaryAmount maxNotional;
MonetaryAmount maxQty;
MonetaryAmount minQty;
MonetaryAmount stepSize;
int32_t avgPriceMins;
bool applyToMarket;
bool applyMinToMarket;
bool applyMaxToMarket;

using trivially_relocatable = is_trivially_relocatable<string>::type;

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

vector<Filter> filters;

vector<string> permissions;

using trivially_relocatable = is_trivially_relocatable<string>::type;

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

vector<Symbol> symbols;

std::optional<int> code;
std::optional<string> msg;
};

// https://binance-docs.github.io/apidocs/spot/en/#current-average-price
struct V3AvgPrice {
MonetaryAmount price;
std::optional<int> code;
std::optional<string> msg;
};

// https://binance-docs.github.io/apidocs/spot/en/#symbol-order-book-ticker
struct V3TickerBookTickerElem {
string symbol;
MonetaryAmount bidPrice;
MonetaryAmount bidQty;
MonetaryAmount askPrice;
MonetaryAmount askQty;

using trivially_relocatable = is_trivially_relocatable<string>::type;
};

using V3TickerBookTicker = vector<V3TickerBookTickerElem>;

// https://binance-docs.github.io/apidocs/spot/en/#order-book
struct V3OrderBook {
using Line = std::array<MonetaryAmount, 2U>; // price is first, then volume

vector<Line> asks;
vector<Line> bids;

std::optional<int> code;
std::optional<string> msg;
};

// https://binance-docs.github.io/apidocs/spot/en/#24hr-ticker-price-change-statistics
struct V3Ticker24hr {
MonetaryAmount volume;

std::optional<int> code;
std::optional<string> msg;
};

// https://binance-docs.github.io/apidocs/spot/en/#recent-trades-list
struct V3Trade {
MonetaryAmount price;
MonetaryAmount qty;
int64_t time{};
bool isBuyerMaker;

using trivially_relocatable = is_trivially_relocatable<string>::type;

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

using V3Trades = vector<V3Trade>;

// https://binance-docs.github.io/apidocs/spot/en/#symbol-price-ticker
struct V3TickerPrice {
MonetaryAmount price;

std::optional<int> code;
std::optional<string> msg;
};

// PRIVATE

using OrderId = uint64_t;

// https://binance-docs.github.io/apidocs/spot/en/#account-status-user_data
Expand Down
4 changes: 2 additions & 2 deletions src/api/exchanges/include/binancepublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <string_view>
#include <unordered_map>

#include "binance-schema.hpp"
#include "cachedresult.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
#include "currencyexchange.hpp"
Expand Down Expand Up @@ -81,7 +81,7 @@ class BinancePublic : public ExchangePublic {
};

struct ExchangeInfoFunc {
using ExchangeInfoDataByMarket = std::unordered_map<Market, json::container>;
using ExchangeInfoDataByMarket = std::unordered_map<Market, schema::binance::V3ExchangeInfo::Symbol>;

ExchangeInfoDataByMarket operator()();

Expand Down
1 change: 1 addition & 0 deletions src/api/exchanges/include/bithumbpublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string_view>

#include "cachedresult.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "currencyexchange.hpp"
#include "exchange-asset-config.hpp"
Expand Down
17 changes: 4 additions & 13 deletions src/api/exchanges/src/binanceprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "orderid.hpp"
#include "ordersconstraints.hpp"
#include "permanentcurloptions.hpp"
#include "read-json.hpp"
#include "recentdeposit.hpp"
#include "ssl_sha.hpp"
#include "stringconv.hpp"
Expand Down Expand Up @@ -152,12 +153,6 @@ bool CheckErrorMsg(std::string_view msg, QueryDelayDir& queryDelayDir, Duration&
return false;
}

template <class T>
using has_msg_t = decltype(std::declval<T>().msg);

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

template <class T>
bool CheckErrorDoRetry(int statusCode, const T& ret, QueryDelayDir& queryDelayDir, Duration& sleepingTime,
Duration& queryDelay) {
Expand Down Expand Up @@ -187,9 +182,7 @@ bool CheckErrorDoRetry(int statusCode, const T& ret, QueryDelayDir& queryDelayDi
return false;
}

template <class T,
json::opts jsonOpts = json::opts{.error_on_unknown_keys = false, .minified = true, .raw_string = true},
class CurlPostDataT = CurlPostData>
template <class T, class CurlPostDataT = CurlPostData>
T PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType requestType, std::string_view endpoint,
Duration& queryDelay, CurlPostDataT&& curlPostData = CurlPostData(), bool throwIfError = true) {
CurlOptions opts(requestType, std::forward<CurlPostDataT>(curlPostData));
Expand All @@ -210,11 +203,9 @@ T PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType req

auto resStr = curlHandle.query(endpoint, opts);

auto ec = json::read<jsonOpts>(ret, resStr);
auto ec = ReadJson<json::opts{.error_on_unknown_keys = false, .minified = true, .raw_string = true}>(
resStr, "binance private", ret);
if (ec) {
std::string_view prefixJsonContent = resStr.substr(0, std::min<int>(resStr.size(), 20));
log::error("Error while reading json content '{}{}': {}", prefixJsonContent,
prefixJsonContent.size() < resStr.size() ? "..." : "", json::format_error(ec, resStr));
statusCode = -1;
continue;
}
Expand Down
Loading

0 comments on commit 2f08e3d

Please sign in to comment.