xrpld
Loading...
Searching...
No Matches
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
20namespace xrpl::path {
21
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,
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
Stream debug() const
Definition Journal.h:297
A wrapper which makes credits unavailable to balances.
void apply(RawView &to)
Apply changes to base view.
Represents the logical ratio of output currency to input currency.
Definition Quality.h:91
Asset const & asset() const
Definition STAmount.h:478
AccountID const & getIssuer() const
Definition STAmount.h:498
Service registry for dependency injection.
virtual beast::Journal getJournal(std::string const &name)=0
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 &registry, Input const *const pInputs=nullptr)
PaymentSandbox & view
Definition RippleCalc.h:99
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:64
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
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
@ tecINTERNAL
Definition TER.h:308
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition Asset.h:275
void setResult(TER const value)
Definition RippleCalc.h:62
T what(T... args)