Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
GatewayBalances.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/Specs.hpp"
26#include "rpc/common/Types.hpp"
27#include "rpc/common/Validators.hpp"
28#include "util/AccountUtils.hpp"
29
30#include <boost/json/array.hpp>
31#include <boost/json/conversion.hpp>
32#include <boost/json/value.hpp>
33#include <xrpl/protocol/AccountID.h>
34#include <xrpl/protocol/ErrorCodes.h>
35#include <xrpl/protocol/PublicKey.h>
36#include <xrpl/protocol/STAmount.h>
37#include <xrpl/protocol/UintTypes.h>
38#include <xrpl/protocol/jss.h>
39#include <xrpl/protocol/tokens.h>
40
41#include <cstdint>
42#include <map>
43#include <memory>
44#include <optional>
45#include <set>
46#include <string>
47#include <string_view>
48#include <vector>
49
50namespace rpc {
51
61 std::shared_ptr<BackendInterface> sharedPtrBackend_;
62
63public:
67 struct Output {
68 std::string ledgerHash;
69 uint32_t ledgerIndex;
70 std::string accountID;
71 bool overflow = false;
72 std::map<ripple::Currency, ripple::STAmount> sums;
73 std::map<ripple::AccountID, std::vector<ripple::STAmount>> hotBalances;
74 std::map<ripple::AccountID, std::vector<ripple::STAmount>> assets;
75 std::map<ripple::AccountID, std::vector<ripple::STAmount>> frozenBalances;
76 std::map<ripple::Currency, ripple::STAmount> locked;
77 // validated should be sent via framework
78 bool validated = true;
79 };
80
84 struct Input {
85 std::string account;
86 std::set<ripple::AccountID> hotWallets;
87 std::optional<std::string> ledgerHash;
88 std::optional<uint32_t> ledgerIndex;
89 };
90
91 using Result = HandlerReturnType<Output>;
92
98 GatewayBalancesHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend)
99 : sharedPtrBackend_(sharedPtrBackend)
100 {
101 }
102
109 static RpcSpecConstRef
110 spec([[maybe_unused]] uint32_t apiVersion)
111 {
112 auto const getHotWalletValidator = [](RippledError errCode) {
114 [errCode](boost::json::value const& value, std::string_view key) -> MaybeError {
115 if (!value.is_string() && !value.is_array())
116 return Error{Status{errCode, std::string(key) + "NotStringOrArray"}};
117
118 // wallet needs to be an valid accountID or public key
119 auto const wallets =
120 value.is_array() ? value.as_array() : boost::json::array{value};
121 auto const getAccountID =
122 [](auto const& j) -> std::optional<ripple::AccountID> {
123 if (j.is_string()) {
125 ripple::TokenType::AccountPublic,
126 boost::json::value_to<std::string>(j)
127 );
128
129 if (pk)
130 return ripple::calcAccountID(*pk);
131
133 boost::json::value_to<std::string>(j)
134 );
135 }
136
137 return {};
138 };
139
140 for (auto const& wallet : wallets) {
141 if (!getAccountID(wallet))
142 return Error{Status{errCode, std::string(key) + "Malformed"}};
143 }
144
145 return MaybeError{};
146 }
147 };
148 };
149
150 static auto const kSPEC_COMMON = RpcSpec{
154 };
155
156 static auto const kSPEC_V1 = RpcSpec{
157 kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_HOTWALLET)}}
158 };
159 static auto const kSPEC_V2 = RpcSpec{
160 kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_PARAMS)}}
161 };
162
163 return apiVersion == 1 ? kSPEC_V1 : kSPEC_V2;
164 }
165
173 Result
174 process(Input const& input, Context const& ctx) const;
175
176private:
183 friend void
184 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
185
192 friend Input
193 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
194};
195
196} // namespace rpc
GatewayBalancesHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend)
Construct a new GatewayBalancesHandler object.
Definition GatewayBalances.hpp:98
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:189
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition GatewayBalances.hpp:110
Result process(Input const &input, Context const &ctx) const
Process the GatewayBalances command.
Definition GatewayBalances.cpp:59
A meta-validator that allows to specify a custom validation function.
Definition Validators.hpp:440
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
RpcSpec const & RpcSpecConstRef
An alias for a const reference to RpcSpec.
Definition Specs.hpp:150
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:81
ripple::error_code_i RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:73
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:55
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:75
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:43
Context of an RPC call.
Definition Types.hpp:118
A struct to hold the input data for the command.
Definition GatewayBalances.hpp:84
A struct to hold the output data of the command.
Definition GatewayBalances.hpp:67
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:102
A status returned from any RPC handler.
Definition Errors.hpp:84
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:521
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:506
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:568
A validator that simply requires a field to be present.
Definition Validators.hpp:47