xrpld
Loading...
Searching...
No Matches
GatewayBalances.cpp
1#include <xrpld/app/main/Application.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/detail/TrustLine.h>
5
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/json/json_value.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/DirectoryHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/ErrorCodes.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/MPTIssue.h>
15#include <xrpl/protocol/RPCErr.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/UintTypes.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/resource/Fees.h>
21
22#include <map>
23#include <memory>
24#include <set>
25#include <stdexcept>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace xrpl {
31
32// Query:
33// 1) Specify ledger to query.
34// 2) Specify issuer account (cold wallet) in "account" field.
35// 3) Specify accounts that hold gateway assets (such as hot wallets)
36// using "hotwallet" field which should be either a string (if just
37// one wallet) or an array of strings (if more than one).
38
39// Response:
40// 1) Array, "obligations", indicating the total obligations of the
41// gateway in each currency. Obligations to specified hot wallets
42// are not counted here.
43// 2) Object, "balances", indicating balances in each account
44// that holds gateway assets. (Those specified in the "hotwallet"
45// field.)
46// 3) Object of "assets" indicating accounts that owe the gateway.
47// (Gateways typically do not hold positive balances. This is unusual.)
48
49// gateway_balances [<ledger>] <account> [<hotwallet> [<hotwallet [...
50
51json::Value
53{
54 auto& params = context.params;
55
56 // Get the current ledger
58 auto result = RPC::lookupLedger(ledger, context);
59
60 if (!ledger)
61 return result;
62
63 if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
64 return RPC::missingFieldError(jss::account);
65
66 std::string const strIdent(
67 params.isMember(jss::account) ? params[jss::account].asString()
68 : params[jss::ident].asString());
69
70 // Get info on account.
71 auto id = parseBase58<AccountID>(strIdent);
72 if (!id)
74 auto const accountID{id.value()};
76
77 result[jss::account] = toBase58(accountID);
78
79 if (context.apiVersion > 1u && !ledger->exists(keylet::account(accountID)))
80 {
82 return result;
83 }
84
85 // Parse the specified hotwallet(s), if any
86 std::set<AccountID> hotWallets;
87
88 if (params.isMember(jss::hotwallet))
89 {
90 auto addHotWallet = [&hotWallets](json::Value const& j) {
91 if (j.isString())
92 {
93 if (auto id = parseBase58<AccountID>(j.asString()); id)
94 {
95 hotWallets.insert(id.value());
96 return true;
97 }
98 }
99
100 return false;
101 };
102
103 json::Value const& hw = params[jss::hotwallet];
104 bool valid = true;
105
106 // null is treated as a valid 0-sized array of hotwallet
107 if (hw.isArrayOrNull())
108 {
109 for (unsigned i = 0; i < hw.size(); ++i)
110 valid &= addHotWallet(hw[i]);
111 }
112 else if (hw.isString())
113 {
114 valid &= addHotWallet(hw);
115 }
116 else
117 {
118 valid = false;
119 }
120
121 if (!valid)
122 {
123 // The documentation states that invalidParams is used when
124 // One or more fields are specified incorrectly.
125 // invalidHotwallet should be used when the account exists, but does
126 // not have currency issued by the account from the request.
127 if (context.apiVersion < 2u)
128 {
130 }
131 else
132 {
134 }
135 return result;
136 }
137 }
138
144
145 // Traverse the cold wallet's trust lines
146 {
147 forEachItem(*ledger, accountID, [&](SLE::const_ref sle) {
148 if (sle->getType() == ltESCROW)
149 {
150 auto const& escrow = sle->getFieldAmount(sfAmount);
151 // Gateway Balance should not include MPTs
152 if (escrow.holds<MPTIssue>())
153 return;
154
155 auto& bal = locked[escrow.get<Issue>().currency];
156 if (bal == beast::kZero)
157 {
158 // This is needed to set the currency code correctly
159 bal = escrow;
160 }
161 else
162 {
163 try
164 {
165 bal += escrow;
166 }
167 catch (std::runtime_error const&)
168 {
169 // Presumably the exception was caused by overflow.
170 // On overflow return the largest valid STAmount.
171 // Very large sums of STAmount are approximations
172 // anyway.
174 }
175 }
176 }
177
178 auto rs = PathFindTrustLine::makeItem(accountID, sle);
179
180 if (!rs)
181 return;
182
183 int const balSign = rs->getBalance().signum();
184 if (balSign == 0)
185 return;
186
187 auto const& peer = rs->getAccountIDPeer();
188
189 // Here, a negative balance means the cold wallet owes (normal)
190 // A positive balance means the cold wallet has an asset
191 // (unusual)
192
193 if (hotWallets.contains(peer))
194 {
195 // This is a specified hot wallet
196 hotBalances[peer].push_back(-rs->getBalance());
197 }
198 else if (balSign > 0)
199 {
200 // This is a gateway asset
201 assets[peer].push_back(rs->getBalance());
202 }
203 else if (rs->getFreeze())
204 {
205 // An obligation the gateway has frozen
206 frozenBalances[peer].push_back(-rs->getBalance());
207 }
208 else
209 {
210 // normal negative balance, obligation to customer
211 auto& bal = sums[rs->getBalance().get<Issue>().currency];
212 if (bal == beast::kZero)
213 {
214 // This is needed to set the currency code correctly
215 bal = -rs->getBalance();
216 }
217 else
218 {
219 try
220 {
221 bal -= rs->getBalance();
222 }
223 catch (std::runtime_error const&)
224 {
225 // Presumably the exception was caused by overflow.
226 // On overflow return the largest valid STAmount.
227 // Very large sums of STAmount are approximations
228 // anyway.
230 }
231 }
232 }
233 });
234 }
235
236 if (!sums.empty())
237 {
238 json::Value j;
239 for (auto const& [k, v] : sums)
240 {
241 j[to_string(k)] = v.getText();
242 }
243 result[jss::obligations] = std::move(j);
244 }
245
246 auto populateResult = [&result](
248 json::StaticString const& name) {
249 if (!array.empty())
250 {
251 json::Value j;
252 for (auto const& [accId, accBalances] : array)
253 {
254 json::Value balanceArray;
255 for (auto const& balance : accBalances)
256 {
257 json::Value entry;
258 entry[jss::currency] = to_string(balance.get<Issue>().currency);
259 entry[jss::value] = balance.getText();
260 balanceArray.append(std::move(entry));
261 }
262 j[to_string(accId)] = std::move(balanceArray);
263 }
264 result[name] = std::move(j);
265 }
266 };
267
268 populateResult(hotBalances, jss::balances);
269 populateResult(frozenBalances, jss::frozen_balances);
270 populateResult(assets, jss::assets);
271
272 // Add total escrow to the result
273 if (!locked.empty())
274 {
275 json::Value j;
276 for (auto const& [k, v] : locked)
277 {
278 j[to_string(k)] = v.getText();
279 }
280 result[jss::locked] = std::move(j);
281 }
282
283 return result;
284}
285
286} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:44
Represents a JSON value.
Definition json_value.h:130
bool isString() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
bool isArrayOrNull() const
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
static std::optional< PathFindTrustLine > makeItem(AccountID const &accountID, SLE::const_ref sle)
Definition TrustLine.cpp:39
static constexpr std::uint64_t kMaxValue
Definition STAmount.h:53
static constexpr int kMaxOffset
Definition STAmount.h:48
std::shared_ptr< STLedgerEntry const > const & const_ref
T contains(T... args)
T empty(T... args)
T insert(T... args)
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
Charge const kFeeHeavyBurdenRpc
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidHotwallet
Definition ErrorCodes.h:63
@ RpcActNotFound
Definition ErrorCodes.h:52
@ RpcActMalformed
Definition ErrorCodes.h:72
@ RpcInvalidParams
Definition ErrorCodes.h:66
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
json::Value doGatewayBalances(RPC::JsonContext &context)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(SLE::const_ref)> const &f)
Iterate all items in the given directory.
Resource::Charge & loadType
Definition Context.h:22
unsigned int apiVersion
Definition Context.h:29
json::Value params
Definition Context.h:43