1#include <xrpld/rpc/handlers/account/AccountChannels.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/basics/strHex.h>
10#include <xrpl/beast/utility/Zero.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/ledger/ReadView.h>
14#include <xrpl/ledger/helpers/DirectoryHelpers.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/LedgerFormats.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STLedgerEntry.h>
22#include <xrpl/protocol/jss.h>
23#include <xrpl/protocol/tokens.h>
24#include <xrpl/resource/Fees.h>
26#include <rpcspec/Errors.hpp>
27#include <rpcspec/handlers/account_channels/Types.hpp>
45appendChannelJson(
SLE const& channel, json::Value& channels)
49 jDst[jss::account] =
to_string(channel[sfAccount]);
50 jDst[jss::destination_account] =
to_string(channel[sfDestination]);
51 jDst[jss::amount] =
channel[sfAmount].getText();
52 jDst[jss::balance] =
channel[sfBalance].getText();
55 PublicKey
const pk(channel[sfPublicKey]);
57 jDst[jss::public_key_hex] =
strHex(pk);
59 jDst[jss::settle_delay] =
channel[sfSettleDelay];
60 if (
auto const& v = channel[~sfExpiration])
61 jDst[jss::expiration] = *v;
62 if (
auto const& v = channel[~sfCancelAfter])
63 jDst[jss::cancel_after] = *v;
64 if (
auto const& v = channel[~sfSourceTag])
65 jDst[jss::source_tag] = *v;
66 if (
auto const& v = channel[~sfDestinationTag])
67 jDst[jss::destination_tag] = *v;
76std::expected<AccountChannelsHandler::Output, ::rpc::Status>
82 if (
auto const status =
getLedger(output.
ledger, input.ledger, context);
83 status != ::rpc::Status::kOK)
85 auto const& ledger = *output.
ledger;
87 auto const accountID = input.account;
93 auto const& destination = input.destinationAccount;
96 auto const parsed = input.marker.has_value()
99 if (!parsed.has_value())
102 UNREACHABLE(
"xrpl::rpc::AccountChannelsHandler::process : marker validated by the spec");
106 auto const [startAfter, startHint] = *parsed;
108 if (input.marker.has_value())
112 auto const sle = ledger.read({
ltANY, startAfter});
130 [&output, &accountID, &destination, &count, &limit, &marker, &nextHint](
135 UNREACHABLE(
"xrpl::rpc::AccountChannelsHandler::process : null SLE");
140 if (++count == limit)
146 if (count <= limit && sle->getType() == ltPAYCHAN &&
147 (*sle)[sfAccount] == accountID &&
148 (!destination.has_value() || *destination == (*sle)[sfDestination]))
162 if (count == limit + 1 && marker.
has_value())
164 output.
limit = limit;
178 if (output.
marker.has_value())
180 value[jss::limit] = output.
limit;
187 for (
auto const& channel : output.
channels)
188 appendChannelJson(*channel, channels);
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.
std::reference_wrapper< JsonContext > context_
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
AccountChannelsHandler(JsonContext &context)
Construct a handler for a single request.
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
Keylet account(AccountID const &id) noexcept
AccountID root.
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountChannels
Limits for the account_channels 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.
::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.
uint256 channel(AccountID const &account, AccountID const &dst, std::uint32_t seqProxyValue)
std::string strHex(FwdIt begin, FwdIt end)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::string to_string(BaseUInt< Bits, Tag > const &a)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
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_channels request produces, written out by writeResult().
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
AccountID account
The account whose channels are listed.
std::vector< SLE::const_pointer > channels
The channels on this page.
std::optional< uint256 > marker
Where the next page starts, if there is one.
std::uint64_t nextHint
The directory page hint written with the marker.
std::uint32_t limit
The limit used; written only with a marker.