1#include <xrpld/rpc/handlers/account/AccountOffers.h>
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/detail/RPCHelpers.h>
5#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
6#include <xrpld/rpc/detail/Tuning.h>
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/beast/utility/Zero.h>
10#include <xrpl/beast/utility/instrumentation.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/ReadView.h>
13#include <xrpl/ledger/helpers/DirectoryHelpers.h>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/ErrorCodes.h>
16#include <xrpl/protocol/Indexes.h>
17#include <xrpl/protocol/LedgerFormats.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/jss.h>
22#include <xrpl/resource/Fees.h>
24#include <rpcspec/Errors.hpp>
25#include <rpcspec/handlers/account_offers/Types.hpp>
47 offer->getFieldAmount(sfTakerPays).setJson(obj[jss::taker_pays]);
48 offer->getFieldAmount(sfTakerGets).setJson(obj[jss::taker_gets]);
49 obj[jss::seq] =
offer->getFieldU32(sfSequence);
50 obj[jss::flags] =
offer->getFieldU32(sfFlags);
51 obj[jss::quality] = dirRate.getText();
52 if (
offer->isFieldPresent(sfExpiration))
53 obj[jss::expiration] =
offer->getFieldU32(sfExpiration);
62std::expected<AccountOffersHandler::Output, ::rpc::Status>
68 if (
auto const status =
getLedger(output.
ledger, input.ledger, context);
69 status != ::rpc::Status::kOK)
71 auto const& ledger = *output.
ledger;
73 if (not input.account.has_value())
78 auto const accountID = *input.account;
86 auto const parsed = input.marker.has_value()
89 if (not parsed.has_value())
92 UNREACHABLE(
"xrpl::rpc::AccountOffersHandler::process : marker validated by the spec");
96 auto const [startAfter, startHint] = *parsed;
98 if (input.marker.has_value())
102 auto const sle = ledger.read({
ltANY, startAfter});
120 [&output, &count, &marker, &limit, &nextHint, &accountID](
SLE::ConstRef sle) {
124 UNREACHABLE(
"xrpl::rpc::AccountOffersHandler::process : null SLE");
129 if (++count == limit)
135 if (count <= limit && sle->getType() == ltOFFER)
137 output.
offers.emplace_back(sle);
149 if (count == limit + 1 && marker.
has_value())
151 output.
limit = limit;
174 for (
auto const& offer : output.
offers)
175 appendOfferJson(offer, jsonOffers);
177 if (output.
marker.has_value())
179 value[jss::limit] = output.
limit;
Value & append(Value const &value)
Append value to array at the end.
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
std::reference_wrapper< JsonContext > context_
AccountOffersHandler(JsonContext &context)
Construct a handler for a single request.
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Keylet account(AccountID const &id) noexcept
AccountID root.
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountOffers
Limits for the account_offers command.
The api_version numbers this server serves.
unsigned int clampLimit(unsigned int limit, tuning::LimitRange const &range, Role const &role)
Clamps a requested limit to the range a role may use.
std::uint64_t getStartHint(SLE::ConstRef sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
std::string invalidFieldMessage(std::string const &name)
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.
bool isRelatedToAccount(ReadView const &ledger, SLE::ConstRef sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
std::optional< std::pair< uint256, std::uint64_t > > parseAccountMarker(std::string_view marker)
Splits an account marker into the entry to start after and its start hint.
std::uint64_t getQuality(UInt256 const &uBase)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::string to_string(BaseUInt< Bits, Tag > const &a)
STAmount amountFromQuality(std::uint64_t rate)
bool forEachItemAfter(ReadView const &view, Keylet const &root, UInt256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::ConstRef)> const &f)
Iterate all items after an item in the given directory.
@ ltANY
A special type, matching any ledger entry type.
The data an account_offers request produces, written out by writeResult().
std::vector< SLE::const_pointer > offers
The offers on this page.
AccountID account
The account whose offers are listed.
std::optional< ErrorCodeI > error
An error written with the ledger fields.
std::uint32_t limit
The limit used; written only with a marker.
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
std::uint64_t nextHint
The directory page hint written with the marker.
std::optional< uint256 > marker
Where the next page starts, if there is one.