Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
GetAggregatePrice.hpp
1#pragma once
2
3#include "data/BackendInterface.hpp"
4#include "rpc/Errors.hpp"
5#include "rpc/JS.hpp"
6#include "rpc/common/MetaProcessors.hpp"
7#include "rpc/common/Modifiers.hpp"
8#include "rpc/common/Specs.hpp"
9#include "rpc/common/Types.hpp"
10#include "rpc/common/Validators.hpp"
11
12#include <boost/asio/spawn.hpp>
13#include <boost/json/array.hpp>
14#include <boost/json/conversion.hpp>
15#include <xrpl/basics/Number.h>
16#include <xrpl/protocol/AccountID.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/STObject.h>
19#include <xrpl/protocol/jss.h>
20
21#include <cstdint>
22#include <functional>
23#include <memory>
24#include <optional>
25#include <string>
26#include <string_view>
27#include <vector>
28
29namespace rpc {
30
35 std::shared_ptr<BackendInterface> sharedPtrBackend_;
36
37public:
41 struct Stats {
42 ripple::STAmount avg{}; // NOLINT(readability-redundant-member-init)
43 // standard deviation
44 ripple::Number sd{}; // NOLINT(readability-redundant-member-init)
45 uint32_t size{0};
46 };
47
51 struct Output {
52 uint32_t time;
53 Stats extireStats{};
54 std::optional<Stats> trimStats;
55 std::string ledgerHash;
56 uint32_t ledgerIndex;
57 std::string median;
58 bool validated = true;
59 };
60
64 struct Oracle {
65 std::uint32_t documentId{0};
66 ripple::AccountID account;
67 };
68
72 struct Input {
73 std::optional<std::string> ledgerHash;
74 std::optional<std::uint32_t> ledgerIndex;
75 std::vector<Oracle> oracles; // valid range is 1-200
76 std::string baseAsset;
77 std::string quoteAsset;
78 std::optional<std::uint32_t> timeThreshold;
79 std::optional<std::uint8_t> trim; // valid range is 1-25
80 };
81
82 using Result = HandlerReturnType<Output>;
83
89 GetAggregatePriceHandler(std::shared_ptr<BackendInterface> sharedPtrBackend)
90 : sharedPtrBackend_(std::move(sharedPtrBackend))
91 {
92 }
93
100 static RpcSpecConstRef
101 spec([[maybe_unused]] uint32_t apiVersion)
102 {
103 static constexpr auto kORACLES_MAX = 200;
104
105 static auto const kORACLES_VALIDATOR = modifiers::CustomModifier{
106 [](boost::json::value& value, std::string_view) -> MaybeError {
107 if (!value.is_array() or value.as_array().empty() or
108 value.as_array().size() > kORACLES_MAX)
109 return Error{Status{RippledError::rpcORACLE_MALFORMED}};
110
111 for (auto& oracle : value.as_array()) {
112 if (!oracle.is_object() or
113 !oracle.as_object().contains(JS(oracle_document_id)) or
114 !oracle.as_object().contains(JS(account)))
115 return Error{Status{RippledError::rpcORACLE_MALFORMED}};
116
118 oracle, JS(oracle_document_id)
119 );
120 if (!maybeError)
121 return maybeError;
122
123 maybeError = modifiers::ToNumber::modify(oracle, JS(oracle_document_id));
124 if (!maybeError)
125 return maybeError;
126
128 oracle.as_object(), JS(account)
129 );
130 if (!maybeError)
131 return Error{Status{RippledError::rpcINVALID_PARAMS}};
132 };
133
134 return MaybeError{};
135 }
136 };
137
138 static auto const kRPC_SPEC = RpcSpec{
141 // validate quoteAsset and base_asset in accordance to the currency code found in XRPL
142 // doc:
143 // https://xrpl.org/docs/references/protocol/data-types/currency-formats#currency-codes
144 // usually Clio returns rpcMALFORMED_CURRENCY , return InvalidParam here just to mimic
145 // rippled
146 {JS(base_asset),
150 Status(RippledError::rpcINVALID_PARAMS)
151 }},
152 {JS(quote_asset),
156 Status(RippledError::rpcINVALID_PARAMS)
157 }},
158 {JS(oracles), validation::Required{}, kORACLES_VALIDATOR},
159 // note: Unlike `rippled`, Clio only supports UInt as input, no string, no `null`, etc.
160 {JS(time_threshold), validation::Type<std::uint32_t>{}},
161 {
162 JS(trim),
165 }
166 };
167
168 return kRPC_SPEC;
169 }
170
178 Result
179 process(Input const& input, Context const& ctx) const;
180
181private:
187 void
188 tracebackOracleObject(
189 boost::asio::yield_context yield,
190 ripple::STObject const& oracleObject,
191 std::function<bool(ripple::STObject const&)> const& callback
192 ) const;
193
200 friend void
201 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
202
209 friend Input
210 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
211};
212
213} // namespace rpc
GetAggregatePriceHandler(std::shared_ptr< BackendInterface > sharedPtrBackend)
Construct a new GetAggregatePrice handler object.
Definition GetAggregatePrice.hpp:89
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition GetAggregatePrice.hpp:101
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert the Output to a JSON object.
Definition GetAggregatePrice.cpp:290
Result process(Input const &input, Context const &ctx) const
Process the GetAggregatePrice command.
Definition GetAggregatePrice.cpp:43
A meta-processor that wraps a validator and produces a custom error in case the wrapped validator fai...
Definition MetaProcessors.hpp:152
Customised modifier allowing user define how to modify input in provided callable.
Definition Modifiers.hpp:127
Validate that value is between specified min and max.
Definition Validators.hpp:159
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
std::expected< void, Status > MaybeError
Return type used for Validators that can return error but don't have specific value to return.
Definition Types.hpp:36
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:56
Context of an RPC call.
Definition Types.hpp:99
A struct to hold the input data for the command.
Definition GetAggregatePrice.hpp:72
A struct to hold the input oracle data.
Definition GetAggregatePrice.hpp:64
A struct to hold the output data of the command.
Definition GetAggregatePrice.hpp:51
A struct to hold the statistics.
Definition GetAggregatePrice.hpp:41
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:110
Represents a Specification of an entire RPC command.
Definition Specs.hpp:82
A status returned from any RPC handler.
Definition Errors.hpp:65
static MaybeError modify(boost::json::value &value, std::string_view key)
Update the input string to integer if it can be converted to integer by stoi.
Definition Modifiers.hpp:103
static CustomValidator currencyValidator
Provides a commonly used validator for currency, including standard currency code and token code.
Definition Validators.hpp:557
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:489
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:551
static CustomValidator accountBase58Validator
Provides a commonly used validator for accounts.
Definition Validators.hpp:511
A validator that simply requires a field to be present.
Definition Validators.hpp:28
Validates that the type of the value is one of the given types.
Definition Validators.hpp:128
MaybeError verify(boost::json::value &value, std::string_view key) const
Verify that the JSON value is (one) of specified type(s).
Definition Validators.hpp:140