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/Specs.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 <boost/json/value_to.hpp>
16#include <xrpl/beast/utility/Zero.h>
17#include <xrpl/protocol/Book.h>
18#include <xrpl/protocol/ErrorCodes.h>
19#include <xrpl/protocol/jss.h>
20
21#include <cstdint>
22#include <memory>
23#include <optional>
24#include <string>
25#include <vector>
26
27namespace rpc {
28
35
37 std::shared_ptr<BackendInterface> sharedPtrBackend_;
38 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
39 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
40
41public:
45 struct Output {
46 // response of stream "ledger"
47 // TODO: use better type than json, this type will be used in the stream as well
48 std::optional<boost::json::object> ledger;
49 // books returns nothing by default, if snapshot is true and both is false, offers go to
50 // offers list
51 // TODO: use better type than json
52 std::optional<boost::json::array> offers;
53 // if snapshot is true and both is true, reversed book' offers go to asks list
54 std::optional<boost::json::array> asks;
55 // if snapshot is true and both is true, original book' offers go to bids list
56 std::optional<boost::json::array> bids;
57 };
58
62 struct OrderBook {
63 ripple::Book book;
64 std::optional<std::string> taker;
65 bool snapshot = false;
66 bool both = false;
67 };
68
72 struct Input {
73 std::optional<std::vector<std::string>> accounts;
74 std::optional<std::vector<std::string>> streams;
75 std::optional<std::vector<std::string>> accountsProposed;
76 std::optional<std::vector<OrderBook>> books;
77 };
78
79 using Result = HandlerReturnType<Output>;
80
89 std::shared_ptr<BackendInterface> sharedPtrBackend,
90 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
91 std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions
92 );
93
100 static RpcSpecConstRef
101 spec([[maybe_unused]] uint32_t apiVersion);
102
110 Result
111 process(Input const& input, Context const& ctx) const;
112
113private:
114 boost::json::object
115 subscribeToStreams(
116 boost::asio::yield_context yield,
117 std::vector<std::string> const& streams,
118 feed::SubscriberSharedPtr const& session
119 ) const;
120
121 void
122 subscribeToAccounts(
123 std::vector<std::string> const& accounts,
124 feed::SubscriberSharedPtr const& session
125 ) const;
126
127 void
128 subscribeToAccountsProposed(
129 std::vector<std::string> const& accounts,
130 feed::SubscriberSharedPtr const& session
131 ) const;
132
133 void
134 subscribeToBooks(
135 std::vector<OrderBook> const& books,
136 feed::SubscriberSharedPtr const& session,
137 boost::asio::yield_context yield,
138 Output& output
139 ) const;
140
147 friend void
148 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
149
156 friend Input
157 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
158};
159
160} // namespace rpc
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition Subscribe.cpp:50
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert output to json value.
Definition Subscribe.cpp:250
Result process(Input const &input, Context const &ctx) const
Process the Subscribe command.
Definition Subscribe.cpp:108
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:38
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:18
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:130
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:62
Context of an RPC call.
Definition Types.hpp:99
A struct to hold the input data for the command.
Definition Subscribe.hpp:72
A struct to hold the data for one order book.
Definition Subscribe.hpp:62
A struct to hold the output data of the command.
Definition Subscribe.hpp:45