rippled
Loading...
Searching...
No Matches
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 <xrpld/app/paths/TrustLine.h>
21#include <xrpld/ledger/ReadView.h>
22#include <xrpld/rpc/Context.h>
23#include <xrpld/rpc/detail/RPCHelpers.h>
24
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/RPCErr.h>
27#include <xrpl/protocol/jss.h>
28
29namespace ripple {
30
33{
34 auto& params = context.params;
35
36 if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
37 return RPC::missing_field_error(jss::account);
38
39 std::string strIdent;
40 if (params.isMember(jss::account))
41 {
42 if (!params[jss::account].isString())
43 return RPC::invalid_field_error(jss::account);
44 strIdent = params[jss::account].asString();
45 }
46 else if (params.isMember(jss::ident))
47 {
48 if (!params[jss::ident].isString())
49 return RPC::invalid_field_error(jss::ident);
50 strIdent = params[jss::ident].asString();
51 }
52
53 // Get the current ledger
55 auto result = RPC::lookupLedger(ledger, context);
56 if (!ledger)
57 return result;
58
59 // Get info on account.
60 auto id = parseBase58<AccountID>(strIdent);
61 if (!id)
62 {
64 return result;
65 }
66 auto const accountID{std::move(id.value())};
67
68 if (!ledger->exists(keylet::account(accountID)))
70
71 std::set<Currency> send, receive;
72 for (auto const& rspEntry : RPCTrustLine::getItems(accountID, *ledger))
73 {
74 STAmount const& saBalance = rspEntry.getBalance();
75
76 if (saBalance < rspEntry.getLimit())
77 receive.insert(saBalance.getCurrency());
78 if ((-saBalance) < rspEntry.getLimitPeer())
79 send.insert(saBalance.getCurrency());
80 }
81
82 send.erase(badCurrency());
83 receive.erase(badCurrency());
84
85 Json::Value& sendCurrencies =
86 (result[jss::send_currencies] = Json::arrayValue);
87 for (auto const& c : send)
88 sendCurrencies.append(to_string(c));
89
90 Json::Value& recvCurrencies =
91 (result[jss::receive_currencies] = Json::arrayValue);
92 for (auto const& c : receive)
93 recvCurrencies.append(to_string(c));
94
95 return result;
96}
97
98} // namespace ripple
Represents a JSON value.
Definition: json_value.h:149
Value & append(Value const &value)
Append value to array at the end.
Definition: json_value.cpp:910
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition: TrustLine.cpp:120
Currency const & getCurrency() const
Definition: STAmount.h:502
T erase(T... args)
T insert(T... args)
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:318
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:226
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:622
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:276
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:184
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:133
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:31
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
Json::Value doAccountCurrencies(RPC::JsonContext &context)
Json::Value params
Definition: Context.h:63