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