@@ -314,79 +314,78 @@ bool MetaDEx_compare::operator()(const CMPMetaDEx &lhs, const CMPMetaDEx &rhs) c
314314 else return lhs.getBlock () < rhs.getBlock ();
315315}
316316
317+ bool mastercore::MetaDEx_INSERT (CMPMetaDEx objMetaDEx)
318+ {
319+ // Create an empty price map (to use in case price map for this property does not already exist)
320+ md_PricesMap temp_prices;
321+ // Attempt to obtain the price map for the property
322+ md_PricesMap *p_prices = get_Prices (objMetaDEx.getProperty ());
323+
324+ // Create an empty set of metadex objects (to use in case no set currently exists at this price)
325+ md_Set temp_indexes;
326+ md_Set *p_indexes = NULL ;
327+
328+ // Prepare for return code
329+ std::pair <md_Set::iterator, bool > ret;
330+
331+ // Attempt to obtain a set of metadex objects for this price from the price map
332+ if (p_prices) p_indexes = get_Indexes (p_prices, objMetaDEx.effectivePrice ());
333+ // See if the set was populated, if not no set exists at this price level, use the empty set that we created earlier
334+ if (!p_indexes) p_indexes = &temp_indexes;
335+
336+ // Attempt to insert the metadex object into the set
337+ ret = p_indexes->insert (objMetaDEx);
338+ if (false == ret.second ) return false ;
339+
340+ // If a prices map did not exist for this property, set p_prices to the temp empty price map
341+ if (!p_prices) p_prices = &temp_prices;
342+
343+ // Update the prices map with the new set at this price
344+ (*p_prices)[objMetaDEx.effectivePrice ()] = *p_indexes;
345+
346+ // Set the metadex map for the property to the updated (or new if it didn't exist) price map
347+ metadex[objMetaDEx.getProperty ()] = *p_prices;
348+
349+ return true ;
350+ }
351+
317352// pretty much directly linked to the ADD TX21 command off the wire
318353int mastercore::MetaDEx_ADD (const std::string& sender_addr, uint32_t prop, int64_t amount, int block, uint32_t property_desired, int64_t amount_desired, const uint256& txid, unsigned int idx)
319354{
320355 int rc = METADEX_ERROR -1 ;
321356
322- // MetaDEx implementation phase 1 check
357+ // Ensure that one side of the trade is MSC/TMSC ( phase 1 check)
323358 if ((prop != OMNI_PROPERTY_MSC) && (property_desired != OMNI_PROPERTY_MSC) &&
324- (prop != OMNI_PROPERTY_TMSC) && (property_desired != OMNI_PROPERTY_TMSC)) {
325- return METADEX_ERROR -800 ;
326- }
359+ (prop != OMNI_PROPERTY_TMSC) && (property_desired != OMNI_PROPERTY_TMSC)) return METADEX_ERROR -800 ;
327360
328- // store the data into the temp MetaDEx object here
361+ // Create a MetaDEx object from paremeters
329362 CMPMetaDEx new_mdex (sender_addr, block, prop, amount, property_desired, amount_desired, txid, idx, CMPTransaction::ADD);
330- XDOUBLE neworder_buyersprice = new_mdex.effectivePrice ();
331-
332363 if (msc_debug_metadex1) file_log (" %s(); buyer obj: %s\n " , __FUNCTION__, new_mdex.ToString ());
333364
334- // given the property and the price find the proper place for insertion
335-
336- if (0 >= neworder_buyersprice) {
337- // do not work with 0 prices
338- return METADEX_ERROR -66 ;
339- }
365+ // Ensure this is not a badly priced trade (for example due to zero amounts)
366+ if (0 >= new_mdex.effectivePrice ()) return METADEX_ERROR -66 ;
340367
368+ // Match against existing trades, remainder of the order will be put into the order book
341369 if (msc_debug_metadex3) MetaDEx_debug_print ();
342-
343- // TRADE, check matches, remainder of the order will be put into the order book
344370 x_Trade (&new_mdex);
345-
346371 if (msc_debug_metadex3) MetaDEx_debug_print ();
347372
348- // plain insert
349- if (0 < new_mdex.getAmountForSale ()) { // not added nor subtracted, insert as new or post-traded amounts
350- md_PricesMap temp_prices, *p_prices = get_Prices (prop);
351- md_Set temp_indexes, *p_indexes = NULL ;
352- std::pair<md_Set::iterator, bool > ret;
353-
354- if (p_prices) {
355- p_indexes = get_Indexes (p_prices, neworder_buyersprice);
356- }
357-
358- if (!p_indexes) p_indexes = &temp_indexes;
359-
360- ret = p_indexes->insert (new_mdex);
361-
362- if (false == ret.second ) {
373+ // Insert the remaining order into the MetaDEx maps
374+ if (0 < new_mdex.getAmountForSale ()) { // switch to getAmountRemaining() when ready
375+ if (!MetaDEx_INSERT (new_mdex)) {
363376 file_log (" %s() ERROR: ALREADY EXISTS, line %d, file: %s\n " , __FUNCTION__, __LINE__, __FILE__);
377+ return METADEX_ERROR -70 ;
364378 } else {
365- // TODO: think about failure scenarios
366- // FIXME
367- if (update_tally_map (sender_addr, prop, -new_mdex.getAmountForSale (), BALANCE)) // subtract from what's available
368- {
369- // TODO: think about failure scenarios
370- // FIXME
371- update_tally_map (sender_addr, prop, new_mdex.getAmountForSale (), METADEX_RESERVE); // put in reserve
379+ // TODO: think about failure scenarios // FIXME
380+ if (update_tally_map (sender_addr, prop, -new_mdex.getAmountForSale (), BALANCE)) { // move from balance to reserve
381+ update_tally_map (sender_addr, prop, new_mdex.getAmountForSale (), METADEX_RESERVE);
372382 }
373-
374- PriceCheck (" Insert" , neworder_buyersprice, new_mdex.effectivePrice ());
375-
376- if (msc_debug_metadex1) file_log (" ==== INSERTED: %s= %s\n " , xToString (neworder_buyersprice), new_mdex.ToString ());
383+ if (msc_debug_metadex1) file_log (" ==== INSERTED: %s= %s\n " , xToString (new_mdex.effectivePrice ()), new_mdex.ToString ());
384+ if (msc_debug_metadex3) MetaDEx_debug_print ();
377385 }
378-
379- if (!p_prices) p_prices = &temp_prices;
380-
381- (*p_prices)[neworder_buyersprice] = *p_indexes;
382-
383- metadex[prop] = *p_prices;
384386 }
385387
386388 rc = 0 ;
387-
388- if (msc_debug_metadex3) MetaDEx_debug_print ();
389-
390389 return rc;
391390}
392391
0 commit comments