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<ripple::Currency, ripple::STAmount> sums;
54 std::map<ripple::AccountID, std::vector<ripple::STAmount>> hotBalances;
55 std::map<ripple::AccountID, std::vector<ripple::STAmount>> assets;
56 std::map<ripple::AccountID, std::vector<ripple::STAmount>> frozenBalances;
57 std::map<ripple::Currency, ripple::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<ripple::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 =
103 [](auto const& j) -> std::optional<ripple::AccountID> {
104 if (j.is_string()) {
106 ripple::TokenType::AccountPublic,
107 boost::json::value_to<std::string>(j)
108 );
109
110 if (pk)
111 return ripple::calcAccountID(*pk);
112
114 boost::json::value_to<std::string>(j)
115 );
116 }
117
118 return {};
119 };
120
121 for (auto const& wallet : wallets) {
122 if (!getAccountID(wallet))
123 return Error{Status{errCode, std::string(key) + "Malformed"}};
124 }
125
126 return MaybeError{};
127 }
128 };
129 };
130
131 static auto const kSPEC_COMMON = RpcSpec{
135 };
136
137 static auto const kSPEC_V1 = RpcSpec{
138 kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_HOTWALLET)}}
139 };
140 static auto const kSPEC_V2 = RpcSpec{
141 kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_PARAMS)}}
142 };
143
144 return apiVersion == 1 ? kSPEC_V1 : kSPEC_V2;
145 }
146
154 Result
155 process(Input const& input, Context const& ctx) const;
156
157private:
164 friend void
165 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
166
173 friend Input
174 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
175};
176
177} // 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:170
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:40
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: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
ripple::error_code_i RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:54
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:65
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