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/protocol/AccountID.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/PublicKey.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/resource/Fees.h>
28 #include <ripple/rpc/Context.h>
29 #include <ripple/rpc/impl/RPCHelpers.h>
30 
31 namespace ripple {
32 
33 // Query:
34 // 1) Specify ledger to query.
35 // 2) Specify issuer account (cold wallet) in "account" field.
36 // 3) Specify accounts that hold gateway assets (such as hot wallets)
37 // using "hotwallet" field which should be either a string (if just
38 // one wallet) or an array of strings (if more than one).
39 
40 // Response:
41 // 1) Array, "obligations", indicating the total obligations of the
42 // gateway in each currency. Obligations to specified hot wallets
43 // are not counted here.
44 // 2) Object, "balances", indicating balances in each account
45 // that holds gateway assets. (Those specified in the "hotwallet"
46 // field.)
47 // 3) Object of "assets" indicating accounts that owe the gateway.
48 // (Gateways typically do not hold positive balances. This is unusual.)
49 
50 // gateway_balances [<ledger>] <account> [<howallet> [<hotwallet [...
51 
54 {
55  auto& params = context.params;
56 
57  // Get the current ledger
59  auto result = RPC::lookupLedger(ledger, context);
60 
61  if (!ledger)
62  return result;
63 
64  if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
65  return RPC::missing_field_error(jss::account);
66 
67  std::string const strIdent(
68  params.isMember(jss::account) ? params[jss::account].asString()
69  : params[jss::ident].asString());
70 
71  bool const bStrict =
72  params.isMember(jss::strict) && params[jss::strict].asBool();
73 
74  // Get info on account.
75  AccountID accountID;
76  auto jvAccepted = RPC::accountFromString(accountID, strIdent, bStrict);
77 
78  if (jvAccepted)
79  return jvAccepted;
80 
82 
83  result[jss::account] = toBase58(accountID);
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  auto const pk = parseBase58<PublicKey>(
94  TokenType::AccountPublic, j.asString());
95  if (pk)
96  {
97  hotWallets.insert(calcAccountID(*pk));
98  return true;
99  }
100 
101  auto const id = parseBase58<AccountID>(j.asString());
102 
103  if (id)
104  {
105  hotWallets.insert(*id);
106  return true;
107  }
108  }
109 
110  return false;
111  };
112 
113  Json::Value const& hw = params[jss::hotwallet];
114  bool valid = true;
115 
116  // null is treated as a valid 0-sized array of hotwallet
117  if (hw.isArrayOrNull())
118  {
119  for (unsigned i = 0; i < hw.size(); ++i)
120  valid &= addHotWallet(hw[i]);
121  }
122  else if (hw.isString())
123  {
124  valid &= addHotWallet(hw);
125  }
126  else
127  {
128  valid = false;
129  }
130 
131  if (!valid)
132  {
133  result[jss::error] = "invalidHotWallet";
134  return result;
135  }
136  }
137 
142 
143  // Traverse the cold wallet's trust lines
144  {
145  forEachItem(
146  *ledger, accountID, [&](std::shared_ptr<SLE const> const& sle) {
147  auto rs = PathFindTrustLine::makeItem(accountID, sle);
148 
149  if (!rs)
150  return;
151 
152  int balSign = rs->getBalance().signum();
153  if (balSign == 0)
154  return;
155 
156  auto const& peer = rs->getAccountIDPeer();
157 
158  // Here, a negative balance means the cold wallet owes (normal)
159  // A positive balance means the cold wallet has an asset
160  // (unusual)
161 
162  if (hotWallets.count(peer) > 0)
163  {
164  // This is a specified hot wallet
165  hotBalances[peer].push_back(-rs->getBalance());
166  }
167  else if (balSign > 0)
168  {
169  // This is a gateway asset
170  assets[peer].push_back(rs->getBalance());
171  }
172  else if (rs->getFreeze())
173  {
174  // An obligation the gateway has frozen
175  frozenBalances[peer].push_back(-rs->getBalance());
176  }
177  else
178  {
179  // normal negative balance, obligation to customer
180  auto& bal = sums[rs->getBalance().getCurrency()];
181  if (bal == beast::zero)
182  {
183  // This is needed to set the currency code correctly
184  bal = -rs->getBalance();
185  }
186  else
187  bal -= rs->getBalance();
188  }
189  });
190  }
191 
192  if (!sums.empty())
193  {
194  Json::Value j;
195  for (auto const& [k, v] : sums)
196  {
197  j[to_string(k)] = v.getText();
198  }
199  result[jss::obligations] = std::move(j);
200  }
201 
202  auto populateResult =
203  [&result](
205  Json::StaticString const& name) {
206  if (!array.empty())
207  {
208  Json::Value j;
209  for (auto const& [accId, accBalances] : array)
210  {
211  Json::Value balanceArray;
212  for (auto const& balance : accBalances)
213  {
214  Json::Value entry;
215  entry[jss::currency] =
216  to_string(balance.issue().currency);
217  entry[jss::value] = balance.getText();
218  balanceArray.append(std::move(entry));
219  }
220  j[to_string(accId)] = std::move(balanceArray);
221  }
222  result[name] = std::move(j);
223  }
224  };
225 
226  populateResult(hotBalances, jss::balances);
227  populateResult(frozenBalances, jss::frozen_balances);
228  populateResult(assets, jss::assets);
229 
230  return result;
231 }
232 
233 } // 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::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:53
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:577
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
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::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:158
Json::Value::size
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:706
std::map
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
std::set::insert
T insert(T... args)
ripple::TokenType::AccountPublic
@ AccountPublic
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::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
std::set
STL class.
ripple::Resource::feeHighBurdenRPC
const Charge feeHighBurdenRPC
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:85
Json::Value
Represents a JSON value.
Definition: json_value.h:145