xrpld
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/Concepts.h>
7#include <xrpl/protocol/Quality.h>
8#include <xrpl/protocol/TER.h>
9
10namespace xrpl {
11
12template <typename TIn, typename TOut>
13class AMMLiquidity;
14class QualityFunction;
15
20template <StepAmount TIn, StepAmount TOut>
22{
23private:
25 // Initial offer amounts. It is fibonacci seq generated for multi-path.
26 // If the offer size is set based on the competing CLOB offer then
27 // the AMM offer size is such that if the offer is consumed then
28 // the updated AMM pool SP quality is going to be equal to competing
29 // CLOB offer quality. If there is no competing CLOB offer then
30 // the initial size is set to in=cMax[Native,Value],balances.out.
31 // While this is not a "real" offer it simulates the case of
32 // the swap out of the entire side of the pool, in which case
33 // the swap in amount is infinite.
35 // Current pool balances.
37 // The Spot Price quality if balances != amounts
38 // else the amounts quality
40 // AMM offer can be consumed once at a given iteration
41 bool consumed_{false};
42
43public:
45 AMMLiquidity<TIn, TOut> const& ammLiquidity,
46 TAmounts<TIn, TOut> const& amounts,
47 TAmounts<TIn, TOut> const& balances,
48 Quality const& quality);
49
50 [[nodiscard]] Quality
51 quality() const noexcept
52 {
53 return quality_;
54 }
55
56 [[nodiscard]] Asset const&
57 assetIn() const;
58
59 [[nodiscard]] Asset const&
60 assetOut() const;
61
62 [[nodiscard]] AccountID const&
63 owner() const;
64
65 [[nodiscard]] std::optional<uint256>
66 key() const
67 {
68 return std::nullopt;
69 }
70
71 [[nodiscard]] TAmounts<TIn, TOut> const&
72 amount() const;
73
74 void
75 consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
76
77 [[nodiscard]] bool
79 {
80 return consumed_;
81 }
82
87 [[nodiscard]] TAmounts<TIn, TOut>
88 limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp) const;
89
94 [[nodiscard]] TAmounts<TIn, TOut>
95 limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp) const;
96
97 [[nodiscard]] QualityFunction
98 getQualityFunc() const;
99
102 template <typename... Args>
103 static TER
104 send(Args&&... args)
105 {
106 return accountSend(
108 }
109
110 [[nodiscard]] bool
111 isFunded() const
112 {
113 // AMM offer is fully funded by the pool
114 return true;
115 }
116
119 {
120 // AMM doesn't pay transfer fee on Payment tx
121 return {ofrInRate, QUALITY_ONE};
122 }
123
127 [[nodiscard]] bool
128 checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const;
129};
130
131} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
AMMLiquidity class provides AMM offers to BookStep class.
static std::pair< std::uint32_t, std::uint32_t > adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
Definition AMMOffer.h:118
bool fullyConsumed() const
Definition AMMOffer.h:78
Asset const & assetOut() const
Definition AMMOffer.cpp:45
Quality const quality_
Definition AMMOffer.h:39
AMMOffer(AMMLiquidity< TIn, TOut > const &ammLiquidity, TAmounts< TIn, TOut > const &amounts, TAmounts< TIn, TOut > const &balances, Quality const &quality)
Definition AMMOffer.cpp:26
TAmounts< TIn, TOut > const amounts_
Definition AMMOffer.h:34
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Definition AMMOffer.cpp:66
TAmounts< TIn, TOut > const & amount() const
Definition AMMOffer.cpp:59
std::optional< uint256 > key() const
Definition AMMOffer.h:66
QualityFunction getQualityFunc() const
Definition AMMOffer.cpp:124
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Definition AMMOffer.cpp:82
Asset const & assetIn() const
Definition AMMOffer.cpp:38
TAmounts< TIn, TOut > const balances_
Definition AMMOffer.h:36
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition AMMOffer.h:104
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition AMMOffer.h:24
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:133
AccountID const & owner() const
Definition AMMOffer.cpp:52
Quality quality() const noexcept
Definition AMMOffer.h:51
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Definition AMMOffer.cpp:107
bool isFunded() const
Definition AMMOffer.h:111
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
Represents the logical ratio of output currency to input currency.
Definition Quality.h:91
T forward(T... args)
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, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634
Represents a pair of input and output currencies.
Definition Quality.h:26