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/beast/utility/Journal.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/ledger/View.h>
27 #include <ripple/protocol/Quality.h>
28 #include <ripple/protocol/Rate.h>
29 #include <ripple/protocol/TER.h>
30 #include <ripple/protocol/TxFlags.h>
31 #include <functional>
32 
33 namespace ripple {
34 
37 
40 {
41 private:
43  Quality quality_;
44  Quality threshold_;
45 
46  bool sell_;
47 
48  // The original in and out quantities.
49  Amounts const original_;
50 
51  // The amounts still left over for us to try and take.
52  Amounts remaining_;
53 
54  // The issuers for the input and output
55  Issue const& issue_in_;
56  Issue const& issue_out_;
57 
58  // The rates that will be paid when the input and output currencies are
59  // transfered and the currency issuer isn't involved:
60  Rate const m_rate_in;
62 
63  // The type of crossing that we are performing
65 
66 protected:
68 
69  struct Flow
70  {
71  explicit Flow() = default;
72 
73  Amounts order;
74  Amounts issuers;
75 
76  bool
77  sanity_check() const
78  {
79  using beast::zero;
80 
81  if (isXRP(order.in) && isXRP(order.out))
82  return false;
83 
84  return order.in >= zero && order.out >= zero &&
85  issuers.in >= zero && issuers.out >= zero;
86  }
87  };
88 
89 private:
90  void
91  log_flow(char const* description, Flow const& flow);
92 
93  Flow
95  Amounts const& offer,
96  Quality quality,
97  STAmount const& owner_funds,
98  STAmount const& taker_funds,
99  Rate const& rate_out);
100 
101  Flow
103  Amounts const& offer,
104  Quality quality,
105  STAmount const& owner_funds,
106  STAmount const& taker_funds,
107  Rate const& rate_in);
108 
109  Flow
111  Amounts const& offer,
112  Quality quality,
113  STAmount const& owner_funds,
114  STAmount const& taker_funds,
115  Rate const& rate_in,
116  Rate const& rate_out);
117 
118  // Calculates the transfer rate that we should use when calculating
119  // flows for a particular issue between two accounts.
120  static Rate
122  Rate const& rate,
123  Issue const& issue,
124  AccountID const& from,
125  AccountID const& to);
126 
127  // The transfer rate for the input currency between the given accounts
128  Rate
129  in_rate(AccountID const& from, AccountID const& to) const
130  {
131  return effective_rate(m_rate_in, original_.in.issue(), from, to);
132  }
133 
134  // The transfer rate for the output currency between the given accounts
135  Rate
136  out_rate(AccountID const& from, AccountID const& to) const
137  {
138  return effective_rate(m_rate_out, original_.out.issue(), from, to);
139  }
140 
141 public:
142  BasicTaker() = delete;
143  BasicTaker(BasicTaker const&) = delete;
144 
145  BasicTaker(
147  AccountID const& account,
148  Amounts const& amount,
149  Quality const& quality,
150  std::uint32_t flags,
151  Rate const& rate_in,
152  Rate const& rate_out,
154 
155  virtual ~BasicTaker() = default;
156 
163  Amounts
164  remaining_offer() const;
165 
167  Amounts const&
168  original_offer() const;
169 
171  AccountID const&
172  account() const noexcept
173  {
174  return account_;
175  }
176 
178  bool
179  reject(Quality const& quality) const noexcept
180  {
181  return quality < threshold_;
182  }
183 
185  CrossType
186  cross_type() const
187  {
188  return cross_type_;
189  }
190 
192  Issue const&
193  issue_in() const
194  {
195  return issue_in_;
196  }
197 
199  Issue const&
200  issue_out() const
201  {
202  return issue_out_;
203  }
204 
206  bool
207  unfunded() const;
208 
213  bool
214  done() const;
215 
220  do_cross(Amounts offer, Quality quality, AccountID const& owner);
221 
226  do_cross(
227  Amounts offer1,
228  Quality quality1,
229  AccountID const& owner1,
230  Amounts offer2,
231  Quality quality2,
232  AccountID const& owner2);
233 
234  virtual STAmount
235  get_funds(AccountID const& account, STAmount const& funds) const = 0;
236 };
237 
238 //------------------------------------------------------------------------------
239 
240 class Taker : public BasicTaker
241 {
242 public:
243  Taker() = delete;
244  Taker(Taker const&) = delete;
245 
246  Taker(
248  ApplyView& view,
249  AccountID const& account,
250  Amounts const& offer,
251  std::uint32_t flags,
252  beast::Journal journal);
253  ~Taker() = default;
254 
255  void
256  consume_offer(Offer& offer, Amounts const& order);
257 
258  STAmount
259  get_funds(AccountID const& account, STAmount const& funds) const override;
260 
261  STAmount const&
262  get_xrp_flow() const
263  {
264  return xrp_flow_;
265  }
266 
269  {
270  return direct_crossings_;
271  }
272 
275  {
276  return bridge_crossings_;
277  }
278 
284  TER
285  cross(Offer& offer);
286 
287  TER
288  cross(Offer& leg1, Offer& leg2);
291 private:
292  static Rate
294  ApplyView const& view,
295  AccountID const& issuer,
296  AccountID const& account);
297 
298  TER
299  fill(BasicTaker::Flow const& flow, Offer& offer);
300 
301  TER
302  fill(
303  BasicTaker::Flow const& flow1,
304  Offer& leg1,
305  BasicTaker::Flow const& flow2,
306  Offer& leg2);
307 
308  TER
309  transferXRP(
310  AccountID const& from,
311  AccountID const& to,
312  STAmount const& amount);
313 
314  TER
315  redeemIOU(
316  AccountID const& account,
317  STAmount const& amount,
318  Issue const& issue);
319 
320  TER
321  issueIOU(
322  AccountID const& account,
323  STAmount const& amount,
324  Issue const& issue);
325 
326 private:
327  // The underlying ledger entry we are dealing with
329 
330  // The amount of XRP that flowed if we were autobridging
332 
333  // The number direct crossings that we performed
335 
336  // The number autobridged crossings that we performed
338 };
339 
340 } // namespace ripple
341 
342 #endif
ripple::Taker::get_funds
STAmount get_funds(AccountID const &account, STAmount const &funds) const override
Definition: Taker.cpp:609
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:683
ripple::Taker
Definition: Taker.h:240
functional
ripple::Taker::view_
ApplyView & view_
Definition: Taker.h:328
ripple::BasicTaker::unfunded
bool unfunded() const
Returns true if the taker has run out of funds.
Definition: Taker.cpp:102
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:325
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:385
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:179
ripple::BasicTaker::remaining_offer
Amounts remaining_offer() const
Returns the amount remaining on the offer.
Definition: Taker.cpp:141
ripple::BasicTaker::in_rate
Rate in_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:129
ripple::CrossType::XrpToIou
@ XrpToIou
ripple::BasicTaker::m_rate_in
const Rate m_rate_in
Definition: Taker.h:60
ripple::Taker::direct_crossings_
std::uint32_t direct_crossings_
Definition: Taker.h:334
ripple::BasicTaker::log_flow
void log_flow(char const *description, Flow const &flow)
Definition: Taker.cpp:192
ripple::CrossType::IouToXrp
@ IouToXrp
ripple::Taker::get_xrp_flow
STAmount const & get_xrp_flow() const
Definition: Taker.h:262
ripple::BasicTaker::issue_in
Issue const & issue_in() const
Returns the Issue associated with the input of the offer.
Definition: Taker.h:193
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:214
ripple::BasicTaker::remaining_
Amounts remaining_
Definition: Taker.h:52
ripple::BasicTaker
State for the active party during order book or payment operations.
Definition: Taker.h:39
ripple::Taker::get_bridge_crossings
std::uint32_t get_bridge_crossings() const
Definition: Taker.h:274
ripple::Taker::get_direct_crossings
std::uint32_t get_direct_crossings() const
Definition: Taker.h:268
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:186
ripple::BasicTaker::Flow::Flow
Flow()=default
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
ripple::BasicTaker::journal_
const beast::Journal journal_
Definition: Taker.h:67
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::Taker::redeemIOU
TER redeemIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:634
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:268
ripple::Taker::calculateRate
static Rate calculateRate(ApplyView const &view, AccountID const &issuer, AccountID const &account)
Definition: Taker.cpp:821
ripple::BasicTaker::sell_
bool sell_
Definition: Taker.h:46
ripple::base_uint< 160, detail::AccountIDTag >
ripple::BasicTaker::original_
const Amounts original_
Definition: Taker.h:49
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:200
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, std::optional< Quality > const &limitQuality, std::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::Flow::order
Amounts order
Definition: Taker.h:73
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:69
ripple::Taker::xrp_flow_
STAmount xrp_flow_
Definition: Taker.h:331
ripple::Taker::issueIOU
TER issueIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:663
ripple::TERSubset< CanCvtToTER >
ripple::BasicTaker::account_
AccountID account_
Definition: Taker.h:42
ripple::BasicTaker::threshold_
Quality threshold_
Definition: Taker.h:44
ripple::STAmount
Definition: STAmount.h:45
ripple::BasicTaker::original_offer
Amounts const & original_offer() const
Returns the amount that the offer was originally placed at.
Definition: Taker.cpp:170
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:89
ripple::Taker::consume_offer
void consume_offer(Offer &offer, Amounts const &order)
Definition: Taker.cpp:587
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::Taker::bridge_crossings_
std::uint32_t bridge_crossings_
Definition: Taker.h:337
std::uint32_t
ripple::BasicTaker::cross_type_
CrossType cross_type_
Definition: Taker.h:64
ripple::BasicTaker::Flow::sanity_check
bool sanity_check() const
Definition: Taker.h:77
ripple::BasicTaker::Flow::issuers
Amounts issuers
Definition: Taker.h:74
ripple::BasicTaker::effective_rate
static Rate effective_rate(Rate const &rate, Issue const &issue, AccountID const &from, AccountID const &to)
Definition: Taker.cpp:83
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:112
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:55
ripple::Taker::transferXRP
TER transferXRP(AccountID const &from, AccountID const &to, STAmount const &amount)
Definition: Taker.cpp:615
ripple::BasicTaker::quality_
Quality quality_
Definition: Taker.h:43
ripple::BasicTaker::m_rate_out
const Rate m_rate_out
Definition: Taker.h:61
ripple::BasicTaker::account
AccountID const & account() const noexcept
Returns the account identifier of the taker.
Definition: Taker.h:172
ripple::TOffer
Definition: Offer.h:49
ripple::BasicTaker::issue_out_
Issue const & issue_out_
Definition: Taker.h:56
ripple::Taker::cross
TER cross(Offer &offer)
Perform a direct or bridged offer crossing as appropriate.
Definition: Taker.cpp:789
ripple::BasicTaker::out_rate
Rate out_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:136
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47