rippled
AccountCurrenciesHandler.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/ErrorCodes.h>
25 #include <ripple/protocol/jss.h>
26 #include <ripple/rpc/Context.h>
27 #include <ripple/rpc/impl/RPCHelpers.h>
28 
29 namespace ripple {
30 
33 {
34  auto& params = context.params;
35 
36  // Get the current ledger
38  auto result = RPC::lookupLedger(ledger, context);
39  if (!ledger)
40  return result;
41 
42  if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
43  return RPC::missing_field_error(jss::account);
44 
45  std::string const strIdent(
46  params.isMember(jss::account) ? params[jss::account].asString()
47  : params[jss::ident].asString());
48 
49  // Get info on account.
50  auto id = parseBase58<AccountID>(strIdent);
51  if (!id)
52  {
54  return result;
55  }
56  auto const accountID{std::move(id.value())};
57 
58  if (!ledger->exists(keylet::account(accountID)))
59  return rpcError(rpcACT_NOT_FOUND);
60 
61  std::set<Currency> send, receive;
62  for (auto const& rspEntry : RPCTrustLine::getItems(accountID, *ledger))
63  {
64  STAmount const& saBalance = rspEntry.getBalance();
65 
66  if (saBalance < rspEntry.getLimit())
67  receive.insert(saBalance.getCurrency());
68  if ((-saBalance) < rspEntry.getLimitPeer())
69  send.insert(saBalance.getCurrency());
70  }
71 
72  send.erase(badCurrency());
73  receive.erase(badCurrency());
74 
75  Json::Value& sendCurrencies =
76  (result[jss::send_currencies] = Json::arrayValue);
77  for (auto const& c : send)
78  sendCurrencies.append(to_string(c));
79 
80  Json::Value& recvCurrencies =
81  (result[jss::receive_currencies] = Json::arrayValue);
82  for (auto const& c : receive)
83  recvCurrencies.append(to_string(c));
84 
85  return result;
86 }
87 
88 } // namespace ripple
ripple::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:135
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::doAccountCurrencies
Json::Value doAccountCurrencies(RPC::JsonContext &context)
Definition: AccountCurrenciesHandler.cpp:32
std::string
STL class.
std::shared_ptr
STL class.
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
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
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:133
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::STAmount
Definition: STAmount.h:45
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
std::set::erase
T erase(T... args)
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
std::set::insert
T insert(T... args)
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
ripple::STAmount::getCurrency
Currency const & getCurrency() const
Definition: STAmount.h:353
ripple::RPC::inject_error
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:212
std::set
STL class.
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::RPCTrustLine::getItems
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition: TrustLine.cpp:120