ORE Studio 0.0.4
Loading...
Searching...
No Matches
trade_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_TRADING_MESSAGING_TRADE_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_TRADE_HANDLER_HPP
22
23#include <optional>
24#include <boost/uuid/string_generator.hpp>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.trading.api/messaging/trade_protocol.hpp"
33#include "ores.trading.core/service/activity_type_service.hpp"
34#include "ores.trading.core/service/trade_service.hpp"
35#include "ores.utility/uuid/tenant_id.hpp"
36
37namespace ores::trading::messaging {
38
39namespace {
40inline auto& trade_handler_lg() {
41 static auto instance = ores::logging::make_logger(
42 "ores.trading.messaging.trade_handler");
43 return instance;
44}
45} // namespace
46
47using ores::service::messaging::reply;
48using ores::service::messaging::decode;
49using ores::service::messaging::error_reply;
50using ores::service::messaging::has_permission;
51using namespace ores::logging;
52
53class trade_handler {
54public:
55 trade_handler(ores::nats::service::client& nats,
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
59
60 void list_activity_types(ores::nats::message msg) {
61 BOOST_LOG_SEV(trade_handler_lg(), debug)
62 << "Handling " << msg.subject;
63 auto req_ctx_expected = ores::service::service::make_request_context(
64 ctx_, msg, verifier_);
65 if (!req_ctx_expected) {
66 error_reply(nats_, msg, req_ctx_expected.error());
67 return;
68 }
69 const auto& req_ctx = *req_ctx_expected;
70 // Activity types are system-level configuration; look them up under
71 // the system tenant so all tenants see the same standard set.
72 const auto sys_ctx = req_ctx.with_tenant(
74 service::activity_type_service svc(sys_ctx);
75 get_activity_types_response resp;
76 try {
77 resp.activity_types = svc.list_types();
78 } catch (...) {}
79 BOOST_LOG_SEV(trade_handler_lg(), debug)
80 << "Completed " << msg.subject;
81 reply(nats_, msg, resp);
82 }
83
84 void list(ores::nats::message msg) {
85 BOOST_LOG_SEV(trade_handler_lg(), debug)
86 << "Handling " << msg.subject;
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, verifier_);
89 if (!ctx_expected) {
90 error_reply(nats_, msg, ctx_expected.error());
91 return;
92 }
93 const auto& ctx = *ctx_expected;
94 service::trade_service svc(ctx);
95 get_trades_response resp;
96 try {
97 if (auto req = decode<get_trades_request>(msg)) {
98 const auto offset =
99 static_cast<std::uint32_t>(req->offset);
100 const auto limit =
101 static_cast<std::uint32_t>(req->limit);
102 if (!req->book_id.empty()) {
103 boost::uuids::string_generator gen;
104 const auto book_uuid = gen(req->book_id);
105 resp.trades = svc.list_trades_filtered(
106 offset, limit,
107 std::optional<boost::uuids::uuid>(book_uuid),
108 std::nullopt);
109 resp.total_available_count =
110 static_cast<int>(svc.count_trades_filtered(
111 std::optional<boost::uuids::uuid>(book_uuid),
112 std::nullopt));
113 } else {
114 resp.trades = svc.list_trades(offset, limit);
115 resp.total_available_count =
116 static_cast<int>(svc.count_trades());
117 }
118 }
119 } catch (...) {}
120 BOOST_LOG_SEV(trade_handler_lg(), debug)
121 << "Completed " << msg.subject;
122 reply(nats_, msg, resp);
123 }
124
125 void save(ores::nats::message msg) {
126 BOOST_LOG_SEV(trade_handler_lg(), debug)
127 << "Handling " << msg.subject;
128 auto ctx_expected = ores::service::service::make_request_context(
129 ctx_, msg, verifier_);
130 if (!ctx_expected) {
131 error_reply(nats_, msg, ctx_expected.error());
132 return;
133 }
134 const auto& ctx = *ctx_expected;
135 if (!has_permission(ctx, "trading::trades:write")) {
136 error_reply(nats_, msg, ores::service::error_code::forbidden);
137 return;
138 }
139 service::trade_service svc(ctx);
140 if (auto req = decode<save_trade_request>(msg)) {
141 try {
142 svc.save_trades(req->trades);
143 BOOST_LOG_SEV(trade_handler_lg(), debug)
144 << "Completed " << msg.subject;
145 reply(nats_, msg, save_trade_response{.success = true});
146 } catch (const std::exception& e) {
147 BOOST_LOG_SEV(trade_handler_lg(), error)
148 << msg.subject << " failed: " << e.what();
149 reply(nats_, msg, save_trade_response{
150 .success = false, .message = e.what()});
151 }
152 } else {
153 BOOST_LOG_SEV(trade_handler_lg(), warn)
154 << "Failed to decode: " << msg.subject;
155 }
156 }
157
158 void remove(ores::nats::message msg) {
159 BOOST_LOG_SEV(trade_handler_lg(), debug)
160 << "Handling " << msg.subject;
161 auto ctx_expected = ores::service::service::make_request_context(
162 ctx_, msg, verifier_);
163 if (!ctx_expected) {
164 error_reply(nats_, msg, ctx_expected.error());
165 return;
166 }
167 const auto& ctx = *ctx_expected;
168 if (!has_permission(ctx, "trading::trades:delete")) {
169 error_reply(nats_, msg, ores::service::error_code::forbidden);
170 return;
171 }
172 service::trade_service svc(ctx);
173 if (auto req = decode<delete_trade_request>(msg)) {
174 try {
175 for (const auto& id : req->ids)
176 svc.remove_trade(id);
177 BOOST_LOG_SEV(trade_handler_lg(), debug)
178 << "Completed " << msg.subject;
179 reply(nats_, msg, delete_trade_response{.success = true});
180 } catch (const std::exception& e) {
181 BOOST_LOG_SEV(trade_handler_lg(), error)
182 << msg.subject << " failed: " << e.what();
183 reply(nats_, msg, delete_trade_response{
184 .success = false, .message = e.what()});
185 }
186 } else {
187 BOOST_LOG_SEV(trade_handler_lg(), warn)
188 << "Failed to decode: " << msg.subject;
189 }
190 }
191
192 void history(ores::nats::message msg) {
193 BOOST_LOG_SEV(trade_handler_lg(), debug)
194 << "Handling " << msg.subject;
195 auto ctx_expected = ores::service::service::make_request_context(
196 ctx_, msg, verifier_);
197 if (!ctx_expected) {
198 error_reply(nats_, msg, ctx_expected.error());
199 return;
200 }
201 const auto& ctx = *ctx_expected;
202 service::trade_service svc(ctx);
203 if (auto req = decode<get_trade_history_request>(msg)) {
204 try {
205 auto versions = svc.get_trade_history(req->id);
206 BOOST_LOG_SEV(trade_handler_lg(), debug)
207 << "Completed " << msg.subject;
208 reply(nats_, msg, get_trade_history_response{
209 .success = true,
210 .versions = std::move(versions)});
211 } catch (const std::exception& e) {
212 BOOST_LOG_SEV(trade_handler_lg(), error)
213 << msg.subject << " failed: " << e.what();
214 reply(nats_, msg, get_trade_history_response{
215 .success = false, .message = e.what()});
216 }
217 } else {
218 BOOST_LOG_SEV(trade_handler_lg(), warn)
219 << "Failed to decode: " << msg.subject;
220 }
221 }
222
223private:
226 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
227};
228
229} // namespace ores::trading::messaging
230
231#endif
STL namespace.
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73
static tenant_id system()
Creates a tenant_id representing the system tenant.
Definition tenant_id.cpp:41