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/core/Config.h>
25#include <xrpld/ledger/View.h>
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#include <functional>
32
33namespace ripple {
34
37
40{
41private:
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
57
58 // The rates that will be paid when the input and output currencies are
59 // transfered and the currency issuer isn't involved:
62
63 // The type of crossing that we are performing
65
66protected:
68
69 struct Flow
70 {
71 explicit Flow() = default;
72
73 Amounts order;
74 Amounts issuers;
75
76 bool
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
89private:
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
141public:
142 BasicTaker() = delete;
143 BasicTaker(BasicTaker const&) = delete;
144
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
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
240class Taker : public BasicTaker
241{
242public:
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
259 get_funds(AccountID const& account, STAmount const& funds) const override;
260
261 STAmount 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);
291private:
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
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
326private:
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
A generic endpoint for log messages.
Definition: Journal.h:59
static Sink & getNullSink()
Returns a Sink which does nothing.
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
State for the active party during order book or payment operations.
Definition: Taker.h:40
Quality quality_
Definition: Taker.h:43
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:339
bool done() const
Returns true if order crossing should not continue.
Definition: Taker.cpp:122
Rate in_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:129
AccountID account_
Definition: Taker.h:42
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:282
BasicTaker(BasicTaker const &)=delete
CrossType cross_type_
Definition: Taker.h:64
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:228
beast::Journal const journal_
Definition: Taker.h:67
CrossType cross_type() const
Returns the type of crossing that is being performed.
Definition: Taker.h:186
AccountID const & account() const noexcept
Returns the account identifier of the taker.
Definition: Taker.h:172
bool reject(Quality const &quality) const noexcept
Returns true if the quality does not meet the taker's requirements.
Definition: Taker.h:179
void log_flow(char const *description, Flow const &flow)
Definition: Taker.cpp:206
Rate const m_rate_out
Definition: Taker.h:61
Issue const & issue_in() const
Returns the Issue associated with the input of the offer.
Definition: Taker.h:193
Amounts remaining_offer() const
Returns the amount remaining on the offer.
Definition: Taker.cpp:151
Rate out_rate(AccountID const &from, AccountID const &to) const
Definition: Taker.h:136
Amounts const & original_offer() const
Returns the amount that the offer was originally placed at.
Definition: Taker.cpp:184
Amounts const original_
Definition: Taker.h:49
Issue const & issue_out_
Definition: Taker.h:56
Quality threshold_
Definition: Taker.h:44
Issue const & issue_out() const
Returns the Issue associated with the output of the offer.
Definition: Taker.h:200
virtual ~BasicTaker()=default
Issue const & issue_in_
Definition: Taker.h:55
virtual STAmount get_funds(AccountID const &account, STAmount const &funds) const =0
Amounts remaining_
Definition: Taker.h:52
static Rate effective_rate(Rate const &rate, Issue const &issue, AccountID const &from, AccountID const &to)
Definition: Taker.cpp:93
Rate const m_rate_in
Definition: Taker.h:60
BasicTaker::Flow do_cross(Amounts offer, Quality quality, AccountID const &owner)
Perform direct crossing through given offer.
Definition: Taker.cpp:399
bool unfunded() const
Returns true if the taker has run out of funds.
Definition: Taker.cpp:112
A currency issued by an account.
Definition: Issue.h:36
ApplyView & view_
Definition: Taker.h:328
STAmount get_funds(AccountID const &account, STAmount const &funds) const override
Definition: Taker.cpp:636
TER issueIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:690
static Rate calculateRate(ApplyView const &view, AccountID const &issuer, AccountID const &account)
Definition: Taker.cpp:853
std::uint32_t direct_crossings_
Definition: Taker.h:334
std::uint32_t get_bridge_crossings() const
Definition: Taker.h:274
~Taker()=default
std::uint32_t get_direct_crossings() const
Definition: Taker.h:268
TER redeemIOU(AccountID const &account, STAmount const &amount, Issue const &issue)
Definition: Taker.cpp:661
STAmount const & get_xrp_flow() const
Definition: Taker.h:262
Taker(Taker const &)=delete
STAmount xrp_flow_
Definition: Taker.h:331
std::uint32_t bridge_crossings_
Definition: Taker.h:337
TER fill(BasicTaker::Flow const &flow, Offer &offer)
Definition: Taker.cpp:710
TER transferXRP(AccountID const &from, AccountID const &to, STAmount const &amount)
Definition: Taker.cpp:642
void consume_offer(Offer &offer, Amounts const &order)
Definition: Taker.cpp:614
TER cross(Offer &offer)
Perform a direct or bridged offer crossing as appropriate.
Definition: Taker.cpp:821
Taker()=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
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:106
CrossType
The flavor of an offer crossing.
Definition: Taker.h:36
bool sanity_check() const
Definition: Taker.h:77
Represents a transfer rate.
Definition: Rate.h:38