Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mastercore_dex.h
Go to the documentation of this file.
1 #ifndef _MASTERCOIN_DEX
2 #define _MASTERCOIN_DEX 1
3 
4 #include "mastercore.h"
5 
6 // this is the internal format for the offer primary key (TODO: replace by a class method)
7 #define STR_SELLOFFER_ADDR_PROP_COMBO(x) ( x + "-" + strprintf("%d", prop))
8 #define STR_ACCEPT_ADDR_PROP_ADDR_COMBO( _seller , _buyer ) ( _seller + "-" + strprintf("%d", prop) + "+" + _buyer)
9 #define STR_PAYMENT_SUBKEY_TXID_PAYMENT_COMBO(txidStr) ( txidStr + "-" + strprintf("%d", paymentNumber))
10 #define STR_REF_SUBKEY_TXID_REF_COMBO(txidStr) ( txidStr + strprintf("%d", refNumber))
11 
12 #define DISPLAY_PRECISION_LEN 50
13 #define INTERNAL_PRECISION_LEN 50
14 
15 #include <boost/multiprecision/cpp_dec_float.hpp>
16 using boost::multiprecision::cpp_dec_float_100;
17 
18 // a single outstanding offer -- from one seller of one property, internally may have many accepts
19 class CMPOffer
20 {
21 private:
23  uint64_t offer_amount_original; // the amount of MSC for sale specified when the offer was placed
24  unsigned int property;
25  uint64_t BTC_desired_original; // amount desired, in BTC
26  uint64_t min_fee;
27  unsigned char blocktimelimit;
29  unsigned char subaction;
30 
31 public:
32  uint256 getHash() const { return txid; }
33  unsigned int getProperty() const { return property; }
34  uint64_t getMinFee() const { return min_fee ; }
35  unsigned char getBlockTimeLimit() const { return blocktimelimit; }
36  unsigned char getSubaction() const { return subaction; }
37 
38  uint64_t getOfferAmountOriginal() const { return offer_amount_original; }
39  uint64_t getBTCDesiredOriginal() const { return BTC_desired_original; }
40 
41  CMPOffer():offerBlock(0),offer_amount_original(0),property(0),BTC_desired_original(0),min_fee(0),blocktimelimit(0),txid(0)
42  {
43  }
44 
45  CMPOffer(int b, uint64_t a, unsigned int cu, uint64_t d, uint64_t fee, unsigned char btl, const uint256 &tx)
46  :offerBlock(b),offer_amount_original(a),property(cu),BTC_desired_original(d),min_fee(fee),blocktimelimit(btl),txid(tx)
47  {
48  if (msc_debug_dex) file_log("%s(%lu): %s , line %d, file: %s\n", __FUNCTION__, a, txid.GetHex(), __LINE__, __FILE__);
49  }
50 
51  void Set(uint64_t d, uint64_t fee, unsigned char btl, unsigned char suba)
52  {
53  BTC_desired_original = d;
54  min_fee = fee;
55  blocktimelimit = btl;
56  subaction = suba;
57  }
58 
59  void saveOffer(ofstream &file, SHA256_CTX *shaCtx, string const &addr ) const {
60  // compose the outputline
61  // seller-address, ...
62  string lineOut = (boost::format("%s,%d,%d,%d,%d,%d,%d,%d,%s")
63  % addr
64  % offerBlock
65  % offer_amount_original
66  % property
67  % BTC_desired_original
68  % ( OMNI_PROPERTY_BTC )
69  % min_fee
70  % (int)blocktimelimit
71  % txid.ToString()).str();
72 
73  // add the line to the hash
74  SHA256_Update(shaCtx, lineOut.c_str(), lineOut.length());
75 
76  // write the line
77  file << lineOut << endl;
78  }
79 }; // end of CMPOffer class
80 
81 // do a map of buyers, primary key is buyer+property
82 // MUST account for many possible accepts and EACH property offer
83 class CMPAccept
84 {
85 private:
86  uint64_t accept_amount_original; // amount of MSC/TMSC desired to purchased
87  uint64_t accept_amount_remaining; // amount of MSC/TMSC remaining to purchased
88 // once accept is seen on the network the amount of MSC being purchased is taken out of seller's SellOffer-Reserve and put into this Buyer's Accept-Reserve
89  unsigned char blocktimelimit; // copied from the offer during creation
90  unsigned int property; // copied from the offer during creation
91 
92  uint64_t offer_amount_original; // copied from the Offer during Accept's creation
93  uint64_t BTC_desired_original; // copied from the Offer during Accept's creation
94 
95  uint256 offer_txid; // the original offers TXIDs, needed to match Accept to the Offer during Accept's destruction, etc.
96 
97 public:
98  uint256 getHash() const { return offer_txid; }
99 
102 
103  int block; // 'accept' message sent in this block
104 
105  unsigned char getBlockTimeLimit() { return blocktimelimit; }
106  unsigned int getProperty() const { return property; }
107 
108  int getAcceptBlock() { return block; }
109 
110  CMPAccept(uint64_t a, int b, unsigned char blt, unsigned int c, uint64_t o, uint64_t btc, const uint256 &txid):accept_amount_remaining(a),blocktimelimit(blt),property(c),
111  offer_amount_original(o), BTC_desired_original(btc),offer_txid(txid),block(b)
112  {
113  accept_amount_original = accept_amount_remaining;
114  file_log("%s(%lu), line %d, file: %s\n", __FUNCTION__, a, __LINE__, __FILE__);
115  }
116 
117  CMPAccept(uint64_t origA, uint64_t remA, int b, unsigned char blt, unsigned int c, uint64_t o, uint64_t btc, const uint256 &txid):accept_amount_original(origA),accept_amount_remaining(remA),blocktimelimit(blt),property(c),
118  offer_amount_original(o), BTC_desired_original(btc),offer_txid(txid),block(b)
119  {
120  file_log("%s(%lu[%lu]), line %d, file: %s\n", __FUNCTION__, remA, origA, __LINE__, __FILE__);
121  }
122 
123  void print()
124  {
125  file_log("buying: %12.8lf (originally= %12.8lf) in block# %d\n",
126  (double)accept_amount_remaining/(double)COIN, (double)accept_amount_original/(double)COIN, block);
127  }
128 
129  uint64_t getAcceptAmountRemaining() const
130  {
131  file_log("%s(); buyer still wants = %lu, line %d, file: %s\n", __FUNCTION__, accept_amount_remaining, __LINE__, __FILE__);
132 
134  }
135 
136  // reduce accept_amount_remaining and return "true" if the customer is fully satisfied (nothing desired to be purchased)
137  bool reduceAcceptAmountRemaining_andIsZero(const uint64_t really_purchased)
138  {
139  bool bRet = false;
140 
141  if (really_purchased >= accept_amount_remaining) bRet = true;
142 
143  accept_amount_remaining -= really_purchased;
144 
145  return bRet;
146  }
147 
148  void saveAccept(ofstream &file, SHA256_CTX *shaCtx, string const &addr, string const &buyer ) const {
149  // compose the outputline
150  // seller-address, property, buyer-address, amount, fee, block
151  string lineOut = (boost::format("%s,%d,%s,%d,%d,%d,%d,%d,%d,%s")
152  % addr
153  % property
154  % buyer
155  % block
156  % accept_amount_remaining
157  % accept_amount_original
158  % (int)blocktimelimit
159  % offer_amount_original
160  % BTC_desired_original
161  % offer_txid.ToString()).str();
162 
163  // add the line to the hash
164  SHA256_Update(shaCtx, lineOut.c_str(), lineOut.length());
165 
166  // write the line
167  file << lineOut << endl;
168  }
169 
170 }; // end of CMPAccept class
171 
172 typedef cpp_dec_float_100 XDOUBLE;
173 // typedef double XDOUBLE;
174 
175 // a metadex trade
177 {
178 private:
179  int block;
181  unsigned int idx; // index within the block
182  unsigned int property;
183  uint64_t amount_forsale; // the amount for sale specified when the offer was placed
184  unsigned int desired_property;
185  int64_t amount_desired;
186 
188 
189  unsigned char subaction;
190 
191  string addr;
192 
193 public:
194  uint256 getHash() const { return txid; }
195  void setHash(uint256 hash) { txid = hash; }
196 
197  unsigned int getProperty() const { return property; }
198 
199  unsigned int getDesProperty() const { return desired_property; }
200  const string & getAddr() const { return addr; }
201 
202  uint64_t getAmountForSale() const { return amount_forsale; }
203  int64_t getAmountDesired() const { return amount_desired; }
204 
205  void setAmountForSale(int64_t ao, const string &label = "")
206  {
207  amount_forsale = ao;
208  file_log("%s(%ld %s):%s\n", __FUNCTION__, ao, label, ToString());
209  }
210 
211  void setAmountDesired(int64_t ad, const string &label = "")
212  {
213  amount_desired = ad;
214  file_log("%s(%ld %s):%s\n", __FUNCTION__, ad, label, ToString());
215  }
216 
217  unsigned char getAction() const { return subaction; }
218 
219  int getBlock() const { return block; }
220  unsigned int getIdx() const { return idx; }
221 
222  uint64_t getBlockTime() const
223  {
224  CBlockIndex* pblockindex = chainActive[block];
225  return pblockindex->GetBlockTime();
226  }
227 
228  // needed only by the RPC functions
229  CMPMetaDEx():block(0),txid(0),idx(0),property(0),amount_forsale(0),desired_property(0),amount_desired(0),still_left_forsale(0),subaction(0)
230  {
231  still_left_forsale = 0;
232  addr.empty();
233  }
234 
235  CMPMetaDEx(const string &, int, unsigned int, uint64_t, unsigned int, uint64_t, const uint256 &, unsigned int, unsigned char, uint64_t lfors = 0);
236 
237  void Set(const string &, int, unsigned int, uint64_t, unsigned int, uint64_t, const uint256 &, unsigned int, unsigned char);
238 
239  std::string ToString() const;
240 
242  {
243  XDOUBLE effective_price = 0;
244 
245  // I am the seller
246  if (amount_forsale) effective_price = (XDOUBLE) amount_desired / (XDOUBLE) amount_forsale; // division by 0 check
247 
248  return (effective_price);
249  }
250 
251  void saveOffer(ofstream &file, SHA256_CTX *shaCtx) const;
252 };
253 
254 unsigned int eraseExpiredAccepts(int blockNow);
255 
256 namespace mastercore
257 {
258 typedef std::map<string, CMPOffer> OfferMap;
259 typedef std::map<string, CMPAccept> AcceptMap;
260 
261 extern OfferMap my_offers;
262 extern AcceptMap my_accepts;
263 
264 typedef std::pair < uint64_t, uint64_t > MetaDExTypePrice; // the price split up into integer & fractional part for precision
265 
267 {
268 public:
269 
270  bool operator()(const CMPMetaDEx &lhs, const CMPMetaDEx &rhs) const;
271 };
272 
273 // ---------------
274 typedef std::set < CMPMetaDEx , MetaDEx_compare > md_Set; // set of objects sorted by block+idx
275 
276 // replaced double with float512 or float1024 // hitting the limit on trading 1 Satoshi for 100 BTC !!!
277 typedef std::map < XDOUBLE , md_Set > md_PricesMap; // map of prices; there is a set of sorted objects for each price
278 typedef std::map < unsigned int, md_PricesMap > md_PropertiesMap; // map of properties; there is a map of prices for each property
279 
280 extern md_PropertiesMap metadex;
281 // TODO: explore a property-pair, instead of a single property as map's key........
282 // ---------------
283 
284 bool DEx_offerExists(const string &seller_addr, unsigned int);
285 CMPOffer *DEx_getOffer(const string &seller_addr, unsigned int);
286 CMPAccept *DEx_getAccept(const string &seller_addr, unsigned int, const string &buyer_addr);
287 int DEx_offerCreate(string seller_addr, unsigned int, uint64_t nValue, int block, uint64_t amount_desired, uint64_t fee, unsigned char btl, const uint256 &txid, uint64_t *nAmended = NULL);
288 int DEx_offerDestroy(const string &seller_addr, unsigned int);
289 int DEx_offerUpdate(const string &seller_addr, unsigned int, uint64_t nValue, int block, uint64_t desired, uint64_t fee, unsigned char btl, const uint256 &txid, uint64_t *nAmended = NULL);
290 int DEx_acceptCreate(const string &buyer, const string &seller, int, uint64_t nValue, int block, uint64_t fee_paid, uint64_t *nAmended = NULL);
291 int DEx_acceptDestroy(const string &buyer, const string &seller, int, bool bForceErase = false);
292 int DEx_payment(uint256 txid, unsigned int vout, string seller, string buyer, uint64_t BTC_paid, int blockNow, uint64_t *nAmended = NULL);
293 
294 int MetaDEx_ADD(const string &sender_addr, unsigned int, uint64_t, int block, unsigned int property_desired, uint64_t amount_desired, const uint256 &txid, unsigned int idx);
295 int MetaDEx_CANCEL_AT_PRICE(const uint256, unsigned int, const string &, unsigned int, uint64_t, unsigned int, uint64_t);
296 int MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256, unsigned int, const string &, unsigned int, unsigned int);
297 int MetaDEx_CANCEL_EVERYTHING(const uint256, unsigned int, const string &);
298 md_PricesMap *get_Prices(unsigned int prop);
299 md_Set *get_Indexes(md_PricesMap *p, XDOUBLE price);
300 
301 void MetaDEx_debug_print(bool bShowPriceLevel = false, bool bDisplay = false);
302 }
303 
304 #endif // #ifndef _MASTERCOIN_DEX
305 
const string & getAddr() const
const int msc_debug_dex
Definition: mastercore.cpp:109
uint64_t offer_amount_original
uint64_t amount_forsale
int DEx_offerCreate(string seller_addr, unsigned int, uint64_t nValue, int block, uint64_t amount_desired, uint64_t fee, unsigned char btl, const uint256 &txid, uint64_t *nAmended=NULL)
void saveOffer(ofstream &file, SHA256_CTX *shaCtx) const
unsigned char getAction() const
void saveAccept(ofstream &file, SHA256_CTX *shaCtx, string const &addr, string const &buyer) const
bool DEx_offerExists(const string &seller_addr, unsigned int)
int64_t getAmountDesired() const
void print()
md_PricesMap * get_Prices(unsigned int prop)
CMPOffer * DEx_getOffer(const string &seller_addr, unsigned int)
int MetaDEx_CANCEL_AT_PRICE(const uint256, unsigned int, const string &, unsigned int, uint64_t, unsigned int, uint64_t)
uint64_t accept_amount_original
AcceptMap my_accepts
Definition: mastercore.cpp:360
uint64_t getBlockTime() const
void setHash(uint256 hash)
cpp_dec_float_100 XDOUBLE
unsigned char blocktimelimit
unsigned int property
void setAmountDesired(int64_t ad, const string &label="")
uint64_t getOfferAmountOriginal() const
uint64_t accept_amount_remaining
void Set(uint64_t d, uint64_t fee, unsigned char btl, unsigned char suba)
uint256 txid
XDOUBLE effectivePrice() const
int MetaDEx_ADD(const string &sender_addr, unsigned int, uint64_t, int block, unsigned int property_desired, uint64_t amount_desired, const uint256 &txid, unsigned int idx)
std::map< string, CMPOffer > OfferMap
uint64_t getBTCDesiredOriginal()
CMPAccept * DEx_getAccept(const string &seller_addr, unsigned int, const string &buyer_addr)
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:48
unsigned char getSubaction() const
unsigned char getBlockTimeLimit()
std::pair< uint64_t, uint64_t > MetaDExTypePrice
uint64_t getAmountForSale() const
int MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256, unsigned int, const string &, unsigned int, unsigned int)
uint64_t getOfferAmountOriginal()
unsigned int eraseExpiredAccepts(int blockNow)
uint64_t getAcceptAmountRemaining() const
int offerBlock
int DEx_payment(uint256 txid, unsigned int vout, string seller, string buyer, uint64_t BTC_paid, int blockNow, uint64_t *nAmended=NULL)
OfferMap my_offers
Definition: mastercore.cpp:359
int64_t amount_desired
bool operator()(const CMPMetaDEx &lhs, const CMPMetaDEx &rhs) const
uint256 getHash() const
std::string ToString() const
uint64_t getBTCDesiredOriginal() const
uint64_t getMinFee() const
bool reduceAcceptAmountRemaining_andIsZero(const uint64_t really_purchased)
#define OMNI_PROPERTY_BTC
Definition: mastercore.h:129
md_PropertiesMap metadex
unsigned int property
uint256 txid
void MetaDEx_debug_print(bool bShowPriceLevel=false, bool bDisplay=false)
int MetaDEx_CANCEL_EVERYTHING(const uint256, unsigned int, const string &)
std::map< XDOUBLE, md_Set > md_PricesMap
int DEx_acceptCreate(const string &buyer, const string &seller, int, uint64_t nValue, int block, uint64_t fee_paid, uint64_t *nAmended=NULL)
unsigned int getProperty() const
int getBlock() const
int DEx_offerUpdate(const string &seller_addr, unsigned int, uint64_t nValue, int block, uint64_t desired, uint64_t fee, unsigned char btl, const uint256 &txid, uint64_t *nAmended=NULL)
unsigned int getProperty() const
std::string GetHex() const
Definition: uint256.h:297
void setAmountForSale(int64_t ao, const string &label="")
int DEx_offerDestroy(const string &seller_addr, unsigned int)
unsigned int idx
unsigned int desired_property
256-bit unsigned integer
Definition: uint256.h:531
static const int64_t COIN
Definition: util.h:38
std::set< CMPMetaDEx, MetaDEx_compare > md_Set
int getAcceptBlock()
void saveOffer(ofstream &file, SHA256_CTX *shaCtx, string const &addr) const
uint64_t still_left_forsale
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:688
uint256 getHash() const
unsigned char getBlockTimeLimit() const
unsigned char blocktimelimit
std::string ToString() const
Definition: uint256.h:340
unsigned int getProperty() const
int DEx_acceptDestroy(const string &buyer, const string &seller, int, bool bForceErase=false)
md_Set * get_Indexes(md_PricesMap *p, XDOUBLE price)
uint64_t BTC_desired_original
unsigned int property
unsigned char subaction
void Set(const string &, int, unsigned int, uint64_t, unsigned int, uint64_t, const uint256 &, unsigned int, unsigned char)
uint64_t min_fee
uint256 offer_txid
unsigned char subaction
int64_t GetBlockTime() const
Definition: main.h:810
unsigned int getIdx() const
void format(FormatIterator &fmtIter)
Definition: tinyformat.h:869
uint64_t BTC_desired_original
CMPOffer(int b, uint64_t a, unsigned int cu, uint64_t d, uint64_t fee, unsigned char btl, const uint256 &tx)
CMPAccept(uint64_t a, int b, unsigned char blt, unsigned int c, uint64_t o, uint64_t btc, const uint256 &txid)
std::map< unsigned int, md_PricesMap > md_PropertiesMap
std::map< string, CMPAccept > AcceptMap
uint256 getHash() const
uint64_t offer_amount_original
unsigned int getDesProperty() const
CMPAccept(uint64_t origA, uint64_t remA, int b, unsigned char blt, unsigned int c, uint64_t o, uint64_t btc, const uint256 &txid)