Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Subscribe.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/AmendmentCenterInterface.hpp"
23#include "data/BackendInterface.hpp"
24#include "feed/SubscriptionManagerInterface.hpp"
25#include "feed/Types.hpp"
26#include "rpc/common/Specs.hpp"
27#include "rpc/common/Types.hpp"
28
29#include <boost/asio/spawn.hpp>
30#include <boost/json/array.hpp>
31#include <boost/json/conversion.hpp>
32#include <boost/json/object.hpp>
33#include <boost/json/value.hpp>
34#include <boost/json/value_to.hpp>
35#include <xrpl/beast/utility/Zero.h>
36#include <xrpl/protocol/Book.h>
37#include <xrpl/protocol/ErrorCodes.h>
38#include <xrpl/protocol/jss.h>
39
40#include <cstdint>
41#include <memory>
42#include <optional>
43#include <string>
44#include <vector>
45
46namespace rpc {
47
54
56 std::shared_ptr<BackendInterface> sharedPtrBackend_;
57 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
58 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
59
60public:
64 struct Output {
65 // response of stream "ledger"
66 // TODO: use better type than json, this type will be used in the stream as well
67 std::optional<boost::json::object> ledger;
68 // books returns nothing by default, if snapshot is true and both is false, offers go to
69 // offers list
70 // TODO: use better type than json
71 std::optional<boost::json::array> offers;
72 // if snapshot is true and both is true, reversed book' offers go to asks list
73 std::optional<boost::json::array> asks;
74 // if snapshot is true and both is true, original book' offers go to bids list
75 std::optional<boost::json::array> bids;
76 };
77
81 struct OrderBook {
82 ripple::Book book;
83 std::optional<std::string> taker;
84 bool snapshot = false;
85 bool both = false;
86 };
87
91 struct Input {
92 std::optional<std::vector<std::string>> accounts;
93 std::optional<std::vector<std::string>> streams;
94 std::optional<std::vector<std::string>> accountsProposed;
95 std::optional<std::vector<OrderBook>> books;
96 };
97
98 using Result = HandlerReturnType<Output>;
99
108 std::shared_ptr<BackendInterface> const& sharedPtrBackend,
109 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
110 std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions
111 );
112
119 static RpcSpecConstRef
120 spec([[maybe_unused]] uint32_t apiVersion);
121
129 Result
130 process(Input const& input, Context const& ctx) const;
131
132private:
133 boost::json::object
134 subscribeToStreams(
135 boost::asio::yield_context yield,
136 std::vector<std::string> const& streams,
137 feed::SubscriberSharedPtr const& session
138 ) const;
139
140 void
141 subscribeToAccounts(
142 std::vector<std::string> const& accounts,
143 feed::SubscriberSharedPtr const& session
144 ) const;
145
146 void
147 subscribeToAccountsProposed(
148 std::vector<std::string> const& accounts,
149 feed::SubscriberSharedPtr const& session
150 ) const;
151
152 void
153 subscribeToBooks(
154 std::vector<OrderBook> const& books,
155 feed::SubscriberSharedPtr const& session,
156 boost::asio::yield_context yield,
157 Output& output
158 ) const;
159
166 friend void
167 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
168
175 friend Input
176 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
177};
178
179} // namespace rpc
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition Subscribe.cpp:68
SubscribeHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend, std::shared_ptr< data::AmendmentCenterInterface const > const &amendmentCenter, std::shared_ptr< feed::SubscriptionManagerInterface > const &subscriptions)
Construct a new BaseSubscribeHandler object.
Definition Subscribe.cpp:56
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert output to json value.
Definition Subscribe.cpp:266
Result process(Input const &input, Context const &ctx) const
Process the Subscribe command.
Definition Subscribe.cpp:124
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:150
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:81
Context of an RPC call.
Definition Types.hpp:118
A struct to hold the input data for the command.
Definition Subscribe.hpp:91
A struct to hold the data for one order book.
Definition Subscribe.hpp:81
A struct to hold the output data of the command.
Definition Subscribe.hpp:64