rippled
AMMOffer.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_APP_AMMOFFER_H_INCLUDED
21 #define RIPPLE_APP_AMMOFFER_H_INCLUDED
22 
23 #include <ripple/ledger/ApplyView.h>
24 #include <ripple/ledger/View.h>
25 #include <ripple/protocol/Quality.h>
26 #include <ripple/protocol/TER.h>
27 
28 namespace ripple {
29 
30 template <typename TIn, typename TOut>
31 class AMMLiquidity;
32 class QualityFunction;
33 
38 template <typename TIn, typename TOut>
39 class AMMOffer
40 {
41 private:
43  // Initial offer amounts. It is fibonacci seq generated for multi-path.
44  // If the offer size is set based on the competing CLOB offer then
45  // the AMM offer size is such that if the offer is consumed then
46  // the updated AMM pool SP quality is going to be equal to competing
47  // CLOB offer quality. If there is no competing CLOB offer then
48  // the initial size is set to in=cMax[Native,Value],balances.out.
49  // While this is not a "real" offer it simulates the case of
50  // the swap out of the entire side of the pool, in which case
51  // the swap in amount is infinite.
52  TAmounts<TIn, TOut> const amounts_;
53  // If seated then current pool balances. Used in one-path limiting steps
54  // to swap in/out.
56  // The Spot Price quality if balances != amounts
57  // else the amounts quality
58  Quality const quality_;
59  // AMM offer can be consumed once at a given iteration
60  bool consumed_;
61 
62 public:
63  AMMOffer(
64  AMMLiquidity<TIn, TOut> const& ammLiquidity,
65  TAmounts<TIn, TOut> const& amounts,
66  std::optional<TAmounts<TIn, TOut>> const& balances,
67  Quality const& quality);
68 
69  Quality
70  quality() const noexcept
71  {
72  return quality_;
73  }
74 
75  Issue const&
76  issueIn() const;
77 
78  Issue const&
79  issueOut() const;
80 
81  AccountID const&
82  owner() const;
83 
85  key() const
86  {
87  return std::nullopt;
88  }
89 
90  TAmounts<TIn, TOut> const&
91  amount() const;
92 
93  void
94  consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
95 
96  bool
98  {
99  return consumed_;
100  }
101 
106  TAmounts<TIn, TOut>
107  limitOut(
108  TAmounts<TIn, TOut> const& offrAmt,
109  TOut const& limit,
110  bool fixReducedOffers,
111  bool roundUp) const;
112 
117  TAmounts<TIn, TOut>
118  limitIn(TAmounts<TIn, TOut> const& offrAmt, TIn const& limit) const;
119 
121  getQualityFunc() const;
122 
125  template <typename... Args>
126  static TER
127  send(Args&&... args)
128  {
129  return accountSend(std::forward<Args>(args)..., WaiveTransferFee::Yes);
130  }
131 
132  bool
133  isFunded() const
134  {
135  // AMM offer is fully funded by the pool
136  return true;
137  }
138 
140  adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
141  {
142  // AMM doesn't pay transfer fee on Payment tx
143  return {ofrInRate, QUALITY_ONE};
144  }
145 };
146 
147 } // namespace ripple
148 
149 #endif // RIPPLE_APP_AMMOFFER_H_INCLUDED
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::AMMOffer::ammLiquidity_
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition: AMMOffer.h:42
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Definition: View.cpp:1142
ripple::WaiveTransferFee::Yes
@ Yes
std::pair
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::AMMOffer::isFunded
bool isFunded() const
Definition: AMMOffer.h:133
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::AMMOffer::quality_
const Quality quality_
Definition: AMMOffer.h:58
ripple::AMMOffer::amounts_
const TAmounts< TIn, TOut > amounts_
Definition: AMMOffer.h:52
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::TERSubset< CanCvtToTER >
ripple::AMMOffer::consume
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition: AMMOffer.cpp:70
ripple::AMMOffer::adjustRates
static std::pair< std::uint32_t, std::uint32_t > adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
Definition: AMMOffer.h:140
std::uint32_t
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::AMMOffer::quality
Quality quality() const noexcept
Definition: AMMOffer.h:70
ripple::AMMLiquidity
AMMLiquidity class provides AMM offers to BookStep class.
Definition: AMMLiquidity.h:52
ripple::AMMOffer::fully_consumed
bool fully_consumed() const
Definition: AMMOffer.h:97
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::AMMOffer::consumed_
bool consumed_
Definition: AMMOffer.h:60
ripple::AMMOffer::issueOut
Issue const & issueOut() const
Definition: AMMOffer.cpp:49
ripple::AMMOffer::send
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition: AMMOffer.h:127
ripple::AMMOffer::key
std::optional< uint256 > key() const
Definition: AMMOffer.h:85
ripple::AMMOffer::owner
AccountID const & owner() const
Definition: AMMOffer.cpp:56
ripple::AMMOffer::balances_
const std::optional< TAmounts< TIn, TOut > > balances_
Definition: AMMOffer.h:55