rippled
CreateOffer.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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_TX_CREATEOFFER_H_INCLUDED
21 #define RIPPLE_TX_CREATEOFFER_H_INCLUDED
22 
23 #include <ripple/app/tx/impl/OfferStream.h>
24 #include <ripple/app/tx/impl/Taker.h>
25 #include <ripple/app/tx/impl/Transactor.h>
26 #include <utility>
27 
28 namespace ripple {
29 
30 class PaymentSandbox;
31 class Sandbox;
32 
34 class CreateOffer : public Transactor
35 {
36 public:
38 
40  explicit CreateOffer(ApplyContext& ctx)
41  : Transactor(ctx), stepCounter_(1000, j_)
42  {
43  }
44 
45  static TxConsequences
47 
49  static NotTEC
50  preflight(PreflightContext const& ctx);
51 
53  static TER
54  preclaim(PreclaimContext const& ctx);
55 
57  void
58  preCompute() override;
59 
61  TER
62  doApply() override;
63 
64 private:
66  applyGuts(Sandbox& view, Sandbox& view_cancel);
67 
68  // Determine if we are authorized to hold the asset we want to get.
69  static TER
71  ReadView const& view,
72  ApplyFlags const flags,
73  AccountID const id,
74  beast::Journal const j,
75  Issue const& issue);
76 
77  bool
78  dry_offer(ApplyView& view, Offer const& offer);
79 
82  bool have_direct,
83  OfferStream const& direct,
84  bool have_bridge,
85  OfferStream const& leg1,
86  OfferStream const& leg2);
87 
90  Taker& taker,
91  ApplyView& view,
92  ApplyView& view_cancel,
93  NetClock::time_point const when);
94 
97  Taker& taker,
98  ApplyView& view,
99  ApplyView& view_cancel,
100  NetClock::time_point const when);
101 
102  // Step through the stream for as long as possible, skipping any offers
103  // that are from the taker or which cross the taker's threshold.
104  // Return false if the is no offer in the book, true otherwise.
105  static bool
106  step_account(OfferStream& stream, Taker const& taker);
107 
108  // True if the number of offers that have been crossed
109  // exceeds the limit.
110  bool
111  reachedOfferCrossingLimit(Taker const& taker) const;
112 
113  // Fill offer as much as possible by consuming offers already on the books,
114  // and adjusting account balances accordingly.
115  //
116  // Charges fees on top to taker.
118  takerCross(Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount);
119 
120  // Use the payment flow code to perform offer crossing.
122  flowCross(
123  PaymentSandbox& psb,
124  PaymentSandbox& psbCancel,
125  Amounts const& takerAmount);
126 
127  // Temporary
128  // This is a central location that invokes both versions of cross
129  // so the results can be compared. Eventually this layer will be
130  // removed once flowCross is determined to be stable.
132  cross(Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount);
133 
134  static std::string
135  format_amount(STAmount const& amount);
136 
137 private:
138  // What kind of offer we are placing
140 
141  // The number of steps to take through order books while crossing
142  OfferStream::StepCounter stepCounter_;
143 };
144 
145 } // namespace ripple
146 
147 #endif
ripple::CreateOffer::takerCross
std::pair< TER, Amounts > takerCross(Sandbox &sb, Sandbox &sbCancel, Amounts const &takerAmount)
Definition: CreateOffer.cpp:627
ripple::CreateOffer::flowCross
std::pair< TER, Amounts > flowCross(PaymentSandbox &psb, PaymentSandbox &psbCancel, Amounts const &takerAmount)
Definition: CreateOffer.cpp:672
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
std::string
STL class.
utility
ripple::Transactor
Definition: Transactor.h:85
ripple::Taker
Definition: Taker.h:240
ripple::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition: PaymentSandbox.h:112
ripple::Transactor::j_
const beast::Journal j_
Definition: Transactor.h:89
std::pair
ripple::CreateOffer::ConsequencesFactory
static constexpr ConsequencesFactoryType ConsequencesFactory
Definition: CreateOffer.h:37
ripple::ApplyFlags
ApplyFlags
Definition: ApplyView.h:29
ripple::OfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:147
ripple::CreateOffer::preflight
static NotTEC preflight(PreflightContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
Definition: CreateOffer.cpp:43
ripple::CreateOffer::CreateOffer
CreateOffer(ApplyContext &ctx)
Construct a Transactor subclass that creates an offer in the ledger.
Definition: CreateOffer.h:40
ripple::CreateOffer::applyGuts
std::pair< TER, bool > applyGuts(Sandbox &view, Sandbox &view_cancel)
Definition: CreateOffer.cpp:900
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::CreateOffer::format_amount
static std::string format_amount(STAmount const &amount)
Definition: CreateOffer.cpp:877
ripple::CreateOffer::bridged_cross
std::pair< TER, Amounts > bridged_cross(Taker &taker, ApplyView &view, ApplyView &view_cancel, NetClock::time_point const when)
Definition: CreateOffer.cpp:322
ripple::Transactor::ConsequencesFactoryType
ConsequencesFactoryType
Definition: Transactor.h:101
ripple::CreateOffer::preclaim
static TER preclaim(PreclaimContext const &ctx)
Enforce constraints beyond those of the Transactor base class.
Definition: CreateOffer.cpp:129
ripple::CrossType
CrossType
The flavor of an offer crossing.
Definition: Taker.h:36
ripple::CreateOffer::preCompute
void preCompute() override
Gather information beyond what the Transactor base class gathers.
Definition: CreateOffer.cpp:886
ripple::TERSubset
Definition: TER.h:340
ripple::CreateOffer::step_account
static bool step_account(OfferStream &stream, Taker const &taker)
Definition: CreateOffer.cpp:604
ripple::Sandbox
Discardable, editable view to a ledger.
Definition: Sandbox.h:34
ripple::STAmount
Definition: STAmount.h:45
std::chrono::time_point
ripple::CreateOffer::makeTxConsequences
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
Definition: CreateOffer.cpp:32
ripple::ApplyContext
State information when applying a tx.
Definition: ApplyContext.h:35
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::CreateOffer
Transactor specialized for creating offers in the ledger.
Definition: CreateOffer.h:34
ripple::CreateOffer::doApply
TER doApply() override
Precondition: fee collection is likely.
Definition: CreateOffer.cpp:1210
ripple::CreateOffer::cross_type_
CrossType cross_type_
Definition: CreateOffer.h:139
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:52
ripple::Transactor::Custom
@ Custom
Definition: Transactor.h:101
ripple::CreateOffer::direct_cross
std::pair< TER, Amounts > direct_cross(Taker &taker, ApplyView &view, ApplyView &view_cancel, NetClock::time_point const when)
Definition: CreateOffer.cpp:511
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CreateOffer::cross
std::pair< TER, Amounts > cross(Sandbox &sb, Sandbox &sbCancel, Amounts const &takerAmount)
Definition: CreateOffer.cpp:856
ripple::Transactor::view
ApplyView & view()
Definition: Transactor.h:107
ripple::CreateOffer::reachedOfferCrossingLimit
bool reachedOfferCrossingLimit(Taker const &taker) const
Definition: CreateOffer.cpp:311
ripple::CreateOffer::dry_offer
bool dry_offer(ApplyView &view, Offer const &offer)
Definition: CreateOffer.cpp:264
ripple::CreateOffer::checkAcceptAsset
static TER checkAcceptAsset(ReadView const &view, ApplyFlags const flags, AccountID const id, beast::Journal const j, Issue const &issue)
Definition: CreateOffer.cpp:205
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:31
ripple::CreateOffer::select_path
static std::pair< bool, Quality > select_path(bool have_direct, OfferStream const &direct, bool have_bridge, OfferStream const &leg1, OfferStream const &leg2)
Definition: CreateOffer.cpp:278
ripple::TxConsequences
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition: applySteps.h:45
ripple::CreateOffer::stepCounter_
OfferStream::StepCounter stepCounter_
Definition: CreateOffer.h:142
ripple::TOffer
Definition: Offer.h:49