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 <xrpld/ledger/ApplyView.h>
24#include <xrpld/ledger/View.h>
25
26#include <xrpl/protocol/Quality.h>
27#include <xrpl/protocol/TER.h>
28
29namespace ripple {
30
31template <typename TIn, typename TOut>
32class AMMLiquidity;
33class QualityFunction;
34
39template <typename TIn, typename TOut>
41{
42private:
44 // Initial offer amounts. It is fibonacci seq generated for multi-path.
45 // If the offer size is set based on the competing CLOB offer then
46 // the AMM offer size is such that if the offer is consumed then
47 // the updated AMM pool SP quality is going to be equal to competing
48 // CLOB offer quality. If there is no competing CLOB offer then
49 // the initial size is set to in=cMax[Native,Value],balances.out.
50 // While this is not a "real" offer it simulates the case of
51 // the swap out of the entire side of the pool, in which case
52 // the swap in amount is infinite.
53 TAmounts<TIn, TOut> const amounts_;
54 // Current pool balances.
55 TAmounts<TIn, TOut> const balances_;
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
61
62public:
64 AMMLiquidity<TIn, TOut> const& ammLiquidity,
65 TAmounts<TIn, TOut> const& amounts,
66 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 AccountID const&
79 owner() const;
80
82 key() const
83 {
84 return std::nullopt;
85 }
86
87 TAmounts<TIn, TOut> const&
88 amount() const;
89
90 void
91 consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
92
93 bool
95 {
96 return consumed_;
97 }
98
103 TAmounts<TIn, TOut>
104 limitOut(
105 TAmounts<TIn, TOut> const& offrAmt,
106 TOut const& limit,
107 bool roundUp) const;
108
113 TAmounts<TIn, TOut>
114 limitIn(TAmounts<TIn, TOut> const& offrAmt, TIn const& limit, bool roundUp)
115 const;
116
118 getQualityFunc() const;
119
122 template <typename... Args>
123 static TER
124 send(Args&&... args)
125 {
126 return accountSend(std::forward<Args>(args)..., WaiveTransferFee::Yes);
127 }
128
129 bool
130 isFunded() const
131 {
132 // AMM offer is fully funded by the pool
133 return true;
134 }
135
138 {
139 // AMM doesn't pay transfer fee on Payment tx
140 return {ofrInRate, QUALITY_ONE};
141 }
142
146 bool
147 checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const;
148};
149
150} // namespace ripple
151
152#endif // RIPPLE_APP_AMMOFFER_H_INCLUDED
A generic endpoint for log messages.
Definition: Journal.h:60
AMMLiquidity class provides AMM offers to BookStep class.
Definition: AMMLiquidity.h:53
Represents synthetic AMM offer in BookStep.
Definition: AMMOffer.h:41
bool isFunded() const
Definition: AMMOffer.h:130
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition: AMMOffer.h:124
Quality const quality_
Definition: AMMOffer.h:58
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:137
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition: AMMOffer.h:43
Quality quality() const noexcept
Definition: AMMOffer.h:70
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:82
TAmounts< TIn, TOut > const balances_
Definition: AMMOffer.h:55
TAmounts< TIn, TOut > const amounts_
Definition: AMMOffer.h:53
bool fully_consumed() const
Definition: AMMOffer.h:94
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:144
A currency issued by an account.
Definition: Issue.h:36
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:26
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Calls static accountSendIOU if saAmount represents Issue.
Definition: View.cpp:1998