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/SpecBackend.hpp"
8#include "rpc/common/Types.hpp"
9
10#include <boost/asio/spawn.hpp>
11#include <boost/json/array.hpp>
12#include <boost/json/conversion.hpp>
13#include <boost/json/object.hpp>
14#include <boost/json/value.hpp>
15#include <rpcspec/handlers/subscribe/Types.hpp>
16#include <xrpl/protocol/AccountID.h>
17#include <xrpl/protocol/UintTypes.h>
18
19#include <expected>
20#include <memory>
21#include <optional>
22#include <vector>
23
24namespace rpc {
25
32
33class SubscribeHandler : public rpc::HandlerFor<rpc::spec::handlers::subscribe::Input> {
34 std::shared_ptr<BackendInterface> sharedPtrBackend_;
35 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
36 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
37
38public:
42 struct Output {
43 // response of stream "ledger"
44 // TODO: use better type than json, this type will be used in the stream as well
45 std::optional<boost::json::object> ledger;
46 // books returns nothing by default, if snapshot is true and both is false, offers go to
47 // offers list
48 // TODO: use better type than json
49 std::optional<boost::json::array> offers;
50 // if snapshot is true and both is true, reversed book' offers go to asks list
51 std::optional<boost::json::array> asks;
52 // if snapshot is true and both is true, original book' offers go to bids list
53 std::optional<boost::json::array> bids;
54 };
55
59 using OrderBook = rpc::spec::handlers::subscribe::OrderBook;
60
64 using StreamType = rpc::spec::handlers::subscribe::StreamType;
65
66 using Result = HandlerReturnType<Output>;
67
76 std::shared_ptr<BackendInterface> sharedPtrBackend,
77 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
78 std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions
79 );
80
88 [[nodiscard]] Result
89 process(Input const& input, Context const& ctx) const;
90
91private:
92 [[nodiscard]] boost::json::object
93 subscribeToStreams(
94 boost::asio::yield_context yield,
95 std::vector<StreamType> const& streams,
96 feed::SubscriberSharedPtr const& session
97 ) const;
98
99 void
100 subscribeToAccounts(
101 std::vector<xrpl::AccountID> const& accounts,
102 feed::SubscriberSharedPtr const& session
103 ) const;
104
105 void
106 subscribeToAccountsProposed(
107 std::vector<xrpl::AccountID> const& accounts,
108 feed::SubscriberSharedPtr const& session
109 ) const;
110
111 void
112 subscribeToMPTIssuances(
113 std::vector<xrpl::MPTID> const& mptIssuanceIDs,
114 feed::SubscriberSharedPtr const& session
115 ) const;
116
117 void
118 subscribeToBooks(
119 std::vector<OrderBook> const& books,
120 feed::SubscriberSharedPtr const& session,
121 boost::asio::yield_context yield,
122 Output& output
123 ) const;
124
131 friend void
132 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
133};
134
135} // namespace rpc
rpc::spec::handlers::subscribe::StreamType StreamType
A subscribable stream type.
Definition Subscribe.hpp:64
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert output to json value.
Definition Subscribe.cpp:208
Result process(Input const &input, Context const &ctx) const
Process the Subscribe command.
Definition Subscribe.cpp:45
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:33
rpc::spec::handlers::subscribe::OrderBook OrderBook
A struct to hold the data for one order book.
Definition Subscribe.hpp:59
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
spec::HandlerFor< InputT, boost::json::value > HandlerFor
The spec-driven base every typed handler inherits.
Definition SpecBackend.hpp:24
Context of an RPC call.
Definition Types.hpp:98
A struct to hold the output data of the command.
Definition Subscribe.hpp:42