rippled
Loading...
Searching...
No Matches
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 <xrpld/app/tx/detail/Offer.h>
24#include <xrpld/ledger/View.h>
25
26#include <xrpl/beast/utility/Journal.h>
27#include <xrpl/protocol/Quality.h>
28#include <xrpl/protocol/Rate.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/TxFlags.h>
31
32namespace ripple {
33
36
39{
40private:
42 Quality quality_;
43 Quality threshold_;
44
45 bool sell_;
46
47 // The original in and out quantities.
48 Amounts const original_;
49
50 // The amounts still left over for us to try and take.
51 Amounts remaining_;
52
53 // The issuers for the input and output
56
57 // The rates that will be paid when the input and output currencies are
58 // transfered and the currency issuer isn't involved:
61
62 // The type of crossing that we are performing
64
65protected:
67
68 struct Flow
69 {
70 explicit Flow() = default;
71
72 Amounts order;
73 Amounts issuers;
74
75 bool
77 {
78 using beast::zero;
79
80 if (isXRP(order.in) && isXRP(order.out))
81 return false;
82
83 return order.in >= zero && order.out >= zero &&
84 issuers.in >= zero && issuers.out >= zero;
85 }
86 };
87
88private:
89 void
90 log_flow(char const* description, Flow const& flow);
91
92 Flow
94 Amounts const& offer,
95 Quality quality,
96 STAmount const& owner_funds,
97 STAmount const& taker_funds,
98 Rate const& rate_out);
99
100 Flow
102 Amounts const& offer,
103 Quality quality,
104 STAmount const& owner_funds,
105 STAmount const& taker_funds,
106 Rate const& rate_in);
107
108 Flow
110 Amounts const& offer,
111 Quality quality,
112 STAmount const& owner_funds,
113 STAmount const& taker_funds,
114 Rate const& rate_in,
115 Rate const& rate_out);
116
117 // Calculates the transfer rate that we should use when calculating
118 // flows for a particular issue between two accounts.
119 static Rate
121 Rate const& rate,
122 Issue const& issue,
123 AccountID const& from,
124 AccountID const& to);
125
126 // The transfer rate for the input currency between the given accounts
127 Rate
128 in_rate(AccountID const& from, AccountID const& to) const
129 {
130 return effective_rate(m_rate_in, original_.in.issue(), from, to);
131 }
132
133 // The transfer rate for the output currency between the given accounts
134 Rate
135 out_rate(AccountID const& from, AccountID const& to) const
136 {
137 return effective_rate(m_rate_out, original_.out.issue(), from, to);
138 }
139
140public:
141 BasicTaker() = delete;
142 BasicTaker(BasicTaker const&) = delete;
143
146 AccountID const& account,
147 Amounts const& amount,
148 Quality const& quality,
149 std::uint32_t flags,
150 Rate const& rate_in,
151 Rate const& rate_out,
153
154 virtual ~BasicTaker() = default;
155
162 Amounts
163 remaining_offer() const;
164
166 Amounts const&
167 original_offer() const;
168
170 AccountID const&
171 account() const noexcept
172 {
173 return account_;
174 }
175
177 bool
178 reject(Quality const& quality) const noexcept
179 {
180 return quality < threshold_;
181 }
182
186 {
187 return cross_type_;
188 }
189
191 Issue const&
192 issue_in() const
193 {
194 return issue_in_;
195 }
196
198 Issue const&
199 issue_out() const
200 {
201 return issue_out_;
202 }
203
205 bool
206 unfunded() const;
207
212 bool
213 done() const;
214
219 do_cross(Amounts offer, Quality quality, AccountID const& owner);
220
225 do_cross(
226 Amounts offer1,
227 Quality quality1,
228 AccountID const& owner1,
229 Amounts offer2,
230 Quality quality2,
231 AccountID const& owner2);
232
233 virtual STAmount
234 get_funds(AccountID const& account, STAmount const& funds) const = 0;
235};
236
237//------------------------------------------------------------------------------
238
239class Taker : public BasicTaker
240{
241public:
242 Taker() = delete;
243 Taker(Taker const&) = delete;
244
245 Taker(
247 ApplyView& view,
248 AccountID const& account,
249 Amounts const& offer,
250 std::uint32_t flags,
251 beast::Journal journal);
252 ~Taker() = default;
253
254 void
255 consume_offer(Offer& offer, Amounts const& order);
256
258 get_funds(AccountID const& account, STAmount const& funds) const override;
259
260 STAmount const&
262 {
263 return xrp_flow_;
264 }
265
268 {
269 return direct_crossings_;
270 }
271
274 {
275 return bridge_crossings_;
276 }
277
283 TER
284 cross(Offer& offer);
285
286 TER
287 cross(Offer& leg1, Offer& leg2);
290private:
291 static Rate
293 ApplyView const& view,
294 AccountID const& issuer,
295 AccountID const& account);
296
297 TER
298 fill(BasicTaker::Flow const& flow, Offer& offer);
299
300 TER
301 fill(
302 BasicTaker::Flow const& flow1,
303 Offer& leg1,
304 BasicTaker::Flow const& flow2,
305 Offer& leg2);
306
307 TER
309 AccountID const& from,
310 AccountID const& to,
311 STAmount const& amount);
312
313 TER
314 redeemIOU(
315 AccountID const& account,
316 STAmount const& amount,
317 Issue const& issue);
318
319 TER
320 issueIOU(
321 AccountID const& account,
322 STAmount const& amount,
323 Issue const& issue);
324
325private:
326 // The underlying ledger entry we are dealing with
328
329 // The amount of XRP that flowed if we were autobridging
331
332 // The number direct crossings that we performed
334
335 // The number autobridged crossings that we performed
337};
338
339} // namespace ripple
340
341#endif
A generic endpoint for log messages.
Definition: Journal.h:60
static Sink & getNullSink()
Returns a Sink which does nothing.
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:144
State for the active party during order book or payment operations.
Definition: Taker.h:39
Quality quality_
Definition: Taker.h:42
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:340
bool done() const
Returns true if order crossing should not continue.
Definition: Taker.cpp:123
Rate in_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:128
AccountID account_
Definition: Taker.h:41
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:283
BasicTaker(BasicTaker const &)=delete
CrossType cross_type_
Definition: Taker.h:63
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:229
beast::Journal const journal_
Definition: Taker.h:66
CrossType cross_type() const
Returns the type of crossing that is being performed.
Definition: Taker.h:185
AccountID const & account() const noexcept
Returns the account identifier of the taker.
Definition: Taker.h:171
bool reject(Quality const &quality) const noexcept
Returns true if the quality does not meet the taker's requirements.
Definition: Taker.h:178
void log_flow(char const *description, Flow const &flow)
Definition: Taker.cpp:207
Rate const m_rate_out
Definition: Taker.h:60
Issue const & issue_in() const
Returns the Issue associated with the input of the offer.
Definition: Taker.h:192
Amounts remaining_offer() const
Returns the amount remaining on the offer.
Definition: Taker.cpp:152
Rate out_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:135
Amounts const & original_offer() const
Returns the amount that the offer was originally placed at.
Definition: Taker.cpp:185
Amounts const original_
Definition: Taker.h:48
Issue const & issue_out_
Definition: Taker.h:55
Quality threshold_
Definition: Taker.h:43
Issue const & issue_out() const
Returns the Issue associated with the output of the offer.
Definition: Taker.h:199
virtual ~BasicTaker()=default
Issue const & issue_in_
Definition: Taker.h:54
virtual STAmount get_funds(AccountID const &account, STAmount const &funds) const =0
Amounts remaining_
Definition: Taker.h:51
static Rate effective_rate(Rate const &rate, Issue const &issue, AccountID const &from, AccountID const &to)
Definition: Taker.cpp:94
Rate const m_rate_in
Definition: Taker.h:59
BasicTaker::Flow do_cross(Amounts offer, Quality quality, AccountID const &owner)
Perform direct crossing through given offer.
Definition: Taker.cpp:400
bool unfunded() const
Returns true if the taker has run out of funds.
Definition: Taker.cpp:113
A currency issued by an account.
Definition: Issue.h:33
ApplyView & view_
Definition: Taker.h:327
STAmount get_funds(AccountID const &account, STAmount const &funds) const override
Definition: Taker.cpp:637
TER issueIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:691
static Rate calculateRate(ApplyView const &view, AccountID const &issuer, AccountID const &account)
Definition: Taker.cpp:854
std::uint32_t direct_crossings_
Definition: Taker.h:333
std::uint32_t get_bridge_crossings() const
Definition: Taker.h:273
~Taker()=default
std::uint32_t get_direct_crossings() const
Definition: Taker.h:267
TER redeemIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:662
STAmount const & get_xrp_flow() const
Definition: Taker.h:261
Taker(Taker const &)=delete
STAmount xrp_flow_
Definition: Taker.h:330
std::uint32_t bridge_crossings_
Definition: Taker.h:336
TER fill(BasicTaker::Flow const &flow, Offer &offer)
Definition: Taker.cpp:711
TER transferXRP(AccountID const &from, AccountID const &to, STAmount const &amount)
Definition: Taker.cpp:643
void consume_offer(Offer &offer, Amounts const &order)
Definition: Taker.cpp:615
TER cross(Offer &offer)
Perform a direct or bridged offer crossing as appropriate.
Definition: Taker.cpp:822
Taker()=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
bool isXRP(AccountID const &c)
Definition: AccountID.h:90
StrandResult< TInAmt, TOutAmt > flow(PaymentSandbox const &baseView, Strand const &strand, std::optional< TInAmt > const &maxIn, TOutAmt const &out, beast::Journal j)
Request out amount from a strand.
Definition: StrandFlow.h:105
CrossType
The flavor of an offer crossing.
Definition: Taker.h:35
bool sanity_check() const
Definition: Taker.h:76
Represents a transfer rate.
Definition: Rate.h:40