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{
59 Asset const srcAsset = [&] -> Asset {
60 if (sendMax)
61 return sendMax->asset();
62 return deliver.asset().visit(
63 [&](Issue const& issue) -> Asset {
64 if (isXRP(issue))
65 return xrpIssue();
66 return Issue(issue.currency, src);
67 },
68 [&](MPTIssue const&) { return deliver.asset(); });
69 }();
70
71 Asset const dstAsset = deliver.asset();
72
73 std::optional<Asset> sendMaxAsset;
74 if (sendMax)
75 sendMaxAsset = sendMax->asset();
76
77 AMMContext ammContext(src, false);
78
79 // convert the paths to a collection of strands. Each strand is the
80 // collection of account->account steps and book steps that may be used in
81 // this payment.
82 auto [toStrandsTer, strands] = toStrands(
83 sb,
84 src,
85 dst,
86 dstAsset,
87 limitQuality,
88 sendMaxAsset,
89 paths,
90 defaultPaths,
91 ownerPaysTransferFee,
92 offerCrossing,
93 ammContext,
94 domainID,
95 j);
96
97 if (!isTesSuccess(toStrandsTer))
98 {
100 result.setResult(toStrandsTer);
101 return result;
102 }
103
104 ammContext.setMultiPath(strands.size() > 1);
105
106 if (j.trace())
107 {
108 j.trace() << "\nsrc: " << src << "\ndst: " << dst << "\nsrcAsset: " << srcAsset
109 << "\ndstAsset: " << dstAsset;
110 j.trace() << "\nNumStrands: " << strands.size();
111 for (auto const& curStrand : strands)
112 {
113 j.trace() << "NumSteps: " << curStrand.size();
114 for (auto const& step : curStrand)
115 {
116 j.trace() << '\n' << *step << '\n';
117 }
118 }
119 }
120
121 // The src account may send either xrp,iou,or mpt. The dst account may
122 // receive either xrp,iou, or mpt. Since XRP, IOU, and MPT amounts are
123 // represented by different types, use templates to tell `flow` about the
124 // amount types.
125 return std::visit(
126 [&, &strands = strands]<typename TIn, typename TOut>(TIn const&, TOut const&) {
127 using TInAmount = TIn::Amount;
128 using TOutAmount = TOut::Amount;
129 return finishFlow(
130 sb,
131 srcAsset,
132 dstAsset,
134 sb,
135 strands,
136 get<TOutAmount>(deliver),
137 partialPayment,
138 offerCrossing,
139 limitQuality,
140 sendMax,
141 j,
142 ammContext));
143 },
144 srcAsset.getAmountType(),
145 dstAsset.getAmountType());
146}
147
148} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
Maintains AMM info per overall payment engine execution and individual iteration.
Definition AMMContext.h:17
void setMultiPath(bool fs)
Definition AMMContext.h:51
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:117
constexpr AmtType getAmountType() const
Definition Asset.h:215
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
A wrapper which makes credits unavailable to balances.
Asset const & asset() const
Definition STAmount.h:496
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
bool isXRP(AccountID const &c)
Definition AccountID.h:84
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
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
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:101
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
OfferCrossing
Definition Steps.h:41
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:64
boost::container::flat_set< UInt256 > removableOffers
Definition RippleCalc.h:52
T visit(T... args)