Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
GatewayBalances.hpp
1#pragma once
2
3#include "data/BackendInterface.hpp"
4#include "rpc/Errors.hpp"
5#include "rpc/JS.hpp"
6#include "rpc/common/Specs.hpp"
7#include "rpc/common/Types.hpp"
8#include "rpc/common/Validators.hpp"
9#include "util/AccountUtils.hpp"
10
11#include <boost/json/array.hpp>
12#include <boost/json/conversion.hpp>
13#include <boost/json/value.hpp>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/ErrorCodes.h>
16#include <xrpl/protocol/PublicKey.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/UintTypes.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/protocol/tokens.h>
21
22#include <cstdint>
23#include <map>
24#include <memory>
25#include <optional>
26#include <set>
27#include <string>
28#include <string_view>
29#include <vector>
30
31namespace rpc {
32
42 std::shared_ptr<BackendInterface> sharedPtrBackend_;
43
44public:
48 struct Output {
49 std::string ledgerHash;
50 uint32_t ledgerIndex;
51 std::string accountID;
52 bool overflow = false;
53 std::map<xrpl::Currency, xrpl::STAmount> sums;
54 std::map<xrpl::AccountID, std::vector<xrpl::STAmount>> hotBalances;
55 std::map<xrpl::AccountID, std::vector<xrpl::STAmount>> assets;
56 std::map<xrpl::AccountID, std::vector<xrpl::STAmount>> frozenBalances;
57 std::map<xrpl::Currency, xrpl::STAmount> locked;
58 // validated should be sent via framework
59 bool validated = true;
60 };
61
65 struct Input {
66 std::string account;
67 std::set<xrpl::AccountID> hotWallets;
68 std::optional<std::string> ledgerHash;
69 std::optional<uint32_t> ledgerIndex;
70 };
71
72 using Result = HandlerReturnType<Output>;
73
79 GatewayBalancesHandler(std::shared_ptr<BackendInterface> sharedPtrBackend)
80 : sharedPtrBackend_(std::move(sharedPtrBackend))
81 {
82 }
83
90 static RpcSpecConstRef
91 spec([[maybe_unused]] uint32_t apiVersion)
92 {
93 auto const getHotWalletValidator = [](RippledError errCode) {
95 [errCode](boost::json::value const& value, std::string_view key) -> MaybeError {
96 if (!value.is_string() && !value.is_array())
97 return Error{Status{errCode, std::string(key) + "NotStringOrArray"}};
98
99 // wallet needs to be an valid accountID or public key
100 auto const wallets =
101 value.is_array() ? value.as_array() : boost::json::array{value};
102 auto const getAccountID = [](auto const& j) -> std::optional<xrpl::AccountID> {
103 if (j.is_string()) {
105 xrpl::TokenType::AccountPublic,
106 boost::json::value_to<std::string>(j)
107 );
108
109 if (pk)
110 return xrpl::calcAccountID(*pk);
111
113 boost::json::value_to<std::string>(j)
114 );
115 }
116
117 return {};
118 };
119
120 for (auto const& wallet : wallets) {
121 if (!getAccountID(wallet))
122 return Error{Status{errCode, std::string(key) + "Malformed"}};
123 }
124
125 return MaybeError{};
126 }
127 };
128 };
129
130 static auto const kSpecCommon = RpcSpec{
134 };
135
136 static auto const kSpecV1 = RpcSpec{
137 kSpecCommon, {{JS(hotwallet), getHotWalletValidator(xrpl::RpcInvalidHotwallet)}}
138 };
139 static auto const kSpecV2 =
140 RpcSpec{kSpecCommon, {{JS(hotwallet), getHotWalletValidator(xrpl::RpcInvalidParams)}}};
141
142 return apiVersion == 1 ? kSpecV1 : kSpecV2;
143 }
144
152 [[nodiscard]] Result
153 process(Input const& input, Context const& ctx) const;
154
155private:
162 friend void
163 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
164
171 friend Input
172 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
173};
174
175} // namespace rpc
GatewayBalancesHandler(std::shared_ptr< BackendInterface > sharedPtrBackend)
Construct a new GatewayBalancesHandler object.
Definition GatewayBalances.hpp:79
friend void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, Output const &output)
Convert the Output to a JSON object.
Definition GatewayBalances.cpp:180
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition GatewayBalances.hpp:91
Result process(Input const &input, Context const &ctx) const
Process the GatewayBalances command.
Definition GatewayBalances.cpp:41
A meta-validator that allows to specify a custom validation function.
Definition Validators.hpp:423
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
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
xrpl::ErrorCodeI RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:60
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
std::optional< T > parseBase58Wrapper(std::string const &str)
A wrapper of parseBase58 function. It adds the check if all characters in the input string are alphan...
Definition AccountUtils.hpp:24
Context of an RPC call.
Definition Types.hpp:99
A struct to hold the input data for the command.
Definition GatewayBalances.hpp:65
A struct to hold the output data of the command.
Definition GatewayBalances.hpp:48
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:73
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:504
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
A validator that simply requires a field to be present.
Definition Validators.hpp:28