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