Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTMarket.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTMarket.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 // A market has a list of OTOffers for all the bids, and another list of
134 // OTOffers for all the asks.
135 // Presumably the server will have different markets for different asset types.
136 
137 #ifndef OPENTXS_CORE_TRADE_OTMARKET_HPP
138 #define OPENTXS_CORE_TRADE_OTMARKET_HPP
139 
140 #include "OTOffer.hpp"
141 #include "../cron/OTCron.hpp"
142 #include "../OTStorage.hpp"
143 
144 namespace opentxs
145 {
146 
147 class OTTrade;
148 
149 #define MAX_MARKET_QUERY_DEPTH \
150  50 // todo add this to the ini file. (Now that we actually have one.)
151 
152 // Multiple offers, mapped by price limit.
153 // Using multi-map since there will be more than one offer for each single
154 // price.
155 // (Map would only allow a single item on the map for each price.)
156 typedef std::multimap<int64_t, OTOffer*> mapOfOffers;
157 
158 // The same offers are also mapped (uniquely) to transaction number.
159 typedef std::map<int64_t, OTOffer*> mapOfOffersTrnsNum;
160 
161 class OTMarket : public OTContract
162 {
163 private: // Private prevents erroneous use by other classes.
164  typedef OTContract ot_super;
165 
166 private:
167  OTCron* m_pCron; // The Cron object that owns this Market.
168 
169  OTDB::TradeListMarket* m_pTradeList;
170 
171  mapOfOffers m_mapBids; // The buyers, ordered by price limit
172  mapOfOffers m_mapAsks; // The sellers, ordered by price limit
173 
174  mapOfOffersTrnsNum m_mapOffers; // All of the offers on a single list,
175  // ordered by transaction number.
176 
177  OTIdentifier m_SERVER_ID; // Always store this in any object that's
178  // associated with a specific server.
179 
180  // Every market involves a certain asset type being traded in a certain
181  // currency.
182  OTIdentifier m_ASSET_TYPE_ID; // This is the GOLD market. (Say.) | (GOLD
183  // for
184  OTIdentifier m_CURRENCY_TYPE_ID; // Gold is trading for DOLLARS. |
185  // DOLLARS, for example.)
186 
187  // Each Offer on the market must have a minimum increment that this divides
188  // equally into.
189  // (There is a "gold for dollars, minimum 1 oz" market, a "gold for dollars,
190  // min 500 oz" market, etc.)
191  int64_t m_lScale;
192 
193  int64_t m_lLastSalePrice;
194  std::string m_strLastSaleDate;
195 
196  // The server stores a map of markets, one for each unique combination of
197  // asset types.
198  // That's what this market class represents: one asset type being traded and
199  // priced in another.
200  // It could be wheat for dollars, wheat for yen, or gold for dollars, or
201  // gold for wheat, or
202  // gold for oil, or oil for wheat. REALLY, THE TWO ARE JUST ARBITRARY ASSET
203  // TYPES. But in
204  // order to keep terminology clear, I will refer to one as the "asset type"
205  // and the other as
206  // the "currency type" so that it stays VERY clear which asset type is up
207  // for sale, and which
208  // asset type (currency type) it is being priced in. Other than that, the
209  // two are technically
210  // interchangeable.
211 
212 public:
213  bool ValidateOfferForMarket(OTOffer& theOffer, OTString* pReason = nullptr);
214 
215  OTOffer* GetOffer(const int64_t& lTransactionNum);
216  bool AddOffer(OTTrade* pTrade, OTOffer& theOffer, bool bSaveFile = true,
217  time64_t tDateAddedToMarket = OT_TIME_ZERO);
218  bool RemoveOffer(const int64_t& lTransactionNum);
219  // returns general information about offers on the market
220  EXPORT bool GetOfferList(OTASCIIArmor& ascOutput, int64_t lDepth,
221  int32_t& nOfferCount);
222  EXPORT bool GetRecentTradeList(OTASCIIArmor& ascOutput,
223  int32_t& nTradeCount);
224 
225  // Returns more detailed information about offers for a specific Nym.
226  bool GetNym_OfferList(const OTIdentifier& NYM_ID,
227  OTDB::OfferListNym& theOutputList,
228  int32_t& nNymOfferCount);
229 
230  // Assumes a few things: Offer is part of Trade, and both have been
231  // proven already to be a part of this market.
232  // Basically the Offer is looked up on the Market by the Trade, and
233  // then both are passed in here.
234  // --Returns True if Trade should stay on the Cron list for more processing.
235  // --Returns False if it should be removed and deleted.
236  void ProcessTrade(OTTrade& theTrade, OTOffer& theOffer,
237  OTOffer& theOtherOffer);
238  bool ProcessTrade(OTTrade& theTrade, OTOffer& theOffer);
239 
240  int64_t GetHighestBidPrice();
241  int64_t GetLowestAskPrice();
242 
243  mapOfOffers::size_type GetBidCount()
244  {
245  return m_mapBids.size();
246  }
247  mapOfOffers::size_type GetAskCount()
248  {
249  return m_mapAsks.size();
250  }
251  void SetAssetID(const OTIdentifier& ASSET_ID)
252  {
253  m_ASSET_TYPE_ID = ASSET_ID;
254  }
255  void SetCurrencyID(const OTIdentifier& CURRENCY_ID)
256  {
257  m_CURRENCY_TYPE_ID = CURRENCY_ID;
258  }
259  void SetServerID(const OTIdentifier& SERVER_ID)
260  {
261  m_SERVER_ID = SERVER_ID;
262  }
263 
264  inline const OTIdentifier& GetAssetID() const
265  {
266  return m_ASSET_TYPE_ID;
267  }
268  inline const OTIdentifier& GetCurrencyID() const
269  {
270  return m_CURRENCY_TYPE_ID;
271  }
272  inline const OTIdentifier& GetServerID() const
273  {
274  return m_SERVER_ID;
275  }
276 
277  inline const int64_t& GetScale() const
278  {
279  return m_lScale;
280  }
281  inline void SetScale(const int64_t& lScale)
282  {
283  m_lScale = lScale;
284  if (m_lScale < 1) m_lScale = 1;
285  }
286 
287  inline const int64_t& GetLastSalePrice()
288  {
289  if (m_lLastSalePrice < 1) m_lLastSalePrice = 1;
290  return m_lLastSalePrice;
291  }
292  inline void SetLastSalePrice(const int64_t& lLastSalePrice)
293  {
294  m_lLastSalePrice = lLastSalePrice;
295  if (m_lLastSalePrice < 1) m_lLastSalePrice = 1;
296  }
297 
298  const std::string& GetLastSaleDate()
299  {
300  return m_strLastSaleDate;
301  }
302  int64_t GetTotalAvailableAssets();
303  OTMarket();
304  OTMarket(const char* szFilename);
305  OTMarket(const OTIdentifier& SERVER_ID, const OTIdentifier& ASSET_TYPE_ID,
306  const OTIdentifier& CURRENCY_TYPE_ID, const int64_t& lScale);
307 
308  virtual ~OTMarket();
309 
310  // Overridden from OTContract.
311  virtual void GetIdentifier(OTIdentifier& theIdentifier) const;
312 
313  inline void SetCronPointer(OTCron& theCron)
314  {
315  m_pCron = &theCron;
316  }
317  inline OTCron* GetCron()
318  {
319  return m_pCron;
320  }
321  bool LoadMarket();
322  bool SaveMarket();
323 
324  void InitMarket();
325 
326  virtual void Release();
327  void Release_Market();
328 
329  // return -1 if error, 0 if nothing, and 1 if the node was processed.
330  virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader*& xml);
331 
332  virtual void UpdateContents(); // Before transmission or serialization, this
333  // is where the ledger saves its contents
334 
335  virtual bool SaveContractWallet(std::ofstream& ofs) const;
336 };
337 
338 } // namespace opentxs
339 
340 #endif // OPENTXS_CORE_TRADE_OTMARKET_HPP
void SetCurrencyID(const OTIdentifier &CURRENCY_ID)
Definition: OTMarket.hpp:255
bool AddOffer(OTTrade *pTrade, OTOffer &theOffer, bool bSaveFile=true, time64_t tDateAddedToMarket=OT_TIME_ZERO)
Definition: OTMarket.cpp:740
OTOffer * GetOffer(const int64_t &lTransactionNum)
Definition: OTMarket.cpp:623
void SetAssetID(const OTIdentifier &ASSET_ID)
Definition: OTMarket.hpp:251
virtual ~OTMarket()
Definition: OTMarket.cpp:2778
void SetLastSalePrice(const int64_t &lLastSalePrice)
Definition: OTMarket.hpp:292
bool GetNym_OfferList(const OTIdentifier &NYM_ID, OTDB::OfferListNym &theOutputList, int32_t &nNymOfferCount)
Definition: OTMarket.cpp:310
mapOfOffers::size_type GetBidCount()
Definition: OTMarket.hpp:243
const OTIdentifier & GetCurrencyID() const
Definition: OTMarket.hpp:268
int64_t GetLowestAskPrice()
Definition: OTMarket.cpp:934
const OTIdentifier & GetServerID() const
Definition: OTMarket.hpp:272
int64_t time64_t
Definition: Common.hpp:209
const OTIdentifier & GetAssetID() const
Definition: OTMarket.hpp:264
virtual void UpdateContents()
Definition: OTMarket.cpp:234
std::map< int64_t, OTOffer * > mapOfOffersTrnsNum
Definition: OTMarket.hpp:159
std::multimap< int64_t, OTOffer * > mapOfOffers
Definition: OTMarket.hpp:156
virtual void Release()
Definition: OTMarket.cpp:2818
virtual void GetIdentifier(OTIdentifier &theIdentifier) const
Definition: OTMarket.cpp:903
virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader *&xml)
Definition: OTMarket.cpp:153
bool ValidateOfferForMarket(OTOffer &theOffer, OTString *pReason=nullptr)
Definition: OTMarket.cpp:2660
virtual bool SaveContractWallet(std::ofstream &ofs) const
Definition: OTMarket.cpp:2830
void SetServerID(const OTIdentifier &SERVER_ID)
Definition: OTMarket.hpp:259
const int64_t & GetScale() const
Definition: OTMarket.hpp:277
EXPORT bool GetRecentTradeList(OTASCIIArmor &ascOutput, int32_t &nTradeCount)
Definition: OTMarket.cpp:408
mapOfOffers::size_type GetAskCount()
Definition: OTMarket.hpp:247
void ProcessTrade(OTTrade &theTrade, OTOffer &theOffer, OTOffer &theOtherOffer)
Definition: OTMarket.cpp:1023
int64_t GetHighestBidPrice()
Definition: OTMarket.cpp:919
void SetScale(const int64_t &lScale)
Definition: OTMarket.hpp:281
EXPORT bool GetOfferList(OTASCIIArmor &ascOutput, int64_t lDepth, int32_t &nOfferCount)
Definition: OTMarket.cpp:478
int64_t GetTotalAvailableAssets()
Definition: OTMarket.cpp:294
#define OT_TIME_ZERO
Definition: Common.hpp:180
const int64_t & GetLastSalePrice()
Definition: OTMarket.hpp:287
const std::string & GetLastSaleDate()
Definition: OTMarket.hpp:298
bool RemoveOffer(const int64_t &lTransactionNum)
Definition: OTMarket.cpp:649
OTCron * GetCron()
Definition: OTMarket.hpp:317
void SetCronPointer(OTCron &theCron)
Definition: OTMarket.hpp:313