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/STLedgerEntry.h>
27 #include <ripple/protocol/SField.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 
49 template<class TIn=STAmount, class TOut=STAmount>
50 class TOffer
51  : private TOfferBase<TIn, TOut>
52 {
53 private:
55  Quality m_quality;
57 
58  TAmounts<TIn, TOut> m_amounts;
59  void setFieldAmounts ();
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
98  fully_consumed () const
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
110  TAmounts<TIn, TOut> const& consumed)
111  {
112  if (consumed.in > m_amounts.in)
113  Throw<std::logic_error> ("can't consume more than is available.");
114 
115  if (consumed.out > m_amounts.out)
116  Throw<std::logic_error> ("can't produce more than is available.");
117 
118  m_amounts -= consumed;
119  setFieldAmounts ();
120  view.update (m_entry);
121  }
122 
123  std::string id () const
124  {
125  return to_string (m_entry->key());
126  }
127 
128  uint256 key () const
129  {
130  return m_entry->key();
131  }
132 
133  Issue issueIn () const;
134  Issue issueOut () const;
135 };
136 
137 using Offer = TOffer <>;
138 
139 template<class TIn, class TOut>
140 TOffer<TIn, TOut>::TOffer (SLE::pointer const& entry, Quality quality)
141  : m_entry (entry)
142  , m_quality (quality)
143  , m_account (m_entry->getAccountID (sfAccount))
144 {
145  auto const tp = m_entry->getFieldAmount (sfTakerPays);
146  auto const tg = m_entry->getFieldAmount (sfTakerGets);
147  m_amounts.in = toAmount<TIn> (tp);
148  m_amounts.out = toAmount<TOut> (tg);
149  this->issIn_ = tp.issue ();
150  this->issOut_ = tg.issue ();
151 }
152 
153 template<>
154 inline
155 TOffer<STAmount, STAmount>::TOffer (SLE::pointer const& entry, Quality quality)
156  : m_entry (entry)
157  , m_quality (quality)
158  , m_account (m_entry->getAccountID (sfAccount))
159  , m_amounts (
160  m_entry->getFieldAmount (sfTakerPays),
161  m_entry->getFieldAmount (sfTakerGets))
162 {
163 }
164 
165 
166 template<class TIn, class TOut>
168 {
169 #ifdef _MSC_VER
170  assert(0);
171 #else
172  static_assert(sizeof(TOut) == -1, "Must be specialized");
173 #endif
174 }
175 
176 template<>
177 inline
179 {
180  m_entry->setFieldAmount (sfTakerPays, m_amounts.in);
181  m_entry->setFieldAmount (sfTakerGets, m_amounts.out);
182 }
183 
184 template<>
185 inline
187 {
188  m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in, issIn_));
189  m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out, issOut_));
190 }
191 
192 template<>
193 inline
195 {
196  m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in, issIn_));
197  m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out));
198 }
199 
200 template<>
201 inline
203 {
204  m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in));
205  m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out, issOut_));
206 }
207 
208 template<class TIn, class TOut>
210 {
211  return this->issIn_;
212 }
213 
214 template<>
215 inline
217 {
218  return m_amounts.in.issue ();
219 }
220 
221 template<class TIn, class TOut>
223 {
224  return this->issOut_;
225 }
226 
227 template<>
228 inline
230 {
231  return m_amounts.out.issue ();
232 }
233 
234 template<class TIn, class TOut>
235 inline
237 operator<< (std::ostream& os, TOffer<TIn, TOut> const& offer)
238 {
239  return os << offer.id ();
240 }
241 
242 }
243 
244 #endif
ripple::TOfferBase
Definition: Offer.h:34
ripple::TOffer::m_amounts
TAmounts< TIn, TOut > m_amounts
Definition: Offer.h:58
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::TOffer::m_quality
Quality m_quality
Definition: Offer.h:55
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::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:460
ripple::sfTakerPays
const SF_Amount sfTakerPays(access, STI_AMOUNT, 4, "TakerPays")
Definition: SField.h:426
ripple::ApplyView::update
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
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:150
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::TOffer::m_account
AccountID m_account
Definition: Offer.h:56
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:31
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:167
ripple::STAmount
Definition: STAmount.h:42
ripple::TOffer::m_entry
SLE::pointer m_entry
Definition: Offer.h:54
ripple::TOffer::issueIn
Issue issueIn() const
Definition: Offer.h:209
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::sfTakerGets
const SF_Amount sfTakerGets(access, STI_AMOUNT, 5, "TakerGets")
Definition: SField.h:427
ripple::TOfferBase::issIn_
Issue issIn_
Definition: Offer.h:37
ripple::TOffer::key
uint256 key() const
Definition: Offer.h:128
ripple::TOffer::issueOut
Issue issueOut() const
Definition: Offer.h:222
ostream
ripple::TOffer
Definition: Offer.h:50