xrpld
Loading...
Searching...
No Matches
AccountCurrencies.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
3#include <xrpld/rpc/detail/TrustLine.h>
4
5#include <xrpl/json/json_value.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/ErrorCodes.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/RPCErr.h>
11#include <xrpl/protocol/STAmount.h>
12#include <xrpl/protocol/UintTypes.h>
13#include <xrpl/protocol/jss.h>
14
15#include <memory>
16#include <set>
17#include <string>
18
19namespace xrpl {
20
21json::Value
23{
24 auto& params = context.params;
25
26 if (!(params.isMember(jss::account) || params.isMember(jss::ident)))
27 return RPC::missingFieldError(jss::account);
28
29 std::string strIdent;
30 if (params.isMember(jss::account))
31 {
32 if (!params[jss::account].isString())
33 return RPC::invalidFieldError(jss::account);
34 strIdent = params[jss::account].asString();
35 }
36 else if (params.isMember(jss::ident))
37 {
38 if (!params[jss::ident].isString())
39 return RPC::invalidFieldError(jss::ident);
40 strIdent = params[jss::ident].asString();
41 }
42
43 // Get the current ledger
45 auto result = RPC::lookupLedger(ledger, context);
46 if (!ledger)
47 return result;
48
49 // Get info on account.
50 auto id = parseBase58<AccountID>(strIdent);
51 if (!id)
52 {
54 return result;
55 }
56 auto const accountID{id.value()};
57
58 if (!ledger->exists(keylet::account(accountID)))
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.get<Issue>().currency);
68 if ((-saBalance) < rspEntry.getLimitPeer())
69 send.insert(saBalance.get<Issue>().currency);
70 }
71
72 send.erase(badCurrency());
73 receive.erase(badCurrency());
74
75 json::Value& sendCurrencies = (result[jss::send_currencies] = json::ValueType::Array);
76 for (auto const& c : send)
77 sendCurrencies.append(to_string(c));
78
79 json::Value& recvCurrencies = (result[jss::receive_currencies] = json::ValueType::Array);
80 for (auto const& c : receive)
81 recvCurrencies.append(to_string(c));
82
83 return result;
84}
85
86} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition TrustLine.cpp:95
constexpr TIss const & get() const
T erase(T... args)
T insert(T... args)
@ Array
array value (ordered list)
Definition json_value.h:25
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:273
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:52
@ RpcActMalformed
Definition ErrorCodes.h:72
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
json::Value doAccountCurrencies(RPC::JsonContext &context)
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
json::Value params
Definition Context.h:43