xrpld
Loading...
Searching...
No Matches
AMMOffer.h
1#pragma once
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/helpers/TokenHelpers.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Concepts.h>
9#include <xrpl/protocol/Quality.h>
10#include <xrpl/protocol/STLedgerEntry.h>
11#include <xrpl/protocol/TER.h>
12
13#include <cstdint>
14#include <optional>
15#include <utility>
16
17namespace xrpl {
18
19template <typename TIn, typename TOut>
20class AMMLiquidity;
21class QualityFunction;
22
28template <StepAmount TIn, StepAmount TOut>
30{
31private:
33 // Initial offer amounts. It is fibonacci seq generated for multi-path.
34 // If the offer size is set based on the competing CLOB offer then
35 // the AMM offer size is such that if the offer is consumed then
36 // the updated AMM pool SP quality is going to be equal to competing
37 // CLOB offer quality. If there is no competing CLOB offer then
38 // the initial size is set to in=cMax[Native,Value],balances.out.
39 // While this is not a "real" offer it simulates the case of
40 // the swap out of the entire side of the pool, in which case
41 // the swap in amount is infinite.
43 // Current pool balances.
45 // The Spot Price quality if balances != amounts
46 // else the amounts quality
48 // AMM offer can be consumed once at a given iteration
49 bool consumed_{false};
50
51public:
53 AMMLiquidity<TIn, TOut> const& ammLiquidity,
54 TAmounts<TIn, TOut> const& amounts,
55 TAmounts<TIn, TOut> const& balances,
56 Quality const& quality);
57
58 [[nodiscard]] Quality
59 quality() const noexcept
60 {
61 return quality_;
62 }
63
64 [[nodiscard]] Asset const&
65 assetIn() const;
66
67 [[nodiscard]] Asset const&
68 assetOut() const;
69
70 [[nodiscard]] AccountID const&
71 owner() const;
72
73 [[nodiscard]] std::optional<uint256>
74 key() const
75 {
76 return std::nullopt;
77 }
78
79 [[nodiscard]] TAmounts<TIn, TOut> const&
80 amount() const;
81
82 void
83 consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed);
84
85 [[nodiscard]] bool
87 {
88 return consumed_;
89 }
90
96 [[nodiscard]] TAmounts<TIn, TOut>
97 limitOut(TAmounts<TIn, TOut> const& offerAmount, TOut const& limit, bool roundUp) const;
98
104 [[nodiscard]] TAmounts<TIn, TOut>
105 limitIn(TAmounts<TIn, TOut> const& offerAmount, TIn const& limit, bool roundUp) const;
106
107 [[nodiscard]] QualityFunction
108 getQualityFunc() const;
109
113 template <typename... Args>
114 static TER
115 send(Args&&... args)
116 {
117 return accountSend(
118 std::forward<Args>(args)...,
119 SLE::pointer(),
122 }
123
124 [[nodiscard]] bool
125 isFunded() const
126 {
127 // AMM offer is fully funded by the pool
128 return true;
129 }
130
133 {
134 // AMM doesn't pay transfer fee on Payment tx
135 return {ofrInRate, QUALITY_ONE};
136 }
137
142 [[nodiscard]] bool
143 checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const;
144};
145
146} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
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:132
bool fullyConsumed() const
Definition AMMOffer.h:86
Asset const & assetOut() const
Definition AMMOffer.cpp:45
Quality const quality_
Definition AMMOffer.h:47
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:42
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:74
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:44
static TER send(Args &&... args)
Send funds without incurring the transfer fee.
Definition AMMOffer.h:115
AMMLiquidity< TIn, TOut > const & ammLiquidity_
Definition AMMOffer.h:32
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:59
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:125
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:134
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:90
std::shared_ptr< STLedgerEntry > pointer
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, SLE::ref sponsorSle={}, 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:34
TERSubset< CanCvtToTER > TER
Definition TER.h:647
Represents a pair of input and output currencies.
Definition Quality.h:29