xrpld
Loading...
Searching...
No Matches
paths.cpp
1#include <test/jtx/paths.h>
2
3#include <test/jtx/Env.h>
4#include <test/jtx/JTx.h>
5#include <test/jtx/amount.h>
6
7#include <xrpld/rpc/detail/AssetCache.h>
8#include <xrpld/rpc/detail/Pathfinder.h>
9
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Issue.h>
14#include <xrpl/protocol/MPTIssue.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/UintTypes.h>
18#include <xrpl/protocol/jss.h>
19
20#include <memory>
21#include <optional>
22
23namespace xrpl::test::jtx {
24
25void
26Paths::operator()(Env& env, JTx& jt) const
27{
28 auto& jv = jt.jv;
29 auto const from = env.lookup(jv[jss::Account].asString());
30 auto const to = env.lookup(jv[jss::Destination].asString());
31 auto const amount = amountFromJson(sfAmount, jv[jss::Amount]);
32
34 if (jv.isMember(sfDomainID.jsonName))
35 {
36 if (!jv[sfDomainID.jsonName].isString())
37 return;
38 uint256 num;
39 auto const s = jv[sfDomainID.jsonName].asString();
40 if (num.parseHex(s))
41 domain = num;
42 }
43
44 Pathfinder pf(
45 std::make_shared<AssetCache>(env.current(), env.app().getJournal("AssetCache")),
46 from,
47 to,
48 in_,
49 in_.getIssuer(),
50 amount,
51 std::nullopt,
52 domain,
53 env.app());
54 if (!pf.findPaths(depth_))
55 return;
56
57 STPath fp;
59 auto const found = pf.getBestPaths(limit_, fp, {}, in_.getIssuer());
60
61 // VFALCO TODO API to allow caller to examine the STPathSet
62 // VFALCO isDefault should be renamed to empty()
63 if (!found.isDefault())
64 jv[jss::Paths] = found.getJson(JsonOptions::Values::None);
65}
66
67//------------------------------------------------------------------------------
68
73
76{
77 return jv_.append(json::ValueType::Object);
78}
79
80void
81Path::appendOne(Account const& account)
82{
83 appendOne(account.id());
84}
85
86void
88{
89 auto& jv = create();
90 jv["account"] = toBase58(account);
91}
92
93void
95{
96 auto& jv = create();
97 jv["currency"] = to_string(iou.currency);
98 jv["account"] = toBase58(iou.account);
99}
100
101void
103{
104 auto& jv = create();
105 book.asset.visit(
106 [&](Issue const& issue) {
107 jv["currency"] = to_string(issue.currency);
108 jv["issuer"] = toBase58(issue.account);
109 },
110 [&](MPTIssue const& issue) { jv["mpt_issuance_id"] = to_string(issue.getMptID()); });
111}
112
113void
114Path::operator()(Env& env, JTx& jt) const
115{
116 jt.jv["Paths"].append(jv_);
117}
118
119} // namespace xrpl::test::jtx
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
Calculates payment paths.
Definition Pathfinder.h:22
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
json::Value getJson(JsonOptions) const override
json::Value getJson(JsonOptions) const
virtual beast::Journal getJournal(std::string const &name)=0
Immutable cryptographic account descriptor.
Definition jtx/Account.h:17
A transaction testing environment.
Definition Env.h:143
Application & app()
Definition Env.h:280
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
Definition Env.cpp:180
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:353
Converts to IOU Issue or STAmount.
json::Value jv_
Definition paths.h:41
void operator()(Env &, JTx &jt) const
Definition paths.cpp:114
json::Value & create()
Definition paths.cpp:75
void appendOne(Account const &account)
Definition paths.cpp:81
unsigned int limit_
Definition paths.h:20
void operator()(Env &, JTx &jt) const
Definition paths.cpp:26
T make_shared(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
STAmount amountFromJson(SField const &name, json::Value const &v)
Definition STAmount.cpp:916
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
BaseUInt< 256 > uint256
Definition base_uint.h:562
Execution context for applying a JSON transaction.
Definition JTx.h:23
json::Value jv
Definition JTx.h:24