xrpld
Loading...
Searching...
No Matches
TrustLine.cpp
1#include <xrpld/rpc/detail/TrustLine.h>
2
3#include <xrpl/json/json_value.h>
4#include <xrpl/ledger/ReadView.h>
5#include <xrpl/ledger/helpers/DirectoryHelpers.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/LedgerFormats.h>
8#include <xrpl/protocol/SField.h>
9#include <xrpl/protocol/STAmount.h>
10#include <xrpl/protocol/STLedgerEntry.h>
11
12#include <optional>
13#include <vector>
14
15namespace xrpl {
16
18 : key_(sle->key())
19 , lowLimit_(sle->getFieldAmount(sfLowLimit))
20 , highLimit_(sle->getFieldAmount(sfHighLimit))
21 , balance_(sle->getFieldAmount(sfBalance))
22 , flags_(sle->getFieldU32(sfFlags))
23 , viewLowest_(lowLimit_.getIssuer() == viewAccount)
24{
25 if (!viewLowest_)
26 balance_.negate();
27}
28
31{
33 ret["low_id"] = to_string(lowLimit_.getIssuer());
34 ret["high_id"] = to_string(highLimit_.getIssuer());
35 return ret;
36}
37
40{
41 if (!sle || sle->getType() != ltRIPPLE_STATE)
42 return {};
43 return std::optional{PathFindTrustLine{sle, accountID}};
44}
45
46namespace detail {
47template <class T>
48std::vector<T>
50 AccountID const& accountID,
51 ReadView const& view,
53{
54 std::vector<T> items;
55 forEachItem(view, accountID, [&items, &accountID, &direction](SLE::const_ref sleCur) {
56 auto ret = T::makeItem(accountID, sleCur);
57 if (ret && (direction == LineDirection::Outgoing || !ret->getNoRipple()))
58 items.push_back(std::move(*ret));
59 });
60 // This list may be around for a while, so free up any unneeded
61 // capacity
62 items.shrink_to_fit();
63
64 return items;
65}
66} // namespace detail
67
70 AccountID const& accountID,
71 ReadView const& view,
72 LineDirection direction)
73{
74 return detail::getTrustLineItems<PathFindTrustLine>(accountID, view, direction);
75}
76
78 : TrustLineBase(sle, viewAccount)
79 , lowQualityIn_(sle->getFieldU32(sfLowQualityIn))
80 , lowQualityOut_(sle->getFieldU32(sfLowQualityOut))
81 , highQualityIn_(sle->getFieldU32(sfHighQualityIn))
82 , highQualityOut_(sle->getFieldU32(sfHighQualityOut))
83{
84}
85
88{
89 if (!sle || sle->getType() != ltRIPPLE_STATE)
90 return {};
91 return std::optional{RPCTrustLine{sle, accountID}};
92}
93
95RPCTrustLine::getItems(AccountID const& accountID, ReadView const& view)
96{
97 return detail::getTrustLineItems<RPCTrustLine>(accountID, view);
98}
99
100} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
static std::vector< PathFindTrustLine > getItems(AccountID const &accountID, ReadView const &view, LineDirection direction)
Definition TrustLine.cpp:69
static std::optional< PathFindTrustLine > makeItem(AccountID const &accountID, SLE::const_ref sle)
Definition TrustLine.cpp:39
TrustLineBase(SLE::const_ref sle, AccountID const &viewAccount)
Definition TrustLine.cpp:17
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition TrustLine.cpp:95
static std::optional< RPCTrustLine > makeItem(AccountID const &accountID, SLE::const_ref sle)
Definition TrustLine.cpp:87
A view into a ledger.
Definition ReadView.h:31
std::shared_ptr< STLedgerEntry const > const & const_ref
TrustLineBase(SLE::const_ref sle, AccountID const &viewAccount)
Definition TrustLine.cpp:17
json::Value getJson(int)
Definition TrustLine.cpp:30
uint256 const & key() const
Returns the state map key for the ledger entry.
Definition TrustLine.h:51
STAmount const lowLimit_
Definition TrustLine.h:159
std::uint32_t flags_
Definition TrustLine.h:164
STAmount const highLimit_
Definition TrustLine.h:160
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
std::vector< T > getTrustLineItems(AccountID const &accountID, ReadView const &view, LineDirection direction=LineDirection::Outgoing)
Definition TrustLine.cpp:49
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition TrustLine.h:21
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(SLE::const_ref)> const &f)
Iterate all items in the given directory.
T push_back(T... args)
T shrink_to_fit(T... args)