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 // validated should be sent via framework
77 bool validated = true;
78 };
79
83 struct Input {
84 std::string account;
85 std::set<ripple::AccountID> hotWallets;
86 std::optional<std::string> ledgerHash;
87 std::optional<uint32_t> ledgerIndex;
88 };
89
90 using Result = HandlerReturnType<Output>;
91
97 GatewayBalancesHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend)
98 : sharedPtrBackend_(sharedPtrBackend)
99 {
100 }
101
108 static RpcSpecConstRef
109 spec([[maybe_unused]] uint32_t apiVersion)
110 {
111 auto const getHotWalletValidator = [](RippledError errCode) {
113 [errCode](boost::json::value const& value, std::string_view key) -> MaybeError {
114 if (!value.is_string() && !value.is_array())
115 return Error{Status{errCode, std::string(key) + "NotStringOrArray"}};
116
117 // wallet needs to be an valid accountID or public key
118 auto const wallets = value.is_array() ? value.as_array() : boost::json::array{value};
119 auto const getAccountID = [](auto const& j) -> std::optional<ripple::AccountID> {
120 if (j.is_string()) {
122 ripple::TokenType::AccountPublic, boost::json::value_to<std::string>(j)
123 );
124
125 if (pk)
126 return ripple::calcAccountID(*pk);
127
128 return util::parseBase58Wrapper<ripple::AccountID>(boost::json::value_to<std::string>(j));
129 }
130
131 return {};
132 };
133
134 for (auto const& wallet : wallets) {
135 if (!getAccountID(wallet))
136 return Error{Status{errCode, std::string(key) + "Malformed"}};
137 }
138
139 return MaybeError{};
140 }
141 };
142 };
143
144 static auto const kSPEC_COMMON = RpcSpec{
148 };
149
150 auto static const kSPEC_V1 =
151 RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_HOTWALLET)}}};
152 auto static const kSPEC_V2 =
153 RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_PARAMS)}}};
154
155 return apiVersion == 1 ? kSPEC_V1 : kSPEC_V2;
156 }
157
165 Result
166 process(Input input, Context const& ctx) const;
167
168private:
175 friend void
176 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
177
184 friend Input
185 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
186};
187
188} // namespace rpc
Handles the gateway_balances command.
Definition GatewayBalances.hpp:60
GatewayBalancesHandler(std::shared_ptr< BackendInterface > const &sharedPtrBackend)
Construct a new GatewayBalancesHandler object.
Definition GatewayBalances.hpp:97
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:156
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition GatewayBalances.hpp:109
Result process(Input 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:423
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
ripple::error_code_i RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:71
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:42
Context of an RPC call.
Definition Types.hpp:118
A struct to hold the input data for the command.
Definition GatewayBalances.hpp:83
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: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 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