Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTTrade.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTTrade.hpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 // An OTTrade is derived from OTCronItem. OTCron has a list of items,
134 // which may be trades or agreements or who knows what next.
135 
136 #ifndef OPENTXS_CORE_TRADE_OTTRADE_HPP
137 #define OPENTXS_CORE_TRADE_OTTRADE_HPP
138 
139 #include "OTMarket.hpp"
140 #include "OTOffer.hpp"
141 #include "../cron/OTCronItem.hpp"
142 
143 namespace opentxs
144 {
145 
146 class OTIdentifier;
147 class OTPseudonym;
148 
149 /*
150  OTTrade
151 
152  Standing Order (for Trades) MUST STORE:
153 
154  X 1) Transaction ID // It took a transaction number to create this trade. We
155  record it here and use it to uniquely identify the trade, like any other
156  transaction.
157  X 4) CURRENCY TYPE ID (Currency type ID of whatever I’m trying to buy or sell
158  WITH. Dollars? Euro?)
159  X 5) Account ID SENDER (for above currency type. This is the account where I
160  make my payments from, to satisfy the trades.)
161  X 6) Valid date range. (Start. Expressed as an absolute date.)
162  X 7) Valid date range. ( End. Expressed as an absolute date.)
163 
164  X 2) Creation date.
165  X 3) INTEGER: Number of trades that have processed through this order.
166 
167  X 8) STOP ORDER — SIGN (nullptr if not a stop order — otherwise GREATER THAN or
168  LESS THAN…)
169  X 9) STOP ORDER — PRICE (…AT X PRICE, POST THE OFFER TO THE MARKET.)
170 
171  Cron for these orders must check expiration dates and stop order prices.
172 
173  ———————————————————————————————
174  */
175 
176 class OTTrade : public OTCronItem
177 {
178 private:
179  typedef OTCronItem ot_super;
180 
181  OTIdentifier currencyTypeID_; // GOLD (Asset) is trading for DOLLARS
182  // (Currency).
183  OTIdentifier currencyAcctID_; // My Dollar account, used for paying for
184  // my Gold (say) trades.
185 
186  OTOffer* offer_; // The pointer to the Offer (NOT responsible for cleaning
187  // this up!!!
188  // The offer is owned by the market and I only keep a pointer here for
189  // convenience.
190 
191  bool hasTradeActivated_; // Has the offer yet been first added to a
192  // market?
193 
194  int64_t stopPrice_; // The price limit that activates the STOP order.
195  char stopSign_; // Value is 0, or '<', or '>'.
196  bool stopActivated_; // If the Stop Order has already activated, I need
197  // to know that.
198 
199  int32_t tradesAlreadyDone_; // How many trades have already processed
200  // through this order? We keep track.
201 
202  OTString marketOffer_; // The market offer associated with this trade.
203 
204 protected:
205  virtual void onFinalReceipt(OTCronItem& origCronItem,
206  const int64_t& newTransactionNumber,
207  OTPseudonym& originator, OTPseudonym* remover);
208  virtual void onRemovalFromCron();
209 
210 public:
211  EXPORT bool VerifyOffer(OTOffer& offer) const;
212  EXPORT bool IssueTrade(OTOffer& offer, char stopSign = 0,
213  int64_t stopPrice = 0);
214 
215  // The Trade always stores the original, signed version of its Offer.
216  // This method allows you to grab a copy of it.
217  inline bool GetOfferString(OTString& offer)
218  {
219  offer.Set(marketOffer_);
220  if (marketOffer_.Exists()) {
221  return true;
222  }
223  return false;
224  }
225 
226  inline bool IsStopOrder() const
227  {
228  if ((stopSign_ == '<') || (stopSign_ == '>')) {
229  return true;
230  }
231  return false;
232  }
233 
234  inline const int64_t& GetStopPrice() const
235  {
236  return stopPrice_;
237  }
238 
239  inline bool IsGreaterThan() const
240  {
241  if (stopSign_ == '>') {
242  return true;
243  }
244  return false;
245  }
246 
247  inline bool IsLessThan() const
248  {
249  if (stopSign_ == '<') {
250  return true;
251  }
252  return false;
253  }
254 
255  // optionally returns the offer's market ID and a pointer to the market.
256  OTOffer* GetOffer(OTIdentifier* offerMarketId = nullptr,
257  OTMarket* *market = nullptr);
258 
259  inline const OTIdentifier& GetCurrencyID() const
260  {
261  return currencyTypeID_;
262  }
263 
264  inline void SetCurrencyID(const OTIdentifier& currencyId)
265  {
266  currencyTypeID_ = currencyId;
267  }
268 
269  inline const OTIdentifier& GetCurrencyAcctID() const
270  {
271  return currencyAcctID_;
272  }
273 
274  inline void SetCurrencyAcctID(const OTIdentifier& currencyAcctID)
275  {
276  currencyAcctID_ = currencyAcctID;
277  }
278 
280  {
281  tradesAlreadyDone_++;
282  }
283 
284  inline int32_t GetCompletedCount()
285  {
286  return tradesAlreadyDone_;
287  }
288 
289  EXPORT int64_t GetAssetAcctClosingNum() const;
290  EXPORT int64_t GetCurrencyAcctClosingNum() const;
291 
292  // Return True if should stay on OTCron's list for more processing.
293  // Return False if expired or otherwise should be removed.
294  virtual bool ProcessCron(); // OTCron calls this regularly, which is my
295  // chance to expire, etc.
296  virtual bool CanRemoveItemFromCron(OTPseudonym& nym);
297 
298  // From OTScriptable, we override this function. OTScriptable now does fancy
299  // stuff like checking to see
300  // if the Nym is an agent working on behalf of a party to the contract.
301  // That's how all OTScriptable-derived
302  // objects work by default. But OTAgreement (payment plan) and OTTrade do
303  // it the old way: they just check to
304  // see if theNym has signed *this.
305  //
306  virtual bool VerifyNymAsAgent(OTPseudonym& nym, OTPseudonym& signerNym,
307  mapOfNyms* preloadedMap = nullptr) const;
308 
309  virtual bool VerifyNymAsAgentForAccount(OTPseudonym& nym,
310  OTAccount& account) const;
311  EXPORT OTTrade();
312  EXPORT OTTrade(const OTIdentifier& serverId, const OTIdentifier& assetId,
313  const OTIdentifier& assetAcctId, const OTIdentifier& userId,
314  const OTIdentifier& currencyId,
315  const OTIdentifier& currencyAcctId);
316  EXPORT virtual ~OTTrade();
317 
318  void InitTrade();
319 
320  void Release_Trade();
321  virtual void Release();
322  virtual int64_t GetClosingNumber(const OTIdentifier& acctId) const;
323  // return -1 if error, 0 if nothing, and 1 if the node was processed.
324  virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader*& xml);
325 
326  virtual void UpdateContents(); // Before transmission or serialization, this
327  // is where the ledger saves its contents
328 
329  virtual bool SaveContractWallet(std::ofstream& ofs) const;
330 };
331 
332 } // namespace opentxs
333 
334 #endif // OPENTXS_CORE_TRADE_OTTRADE_HPP
virtual bool SaveContractWallet(std::ofstream &ofs) const
Definition: OTTrade.cpp:1409
int32_t GetCompletedCount()
Definition: OTTrade.hpp:284
virtual bool VerifyNymAsAgent(OTPseudonym &nym, OTPseudonym &signerNym, mapOfNyms *preloadedMap=nullptr) const
Definition: OTTrade.cpp:165
bool IsGreaterThan() const
Definition: OTTrade.hpp:239
virtual int64_t GetClosingNumber(const OTIdentifier &acctId) const
Definition: OTTrade.cpp:790
virtual void Release()
Definition: OTTrade.cpp:1376
bool IsLessThan() const
Definition: OTTrade.hpp:247
std::map< std::string, OTPseudonym * > mapOfNyms
Definition: OTWallet.hpp:161
const OTIdentifier & GetCurrencyAcctID() const
Definition: OTTrade.hpp:269
const int64_t & GetStopPrice() const
Definition: OTTrade.hpp:234
EXPORT bool IssueTrade(OTOffer &offer, char stopSign=0, int64_t stopPrice=0)
Definition: OTTrade.cpp:1262
void IncrementTradesAlreadyDone()
Definition: OTTrade.hpp:279
virtual EXPORT ~OTTrade()
Definition: OTTrade.cpp:1360
EXPORT bool Exists() const
Definition: OTString.cpp:1035
EXPORT bool VerifyOffer(OTOffer &offer) const
Definition: OTTrade.cpp:389
virtual void onRemovalFromCron()
Definition: OTTrade.cpp:716
virtual void onFinalReceipt(OTCronItem &origCronItem, const int64_t &newTransactionNumber, OTPseudonym &originator, OTPseudonym *remover)
Definition: OTTrade.cpp:892
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
virtual bool ProcessCron()
Definition: OTTrade.cpp:1138
void Release_Trade()
Definition: OTTrade.cpp:1366
void SetCurrencyAcctID(const OTIdentifier &currencyAcctID)
Definition: OTTrade.hpp:274
const OTIdentifier & GetCurrencyID() const
Definition: OTTrade.hpp:259
void SetCurrencyID(const OTIdentifier &currencyId)
Definition: OTTrade.hpp:264
virtual bool CanRemoveItemFromCron(OTPseudonym &nym)
Definition: OTTrade.cpp:813
EXPORT int64_t GetCurrencyAcctClosingNum() const
Definition: OTTrade.cpp:805
EXPORT int64_t GetAssetAcctClosingNum() const
Definition: OTTrade.cpp:799
EXPORT OTTrade()
Definition: OTTrade.cpp:1316
bool GetOfferString(OTString &offer)
Definition: OTTrade.hpp:217
virtual bool VerifyNymAsAgentForAccount(OTPseudonym &nym, OTAccount &account) const
Definition: OTTrade.cpp:175
virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader *&xml)
Definition: OTTrade.cpp:182
OTOffer * GetOffer(OTIdentifier *offerMarketId=nullptr, OTMarket **market=nullptr)
Definition: OTTrade.cpp:421
virtual void UpdateContents()
Definition: OTTrade.cpp:311
bool IsStopOrder() const
Definition: OTTrade.hpp:226