rippled
Loading...
Searching...
No Matches
AccountCurrenciesHandler.cpp
1#include <xrpld/app/paths/TrustLine.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/protocol/ErrorCodes.h>
7#include <xrpl/protocol/RPCErr.h>
8#include <xrpl/protocol/jss.h>
9
10namespace ripple {
11
14{
15 auto& params = context.params;
16
17 if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
18 return RPC::missing_field_error(jss::account);
19
20 std::string strIdent;
21 if (params.isMember(jss::account))
22 {
23 if (!params[jss::account].isString())
24 return RPC::invalid_field_error(jss::account);
25 strIdent = params[jss::account].asString();
26 }
27 else if (params.isMember(jss::ident))
28 {
29 if (!params[jss::ident].isString())
30 return RPC::invalid_field_error(jss::ident);
31 strIdent = params[jss::ident].asString();
32 }
33
34 // Get the current ledger
36 auto result = RPC::lookupLedger(ledger, context);
37 if (!ledger)
38 return result;
39
40 // Get info on account.
41 auto id = parseBase58<AccountID>(strIdent);
42 if (!id)
43 {
45 return result;
46 }
47 auto const accountID{std::move(id.value())};
48
49 if (!ledger->exists(keylet::account(accountID)))
51
52 std::set<Currency> send, receive;
53 for (auto const& rspEntry : RPCTrustLine::getItems(accountID, *ledger))
54 {
55 STAmount const& saBalance = rspEntry.getBalance();
56
57 if (saBalance < rspEntry.getLimit())
58 receive.insert(saBalance.getCurrency());
59 if ((-saBalance) < rspEntry.getLimitPeer())
60 send.insert(saBalance.getCurrency());
61 }
62
63 send.erase(badCurrency());
64 receive.erase(badCurrency());
65
66 Json::Value& sendCurrencies =
67 (result[jss::send_currencies] = Json::arrayValue);
68 for (auto const& c : send)
69 sendCurrencies.append(to_string(c));
70
71 Json::Value& recvCurrencies =
72 (result[jss::receive_currencies] = Json::arrayValue);
73 for (auto const& c : receive)
74 recvCurrencies.append(to_string(c));
75
76 return result;
77}
78
79} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Currency const & getCurrency() const
Definition STAmount.h:483
T erase(T... args)
T insert(T... args)
@ arrayValue
array value (ordered list)
Definition json_value.h:26
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:306
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
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.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
Json::Value doAccountCurrencies(RPC::JsonContext &context)