xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
tx
paths
RippleCalc.cpp
1
#include <xrpl/tx/paths/RippleCalc.h>
2
3
#include <xrpl/basics/Log.h>
4
#include <xrpl/basics/base_uint.h>
5
#include <xrpl/beast/utility/Zero.h>
6
#include <xrpl/core/ServiceRegistry.h>
7
#include <xrpl/ledger/PaymentSandbox.h>
8
#include <xrpl/protocol/AccountID.h>
9
#include <xrpl/protocol/Asset.h>
10
#include <xrpl/protocol/Quality.h>
11
#include <xrpl/protocol/STAmount.h>
12
#include <xrpl/protocol/STPathSet.h>
13
#include <xrpl/protocol/TER.h>
14
#include <xrpl/tx/paths/Flow.h>
15
#include <xrpl/tx/paths/detail/Steps.h>
16
17
#include <
exception
>
18
#include <
optional
>
19
20
namespace
xrpl::path
{
21
22
RippleCalc::Output
23
RippleCalc::rippleCalculate
(
24
PaymentSandbox
&
view
,
25
26
// Compute paths using this ledger entry set. Up to caller to actually
27
// apply to ledger.
28
29
// Issuer:
30
// XRP: xrpAccount()
31
// non-XRP: uSrcAccountID (for any issuer) or another account with
32
// trust node.
33
STAmount
const
& saMaxAmountReq,
// --> -1 = no limit.
34
35
// Issuer:
36
// XRP: xrpAccount()
37
// non-XRP: uDstAccountID (for any issuer) or another account with
38
// trust node.
39
STAmount
const
& saDstAmountReq,
40
41
AccountID
const
& uDstAccountID,
42
AccountID
const
& uSrcAccountID,
43
44
// A set of paths that are included in the transaction that we'll
45
// explore for liquidity.
46
STPathSet
const
& spsPaths,
47
48
std::optional<UInt256>
const
& domainID,
49
ServiceRegistry
& registry,
50
Input
const
*
const
pInputs)
51
{
52
Output
flowOut;
53
PaymentSandbox
flowSB(&
view
);
54
auto
j = registry.
getJournal
(
"Flow"
);
55
56
{
57
bool
const
defaultPaths = (pInputs ==
nullptr
) ?
true
: pInputs->
defaultPathsAllowed
;
58
59
bool
const
partialPayment = (pInputs ==
nullptr
) ?
false
: pInputs->
partialPaymentAllowed
;
60
61
auto
const
limitQuality = [&]() ->
std::optional<Quality>
{
62
if
(pInputs && pInputs->
limitQuality
&& saMaxAmountReq >
beast::kZero
)
63
return
Quality
{
Amounts
(saMaxAmountReq, saDstAmountReq)};
64
return
std::nullopt;
65
}();
66
67
auto
const
sendMax = [&]() ->
std::optional<STAmount>
{
68
if
(saMaxAmountReq >=
beast::kZero
||
69
!
equalTokens
(saMaxAmountReq.
asset
(), saDstAmountReq.
asset
()) ||
70
saMaxAmountReq.
getIssuer
() != uSrcAccountID)
71
{
72
return
saMaxAmountReq;
73
}
74
return
std::nullopt;
75
}();
76
77
try
78
{
79
flowOut =
flow
(
80
flowSB,
81
saDstAmountReq,
82
uSrcAccountID,
83
uDstAccountID,
84
spsPaths,
85
defaultPaths,
86
partialPayment,
87
false
,
88
OfferCrossing::No
,
89
limitQuality,
90
sendMax,
91
domainID,
92
j,
93
nullptr
);
94
}
95
catch
(
std::exception
& e)
96
{
97
JLOG(j.error()) <<
"Exception from flow: "
<< e.
what
();
98
99
// return a tec so the tx is stored
100
path::RippleCalc::Output
exceptResult;
101
exceptResult.
setResult
(
tecINTERNAL
);
102
return
exceptResult;
103
}
104
}
105
106
j.
debug
() <<
"RippleCalc Result> "
107
<<
" actualIn: "
<< flowOut.
actualAmountIn
108
<<
", actualOut: "
<< flowOut.
actualAmountOut
<<
", result: "
<< flowOut.
result
()
109
<<
", dstAmtReq: "
<< saDstAmountReq <<
", sendMax: "
<< saMaxAmountReq;
110
111
flowSB.
apply
(
view
);
112
return
flowOut;
113
}
114
115
}
// namespace xrpl::path
beast::Journal::debug
Stream debug() const
Definition
Journal.h:344
xrpl::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition
PaymentSandbox.h:149
xrpl::PaymentSandbox::apply
void apply(RawView &to)
Apply changes to base view.
Definition
PaymentSandbox.cpp:453
xrpl::Quality
Represents the logical ratio of output currency to input currency.
Definition
Quality.h:90
xrpl::STAmount
Definition
STAmount.h:46
xrpl::STAmount::asset
Asset const & asset() const
Definition
STAmount.h:496
xrpl::STAmount::getIssuer
AccountID const & getIssuer() const
Definition
STAmount.h:516
xrpl::STPathSet
Definition
STPathSet.h:200
xrpl::ServiceRegistry
Service registry for dependency injection.
Definition
ServiceRegistry.h:98
xrpl::ServiceRegistry::getJournal
virtual beast::Journal getJournal(std::string const &name)=0
xrpl::path::RippleCalc::rippleCalculate
static Output rippleCalculate(PaymentSandbox &view, STAmount const &saMaxAmountReq, STAmount const &saDstAmountReq, AccountID const &uDstAccountID, AccountID const &uSrcAccountID, STPathSet const &spsPaths, std::optional< UInt256 > const &domainID, ServiceRegistry ®istry, Input const *const pInputs=nullptr)
Definition
RippleCalc.cpp:23
xrpl::path::RippleCalc::view
PaymentSandbox & view
Definition
RippleCalc.h:105
exception
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
xrpl::path
Definition
FlowDebugInfo.h:21
xrpl::Amounts
TAmounts< STAmount, STAmount > Amounts
Definition
Quality.h:69
xrpl::flow
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:103
xrpl::AccountID
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition
AccountID.h:34
xrpl::tecINTERNAL
@ tecINTERNAL
Definition
TER.h:318
xrpl::OfferCrossing::No
@ No
Definition
Steps.h:38
xrpl::equalTokens
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition
Asset.h:286
optional
xrpl::path::RippleCalc::Input
Definition
RippleCalc.h:33
xrpl::path::RippleCalc::Input::limitQuality
bool limitQuality
Definition
RippleCalc.h:38
xrpl::path::RippleCalc::Input::defaultPathsAllowed
bool defaultPathsAllowed
Definition
RippleCalc.h:37
xrpl::path::RippleCalc::Input::partialPaymentAllowed
bool partialPaymentAllowed
Definition
RippleCalc.h:36
xrpl::path::RippleCalc::Output
Definition
RippleCalc.h:42
xrpl::path::RippleCalc::Output::actualAmountOut
STAmount actualAmountOut
Definition
RippleCalc.h:49
xrpl::path::RippleCalc::Output::actualAmountIn
STAmount actualAmountIn
Definition
RippleCalc.h:46
xrpl::path::RippleCalc::Output::setResult
void setResult(TER const value)
Definition
RippleCalc.h:68
xrpl::path::RippleCalc::Output::result
TER result() const
Definition
RippleCalc.h:63
std::exception::what
T what(T... args)
Generated by
1.17.0