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