ORE Studio 0.0.4
Loading...
Searching...
No Matches
instrument_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_INSTRUMENT_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_INSTRUMENT_HANDLER_HPP
22
23#include <optional>
24#include "ores.logging/make_logger.hpp"
25#include "ores.nats/domain/message.hpp"
26#include "ores.nats/service/client.hpp"
27#include "ores.database/domain/context.hpp"
28#include "ores.security/jwt/jwt_authenticator.hpp"
29#include "ores.service/messaging/handler_helpers.hpp"
30#include "ores.service/service/request_context.hpp"
31#include "ores.trading.api/messaging/instrument_protocol.hpp"
32#include "ores.trading.core/service/instrument_service.hpp"
33
34namespace ores::trading::messaging {
35
36namespace {
37inline auto& instrument_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.trading.messaging.instrument_handler");
40 return instance;
41}
42} // namespace
43
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
48using namespace ores::logging;
49
50class instrument_handler {
51public:
52 instrument_handler(ores::nats::service::client& nats,
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
56
57 void list(ores::nats::message msg) {
58 BOOST_LOG_SEV(instrument_handler_lg(), debug)
59 << "Handling " << msg.subject;
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
62 if (!ctx_expected) {
63 error_reply(nats_, msg, ctx_expected.error());
64 return;
65 }
66 const auto& ctx = *ctx_expected;
67 service::instrument_service svc(ctx);
68 get_instruments_response resp;
69 try {
70 if (auto req = decode<get_instruments_request>(msg)) {
71 const auto offset =
72 static_cast<std::uint32_t>(req->offset);
73 const auto limit =
74 static_cast<std::uint32_t>(req->limit);
75 resp.instruments = svc.list_instruments(offset, limit);
76 resp.total_available_count =
77 static_cast<int>(svc.count_instruments());
78 }
79 } catch (const std::exception& e) {
80 BOOST_LOG_SEV(instrument_handler_lg(), error)
81 << msg.subject << " failed: " << e.what();
82 resp.success = false;
83 resp.message = e.what();
84 }
85 BOOST_LOG_SEV(instrument_handler_lg(), debug)
86 << "Completed " << msg.subject;
87 reply(nats_, msg, resp);
88 }
89
90 void save(ores::nats::message msg) {
91 BOOST_LOG_SEV(instrument_handler_lg(), debug)
92 << "Handling " << msg.subject;
93 auto ctx_expected = ores::service::service::make_request_context(
94 ctx_, msg, verifier_);
95 if (!ctx_expected) {
96 error_reply(nats_, msg, ctx_expected.error());
97 return;
98 }
99 const auto& ctx = *ctx_expected;
100 if (!has_permission(ctx, "trading::instruments:write")) {
101 error_reply(nats_, msg, ores::service::error_code::forbidden);
102 return;
103 }
104 service::instrument_service svc(ctx);
105 if (auto req = decode<save_instrument_request>(msg)) {
106 try {
107 svc.save_instrument(req->data, req->legs);
108 BOOST_LOG_SEV(instrument_handler_lg(), debug)
109 << "Completed " << msg.subject;
110 reply(nats_, msg, save_instrument_response{.success = true});
111 } catch (const std::exception& e) {
112 BOOST_LOG_SEV(instrument_handler_lg(), error)
113 << msg.subject << " failed: " << e.what();
114 reply(nats_, msg, save_instrument_response{
115 .success = false, .message = e.what()});
116 }
117 } else {
118 BOOST_LOG_SEV(instrument_handler_lg(), warn)
119 << "Failed to decode: " << msg.subject;
120 }
121 }
122
123 void remove(ores::nats::message msg) {
124 BOOST_LOG_SEV(instrument_handler_lg(), debug)
125 << "Handling " << msg.subject;
126 auto ctx_expected = ores::service::service::make_request_context(
127 ctx_, msg, verifier_);
128 if (!ctx_expected) {
129 error_reply(nats_, msg, ctx_expected.error());
130 return;
131 }
132 const auto& ctx = *ctx_expected;
133 if (!has_permission(ctx, "trading::instruments:delete")) {
134 error_reply(nats_, msg, ores::service::error_code::forbidden);
135 return;
136 }
137 service::instrument_service svc(ctx);
138 if (auto req = decode<delete_instrument_request>(msg)) {
139 try {
140 for (const auto& id : req->ids)
141 svc.remove_instrument(id);
142 BOOST_LOG_SEV(instrument_handler_lg(), debug)
143 << "Completed " << msg.subject;
144 reply(nats_, msg, delete_instrument_response{.success = true});
145 } catch (const std::exception& e) {
146 BOOST_LOG_SEV(instrument_handler_lg(), error)
147 << msg.subject << " failed: " << e.what();
148 reply(nats_, msg, delete_instrument_response{
149 .success = false, .message = e.what()});
150 }
151 } else {
152 BOOST_LOG_SEV(instrument_handler_lg(), warn)
153 << "Failed to decode: " << msg.subject;
154 }
155 }
156
157 void history(ores::nats::message msg) {
158 BOOST_LOG_SEV(instrument_handler_lg(), debug)
159 << "Handling " << msg.subject;
160 auto ctx_expected = ores::service::service::make_request_context(
161 ctx_, msg, verifier_);
162 if (!ctx_expected) {
163 error_reply(nats_, msg, ctx_expected.error());
164 return;
165 }
166 const auto& ctx = *ctx_expected;
167 service::instrument_service svc(ctx);
168 if (auto req = decode<get_instrument_history_request>(msg)) {
169 try {
170 auto versions = svc.get_instrument_history(req->id);
171 BOOST_LOG_SEV(instrument_handler_lg(), debug)
172 << "Completed " << msg.subject;
173 reply(nats_, msg, get_instrument_history_response{
174 .success = true,
175 .history = std::move(versions)});
176 } catch (const std::exception& e) {
177 BOOST_LOG_SEV(instrument_handler_lg(), error)
178 << msg.subject << " failed: " << e.what();
179 reply(nats_, msg, get_instrument_history_response{
180 .success = false, .message = e.what()});
181 }
182 } else {
183 BOOST_LOG_SEV(instrument_handler_lg(), warn)
184 << "Failed to decode: " << msg.subject;
185 }
186 }
187
188 void list_legs(ores::nats::message msg) {
189 BOOST_LOG_SEV(instrument_handler_lg(), debug)
190 << "Handling " << msg.subject;
191 auto ctx_expected = ores::service::service::make_request_context(
192 ctx_, msg, verifier_);
193 if (!ctx_expected) {
194 error_reply(nats_, msg, ctx_expected.error());
195 return;
196 }
197 const auto& ctx = *ctx_expected;
198 service::instrument_service svc(ctx);
199 get_swap_legs_response resp;
200 try {
201 if (auto req = decode<get_swap_legs_request>(msg)) {
202 resp.legs = svc.get_legs(req->instrument_id);
203 }
204 } catch (const std::exception& e) {
205 BOOST_LOG_SEV(instrument_handler_lg(), error)
206 << msg.subject << " failed: " << e.what();
207 resp.success = false;
208 resp.message = e.what();
209 }
210 BOOST_LOG_SEV(instrument_handler_lg(), debug)
211 << "Completed " << msg.subject;
212 reply(nats_, msg, resp);
213 }
214
215private:
218 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
219};
220
221} // namespace ores::trading::messaging
222
223#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