rippled
Taker.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_TAKER_H_INCLUDED
21 #define RIPPLE_APP_BOOK_TAKER_H_INCLUDED
22 
23 #include <ripple/app/tx/impl/Offer.h>
24 #include <ripple/core/Config.h>
25 #include <ripple/ledger/View.h>
26 #include <ripple/protocol/Quality.h>
27 #include <ripple/protocol/Rate.h>
28 #include <ripple/protocol/TER.h>
29 #include <ripple/protocol/TxFlags.h>
30 #include <ripple/beast/utility/Journal.h>
31 #include <functional>
32 
33 namespace ripple {
34 
36 enum class CrossType
37 {
38  XrpToIou,
39  IouToXrp,
40  IouToIou
41 };
42 
45 {
46 private:
48  Quality quality_;
49  Quality threshold_;
50 
51  bool sell_;
52 
53  // The original in and out quantities.
54  Amounts const original_;
55 
56  // The amounts still left over for us to try and take.
57  Amounts remaining_;
58 
59  // The issuers for the input and output
60  Issue const& issue_in_;
61  Issue const& issue_out_;
62 
63  // The rates that will be paid when the input and output currencies are
64  // transfered and the currency issuer isn't involved:
65  Rate const m_rate_in;
67 
68  // The type of crossing that we are performing
70 
71 protected:
73 
74  struct Flow
75  {
76  explicit Flow() = default;
77 
78  Amounts order;
79  Amounts issuers;
80 
81  bool sanity_check () const
82  {
83  using beast::zero;
84 
85  if (isXRP (order.in) && isXRP (order.out))
86  return false;
87 
88  return order.in >= zero &&
89  order.out >= zero &&
90  issuers.in >= zero &&
91  issuers.out >= zero;
92  }
93  };
94 
95 private:
96  void
97  log_flow (char const* description, Flow const& flow);
98 
99  Flow
100  flow_xrp_to_iou (Amounts const& offer, Quality quality,
101  STAmount const& owner_funds, STAmount const& taker_funds,
102  Rate const& rate_out);
103 
104  Flow
105  flow_iou_to_xrp (Amounts const& offer, Quality quality,
106  STAmount const& owner_funds, STAmount const& taker_funds,
107  Rate const& rate_in);
108 
109  Flow
110  flow_iou_to_iou (Amounts const& offer, Quality quality,
111  STAmount const& owner_funds, STAmount const& taker_funds,
112  Rate const& rate_in, Rate const& rate_out);
113 
114  // Calculates the transfer rate that we should use when calculating
115  // flows for a particular issue between two accounts.
116  static
117  Rate
118  effective_rate (Rate const& rate, Issue const &issue,
119  AccountID const& from, AccountID const& to);
120 
121  // The transfer rate for the input currency between the given accounts
122  Rate
123  in_rate (AccountID const& from, AccountID const& to) const
124  {
125  return effective_rate (m_rate_in, original_.in.issue (), from, to);
126  }
127 
128  // The transfer rate for the output currency between the given accounts
129  Rate
130  out_rate (AccountID const& from, AccountID const& to) const
131  {
132  return effective_rate (m_rate_out, original_.out.issue (), from, to);
133  }
134 
135 public:
136  BasicTaker () = delete;
137  BasicTaker (BasicTaker const&) = delete;
138 
139  BasicTaker (
140  CrossType cross_type, AccountID const& account, Amounts const& amount,
141  Quality const& quality, std::uint32_t flags, Rate const& rate_in,
142  Rate const& rate_out,
144 
145  virtual ~BasicTaker () = default;
146 
153  Amounts
154  remaining_offer () const;
155 
157  Amounts const&
158  original_offer () const;
159 
161  AccountID const&
162  account () const noexcept
163  {
164  return account_;
165  }
166 
168  bool
169  reject (Quality const& quality) const noexcept
170  {
171  return quality < threshold_;
172  }
173 
175  CrossType
176  cross_type () const
177  {
178  return cross_type_;
179  }
180 
182  Issue const&
183  issue_in () const
184  {
185  return issue_in_;
186  }
187 
189  Issue const&
190  issue_out () const
191  {
192  return issue_out_;
193  }
194 
196  bool
197  unfunded () const;
198 
203  bool
204  done () const;
205 
210  do_cross (Amounts offer, Quality quality, AccountID const& owner);
211 
216  do_cross (
217  Amounts offer1, Quality quality1, AccountID const& owner1,
218  Amounts offer2, Quality quality2, AccountID const& owner2);
219 
220  virtual
221  STAmount
222  get_funds (AccountID const& account, STAmount const& funds) const = 0;
223 };
224 
225 //------------------------------------------------------------------------------
226 
227 class Taker
228  : public BasicTaker
229 {
230 public:
231  Taker () = delete;
232  Taker (Taker const&) = delete;
233 
235  AccountID const& account, Amounts const& offer,
236  std::uint32_t flags,
237  beast::Journal journal);
238  ~Taker () = default;
239 
240  void
241  consume_offer (Offer& offer, Amounts const& order);
242 
243  STAmount
244  get_funds (AccountID const& account, STAmount const& funds) const override;
245 
246  STAmount const&
247  get_xrp_flow () const
248  {
249  return xrp_flow_;
250  }
251 
254  {
255  return direct_crossings_;
256  }
257 
260  {
261  return bridge_crossings_;
262  }
263 
269  TER
270  cross (Offer& offer);
271 
272  TER
273  cross (Offer& leg1, Offer& leg2);
276 private:
277  static
278  Rate
279  calculateRate (ApplyView const& view,
280  AccountID const& issuer,
281  AccountID const& account);
282 
283  TER
284  fill (BasicTaker::Flow const& flow, Offer& offer);
285 
286  TER
287  fill (
288  BasicTaker::Flow const& flow1, Offer& leg1,
289  BasicTaker::Flow const& flow2, Offer& leg2);
290 
291  TER
292  transferXRP (AccountID const& from, AccountID const& to, STAmount const& amount);
293 
294  TER
295  redeemIOU (AccountID const& account, STAmount const& amount, Issue const& issue);
296 
297  TER
298  issueIOU (AccountID const& account, STAmount const& amount, Issue const& issue);
299 
300 private:
301  // The underlying ledger entry we are dealing with
303 
304  // The amount of XRP that flowed if we were autobridging
306 
307  // The number direct crossings that we performed
309 
310  // The number autobridged crossings that we performed
312 };
313 
314 }
315 
316 #endif
ripple::Taker::get_funds
STAmount get_funds(AccountID const &account, STAmount const &funds) const override
Definition: Taker.cpp:568
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::Rate
Represents a transfer rate.
Definition: Rate.h:37
ripple::Taker::fill
TER fill(BasicTaker::Flow const &flow, Offer &offer)
Definition: Taker.cpp:639
ripple::Taker
Definition: Taker.h:227
functional
ripple::Taker::view_
ApplyView & view_
Definition: Taker.h:302
ripple::BasicTaker::unfunded
bool unfunded() const
Returns true if the taker has run out of funds.
Definition: Taker.cpp:96
ripple::BasicTaker::flow_iou_to_iou
Flow flow_iou_to_iou(Amounts const &offer, Quality quality, STAmount const &owner_funds, STAmount const &taker_funds, Rate const &rate_in, Rate const &rate_out)
Definition: Taker.cpp:315
std::pair
ripple::BasicTaker::do_cross
BasicTaker::Flow do_cross(Amounts offer, Quality quality, AccountID const &owner)
Perform direct crossing through given offer.
Definition: Taker.cpp:372
ripple::BasicTaker::reject
bool reject(Quality const &quality) const noexcept
Returns true if the quality does not meet the taker's requirements.
Definition: Taker.h:169
ripple::BasicTaker::remaining_offer
Amounts remaining_offer() const
Returns the amount remaining on the offer.
Definition: Taker.cpp:134
ripple::BasicTaker::in_rate
Rate in_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:123
ripple::CrossType::XrpToIou
@ XrpToIou
ripple::BasicTaker::m_rate_in
const Rate m_rate_in
Definition: Taker.h:65
ripple::Taker::direct_crossings_
std::uint32_t direct_crossings_
Definition: Taker.h:308
ripple::BasicTaker::log_flow
void log_flow(char const *description, Flow const &flow)
Definition: Taker.cpp:186
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:48
ripple::CrossType::IouToXrp
@ IouToXrp
ripple::Taker::get_xrp_flow
STAmount const & get_xrp_flow() const
Definition: Taker.h:247
ripple::BasicTaker::issue_in
Issue const & issue_in() const
Returns the Issue associated with the input of the offer.
Definition: Taker.h:183
ripple::BasicTaker::flow_xrp_to_iou
Flow flow_xrp_to_iou(Amounts const &offer, Quality quality, STAmount const &owner_funds, STAmount const &taker_funds, Rate const &rate_out)
Definition: Taker.cpp:208
ripple::BasicTaker::remaining_
Amounts remaining_
Definition: Taker.h:57
ripple::BasicTaker
State for the active party during order book or payment operations.
Definition: Taker.h:44
ripple::Taker::get_bridge_crossings
std::uint32_t get_bridge_crossings() const
Definition: Taker.h:259
ripple::Taker::get_direct_crossings
std::uint32_t get_direct_crossings() const
Definition: Taker.h:253
ripple::BasicTaker::BasicTaker
BasicTaker()=delete
ripple::BasicTaker::cross_type
CrossType cross_type() const
Returns the type of crossing that is being performed.
Definition: Taker.h:176
ripple::BasicTaker::Flow::Flow
Flow()=default
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:67
ripple::BasicTaker::journal_
const beast::Journal journal_
Definition: Taker.h:72
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:150
ripple::Taker::redeemIOU
TER redeemIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:591
ripple::BasicTaker::~BasicTaker
virtual ~BasicTaker()=default
ripple::BasicTaker::flow_iou_to_xrp
Flow flow_iou_to_xrp(Amounts const &offer, Quality quality, STAmount const &owner_funds, STAmount const &taker_funds, Rate const &rate_in)
Definition: Taker.cpp:260
ripple::Taker::calculateRate
static Rate calculateRate(ApplyView const &view, AccountID const &issuer, AccountID const &account)
Definition: Taker.cpp:763
ripple::BasicTaker::sell_
bool sell_
Definition: Taker.h:51
ripple::base_uint< 160, detail::AccountIDTag >
ripple::BasicTaker::original_
const Amounts original_
Definition: Taker.h:54
ripple::Taker::~Taker
~Taker()=default
ripple::BasicTaker::issue_out
Issue const & issue_out() const
Returns the Issue associated with the output of the offer.
Definition: Taker.h:190
ripple::BasicTaker::Flow::order
Amounts order
Definition: Taker.h:78
ripple::CrossType
CrossType
The flavor of an offer crossing.
Definition: Taker.h:36
ripple::CrossType::IouToIou
@ IouToIou
ripple::Taker::Taker
Taker()=delete
ripple::BasicTaker::Flow
Definition: Taker.h:74
ripple::Taker::xrp_flow_
STAmount xrp_flow_
Definition: Taker.h:305
ripple::Taker::issueIOU
TER issueIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:619
ripple::TERSubset< CanCvtToTER >
ripple::BasicTaker::account_
AccountID account_
Definition: Taker.h:47
ripple::BasicTaker::threshold_
Quality threshold_
Definition: Taker.h:49
ripple::STAmount
Definition: STAmount.h:42
ripple::BasicTaker::original_offer
Amounts const & original_offer() const
Returns the amount that the offer was originally placed at.
Definition: Taker.cpp:162
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:121
ripple::Taker::consume_offer
void consume_offer(Offer &offer, Amounts const &order)
Definition: Taker.cpp:546
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::Taker::bridge_crossings_
std::uint32_t bridge_crossings_
Definition: Taker.h:311
std::uint32_t
ripple::BasicTaker::cross_type_
CrossType cross_type_
Definition: Taker.h:69
ripple::BasicTaker::Flow::sanity_check
bool sanity_check() const
Definition: Taker.h:81
ripple::BasicTaker::Flow::issuers
Amounts issuers
Definition: Taker.h:79
ripple::BasicTaker::effective_rate
static Rate effective_rate(Rate const &rate, Issue const &issue, AccountID const &from, AccountID const &to)
Definition: Taker.cpp:77
ripple::BasicTaker::get_funds
virtual STAmount get_funds(AccountID const &account, STAmount const &funds) const =0
ripple::BasicTaker::done
bool done() const
Returns true if order crossing should not continue.
Definition: Taker.cpp:106
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::BasicTaker::issue_in_
Issue const & issue_in_
Definition: Taker.h:60
ripple::Taker::transferXRP
TER transferXRP(AccountID const &from, AccountID const &to, STAmount const &amount)
Definition: Taker.cpp:573
ripple::flow
path::RippleCalc::Output flow(PaymentSandbox &view, STAmount const &deliver, AccountID const &src, AccountID const &dst, STPathSet const &paths, bool defaultPaths, bool partialPayment, bool ownerPaysTransferFee, bool offerCrossing, boost::optional< Quality > const &limitQuality, boost::optional< STAmount > const &sendMax, beast::Journal j, path::detail::FlowDebugInfo *flowDebugInfo=nullptr)
Make a payment from the src account to the dst account.
ripple::BasicTaker::quality_
Quality quality_
Definition: Taker.h:48
ripple::BasicTaker::m_rate_out
const Rate m_rate_out
Definition: Taker.h:66
ripple::BasicTaker::account
AccountID const & account() const noexcept
Returns the account identifier of the taker.
Definition: Taker.h:162
ripple::TOffer
Definition: Offer.h:50
ripple::BasicTaker::issue_out_
Issue const & issue_out_
Definition: Taker.h:61
ripple::Taker::cross
TER cross(Offer &offer)
Perform a direct or bridged offer crossing as appropriate.
Definition: Taker.cpp:735
ripple::BasicTaker::out_rate
Rate out_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:130