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 xrpl {
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> [<hotwallet> [<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() : params[jss::ident].asString());
51
52 // Get info on account.
53 auto id = parseBase58<AccountID>(strIdent);
54 if (!id)
56 auto const accountID{std::move(id.value())};
58
59 result[jss::account] = toBase58(accountID);
60
61 if (context.apiVersion > 1u && !ledger->exists(keylet::account(accountID)))
62 {
64 return result;
65 }
66
67 // Parse the specified hotwallet(s), if any
68 std::set<AccountID> hotWallets;
69
70 if (params.isMember(jss::hotwallet))
71 {
72 auto addHotWallet = [&hotWallets](Json::Value const& j) {
73 if (j.isString())
74 {
75 if (auto id = parseBase58<AccountID>(j.asString()); id)
76 {
77 hotWallets.insert(std::move(id.value()));
78 return true;
79 }
80 }
81
82 return false;
83 };
84
85 Json::Value const& hw = params[jss::hotwallet];
86 bool valid = true;
87
88 // null is treated as a valid 0-sized array of hotwallet
89 if (hw.isArrayOrNull())
90 {
91 for (unsigned i = 0; i < hw.size(); ++i)
92 valid &= addHotWallet(hw[i]);
93 }
94 else if (hw.isString())
95 {
96 valid &= addHotWallet(hw);
97 }
98 else
99 {
100 valid = false;
101 }
102
103 if (!valid)
104 {
105 // The documentation states that invalidParams is used when
106 // One or more fields are specified incorrectly.
107 // invalidHotwallet should be used when the account exists, but does
108 // not have currency issued by the account from the request.
109 if (context.apiVersion < 2u)
110 {
112 }
113 else
114 {
116 }
117 return result;
118 }
119 }
120
126
127 // Traverse the cold wallet's trust lines
128 {
129 forEachItem(*ledger, accountID, [&](std::shared_ptr<SLE const> const& sle) {
130 if (sle->getType() == ltESCROW)
131 {
132 auto const& escrow = sle->getFieldAmount(sfAmount);
133 auto& bal = locked[escrow.getCurrency()];
134 if (bal == beast::zero)
135 {
136 // This is needed to set the currency code correctly
137 bal = escrow;
138 }
139 else
140 {
141 try
142 {
143 bal += escrow;
144 }
145 catch (std::runtime_error const&)
146 {
147 // Presumably the exception was caused by overflow.
148 // On overflow return the largest valid STAmount.
149 // Very large sums of STAmount are approximations
150 // anyway.
152 }
153 }
154 }
155
156 auto rs = PathFindTrustLine::makeItem(accountID, sle);
157
158 if (!rs)
159 return;
160
161 int balSign = rs->getBalance().signum();
162 if (balSign == 0)
163 return;
164
165 auto const& peer = rs->getAccountIDPeer();
166
167 // Here, a negative balance means the cold wallet owes (normal)
168 // A positive balance means the cold wallet has an asset
169 // (unusual)
170
171 if (hotWallets.count(peer) > 0)
172 {
173 // This is a specified hot wallet
174 hotBalances[peer].push_back(-rs->getBalance());
175 }
176 else if (balSign > 0)
177 {
178 // This is a gateway asset
179 assets[peer].push_back(rs->getBalance());
180 }
181 else if (rs->getFreeze())
182 {
183 // An obligation the gateway has frozen
184 frozenBalances[peer].push_back(-rs->getBalance());
185 }
186 else
187 {
188 // normal negative balance, obligation to customer
189 auto& bal = sums[rs->getBalance().getCurrency()];
190 if (bal == beast::zero)
191 {
192 // This is needed to set the currency code correctly
193 bal = -rs->getBalance();
194 }
195 else
196 {
197 try
198 {
199 bal -= rs->getBalance();
200 }
201 catch (std::runtime_error const&)
202 {
203 // Presumably the exception was caused by overflow.
204 // On overflow return the largest valid STAmount.
205 // Very large sums of STAmount are approximations
206 // anyway.
208 }
209 }
210 }
211 });
212 }
213
214 if (!sums.empty())
215 {
216 Json::Value j;
217 for (auto const& [k, v] : sums)
218 {
219 j[to_string(k)] = v.getText();
220 }
221 result[jss::obligations] = std::move(j);
222 }
223
224 auto populateResult = [&result](
226 if (!array.empty())
227 {
228 Json::Value j;
229 for (auto const& [accId, accBalances] : array)
230 {
231 Json::Value balanceArray;
232 for (auto const& balance : accBalances)
233 {
235 entry[jss::currency] = to_string(balance.issue().currency);
236 entry[jss::value] = balance.getText();
237 balanceArray.append(std::move(entry));
238 }
239 j[to_string(accId)] = std::move(balanceArray);
240 }
241 result[name] = std::move(j);
242 }
243 };
244
245 populateResult(hotBalances, jss::balances);
246 populateResult(frozenBalances, jss::frozen_balances);
247 populateResult(assets, jss::assets);
248
249 // Add total escrow to the result
250 if (!locked.empty())
251 {
252 Json::Value j;
253 for (auto const& [k, v] : locked)
254 {
255 j[to_string(k)] = v.getText();
256 }
257 result[jss::locked] = std::move(j);
258 }
259
260 return result;
261}
262
263} // 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:31
static constexpr std::uint64_t cMaxValue
Definition STAmount.h:51
static int const cMaxOffset
Definition STAmount.h:46
T count(T... args)
T empty(T... args)
T insert(T... args)
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:226
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:160
Json::Value entry(jtx::Env &env, jtx::Account const &account, jtx::Account const &authorize)
Definition delegate.cpp:34
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.
Definition View.cpp:598
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)