Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
BookOffers.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 "rpc/Errors.hpp"
24#include "rpc/JS.hpp"
25#include "rpc/common/MetaProcessors.hpp"
26#include "rpc/common/Modifiers.hpp"
27#include "rpc/common/Specs.hpp"
28#include "rpc/common/Types.hpp"
29#include "rpc/common/Validators.hpp"
30
31#include <boost/json/array.hpp>
32#include <boost/json/conversion.hpp>
33#include <boost/json/object.hpp>
34#include <boost/json/value.hpp>
35#include <xrpl/protocol/AccountID.h>
36#include <xrpl/protocol/ErrorCodes.h>
37#include <xrpl/protocol/UintTypes.h>
38#include <xrpl/protocol/jss.h>
39
40#include <cstdint>
41#include <memory>
42#include <optional>
43#include <string>
44
45namespace rpc {
46
53 std::shared_ptr<BackendInterface> sharedPtrBackend_;
54
55public:
56 static constexpr auto kLIMIT_MIN = 1;
57 static constexpr auto kLIMIT_MAX = 100;
58 static constexpr auto kLIMIT_DEFAULT = 60;
59
63 struct Output {
64 std::string ledgerHash;
65 uint32_t ledgerIndex;
66 boost::json::array offers;
67 bool validated = true;
68 };
69
76 struct Input {
77 std::optional<std::string> ledgerHash;
78 std::optional<uint32_t> ledgerIndex;
79 uint32_t limit = kLIMIT_DEFAULT;
80 std::optional<ripple::AccountID> taker;
81 ripple::Currency paysCurrency;
82 ripple::Currency getsCurrency;
83 // accountID will be filled by input converter, if no issuer is given, will use XRP issuer
84 ripple::AccountID paysID = ripple::xrpAccount();
85 ripple::AccountID getsID = ripple::xrpAccount();
86 };
87
88 using Result = HandlerReturnType<Output>;
89
95 BookOffersHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend) : sharedPtrBackend_(sharedPtrBackend)
96 {
97 }
98
105 static RpcSpecConstRef
106 spec([[maybe_unused]] uint32_t apiVersion)
107 {
108 static auto const kRPC_SPEC = RpcSpec{
109 {JS(taker_gets),
113 {JS(currency),
116 validation::CustomValidators::currencyValidator, Status(RippledError::rpcDST_AMT_MALFORMED)
117 }},
118 {JS(issuer),
120 validation::CustomValidators::issuerValidator, Status(RippledError::rpcDST_ISR_MALFORMED)
121 }}
122 }},
123 {JS(taker_pays),
127 {JS(currency),
130 validation::CustomValidators::currencyValidator, Status(RippledError::rpcSRC_CUR_MALFORMED)
131 }},
132 {JS(issuer),
134 validation::CustomValidators::issuerValidator, Status(RippledError::rpcSRC_ISR_MALFORMED)
135 }}
136 }},
137 // return INVALID_PARAMS if account format is wrong for "taker"
138 {JS(taker),
141 Status(RippledError::rpcINVALID_PARAMS, "Invalid field 'taker'.")
142 }},
143 {JS(limit),
145 validation::Min(1u),
146 modifiers::Clamp<int32_t>{kLIMIT_MIN, kLIMIT_MAX}},
149 };
150
151 return kRPC_SPEC;
152 }
153
161 Result
162 process(Input input, Context const& ctx) const;
163
164private:
171 friend void
172 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
173
180 friend Input
181 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
182};
183} // namespace rpc
The book_offers method retrieves a list of Offers between two currencies, also known as an order book...
Definition BookOffers.hpp:52
Result process(Input input, Context const &ctx) const
Process the BookOffers command.
Definition BookOffers.cpp:47
BookOffersHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend)
Construct a new BookOffersHandler object.
Definition BookOffers.hpp:95
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert the Output to a JSON object.
Definition BookOffers.cpp:82
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition BookOffers.hpp:106
A meta-processor that acts as a spec for a sub-object/section.
Definition MetaProcessors.hpp:43
A meta-processor that wraps a validator and produces a custom error in case the wrapped validator fai...
Definition MetaProcessors.hpp:166
Clamp value between min and max.
Definition Modifiers.hpp:42
Validate that value is equal or greater than the specified min.
Definition Validators.hpp:215
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
A struct to hold the input data for the command.
Definition BookOffers.hpp:76
A struct to hold the output data of the command.
Definition BookOffers.hpp:63
Context of an RPC call.
Definition Types.hpp:118
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:129
Represents a Specification of an entire RPC command.
Definition Specs.hpp:98
A status returned from any RPC handler.
Definition Errors.hpp:82
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:491
static CustomValidator currencyValidator
Provides a commonly used validator for currency, including standard currency code and token code.
Definition Validators.hpp:535
static CustomValidator issuerValidator
Provides a commonly used validator for issuer type.
Definition Validators.hpp:542
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:484
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:530
A validator that simply requires a field to be present.
Definition Validators.hpp:47
Validates that the type of the value is one of the given types.
Definition Validators.hpp:142