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