rippled
Loading...
Searching...
No Matches
Flow.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/helpers/RippleStateHelpers.h>
3#include <xrpl/protocol/IOUAmount.h>
4#include <xrpl/protocol/XRPAmount.h>
5#include <xrpl/tx/paths/Flow.h>
6#include <xrpl/tx/paths/detail/AmountSpec.h>
7#include <xrpl/tx/paths/detail/Steps.h>
8#include <xrpl/tx/paths/detail/StrandFlow.h>
9#include <xrpl/tx/transactors/dex/AMMContext.h>
10
11namespace xrpl {
12
13template <class FlowResult>
14static auto
15finishFlow(PaymentSandbox& sb, Issue const& srcIssue, Issue const& dstIssue, FlowResult&& f)
16{
18 if (isTesSuccess(f.ter))
19 {
20 f.sandbox->apply(sb);
21 }
22 else
23 {
24 result.removableOffers = std::move(f.removableOffers);
25 }
26
27 result.setResult(f.ter);
28 result.actualAmountIn = toSTAmount(f.in, srcIssue);
29 result.actualAmountOut = toSTAmount(f.out, dstIssue);
30
31 return result;
32};
33
34path::RippleCalc::Output
37 STAmount const& deliver,
38 AccountID const& src,
39 AccountID const& dst,
40 STPathSet const& paths,
41 bool defaultPaths,
42 bool partialPayment,
43 bool ownerPaysTransferFee,
44 OfferCrossing offerCrossing,
45 std::optional<Quality> const& limitQuality,
46 std::optional<STAmount> const& sendMax,
47 std::optional<uint256> const& domainID,
49 path::detail::FlowDebugInfo* flowDebugInfo)
50{
51 Issue const srcIssue = [&] {
52 if (sendMax)
53 return sendMax->issue();
54 if (!isXRP(deliver.issue().currency))
55 return Issue(deliver.issue().currency, src);
56 return xrpIssue();
57 }();
58
59 Issue const dstIssue = deliver.issue();
60
61 std::optional<Issue> sendMaxIssue;
62 if (sendMax)
63 sendMaxIssue = sendMax->issue();
64
65 AMMContext ammContext(src, false);
66
67 // convert the paths to a collection of strands. Each strand is the
68 // collection of account->account steps and book steps that may be used in
69 // this payment.
70 auto [toStrandsTer, strands] = toStrands(
71 sb,
72 src,
73 dst,
74 dstIssue,
75 limitQuality,
76 sendMaxIssue,
77 paths,
78 defaultPaths,
79 ownerPaysTransferFee,
80 offerCrossing,
81 ammContext,
82 domainID,
83 j);
84
85 if (!isTesSuccess(toStrandsTer))
86 {
88 result.setResult(toStrandsTer);
89 return result;
90 }
91
92 ammContext.setMultiPath(strands.size() > 1);
93
94 if (j.trace())
95 {
96 j.trace() << "\nsrc: " << src << "\ndst: " << dst << "\nsrcIssue: " << srcIssue
97 << "\ndstIssue: " << dstIssue;
98 j.trace() << "\nNumStrands: " << strands.size();
99 for (auto const& curStrand : strands)
100 {
101 j.trace() << "NumSteps: " << curStrand.size();
102 for (auto const& step : curStrand)
103 {
104 j.trace() << '\n' << *step << '\n';
105 }
106 }
107 }
108
109 bool const srcIsXRP = isXRP(srcIssue.currency);
110 bool const dstIsXRP = isXRP(dstIssue.currency);
111
112 auto const asDeliver = toAmountSpec(deliver);
113
114 // The src account may send either xrp or iou. The dst account may receive
115 // either xrp or iou. Since XRP and IOU amounts are represented by different
116 // types, use templates to tell `flow` about the amount types.
117 if (srcIsXRP && dstIsXRP)
118 {
119 return finishFlow(
120 sb,
121 srcIssue,
122 dstIssue,
123 flow<XRPAmount, XRPAmount>(
124 sb,
125 strands,
126 asDeliver.xrp,
127 partialPayment,
128 offerCrossing,
129 limitQuality,
130 sendMax,
131 j,
132 ammContext,
133 flowDebugInfo));
134 }
135
136 if (srcIsXRP && !dstIsXRP)
137 {
138 return finishFlow(
139 sb,
140 srcIssue,
141 dstIssue,
142 flow<XRPAmount, IOUAmount>(
143 sb,
144 strands,
145 asDeliver.iou,
146 partialPayment,
147 offerCrossing,
148 limitQuality,
149 sendMax,
150 j,
151 ammContext,
152 flowDebugInfo));
153 }
154
155 if (!srcIsXRP && dstIsXRP)
156 {
157 return finishFlow(
158 sb,
159 srcIssue,
160 dstIssue,
161 flow<IOUAmount, XRPAmount>(
162 sb,
163 strands,
164 asDeliver.xrp,
165 partialPayment,
166 offerCrossing,
167 limitQuality,
168 sendMax,
169 j,
170 ammContext,
171 flowDebugInfo));
172 }
173
174 XRPL_ASSERT(!srcIsXRP && !dstIsXRP, "xrpl::flow : neither is XRP");
175 return finishFlow(
176 sb,
177 srcIssue,
178 dstIssue,
179 flow<IOUAmount, IOUAmount>(
180 sb,
181 strands,
182 asDeliver.iou,
183 partialPayment,
184 offerCrossing,
185 limitQuality,
186 sendMax,
187 j,
188 ammContext,
189 flowDebugInfo));
190}
191
192} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream trace() const
Severity stream access functions.
Definition Journal.h:295
Maintains AMM info per overall payment engine execution and individual iteration.
Definition AMMContext.h:16
void setMultiPath(bool fs)
Definition AMMContext.h:49
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
A wrapper which makes credits unavailable to balances.
Issue const & issue() const
Definition STAmount.h:470
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:97
std::pair< TER, std::vector< Strand > > toStrands(ReadView const &sb, AccountID const &src, AccountID const &dst, Issue const &deliver, std::optional< Quality > const &limitQuality, std::optional< Issue > 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:423
bool isXRP(AccountID const &c)
Definition AccountID.h:70
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
static auto finishFlow(PaymentSandbox &sb, Issue const &srcIssue, Issue const &dstIssue, FlowResult &&f)
Definition Flow.cpp:15
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:82
bool isTesSuccess(TER x) noexcept
Definition TER.h:651
AmountSpec toAmountSpec(STAmount const &amt)
Definition AmountSpec.h:145
OfferCrossing
Definition Steps.h:24
void setResult(TER const value)
Definition RippleCalc.h:62
boost::container::flat_set< uint256 > removableOffers
Definition RippleCalc.h:50