rippled
AMMOffer.cpp
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 #include <ripple/app/paths/AMMOffer.h>
20 
21 #include <ripple/app/paths/AMMLiquidity.h>
22 #include <ripple/protocol/QualityFunction.h>
23 
24 namespace ripple {
25 
26 template <typename TIn, typename TOut>
28  AMMLiquidity<TIn, TOut> const& ammLiquidity,
29  TAmounts<TIn, TOut> const& amounts,
30  std::optional<TAmounts<TIn, TOut>> const& balances,
31  Quality const& quality)
32  : ammLiquidity_(ammLiquidity)
33  , amounts_(amounts)
34  , balances_(balances)
35  , quality_(quality)
36  , consumed_(false)
37 {
38 }
39 
40 template <typename TIn, typename TOut>
41 Issue const&
43 {
44  return ammLiquidity_.issueIn();
45 }
46 
47 template <typename TIn, typename TOut>
48 Issue const&
50 {
51  return ammLiquidity_.issueOut();
52 }
53 
54 template <typename TIn, typename TOut>
55 AccountID const&
57 {
58  return ammLiquidity_.ammAccount();
59 }
60 
61 template <typename TIn, typename TOut>
62 TAmounts<TIn, TOut> const&
64 {
65  return amounts_;
66 }
67 
68 template <typename TIn, typename TOut>
69 void
71  ApplyView& view,
72  TAmounts<TIn, TOut> const& consumed)
73 {
74  // Consumed offer must be less or equal to the original
75  if (consumed.in > amounts_.in || consumed.out > amounts_.out)
76  Throw<std::logic_error>("Invalid consumed AMM offer.");
77  // AMM pool is updated when the amounts are transferred
78  // in BookStep::consumeOffer().
79 
80  consumed_ = true;
81 
82  // Let the context know AMM offer is consumed
83  ammLiquidity_.context().setAMMUsed();
84 }
85 
86 template <typename TIn, typename TOut>
87 TAmounts<TIn, TOut>
89  TAmounts<TIn, TOut> const& offrAmt,
90  TOut const& limit,
91  bool fixReducedOffers,
92  bool roundUp) const
93 {
94  // Change the offer size proportionally to the original offer quality
95  // to keep the strands quality order unchanged. The taker pays slightly
96  // more for the offer in this case, which results in a slightly higher
97  // pool product than the original pool product. I.e. if the original
98  // pool is poolPays, poolGets and the offer is assetIn, assetOut then
99  // poolPays * poolGets < (poolPays - assetOut) * (poolGets + assetIn)
100  if (ammLiquidity_.multiPath())
101  {
102  if (fixReducedOffers)
103  // It turns out that the ceil_out implementation has some slop in
104  // it. ceil_out_strict removes that slop. But removing that slop
105  // affects transaction outcomes, so the change must be made using
106  // an amendment.
107  return quality().ceil_out_strict(offrAmt, limit, roundUp);
108  return quality().ceil_out(offrAmt, limit);
109  }
110  // Change the offer size according to the conservation function. The offer
111  // quality is increased in this case, but it doesn't matter since there is
112  // only one path.
113  return {swapAssetOut(*balances_, limit, ammLiquidity_.tradingFee()), limit};
114 }
115 
116 template <typename TIn, typename TOut>
117 TAmounts<TIn, TOut>
119  TAmounts<TIn, TOut> const& offrAmt,
120  TIn const& limit) const
121 {
122  // See the comments above in limitOut().
123  if (ammLiquidity_.multiPath())
124  return quality().ceil_in(offrAmt, limit);
125  return {limit, swapAssetIn(*balances_, limit, ammLiquidity_.tradingFee())};
126 }
127 
128 template <typename TIn, typename TOut>
131 {
132  if (ammLiquidity_.multiPath())
133  return QualityFunction{quality(), QualityFunction::CLOBLikeTag{}};
134  return QualityFunction{
135  *balances_, ammLiquidity_.tradingFee(), QualityFunction::AMMTag{}};
136 }
137 
138 template class AMMOffer<STAmount, STAmount>;
139 template class AMMOffer<IOUAmount, IOUAmount>;
140 template class AMMOffer<XRPAmount, IOUAmount>;
141 template class AMMOffer<IOUAmount, XRPAmount>;
142 
143 } // namespace ripple
ripple::AMMOffer::amount
TAmounts< TIn, TOut > const & amount() const
Definition: AMMOffer.cpp:63
ripple::AMMOffer::limitOut
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offrAmt, TOut const &limit, bool fixReducedOffers, bool roundUp) const
Limit out of the provided offer.
Definition: AMMOffer.cpp:88
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::AMMOffer::getQualityFunc
QualityFunction getQualityFunc() const
Definition: AMMOffer.cpp:130
ripple::AMMOffer::issueIn
Issue const & issueIn() const
Definition: AMMOffer.cpp:42
ripple::AMMOffer::limitIn
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offrAmt, TIn const &limit) const
Limit in of the provided offer.
Definition: AMMOffer.cpp:118
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::QualityFunction::AMMTag
Definition: QualityFunction.h:50
ripple::AMMOffer::consume
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition: AMMOffer.cpp:70
ripple::AMMOffer
Represents synthetic AMM offer in BookStep.
Definition: AMMLiquidity.h:35
ripple::AMMOffer::AMMOffer
AMMOffer(AMMLiquidity< TIn, TOut > const &ammLiquidity, TAmounts< TIn, TOut > const &amounts, std::optional< TAmounts< TIn, TOut >> const &balances, Quality const &quality)
Definition: AMMOffer.cpp:27
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::AMMLiquidity
AMMLiquidity class provides AMM offers to BookStep class.
Definition: AMMLiquidity.h:52
ripple::QualityFunction
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
Definition: QualityFunction.h:39
std::optional
ripple::QualityFunction::CLOBLikeTag
Definition: QualityFunction.h:55
ripple::AMMOffer::issueOut
Issue const & issueOut() const
Definition: AMMOffer.cpp:49
ripple::AMMOffer::owner
AccountID const & owner() const
Definition: AMMOffer.cpp:56