rippled
Loading...
Searching...
No Matches
AMMOffer.cpp
1#include <xrpld/app/paths/AMMLiquidity.h>
2#include <xrpld/app/paths/AMMOffer.h>
3
4#include <xrpl/protocol/QualityFunction.h>
5
6namespace xrpl {
7
8template <typename TIn, typename TOut>
10 AMMLiquidity<TIn, TOut> const& ammLiquidity,
11 TAmounts<TIn, TOut> const& amounts,
12 TAmounts<TIn, TOut> const& balances,
13 Quality const& quality)
14 : ammLiquidity_(ammLiquidity), amounts_(amounts), balances_(balances), quality_(quality), consumed_(false)
15{
16}
17
18template <typename TIn, typename TOut>
19Issue const&
21{
22 return ammLiquidity_.issueIn();
23}
24
25template <typename TIn, typename TOut>
26AccountID const&
28{
29 return ammLiquidity_.ammAccount();
30}
31
32template <typename TIn, typename TOut>
33TAmounts<TIn, TOut> const&
35{
36 return amounts_;
37}
38
39template <typename TIn, typename TOut>
40void
41AMMOffer<TIn, TOut>::consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed)
42{
43 // Consumed offer must be less or equal to the original
44 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
45 Throw<std::logic_error>("Invalid consumed AMM offer.");
46 // AMM pool is updated when the amounts are transferred
47 // in BookStep::consumeOffer().
48
49 consumed_ = true;
50
51 // Let the context know AMM offer is consumed
52 ammLiquidity_.context().setAMMUsed();
53}
54
55template <typename TIn, typename TOut>
56TAmounts<TIn, TOut>
57AMMOffer<TIn, TOut>::limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp) const
58{
59 // Change the offer size proportionally to the original offer quality
60 // to keep the strands quality order unchanged. The taker pays slightly
61 // more for the offer in this case, which results in a slightly higher
62 // pool product than the original pool product. I.e. if the original
63 // pool is poolPays, poolGets and the offer is assetIn, assetOut then
64 // poolPays * poolGets < (poolPays - assetOut) * (poolGets + assetIn)
65 if (ammLiquidity_.multiPath())
66 {
67 // It turns out that the ceil_out implementation has some slop in
68 // it, which ceil_out_strict removes.
69 return quality().ceil_out_strict(offerAmount, limit, roundUp);
70 }
71 // Change the offer size according to the conservation function. The offer
72 // quality is increased in this case, but it doesn't matter since there is
73 // only one path.
74 return {swapAssetOut(balances_, limit, ammLiquidity_.tradingFee()), limit};
75}
76
77template <typename TIn, typename TOut>
78TAmounts<TIn, TOut>
79AMMOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp) const
80{
81 // See the comments above in limitOut().
82 if (ammLiquidity_.multiPath())
83 {
84 if (auto const& rules = getCurrentTransactionRules(); rules && rules->enabled(fixReducedOffersV2))
85 return quality().ceil_in_strict(offerAmount, limit, roundUp);
86
87 return quality().ceil_in(offerAmount, limit);
88 }
89 return {limit, swapAssetIn(balances_, limit, ammLiquidity_.tradingFee())};
90}
91
92template <typename TIn, typename TOut>
95{
96 if (ammLiquidity_.multiPath())
98 return QualityFunction{balances_, ammLiquidity_.tradingFee(), QualityFunction::AMMTag{}};
99}
100
101template <typename TIn, typename TOut>
102bool
103AMMOffer<TIn, TOut>::checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const
104{
105 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
106 {
107 JLOG(j.error()) << "AMMOffer::checkInvariant failed: consumed " << to_string(consumed.in) << " "
108 << to_string(consumed.out) << " amounts " << to_string(amounts_.in) << " "
109 << to_string(amounts_.out);
110
111 return false;
112 }
113
114 Number const product = balances_.in * balances_.out;
115 auto const newBalances = TAmounts<TIn, TOut>{balances_.in + consumed.in, balances_.out - consumed.out};
116 Number const newProduct = newBalances.in * newBalances.out;
117
118 if (newProduct >= product || withinRelativeDistance(product, newProduct, Number{1, -7}))
119 return true;
120
121 JLOG(j.error()) << "AMMOffer::checkInvariant failed: balances " << to_string(balances_.in) << " "
122 << to_string(balances_.out) << " new balances " << to_string(newBalances.in) << " "
123 << to_string(newBalances.out) << " product/newProduct " << product << " " << newProduct << " diff "
124 << (product != Number{0} ? to_string((product - newProduct) / product) : "undefined");
125 return false;
126}
127
128template class AMMOffer<STAmount, STAmount>;
129template class AMMOffer<IOUAmount, IOUAmount>;
130template class AMMOffer<XRPAmount, IOUAmount>;
131template class AMMOffer<IOUAmount, XRPAmount>;
132
133} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream error() const
Definition Journal.h:319
AMMLiquidity class provides AMM offers to BookStep class.
Represents synthetic AMM offer in BookStep.
Definition AMMOffer.h:21
AMMOffer(AMMLiquidity< TIn, TOut > const &ammLiquidity, TAmounts< TIn, TOut > const &amounts, TAmounts< TIn, TOut > const &balances, Quality const &quality)
Definition AMMOffer.cpp:9
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition AMMOffer.cpp:41
TAmounts< TIn, TOut > const & amount() const
Definition AMMOffer.cpp:34
QualityFunction getQualityFunc() const
Definition AMMOffer.cpp:94
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Definition AMMOffer.cpp:57
Issue const & issueIn() const
Definition AMMOffer.cpp:20
bool checkInvariant(TAmounts< TIn, TOut > const &consumed, beast::Journal j) const
Check the new pool product is greater or equal to the old pool product or if decreases then within so...
Definition AMMOffer.cpp:103
AccountID const & owner() const
Definition AMMOffer.cpp:27
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Definition AMMOffer.cpp:79
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:115
A currency issued by an account.
Definition Issue.h:14
Number is a floating point type that can represent a wide range of values.
Definition Number.h:208
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:31
TOut swapAssetIn(TAmounts< TIn, TOut > const &pool, TIn const &assetIn, std::uint16_t tfee)
AMM pool invariant - the product (A * B) after swap in/out has to remain at least the same: (A + in) ...
Definition AMMHelpers.h:396
TIn swapAssetOut(TAmounts< TIn, TOut > const &pool, TOut const &assetOut, std::uint16_t tfee)
Swap assetOut out of the pool and swap in a proportional amount of the other asset.
Definition AMMHelpers.h:464
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.
Definition AMMHelpers.h:99