rippled
AMMContext.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_PATHS_AMMCONTEXT_H_INCLUDED
21 #define RIPPLE_APP_PATHS_AMMCONTEXT_H_INCLUDED
22 
23 #include <ripple/protocol/AccountID.h>
24 
25 #include <cstdint>
26 
27 namespace ripple {
28 
36 {
37 public:
38  // Restrict number of AMM offers. If this restriction is removed
39  // then need to restrict in some other way because AMM offers are
40  // not counted in the BookStep offer counter.
41  constexpr static std::uint8_t MaxIterations = 30;
42 
43 private:
44  // Tx account owner is required to get the AMM trading fee in BookStep
46  // true if payment has multiple paths
47  bool multiPath_{false};
48  // Is true if AMM offer is consumed during a payment engine iteration.
49  bool ammUsed_{false};
50  // Counter of payment engine iterations with consumed AMM
52 
53 public:
56  {
57  }
58  ~AMMContext() = default;
59  AMMContext(AMMContext const&) = delete;
60  AMMContext&
61  operator=(AMMContext const&) = delete;
62 
63  bool
64  multiPath() const
65  {
66  return multiPath_;
67  }
68 
69  void
70  setMultiPath(bool fs)
71  {
72  multiPath_ = fs;
73  }
74 
75  void
77  {
78  ammUsed_ = true;
79  }
80 
81  void
83  {
84  if (ammUsed_)
85  ++ammIters_;
86  ammUsed_ = false;
87  }
88 
89  bool
91  {
92  return ammIters_ >= MaxIterations;
93  }
94 
96  curIters() const
97  {
98  return ammIters_;
99  }
100 
101  AccountID
102  account() const
103  {
104  return account_;
105  }
106 
110  void
112  {
113  ammUsed_ = false;
114  }
115 };
116 
117 } // namespace ripple
118 
119 #endif // RIPPLE_APP_PATHS_AMMCONTEXT_H_INCLUDED
ripple::AMMContext::ammIters_
std::uint16_t ammIters_
Definition: AMMContext.h:51
ripple::AMMContext::update
void update()
Definition: AMMContext.h:82
ripple::AMMContext::account_
AccountID account_
Definition: AMMContext.h:45
ripple::AMMContext::clear
void clear()
Strand execution may fail.
Definition: AMMContext.h:111
ripple::AMMContext::operator=
AMMContext & operator=(AMMContext const &)=delete
ripple::base_uint< 160, detail::AccountIDTag >
ripple::AMMContext::multiPath
bool multiPath() const
Definition: AMMContext.h:64
ripple::AMMContext
Maintains AMM info per overall payment engine execution and individual iteration.
Definition: AMMContext.h:35
ripple::AMMContext::account
AccountID account() const
Definition: AMMContext.h:102
ripple::AMMContext::AMMContext
AMMContext(AccountID const &account, bool multiPath)
Definition: AMMContext.h:54
cstdint
std::uint8_t
ripple::AMMContext::multiPath_
bool multiPath_
Definition: AMMContext.h:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::AMMContext::curIters
std::uint16_t curIters() const
Definition: AMMContext.h:96
ripple::AMMContext::MaxIterations
constexpr static std::uint8_t MaxIterations
Definition: AMMContext.h:41
ripple::AMMContext::maxItersReached
bool maxItersReached() const
Definition: AMMContext.h:90
ripple::AMMContext::ammUsed_
bool ammUsed_
Definition: AMMContext.h:49
ripple::AMMContext::~AMMContext
~AMMContext()=default
ripple::AMMContext::setMultiPath
void setMultiPath(bool fs)
Definition: AMMContext.h:70
ripple::AMMContext::setAMMUsed
void setAMMUsed()
Definition: AMMContext.h:76