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 = value.is_array() ? value.as_array() : boost::json::array{value};
120 auto const getAccountID = [](auto const& j) -> std::optional<ripple::AccountID> {
121 if (j.is_string()) {
123 ripple::TokenType::AccountPublic, boost::json::value_to<std::string>(j)
124 );
125
126 if (pk)
127 return ripple::calcAccountID(*pk);
128
129 return util::parseBase58Wrapper<ripple::AccountID>(boost::json::value_to<std::string>(j));
130 }
131
132 return {};
133 };
134
135 for (auto const& wallet : wallets) {
136 if (!getAccountID(wallet))
137 return Error{Status{errCode, std::string(key) + "Malformed"}};
138 }
139
140 return MaybeError{};
141 }
142 };
143 };
144
145 static auto const kSPEC_COMMON = RpcSpec{
149 };
150
151 static auto const kSPEC_V1 =
152 RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_HOTWALLET)}}};
153 static auto const kSPEC_V2 =
154 RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_PARAMS)}}};
155
156 return apiVersion == 1 ? kSPEC_V1 : kSPEC_V2;
157 }
158
166 Result
167 process(Input input, Context const& ctx) const;
168
169private:
176 friend void
177 tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
178
185 friend Input
186 tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
187};
188
189} // 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: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:180
static RpcSpecConstRef spec(uint32_t apiVersion)
Returns the API specification for the command.
Definition GatewayBalances.hpp:110
Result process(Input input, Context const &ctx) const
Process the GatewayBalances command.
Definition GatewayBalances.cpp:58
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:37
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:72
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: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:98
A status returned from any RPC handler.
Definition Errors.hpp:83
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:494
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:487
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:533
A validator that simply requires a field to be present.
Definition Validators.hpp:47