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/BackendInterface.hpp"
23#include "feed/SubscriptionManagerInterface.hpp"
24#include "feed/Types.hpp"
25#include "rpc/common/Specs.hpp"
26#include "rpc/common/Types.hpp"
27
28#include <boost/asio/spawn.hpp>
29#include <boost/json/array.hpp>
30#include <boost/json/conversion.hpp>
31#include <boost/json/object.hpp>
32#include <boost/json/value.hpp>
33#include <boost/json/value_to.hpp>
34#include <xrpl/beast/utility/Zero.h>
35#include <xrpl/protocol/Book.h>
36#include <xrpl/protocol/ErrorCodes.h>
37#include <xrpl/protocol/jss.h>
38
39#include <cstdint>
40#include <memory>
41#include <optional>
42#include <string>
43#include <vector>
44
45namespace rpc {
46
55 std::shared_ptr<BackendInterface> sharedPtrBackend_;
56 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
57
58public:
62 struct Output {
63 // response of stream "ledger"
64 // TODO: use better type than json, this type will be used in the stream as well
65 std::optional<boost::json::object> ledger;
66 // books returns nothing by default, if snapshot is true and both is false, offers go to offers list
67 // TODO: use better type than json
68 std::optional<boost::json::array> offers;
69 // if snapshot is true and both is true, reversed book' offers go to asks list
70 std::optional<boost::json::array> asks;
71 // if snapshot is true and both is true, original book' offers go to bids list
72 std::optional<boost::json::array> bids;
73 };
74
78 struct OrderBook {
79 ripple::Book book;
80 std::optional<std::string> taker;
81 bool snapshot = false;
82 bool both = false;
83 };
84
88 struct Input {
89 std::optional<std::vector<std::string>> accounts;
90 std::optional<std::vector<std::string>> streams;
91 std::optional<std::vector<std::string>> accountsProposed;
92 std::optional<std::vector<OrderBook>> books;
93 };
94
95 using Result = HandlerReturnType<Output>;
96
104 std::shared_ptr<BackendInterface> const& sharedPtrBackend,
105 std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions
106 );
107
114 static RpcSpecConstRef
115 spec([[maybe_unused]] uint32_t apiVersion);
116
124 Result
125 process(Input input, Context const& ctx) const;
126
127private:
128 boost::json::object
129 subscribeToStreams(
130 boost::asio::yield_context yield,
131 std::vector<std::string> const& streams,
132 feed::SubscriberSharedPtr const& session
133 ) const;
134
135 void
136 subscribeToAccounts(std::vector<std::string> const& accounts, feed::SubscriberSharedPtr const& session) const;
137
138 void
139 subscribeToAccountsProposed(std::vector<std::string> const& accounts, feed::SubscriberSharedPtr const& session)
140 const;
141
142 void
143 subscribeToBooks(
144 std::vector<OrderBook> const& books,
145 feed::SubscriberSharedPtr const& session,
146 boost::asio::yield_context yield,
147 Output& output
148 ) const;
149
156 friend void
157 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
158
165 friend Input
166 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
167};
168
169} // namespace rpc
Contains functionality for handling the subscribe command. The subscribe method requests periodic not...
Definition Subscribe.hpp:54
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition Subscribe.cpp:65
Result process(Input input, Context const &ctx) const
Process the Subscribe command.
Definition Subscribe.cpp:114
SubscribeHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend, 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:246
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:145
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:88
A struct to hold the data for one order book.
Definition Subscribe.hpp:78
A struct to hold the output data of the command.
Definition Subscribe.hpp:62