rippled
Loading...
Searching...
No Matches
QualityFunction.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_QUALITYFUNCTION_H_INCLUDED
21#define RIPPLE_PROTOCOL_QUALITYFUNCTION_H_INCLUDED
22
23#include <xrpl/basics/Number.h>
24#include <xrpl/protocol/AMMCore.h>
25#include <xrpl/protocol/Quality.h>
26
27namespace ripple {
28
40{
41private:
42 // slope
44 // intercept
46 // seated if QF is for CLOB offer.
48
49public:
50 struct AMMTag
51 {
52 };
53 // AMMOffer for multi-path is like CLOB, i.e. the offer size
54 // changes proportionally to its quality.
56 {
57 };
58 QualityFunction(Quality const& quality, CLOBLikeTag);
59 template <typename TIn, typename TOut>
61 TAmounts<TIn, TOut> const& amounts,
62 std::uint32_t tfee,
63 AMMTag);
64
67 void
68 combine(QualityFunction const& qf);
69
75 outFromAvgQ(Quality const& quality);
76
79 bool
80 isConst() const
81 {
82 return quality_.has_value();
83 }
84
86 quality() const
87 {
88 return quality_;
89 }
90};
91
92template <typename TIn, typename TOut>
94 TAmounts<TIn, TOut> const& amounts,
95 std::uint32_t tfee,
97{
98 if (amounts.in <= beast::zero || amounts.out <= beast::zero)
99 Throw<std::runtime_error>("QualityFunction amounts are 0.");
100 Number const cfee = feeMult(tfee);
101 m_ = -cfee / amounts.in;
102 b_ = amounts.out * cfee / amounts.in;
103}
104
105} // namespace ripple
106
107#endif // RIPPLE_PROTOCOL_QUALITYFUNCTION_H_INCLUDED
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
QualityFunction(Quality const &quality, CLOBLikeTag)
void combine(QualityFunction const &qf)
Combines QF with the next step QF.
std::optional< Number > outFromAvgQ(Quality const &quality)
Find output to produce the requested average quality.
std::optional< Quality > const & quality() const
bool isConst() const
Return true if the quality function is constant.
std::optional< Quality > quality_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Definition AMMCore.h:110
T has_value(T... args)