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)
15 , amounts_(amounts)
16 , balances_(balances)
17 , quality_(quality)
18 , consumed_(false)
19{
20}
21
22template <typename TIn, typename TOut>
23Issue const&
25{
26 return ammLiquidity_.issueIn();
27}
28
29template <typename TIn, typename TOut>
30AccountID const&
32{
33 return ammLiquidity_.ammAccount();
34}
35
36template <typename TIn, typename TOut>
37TAmounts<TIn, TOut> const&
39{
40 return amounts_;
41}
42
43template <typename TIn, typename TOut>
44void
45AMMOffer<TIn, TOut>::consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed)
46{
47 // Consumed offer must be less or equal to the original
48 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
49 Throw<std::logic_error>("Invalid consumed AMM offer.");
50 // AMM pool is updated when the amounts are transferred
51 // in BookStep::consumeOffer().
52
53 consumed_ = true;
54
55 // Let the context know AMM offer is consumed
56 ammLiquidity_.context().setAMMUsed();
57}
58
59template <typename TIn, typename TOut>
60TAmounts<TIn, TOut>
62 TAmounts<TIn, TOut> const& offerAmount,
63 TOut const& limit,
64 bool roundUp) const
65{
66 // Change the offer size proportionally to the original offer quality
67 // to keep the strands quality order unchanged. The taker pays slightly
68 // more for the offer in this case, which results in a slightly higher
69 // pool product than the original pool product. I.e. if the original
70 // pool is poolPays, poolGets and the offer is assetIn, assetOut then
71 // poolPays * poolGets < (poolPays - assetOut) * (poolGets + assetIn)
72 if (ammLiquidity_.multiPath())
73 {
74 // It turns out that the ceil_out implementation has some slop in
75 // it, which ceil_out_strict removes.
76 return quality().ceil_out_strict(offerAmount, limit, roundUp);
77 }
78 // Change the offer size according to the conservation function. The offer
79 // quality is increased in this case, but it doesn't matter since there is
80 // only one path.
81 return {swapAssetOut(balances_, limit, ammLiquidity_.tradingFee()), limit};
82}
83
84template <typename TIn, typename TOut>
85TAmounts<TIn, TOut>
86AMMOffer<TIn, TOut>::limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp)
87 const
88{
89 // See the comments above in limitOut().
90 if (ammLiquidity_.multiPath())
91 {
92 if (auto const& rules = getCurrentTransactionRules();
93 rules && rules->enabled(fixReducedOffersV2))
94 return quality().ceil_in_strict(offerAmount, limit, roundUp);
95
96 return quality().ceil_in(offerAmount, limit);
97 }
98 return {limit, swapAssetIn(balances_, limit, ammLiquidity_.tradingFee())};
99}
100
101template <typename TIn, typename TOut>
104{
105 if (ammLiquidity_.multiPath())
107 return QualityFunction{balances_, ammLiquidity_.tradingFee(), QualityFunction::AMMTag{}};
108}
109
110template <typename TIn, typename TOut>
111bool
112AMMOffer<TIn, TOut>::checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const
113{
114 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
115 {
116 JLOG(j.error()) << "AMMOffer::checkInvariant failed: consumed " << to_string(consumed.in)
117 << " " << to_string(consumed.out) << " amounts " << to_string(amounts_.in)
118 << " " << to_string(amounts_.out);
119
120 return false;
121 }
122
123 Number const product = balances_.in * balances_.out;
124 auto const newBalances =
125 TAmounts<TIn, TOut>{balances_.in + consumed.in, balances_.out - consumed.out};
126 Number const newProduct = newBalances.in * newBalances.out;
127
128 if (newProduct >= product || withinRelativeDistance(product, newProduct, Number{1, -7}))
129 return true;
130
131 JLOG(j.error()) << "AMMOffer::checkInvariant failed: balances " << to_string(balances_.in)
132 << " " << to_string(balances_.out) << " new balances "
133 << to_string(newBalances.in) << " " << to_string(newBalances.out)
134 << " product/newProduct " << product << " " << newProduct << " diff "
135 << (product != Number{0} ? to_string((product - newProduct) / product)
136 : "undefined");
137 return false;
138}
139
140template class AMMOffer<STAmount, STAmount>;
141template class AMMOffer<IOUAmount, IOUAmount>;
142template class AMMOffer<XRPAmount, IOUAmount>;
143template class AMMOffer<IOUAmount, XRPAmount>;
144
145} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream error() const
Definition Journal.h:319
AMMLiquidity class provides AMM offers to BookStep class.
Represents synthetic AMM offer in BookStep.
Definition AMMOffer.h:20
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:45
TAmounts< TIn, TOut > const & amount() const
Definition AMMOffer.cpp:38
QualityFunction getQualityFunc() const
Definition AMMOffer.cpp:103
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Definition AMMOffer.cpp:61
Issue const & issueIn() const
Definition AMMOffer.cpp:24
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:112
AccountID const & owner() const
Definition AMMOffer.cpp:31
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Definition AMMOffer.cpp:86
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:116
A currency issued by an account.
Definition Issue.h:13
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
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:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:600
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:416
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:484
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:106