xrpld
Loading...
Searching...
No Matches
Flow.cpp
1#include <xrpl/tx/paths/Flow.h>
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Asset.h>
7#include <xrpl/protocol/Issue.h>
8#include <xrpl/protocol/MPTIssue.h>
9#include <xrpl/protocol/Quality.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STPathSet.h>
12#include <xrpl/tx/paths/RippleCalc.h>
13#include <xrpl/tx/paths/detail/Steps.h>
14#include <xrpl/tx/paths/detail/StrandFlow.h>
15#include <xrpl/tx/transactors/dex/AMMContext.h>
16
17#include <optional>
18#include <variant>
19
20namespace xrpl {
21
22template <class FlowResult>
23static auto
24finishFlow(PaymentSandbox& sb, Asset const& srcAsset, Asset const& dstAsset, FlowResult&& f)
25{
27 if (isTesSuccess(f.ter))
28 {
29 f.sandbox->apply(sb); // NOLINT(bugprone-unchecked-optional-access) sandbox set on success
30 }
31 else
32 {
33 result.removableOffers = std::move(f.removableOffers);
34 }
35
36 result.setResult(f.ter);
37 result.actualAmountIn = toSTAmount(f.in, srcAsset);
38 result.actualAmountOut = toSTAmount(f.out, dstAsset);
39
40 return result;
41};
42
43path::RippleCalc::Output
46 STAmount const& deliver,
47 AccountID const& src,
48 AccountID const& dst,
49 STPathSet const& paths,
50 bool defaultPaths,
51 bool partialPayment,
52 bool ownerPaysTransferFee,
53 OfferCrossing offerCrossing,
54 std::optional<Quality> const& limitQuality,
55 std::optional<STAmount> const& sendMax,
56 std::optional<uint256> const& domainID,
58 path::detail::FlowDebugInfo* flowDebugInfo)
59{
60 Asset const srcAsset = [&]() -> Asset {
61 if (sendMax)
62 return sendMax->asset();
63 return deliver.asset().visit(
64 [&](Issue const& issue) -> Asset {
65 if (isXRP(issue))
66 return xrpIssue();
67 return Issue(issue.currency, src);
68 },
69 [&](MPTIssue const&) { return deliver.asset(); });
70 }();
71
72 Asset const dstAsset = deliver.asset();
73
74 std::optional<Asset> sendMaxAsset;
75 if (sendMax)
76 sendMaxAsset = sendMax->asset();
77
78 AMMContext ammContext(src, false);
79
80 // convert the paths to a collection of strands. Each strand is the
81 // collection of account->account steps and book steps that may be used in
82 // this payment.
83 auto [toStrandsTer, strands] = toStrands(
84 sb,
85 src,
86 dst,
87 dstAsset,
88 limitQuality,
89 sendMaxAsset,
90 paths,
91 defaultPaths,
92 ownerPaysTransferFee,
93 offerCrossing,
94 ammContext,
95 domainID,
96 j);
97
98 if (!isTesSuccess(toStrandsTer))
99 {
101 result.setResult(toStrandsTer);
102 return result;
103 }
104
105 ammContext.setMultiPath(strands.size() > 1);
106
107 if (j.trace())
108 {
109 j.trace() << "\nsrc: " << src << "\ndst: " << dst << "\nsrcAsset: " << srcAsset
110 << "\ndstAsset: " << dstAsset;
111 j.trace() << "\nNumStrands: " << strands.size();
112 for (auto const& curStrand : strands)
113 {
114 j.trace() << "NumSteps: " << curStrand.size();
115 for (auto const& step : curStrand)
116 {
117 j.trace() << '\n' << *step << '\n';
118 }
119 }
120 }
121
122 // The src account may send either xrp,iou,or mpt. The dst account may
123 // receive either xrp,iou, or mpt. Since XRP, IOU, and MPT amounts are
124 // represented by different types, use templates to tell `flow` about the
125 // amount types.
126 return std::visit(
127 [&, &strands = strands]<typename TIn, typename TOut>(TIn const&, TOut const&) {
128 using TIn_ = TIn::amount_type;
129 using TOut_ = TOut::amount_type;
130 return finishFlow(
131 sb,
132 srcAsset,
133 dstAsset,
135 sb,
136 strands,
137 get<TOut_>(deliver),
138 partialPayment,
139 offerCrossing,
140 limitQuality,
141 sendMax,
142 j,
143 ammContext,
144 flowDebugInfo));
145 },
146 srcAsset.getAmountType(),
147 dstAsset.getAmountType());
148}
149
150} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream trace() const
Severity stream access functions.
Definition Journal.h:291
Maintains AMM info per overall payment engine execution and individual iteration.
Definition AMMContext.h:16
void setMultiPath(bool fs)
Definition AMMContext.h:50
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:107
constexpr AmtType getAmountType() const
Definition Asset.h:204
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
A wrapper which makes credits unavailable to balances.
Asset const & asset() const
Definition STAmount.h:478
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::pair< TER, std::vector< Strand > > toStrands(ReadView const &sb, AccountID const &src, AccountID const &dst, Asset const &deliver, std::optional< Quality > const &limitQuality, std::optional< Asset > const &sendMax, STPathSet const &paths, bool addDefaultPath, bool ownerPaysTransferFee, OfferCrossing offerCrossing, AMMContext &ammContext, std::optional< uint256 > const &domainID, beast::Journal j)
Create a Strand for each specified path (including the default path, if indicated).
Definition PaySteps.cpp:574
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
bool isXRP(AccountID const &c)
Definition AccountID.h:70
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
StrandResult< TInAmt, TOutAmt > flow(PaymentSandbox const &baseView, Strand const &strand, std::optional< TInAmt > const &maxIn, TOutAmt const &out, beast::Journal j)
Request out amount from a strand.
Definition StrandFlow.h:81
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
OfferCrossing
Definition Steps.h:24
static auto finishFlow(PaymentSandbox &sb, Asset const &srcAsset, Asset const &dstAsset, FlowResult &&f)
Definition Flow.cpp:24
STAmount toSTAmount(IOUAmount const &iou, Asset const &asset)
void setResult(TER const value)
Definition RippleCalc.h:62
boost::container::flat_set< uint256 > removableOffers
Definition RippleCalc.h:50
T visit(T... args)