xrpld
Loading...
Searching...
No Matches
AccountCurrencies.cpp
1#include <xrpld/rpc/handlers/account/AccountCurrencies.h>
2
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
5#include <xrpld/rpc/detail/TrustLine.h>
6
7#include <xrpl/json/json_value.h>
8#include <xrpl/protocol/ErrorCodes.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/jss.h>
13
14#include <rpcspec/Errors.hpp>
15
16#include <expected>
17
18namespace xrpl::rpc {
19
23
24std::expected<AccountCurrenciesHandler::Output, ::rpc::Status>
25AccountCurrenciesHandler::process(Input const& input) const
26{
27 Output output;
28 if (auto const status = getLedger(output.ledger, input.ledger, context_.get());
29 status != ::rpc::Status::kOK)
30 return std::unexpected{status};
31
32 if (!input.account.has_value())
33 {
34 output.error = RpcActMalformed;
35 return output;
36 }
37 auto const& accountID = *input.account;
38
39 if (!output.ledger->exists(keylet::account(accountID)))
41
42 for (auto const& rspEntry : RPCTrustLine::getItems(accountID, *output.ledger))
43 {
44 STAmount const& saBalance = rspEntry.getBalance();
45
46 if (saBalance < rspEntry.getLimit())
47 output.receive.insert(saBalance.get<Issue>().currency);
48 if ((-saBalance) < rspEntry.getLimitPeer())
49 output.send.insert(saBalance.get<Issue>().currency);
50 }
51
52 output.send.erase(badCurrency());
53 output.receive.erase(badCurrency());
54
55 return output;
56}
57
58void
60{
61 injectLedgerFields(*output.ledger, context_.get(), value);
62
63 if (output.error.has_value())
64 {
65 injectError(*output.error, value);
66 return;
67 }
68
69 json::Value& sendCurrencies = (value[jss::send_currencies] = json::ValueType::Array);
70 for (auto const& c : output.send)
71 sendCurrencies.append(to_string(c));
72
73 json::Value& recvCurrencies = (value[jss::receive_currencies] = json::ValueType::Array);
74 for (auto const& c : output.receive)
75 recvCurrencies.append(to_string(c));
76}
77
78} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition TrustLine.cpp:95
constexpr TIss const & get() const
std::reference_wrapper< JsonContext > context_
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
AccountCurrenciesHandler(JsonContext &context)
Construct a handler for a single request.
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
@ Array
array value (ordered list)
Definition json_value.h:32
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
The api_version numbers this server serves.
Definition ApiVersion.h:26
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T has_value(T... args)
The data an account_currencies request produces, written out by writeResult().
std::optional< ErrorCodeI > error
An error written with the ledger fields.
std::set< Currency > receive
Currencies the account can receive.
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
std::set< Currency > send
Currencies the account can send.
T unexpected(T... args)