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/RippleState.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  bool const bStrict =
50  params.isMember(jss::strict) && params[jss::strict].asBool();
51 
52  // Get info on account.
53  AccountID accountID; // out param
54  if (auto jvAccepted = RPC::accountFromString(accountID, strIdent, bStrict))
55  return jvAccepted;
56 
57  if (!ledger->exists(keylet::account(accountID)))
58  return rpcError(rpcACT_NOT_FOUND);
59 
60  std::set<Currency> send, receive;
61  for (auto const& item : getRippleStateItems(accountID, *ledger))
62  {
63  auto const rspEntry = item.get();
64 
65  STAmount const& saBalance = rspEntry->getBalance();
66 
67  if (saBalance < rspEntry->getLimit())
68  receive.insert(saBalance.getCurrency());
69  if ((-saBalance) < rspEntry->getLimitPeer())
70  send.insert(saBalance.getCurrency());
71  }
72 
73  send.erase(badCurrency());
74  receive.erase(badCurrency());
75 
76  Json::Value& sendCurrencies =
77  (result[jss::send_currencies] = Json::arrayValue);
78  for (auto const& c : send)
79  sendCurrencies.append(to_string(c));
80 
81  Json::Value& recvCurrencies =
82  (result[jss::receive_currencies] = Json::arrayValue);
83  for (auto const& c : receive)
84  recvCurrencies.append(to_string(c));
85 
86  return result;
87 }
88 
89 } // 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:138
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.
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp: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:487
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:236
ripple::getRippleStateItems
std::vector< RippleState::pointer > getRippleStateItems(AccountID const &accountID, ReadView const &view)
Definition: RippleState.cpp:70
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:73
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:134
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::STAmount
Definition: STAmount.h:42
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::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:29
ripple::STAmount::getCurrency
Currency const & getCurrency() const
Definition: STAmount.h:204
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::set::insert
T insert(T... args)
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
std::set
STL class.
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:82
Json::Value
Represents a JSON value.
Definition: json_value.h:145