xrpld
Loading...
Searching...
No Matches
QualityFunction.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/protocol/AMMCore.h>
7#include <xrpl/protocol/Quality.h>
8
9#include <cstdint>
10#include <optional>
11#include <stdexcept>
12
13namespace xrpl {
14
27{
28private:
29 // slope
31 // intercept
33 // seated if QF is for CLOB offer.
35
36public:
37 struct AMMTag
38 {
39 };
40 // AMMOffer for multi-path is like CLOB, i.e. the offer size
41 // changes proportionally to its quality.
43 {
44 };
46 template <typename TIn, typename TOut>
48
52 void
53 combine(QualityFunction const& qf);
54
62
69 [[nodiscard]] bool
70 satisfiesAvgQ(Quality const& quality, Number const& out) const;
71
75 [[nodiscard]] bool
76 isConst() const
77 {
78 return quality_.has_value();
79 }
80
81 [[nodiscard]] std::optional<Quality> const&
82 quality() const
83 {
84 return quality_;
85 }
86};
87
88template <typename TIn, typename TOut>
90 TAmounts<TIn, TOut> const& amounts,
91 std::uint32_t tfee,
93{
94 if (amounts.in <= beast::kZero || amounts.out <= beast::kZero)
95 Throw<std::runtime_error>("QualityFunction amounts are 0.");
96 Number const cfee = feeMult(tfee);
97 m_ = -cfee / amounts.in;
98 b_ = amounts.out * cfee / amounts.in;
99}
100
101} // namespace xrpl
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
std::optional< Quality > const & quality() const
QualityFunction(Quality const &quality, CLOBLikeTag)
void combine(QualityFunction const &qf)
Combines QF with the next step QF.
std::optional< Quality > quality_
std::optional< Number > outFromAvgQ(Quality const &quality)
Find output to produce the requested average quality.
bool satisfiesAvgQ(Quality const &quality, Number const &out) const
Return whether out produces at least the requested average quality.
bool isConst() const
Return true if the quality function is constant.
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Definition AMMCore.h:99
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Represents a pair of input and output currencies.
Definition Quality.h:29