rippled
Offer.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2014 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_BOOK_OFFER_H_INCLUDED
21 #define RIPPLE_APP_BOOK_OFFER_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <ripple/ledger/View.h>
25 #include <ripple/protocol/Quality.h>
26 #include <ripple/protocol/SField.h>
27 #include <ripple/protocol/STLedgerEntry.h>
28 #include <ostream>
29 #include <stdexcept>
30 
31 namespace ripple {
32 
33 template <class TIn, class TOut>
35 {
36 protected:
39 };
40 
41 template <>
43 {
44 public:
45  explicit TOfferBase() = default;
46 };
47 
48 template <class TIn = STAmount, class TOut = STAmount>
49 class TOffer : private TOfferBase<TIn, TOut>
50 {
51 private:
53  Quality m_quality;
55 
56  TAmounts<TIn, TOut> m_amounts;
57  void
59 
60 public:
61  TOffer() = default;
62 
63  TOffer(SLE::pointer const& entry, Quality quality);
64 
74  Quality const
75  quality() const noexcept
76  {
77  return m_quality;
78  }
79 
81  AccountID const&
82  owner() const
83  {
84  return m_account;
85  }
86 
90  TAmounts<TIn, TOut> const&
91  amount() const
92  {
93  return m_amounts;
94  }
95 
97  bool
99  {
100  if (m_amounts.in <= beast::zero)
101  return true;
102  if (m_amounts.out <= beast::zero)
103  return true;
104  return false;
105  }
106 
108  void
109  consume(ApplyView& view, TAmounts<TIn, TOut> const& consumed)
110  {
111  if (consumed.in > m_amounts.in)
112  Throw<std::logic_error>("can't consume more than is available.");
113 
114  if (consumed.out > m_amounts.out)
115  Throw<std::logic_error>("can't produce more than is available.");
116 
117  m_amounts -= consumed;
118  setFieldAmounts();
119  view.update(m_entry);
120  }
121 
123  id() const
124  {
125  return to_string(m_entry->key());
126  }
127 
128  uint256
129  key() const
130  {
131  return m_entry->key();
132  }
133 
134  Issue
135  issueIn() const;
136  Issue
137  issueOut() const;
138 };
139 
140 using Offer = TOffer<>;
141 
142 template <class TIn, class TOut>
143 TOffer<TIn, TOut>::TOffer(SLE::pointer const& entry, Quality quality)
144  : m_entry(entry)
145  , m_quality(quality)
146  , m_account(m_entry->getAccountID(sfAccount))
147 {
148  auto const tp = m_entry->getFieldAmount(sfTakerPays);
149  auto const tg = m_entry->getFieldAmount(sfTakerGets);
150  m_amounts.in = toAmount<TIn>(tp);
151  m_amounts.out = toAmount<TOut>(tg);
152  this->issIn_ = tp.issue();
153  this->issOut_ = tg.issue();
154 }
155 
156 template <>
158  SLE::pointer const& entry,
159  Quality quality)
160  : m_entry(entry)
161  , m_quality(quality)
162  , m_account(m_entry->getAccountID(sfAccount))
163  , m_amounts(
164  m_entry->getFieldAmount(sfTakerPays),
165  m_entry->getFieldAmount(sfTakerGets))
166 {
167 }
168 
169 template <class TIn, class TOut>
170 void
172 {
173 #ifdef _MSC_VER
174  assert(0);
175 #else
176  static_assert(sizeof(TOut) == -1, "Must be specialized");
177 #endif
178 }
179 
180 template <>
181 inline void
183 {
184  m_entry->setFieldAmount(sfTakerPays, m_amounts.in);
185  m_entry->setFieldAmount(sfTakerGets, m_amounts.out);
186 }
187 
188 template <>
189 inline void
191 {
192  m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_));
193  m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_));
194 }
195 
196 template <>
197 inline void
199 {
200  m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_));
201  m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out));
202 }
203 
204 template <>
205 inline void
207 {
208  m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in));
209  m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_));
210 }
211 
212 template <class TIn, class TOut>
213 Issue
215 {
216  return this->issIn_;
217 }
218 
219 template <>
220 inline Issue
222 {
223  return m_amounts.in.issue();
224 }
225 
226 template <class TIn, class TOut>
227 Issue
229 {
230  return this->issOut_;
231 }
232 
233 template <>
234 inline Issue
236 {
237  return m_amounts.out.issue();
238 }
239 
240 template <class TIn, class TOut>
241 inline std::ostream&
243 {
244  return os << offer.id();
245 }
246 
247 } // namespace ripple
248 
249 #endif
ripple::TOfferBase
Definition: Offer.h:34
ripple::TOffer::m_amounts
TAmounts< TIn, TOut > m_amounts
Definition: Offer.h:56
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::TOffer::m_quality
Quality m_quality
Definition: Offer.h:53
std::string
STL class.
std::shared_ptr< STLedgerEntry >
ripple::TOffer::fully_consumed
bool fully_consumed() const
Returns true if no more funds can flow through this offer.
Definition: Offer.h:98
ripple::ApplyView::update
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::TOffer::id
std::string id() const
Definition: Offer.h:123
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::TOffer::quality
const Quality quality() const noexcept
Returns the quality of the offer.
Definition: Offer.h:75
ripple::TOffer::amount
TAmounts< TIn, TOut > const & amount() const
Returns the in and out amounts.
Definition: Offer.h:91
stdexcept
ripple::base_uint< 160, detail::AccountIDTag >
ripple::sfTakerPays
const SF_AMOUNT sfTakerPays
ripple::TOffer::m_account
AccountID m_account
Definition: Offer.h:54
ripple::TOffer::TOffer
TOffer()=default
ripple::TOfferBase::issOut_
Issue issOut_
Definition: Offer.h:38
std::ostream
STL class.
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:30
ripple::TOffer::owner
AccountID const & owner() const
Returns the account id of the offer's owner.
Definition: Offer.h:82
ripple::TOffer::setFieldAmounts
void setFieldAmounts()
Definition: Offer.h:171
ripple::STAmount
Definition: STAmount.h:45
ripple::sfTakerGets
const SF_AMOUNT sfTakerGets
ripple::TOffer::m_entry
SLE::pointer m_entry
Definition: Offer.h:52
ripple::TOffer::issueIn
Issue issueIn() const
Definition: Offer.h:214
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TOffer::consume
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Adjusts the offer to indicate that we consumed some (or all) of it.
Definition: Offer.h:109
ripple::TOfferBase::issIn_
Issue issIn_
Definition: Offer.h:37
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::TOffer::key
uint256 key() const
Definition: Offer.h:129
ripple::TOffer::issueOut
Issue issueOut() const
Definition: Offer.h:228
ostream
ripple::TOffer
Definition: Offer.h:49