Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Subscribe.hpp
1#pragma once
2
3#include "data/AmendmentCenterInterface.hpp"
4#include "data/BackendInterface.hpp"
5#include "feed/SubscriptionManagerInterface.hpp"
6#include "feed/Types.hpp"
7#include "rpc/common/Types.hpp"
8
9#include <boost/asio/spawn.hpp>
10#include <boost/json/array.hpp>
11#include <boost/json/conversion.hpp>
12#include <boost/json/object.hpp>
13#include <boost/json/value.hpp>
14#include <rpcspec/HandlerFor.hpp>
15#include <rpcspec/handlers/subscribe/Types.hpp>
16#include <xrpl/protocol/AccountID.h>
17
18#include <expected>
19#include <memory>
20#include <optional>
21#include <vector>
22
23namespace rpc {
24
31
32class SubscribeHandler : public rpc::spec::HandlerFor<rpc::spec::handlers::subscribe::Input> {
33 std::shared_ptr<BackendInterface> sharedPtrBackend_;
34 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
35 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
36
37public:
41 struct Output {
42 // response of stream "ledger"
43 // TODO: use better type than json, this type will be used in the stream as well
44 std::optional<boost::json::object> ledger;
45 // books returns nothing by default, if snapshot is true and both is false, offers go to
46 // offers list
47 // TODO: use better type than json
48 std::optional<boost::json::array> offers;
49 // if snapshot is true and both is true, reversed book' offers go to asks list
50 std::optional<boost::json::array> asks;
51 // if snapshot is true and both is true, original book' offers go to bids list
52 std::optional<boost::json::array> bids;
53 };
54
58 using OrderBook = rpc::spec::handlers::subscribe::OrderBook;
59
63 using StreamType = rpc::spec::handlers::subscribe::StreamType;
64
65 using Result = HandlerReturnType<Output>;
66
75 std::shared_ptr<BackendInterface> sharedPtrBackend,
76 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
77 std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions
78 );
79
87 [[nodiscard]] Result
88 process(Input const& input, Context const& ctx) const;
89
90private:
91 [[nodiscard]] boost::json::object
92 subscribeToStreams(
93 boost::asio::yield_context yield,
94 std::vector<StreamType> const& streams,
95 feed::SubscriberSharedPtr const& session
96 ) const;
97
98 void
99 subscribeToAccounts(
100 std::vector<xrpl::AccountID> const& accounts,
101 feed::SubscriberSharedPtr const& session
102 ) const;
103
104 void
105 subscribeToAccountsProposed(
106 std::vector<xrpl::AccountID> const& accounts,
107 feed::SubscriberSharedPtr const& session
108 ) const;
109
110 void
111 subscribeToBooks(
112 std::vector<OrderBook> const& books,
113 feed::SubscriberSharedPtr const& session,
114 boost::asio::yield_context yield,
115 Output& output
116 ) const;
117
124 friend void
125 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
126};
127
128} // namespace rpc
rpc::spec::handlers::subscribe::StreamType StreamType
A subscribable stream type.
Definition Subscribe.hpp:63
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert output to json value.
Definition Subscribe.cpp:194
Result process(Input const &input, Context const &ctx) const
Process the Subscribe command.
Definition Subscribe.cpp:44
SubscribeHandler(std::shared_ptr< BackendInterface > sharedPtrBackend, std::shared_ptr< data::AmendmentCenterInterface const > const &amendmentCenter, std::shared_ptr< feed::SubscriptionManagerInterface > const &subscriptions)
Construct a new BaseSubscribeHandler object.
Definition Subscribe.cpp:32
rpc::spec::handlers::subscribe::OrderBook OrderBook
A struct to hold the data for one order book.
Definition Subscribe.hpp:58
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:61
Context of an RPC call.
Definition Types.hpp:98
A struct to hold the output data of the command.
Definition Subscribe.hpp:41