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/RippleState.h>
22 #include <ripple/ledger/ReadView.h>
23 #include <ripple/protocol/AccountID.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/jss.h>
26 #include <ripple/protocol/PublicKey.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 
53 {
54  auto& params = context.params;
55 
56  // Get the current ledger
58  auto result = RPC::lookupLedger (ledger, context);
59 
60  if (!ledger)
61  return result;
62 
63  if (!(params.isMember (jss::account) || params.isMember (jss::ident)))
64  return RPC::missing_field_error (jss::account);
65 
66  std::string const strIdent (params.isMember (jss::account)
67  ? params[jss::account].asString ()
68  : params[jss::ident].asString ());
69 
70  bool const bStrict = params.isMember (jss::strict) &&
71  params[jss::strict].asBool ();
72 
73  // Get info on account.
74  AccountID accountID;
75  auto jvAccepted = RPC::accountFromString (accountID, strIdent, bStrict);
76 
77  if (jvAccepted)
78  return jvAccepted;
79 
81 
82  result[jss::account] = context.app.accountIDCache().toBase58 (accountID);
83 
84  // Parse the specified hotwallet(s), if any
85  std::set <AccountID> hotWallets;
86 
87  if (params.isMember (jss::hotwallet))
88  {
89 
90  auto addHotWallet = [&hotWallets](Json::Value const& j)
91  {
92  if (j.isString())
93  {
94  auto const pk = parseBase58<PublicKey>(
96  j.asString ());
97  if (pk)
98  {
99  hotWallets.insert(calcAccountID(*pk));
100  return true;
101  }
102 
103  auto const id = parseBase58<AccountID>(j.asString());
104 
105  if (id)
106  {
107  hotWallets.insert(*id);
108  return true;
109  }
110  }
111 
112  return false;
113  };
114 
115  Json::Value const& hw = params[jss::hotwallet];
116  bool valid = true;
117 
118  // null is treated as a valid 0-sized array of hotwallet
119  if (hw.isArrayOrNull())
120  {
121  for (unsigned i = 0; i < hw.size(); ++i)
122  valid &= addHotWallet (hw[i]);
123  }
124  else if (hw.isString())
125  {
126  valid &= addHotWallet (hw);
127  }
128  else
129  {
130  valid = false;
131  }
132 
133  if (! valid)
134  {
135  result[jss::error] = "invalidHotWallet";
136  return result;
137  }
138 
139  }
140 
145 
146  // Traverse the cold wallet's trust lines
147  {
148  forEachItem(*ledger, accountID,
149  [&](std::shared_ptr<SLE const> const& sle)
150  {
151  auto rs = RippleState::makeItem (accountID, sle);
152 
153  if (!rs)
154  return;
155 
156  int balSign = rs->getBalance().signum();
157  if (balSign == 0)
158  return;
159 
160  auto const& peer = rs->getAccountIDPeer();
161 
162  // Here, a negative balance means the cold wallet owes (normal)
163  // A positive balance means the cold wallet has an asset (unusual)
164 
165  if (hotWallets.count (peer) > 0)
166  {
167  // This is a specified hot wallet
168  hotBalances[peer].push_back (-rs->getBalance ());
169  }
170  else if (balSign > 0)
171  {
172  // This is a gateway asset
173  assets[peer].push_back (rs->getBalance ());
174  }
175  else if (rs->getFreeze())
176  {
177  // An obligation the gateway has frozen
178  frozenBalances[peer].push_back (-rs->getBalance ());
179  }
180  else
181  {
182  // normal negative balance, obligation to customer
183  auto& bal = sums[rs->getBalance().getCurrency()];
184  if (bal == beast::zero)
185  {
186  // This is needed to set the currency code correctly
187  bal = -rs->getBalance();
188  }
189  else
190  bal -= rs->getBalance();
191  }
192  });
193  }
194 
195  if (! sums.empty())
196  {
197  Json::Value j;
198  for (auto const& [k, v] : sums)
199  {
200  j[to_string (k)] = v.getText ();
201  }
202  result [jss::obligations] = std::move (j);
203  }
204 
205  auto populateResult = [&result](
207  Json::StaticString const& name)
208  {
209  if (!array.empty())
210  {
211  Json::Value j;
212  for (auto const& [accId, accBalances] : array)
213  {
214  Json::Value balanceArray;
215  for (auto const& balance : accBalances)
216  {
217  Json::Value entry;
218  entry[jss::currency] = 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 } // ripple
ripple::RPC::JsonContext
Definition: Context.h:52
std::string
STL class.
std::shared_ptr
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::doGatewayBalances
Json::Value doGatewayBalances(RPC::JsonContext &context)
Definition: GatewayBalances.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:484
ripple::Application::accountIDCache
virtual AccountIDCache const & accountIDCache() const =0
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:229
ripple::forEachItem
void forEachItem(ReadView const &view, AccountID const &id, std::function< void(std::shared_ptr< SLE const > const &)> f)
Iterate all items in an account's owner directory.
Definition: View.cpp:244
ripple::base_uint
Definition: base_uint.h:65
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
Json::Value::isArrayOrNull
bool isArrayOrNull() const
Definition: json_value.cpp:1062
ripple::RippleState::makeItem
static RippleState::pointer makeItem(AccountID const &accountID, std::shared_ptr< SLE const > sle)
Definition: RippleState.cpp:29
ripple::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:136
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
Json::Value::size
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:720
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:62
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::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
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:86
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::AccountIDCache::toBase58
std::string toBase58(AccountID const &) const
Return ripple::toBase58 for the AccountID.
Definition: AccountID.cpp:199