Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerIndex.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, 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#include "data/BackendInterface.hpp"
22#include "rpc/JS.hpp"
23#include "rpc/common/Specs.hpp"
24#include "rpc/common/Types.hpp"
25#include "rpc/common/Validators.hpp"
26
27#include <boost/json/conversion.hpp>
28#include <boost/json/value.hpp>
29#include <xrpl/protocol/jss.h>
30
31#include <cstdint>
32#include <memory>
33#include <optional>
34#include <string>
35
36namespace rpc {
37
43 std::shared_ptr<BackendInterface> sharedPtrBackend_;
44 static constexpr auto kDATE_FORMAT = "%Y-%m-%dT%TZ";
45
46public:
50 struct Output {
51 uint32_t ledgerIndex{};
52 std::string ledgerHash;
53 std::string closeTimeIso;
54 };
55
59 struct Input {
60 std::optional<std::string> date;
61 };
62
63 using Result = HandlerReturnType<Output>;
64
70 LedgerIndexHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend) : sharedPtrBackend_(sharedPtrBackend)
71 {
72 }
73
80 static RpcSpecConstRef
81 spec([[maybe_unused]] uint32_t apiVersion)
82 {
83 static auto const kRPC_SPEC = RpcSpec{
85 };
86 return kRPC_SPEC;
87 }
88
96 Result
97 process(Input input, Context const& ctx) const;
98
99private:
106 friend void
107 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
108
115 friend Input
116 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
117};
118
119} // namespace rpc
The ledger_index method fetches the lastest closed ledger before the given date.
Definition LedgerIndex.hpp:42
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert the Output to a JSON object.
Definition LedgerIndex.cpp:105
Result process(Input input, Context const &ctx) const
Process the LedgerIndex command.
Definition LedgerIndex.cpp:43
LedgerIndexHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend)
Construct a new LedgerIndexHandler object.
Definition LedgerIndex.hpp:70
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition LedgerIndex.hpp:81
Validate that value can be converted to time according to the given format.
Definition Validators.hpp:296
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 LedgerIndex.hpp:59
A struct to hold the output data of the command.
Definition LedgerIndex.hpp:50
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
Validates that the type of the value is one of the given types.
Definition Validators.hpp:142