rippled
GatewayBalances.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or 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 #include <ripple/app/main/Application.h>
21 #include <ripple/app/paths/TrustLine.h>
22 #include <ripple/ledger/ReadView.h>
23 #include <ripple/net/RPCErr.h>
24 #include <ripple/protocol/AccountID.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/PublicKey.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/resource/Fees.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/impl/RPCHelpers.h>
31 
32 namespace ripple {
33 
34 // Query:
35 // 1) Specify ledger to query.
36 // 2) Specify issuer account (cold wallet) in "account" field.
37 // 3) Specify accounts that hold gateway assets (such as hot wallets)
38 // using "hotwallet" field which should be either a string (if just
39 // one wallet) or an array of strings (if more than one).
40 
41 // Response:
42 // 1) Array, "obligations", indicating the total obligations of the
43 // gateway in each currency. Obligations to specified hot wallets
44 // are not counted here.
45 // 2) Object, "balances", indicating balances in each account
46 // that holds gateway assets. (Those specified in the "hotwallet"
47 // field.)
48 // 3) Object of "assets" indicating accounts that owe the gateway.
49 // (Gateways typically do not hold positive balances. This is unusual.)
50 
51 // gateway_balances [<ledger>] <account> [<howallet> [<hotwallet [...
52 
55 {
56  auto& params = context.params;
57 
58  // Get the current ledger
60  auto result = RPC::lookupLedger(ledger, context);
61 
62  if (!ledger)
63  return result;
64 
65  if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
66  return RPC::missing_field_error(jss::account);
67 
68  std::string const strIdent(
69  params.isMember(jss::account) ? params[jss::account].asString()
70  : params[jss::ident].asString());
71 
72  // Get info on account.
73  auto id = parseBase58<AccountID>(strIdent);
74  if (!id)
75  return rpcError(rpcACT_MALFORMED);
76  auto const accountID{std::move(id.value())};
78 
79  result[jss::account] = toBase58(accountID);
80 
81  // Parse the specified hotwallet(s), if any
82  std::set<AccountID> hotWallets;
83 
84  if (params.isMember(jss::hotwallet))
85  {
86  auto addHotWallet = [&hotWallets](Json::Value const& j) {
87  if (j.isString())
88  {
89  if (auto id = parseBase58<AccountID>(j.asString()); id)
90  {
91  hotWallets.insert(std::move(id.value()));
92  return true;
93  }
94  }
95 
96  return false;
97  };
98 
99  Json::Value const& hw = params[jss::hotwallet];
100  bool valid = true;
101 
102  // null is treated as a valid 0-sized array of hotwallet
103  if (hw.isArrayOrNull())
104  {
105  for (unsigned i = 0; i < hw.size(); ++i)
106  valid &= addHotWallet(hw[i]);
107  }
108  else if (hw.isString())
109  {
110  valid &= addHotWallet(hw);
111  }
112  else
113  {
114  valid = false;
115  }
116 
117  if (!valid)
118  {
119  result[jss::error] = "invalidHotWallet";
120  return result;
121  }
122  }
123 
128 
129  // Traverse the cold wallet's trust lines
130  {
131  forEachItem(
132  *ledger, accountID, [&](std::shared_ptr<SLE const> const& sle) {
133  auto rs = PathFindTrustLine::makeItem(accountID, sle);
134 
135  if (!rs)
136  return;
137 
138  int balSign = rs->getBalance().signum();
139  if (balSign == 0)
140  return;
141 
142  auto const& peer = rs->getAccountIDPeer();
143 
144  // Here, a negative balance means the cold wallet owes (normal)
145  // A positive balance means the cold wallet has an asset
146  // (unusual)
147 
148  if (hotWallets.count(peer) > 0)
149  {
150  // This is a specified hot wallet
151  hotBalances[peer].push_back(-rs->getBalance());
152  }
153  else if (balSign > 0)
154  {
155  // This is a gateway asset
156  assets[peer].push_back(rs->getBalance());
157  }
158  else if (rs->getFreeze())
159  {
160  // An obligation the gateway has frozen
161  frozenBalances[peer].push_back(-rs->getBalance());
162  }
163  else
164  {
165  // normal negative balance, obligation to customer
166  auto& bal = sums[rs->getBalance().getCurrency()];
167  if (bal == beast::zero)
168  {
169  // This is needed to set the currency code correctly
170  bal = -rs->getBalance();
171  }
172  else
173  {
174  try
175  {
176  bal -= rs->getBalance();
177  }
178  catch (std::runtime_error const&)
179  {
180  // Presumably the exception was caused by overflow.
181  // On overflow return the largest valid STAmount.
182  // Very large sums of STAmount are approximations
183  // anyway.
184  bal = STAmount(
185  bal.issue(),
188  }
189  }
190  }
191  });
192  }
193 
194  if (!sums.empty())
195  {
196  Json::Value j;
197  for (auto const& [k, v] : sums)
198  {
199  j[to_string(k)] = v.getText();
200  }
201  result[jss::obligations] = std::move(j);
202  }
203 
204  auto populateResult =
205  [&result](
207  Json::StaticString const& name) {
208  if (!array.empty())
209  {
210  Json::Value j;
211  for (auto const& [accId, accBalances] : array)
212  {
213  Json::Value balanceArray;
214  for (auto const& balance : accBalances)
215  {
216  Json::Value entry;
217  entry[jss::currency] =
218  to_string(balance.issue().currency);
219  entry[jss::value] = balance.getText();
220  balanceArray.append(std::move(entry));
221  }
222  j[to_string(accId)] = std::move(balanceArray);
223  }
224  result[name] = std::move(j);
225  }
226  };
227 
228  populateResult(hotBalances, jss::balances);
229  populateResult(frozenBalances, jss::frozen_balances);
230  populateResult(assets, jss::assets);
231 
232  return result;
233 }
234 
235 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
std::shared_ptr
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::doGatewayBalances
Json::Value doGatewayBalances(RPC::JsonContext &context)
Definition: GatewayBalances.cpp:54
ripple::PathFindTrustLine::makeItem
static std::optional< PathFindTrustLine > makeItem(AccountID const &accountID, std::shared_ptr< SLE const > const &sle)
Definition: TrustLine.cpp:52
ripple::RPC::lookupLedger
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.
Definition: RPCHelpers.cpp:675
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::Value::isArrayOrNull
bool isArrayOrNull() const
Definition: json_value.cpp:1021
ripple::STAmount
Definition: STAmount.h:45
Json::Value::size
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:706
std::runtime_error
STL class.
std::map
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcACT_MALFORMED
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
std::set::insert
T insert(T... args)
std::set::count
T count(T... args)
std::map::empty
T empty(T... args)
ripple::forEachItem
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:367
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::STAmount::cMaxOffset
static const int cMaxOffset
Definition: STAmount.h:63
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
std::set
STL class.
ripple::Resource::feeHighBurdenRPC
const Charge feeHighBurdenRPC
ripple::STAmount::cMaxValue
static const std::uint64_t cMaxValue
Definition: STAmount.h:67
Json::Value
Represents a JSON value.
Definition: json_value.h:145