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