rippled
Loading...
Searching...
No Matches
AMMOffer.h
1#pragma once
2
3#include <xrpl/ledger/ApplyView.h>
4#include <xrpl/ledger/View.h>
5#include <xrpl/protocol/Quality.h>
6#include <xrpl/protocol/TER.h>
7
8namespace xrpl {
9
10template <typename TIn, typename TOut>
11class AMMLiquidity;
12class QualityFunction;
13
18template <typename TIn, typename TOut>
20{
21private:
23 // Initial offer amounts. It is fibonacci seq generated for multi-path.
24 // If the offer size is set based on the competing CLOB offer then
25 // the AMM offer size is such that if the offer is consumed then
26 // the updated AMM pool SP quality is going to be equal to competing
27 // CLOB offer quality. If there is no competing CLOB offer then
28 // the initial size is set to in=cMax[Native,Value],balances.out.
29 // While this is not a "real" offer it simulates the case of
30 // the swap out of the entire side of the pool, in which case
31 // the swap in amount is infinite.
32 TAmounts<TIn, TOut> const amounts_;
33 // Current pool balances.
34 TAmounts<TIn, TOut> const balances_;
35 // The Spot Price quality if balances != amounts
36 // else the amounts quality
37 Quality const quality_;
38 // AMM offer can be consumed once at a given iteration
40
41public:
43 AMMLiquidity<TIn, TOut> const& ammLiquidity,
44 TAmounts<TIn, TOut> const& amounts,
45 TAmounts<TIn, TOut> const& balances,
46 Quality const& quality);
47
48 Quality
49 quality() const noexcept
50 {
51 return quality_;
52 }
53
54 Issue const&
55 issueIn() const;
56
57 AccountID const&
58 owner() const;
59
61 key() const
62 {
63 return std::nullopt;
64 }
65
66 TAmounts<TIn, TOut> const&
67 amount() const;
68
69 void
70 consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
71
72 bool
74 {
75 return consumed_;
76 }
77
82 TAmounts<TIn, TOut>
83 limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp) const;
84
89 TAmounts<TIn, TOut>
90 limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp) const;
91
93 getQualityFunc() const;
94
97 template <typename... Args>
98 static TER
99 send(Args&&... args)
100 {
102 }
103
104 bool
105 isFunded() const
106 {
107 // AMM offer is fully funded by the pool
108 return true;
109 }
110
113 {
114 // AMM doesn't pay transfer fee on Payment tx
115 return {ofrInRate, QUALITY_ONE};
116 }
117
121 bool
122 checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const;
123};
124
125} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
AMMLiquidity class provides AMM offers to BookStep class.
Represents synthetic AMM offer in BookStep.
Definition AMMOffer.h:20
static std::pair< std::uint32_t, std::uint32_t > adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
Definition AMMOffer.h:112
Quality const quality_
Definition AMMOffer.h:37
TAmounts< TIn, TOut > const amounts_
Definition AMMOffer.h:32
bool fully_consumed() const
Definition AMMOffer.h:73
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition AMMOffer.cpp:41
TAmounts< TIn, TOut > const & amount() const
Definition AMMOffer.cpp:34
std::optional< uint256 > key() const
Definition AMMOffer.h:61
QualityFunction getQualityFunc() const
Definition AMMOffer.cpp:94
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Definition AMMOffer.cpp:57
Issue const & issueIn() const
Definition AMMOffer.cpp:20
TAmounts< TIn, TOut > const balances_
Definition AMMOffer.h:34
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition AMMOffer.h:99
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition AMMOffer.h:22
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:103
AccountID const & owner() const
Definition AMMOffer.cpp:27
Quality quality() const noexcept
Definition AMMOffer.h:49
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Definition AMMOffer.cpp:79
bool isFunded() const
Definition AMMOffer.h:105
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:114
A currency issued by an account.
Definition Issue.h:13
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:5
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:2446