FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
request_id.hpp
Go to the documentation of this file.
1#pragma once
2// Request ids shared by the JSON order-entry encoders: "<kind><cl_ord_id>" with kind n/c/r/a
3// (1 + 14 chars), so a WebSocket API response maps back to the command with no lookup
4// table. Binance allows a 36-char string id, Bybit a 36-char reqId.
5#include "fastmm/core/fixed_string.hpp"
7
8#include <optional>
9#include <string_view>
10#include <utility>
11
12namespace fastmm::venues {
13
14using RequestId = FixedString<16>;
15// Amend: a replace the venue applies to the resting order (Binance Spot keepPriority); its
16// response has a different shape from a cancel-replace, so it needs its own kind.
17enum class RequestKind : char {
18 New = 'n',
19 Cancel = 'c',
20 Replace = 'r',
21 Amend = 'a',
22 Other = 'x',
23};
24
25[[nodiscard]] inline RequestId make_request_id(RequestKind kind, ClientOrderId id) noexcept {
26 RequestId out;
27 out.push_back(static_cast<char>(kind));
28 out.append(encode_cl_ord_id(id).view());
29 return out;
30}
31// Returns the kind and id encoded by make_request_id, or nullopt for foreign ids.
32[[nodiscard]] inline std::optional<std::pair<RequestKind, ClientOrderId>> parse_request_id(
33 std::string_view id) noexcept {
34 if (id.size() != 1 + kClOrdIdChars) return std::nullopt;
35 const char k = id[0];
36 if (k != 'n' && k != 'c' && k != 'r' && k != 'a') return std::nullopt;
37 const auto cl = decode_cl_ord_id(id.substr(1));
38 if (!cl) return std::nullopt;
39 return std::make_pair(static_cast<RequestKind>(k), *cl);
40}
41
42} // namespace fastmm::venues
Definition venue.hpp:38
RequestId make_request_id(RequestKind kind, ClientOrderId id) noexcept
Definition request_id.hpp:27
FixedString< 16 > RequestId
Definition request_id.hpp:16
std::optional< std::pair< RequestKind, ClientOrderId > > parse_request_id(std::string_view id) noexcept
Returns the kind and id encoded by make_request_id, or nullopt for foreign ids.
Definition request_id.hpp:34
RequestKind
Amend: a replace the venue applies to the resting order (Binance Spot keepPriority); its response has...
Definition request_id.hpp:19
constexpr std::size_t kClOrdIdChars
"fm" + 12 lowercase hex digits (48 bits) = 14 chars. Fits Binance (36) and Bybit (36).
Definition strong_id.hpp:63
std::optional< ClientOrderId > decode_cl_ord_id(std::string_view s) noexcept
Definition strong_id.hpp:84
FixedString< 16 > encode_cl_ord_id(ClientOrderId id) noexcept
Definition strong_id.hpp:77
Strongly typed identifiers.