rippled
Loading...
Searching...
No Matches
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 <xrpl/ledger/ApplyView.h>
24#include <xrpl/ledger/View.h>
25#include <xrpl/protocol/Quality.h>
26#include <xrpl/protocol/TER.h>
27
28namespace ripple {
29
30template <typename TIn, typename TOut>
31class AMMLiquidity;
32class QualityFunction;
33
38template <typename TIn, typename TOut>
40{
41private:
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 // Current pool balances.
54 TAmounts<TIn, TOut> const balances_;
55 // The Spot Price quality if balances != amounts
56 // else the amounts quality
57 Quality const quality_;
58 // AMM offer can be consumed once at a given iteration
60
61public:
63 AMMLiquidity<TIn, TOut> const& ammLiquidity,
64 TAmounts<TIn, TOut> const& amounts,
65 TAmounts<TIn, TOut> const& balances,
66 Quality const& quality);
67
68 Quality
69 quality() const noexcept
70 {
71 return quality_;
72 }
73
74 Issue const&
75 issueIn() const;
76
77 AccountID const&
78 owner() const;
79
81 key() const
82 {
83 return std::nullopt;
84 }
85
86 TAmounts<TIn, TOut> const&
87 amount() const;
88
89 void
90 consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
91
92 bool
94 {
95 return consumed_;
96 }
97
102 TAmounts<TIn, TOut>
103 limitOut(
104 TAmounts<TIn, TOut> const& offrAmt,
105 TOut const& limit,
106 bool roundUp) const;
107
112 TAmounts<TIn, TOut>
113 limitIn(TAmounts<TIn, TOut> const& offrAmt, TIn const& limit, bool roundUp)
114 const;
115
117 getQualityFunc() const;
118
121 template <typename... Args>
122 static TER
123 send(Args&&... args)
124 {
126 }
127
128 bool
129 isFunded() const
130 {
131 // AMM offer is fully funded by the pool
132 return true;
133 }
134
137 {
138 // AMM doesn't pay transfer fee on Payment tx
139 return {ofrInRate, QUALITY_ONE};
140 }
141
145 bool
146 checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const;
147};
148
149} // namespace ripple
150
151#endif // RIPPLE_APP_AMMOFFER_H_INCLUDED
A generic endpoint for log messages.
Definition Journal.h:60
AMMLiquidity class provides AMM offers to BookStep class.
Represents synthetic AMM offer in BookStep.
Definition AMMOffer.h:40
bool isFunded() const
Definition AMMOffer.h:129
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition AMMOffer.h:123
Quality const quality_
Definition AMMOffer.h:57
AccountID const & owner() const
Definition AMMOffer.cpp:50
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offrAmt, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Definition AMMOffer.cpp:82
static std::pair< std::uint32_t, std::uint32_t > adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
Definition AMMOffer.h:136
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition AMMOffer.h:42
Quality quality() const noexcept
Definition AMMOffer.h:69
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:141
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition AMMOffer.cpp:64
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offrAmt, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Definition AMMOffer.cpp:112
std::optional< uint256 > key() const
Definition AMMOffer.h:81
TAmounts< TIn, TOut > const balances_
Definition AMMOffer.h:54
TAmounts< TIn, TOut > const amounts_
Definition AMMOffer.h:52
bool fully_consumed() const
Definition AMMOffer.h:93
Issue const & issueIn() const
Definition AMMOffer.cpp:43
TAmounts< TIn, TOut > const & amount() const
Definition AMMOffer.cpp:57
QualityFunction getQualityFunc() const
Definition AMMOffer.cpp:131
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:143
A currency issued by an account.
Definition Issue.h:33
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
Definition View.cpp:2174