Skip to content

Commit bcc43c2

Browse files
MetaDEx: Add MetaDEx_INSERT
1 parent 64fde48 commit bcc43c2

File tree

3 files changed

+61
-100
lines changed

3 files changed

+61
-100
lines changed

src/mastercore.cpp

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,55 +1685,17 @@ int input_mp_offers_string(const string &s)
16851685
blocktimelimit = atoi(vstr[i++]); // metadex: index of tx in block
16861686
txidStr = vstr[i++];
16871687

1688-
if (OMNI_PROPERTY_BTC == prop_desired)
1689-
{
1690-
const string combo = STR_SELLOFFER_ADDR_PROP_COMBO(sellerAddr);
1691-
CMPOffer newOffer(offerBlock, amountOriginal, prop, btcDesired, minFee, blocktimelimit, uint256(txidStr));
1692-
1693-
if (my_offers.insert(std::make_pair(combo, newOffer)).second)
1694-
{
1695-
return 0;
1696-
}
1697-
else
1698-
{
1699-
return -1;
1700-
}
1701-
}
1702-
else
1703-
{
1704-
assert(10 == vstr.size());
1705-
1706-
left_forsale = boost::lexical_cast<uint64_t>(vstr[i++]);
1707-
1708-
CMPMetaDEx new_mdex(sellerAddr, offerBlock, prop, amountOriginal, prop_desired,
1709-
btcDesired, uint256(txidStr), blocktimelimit, (unsigned char) minFee, left_forsale );
1710-
1711-
XDOUBLE neworder_price = (XDOUBLE)amountOriginal / (XDOUBLE)btcDesired;
1712-
1713-
if (0 >= neworder_price) return METADEX_ERROR -66;
1714-
1715-
md_PricesMap temp_prices, *p_prices = get_Prices(prop);
1716-
md_Set temp_indexes, *p_indexes = NULL;
1717-
1718-
std::pair<md_Set::iterator,bool> ret;
1719-
1720-
if (p_prices) p_indexes = get_Indexes(p_prices, neworder_price);
1721-
1722-
if (!p_indexes) p_indexes = &temp_indexes;
1723-
{
1724-
ret = p_indexes->insert(new_mdex);
1725-
1726-
if (false == ret.second) return -1;
1727-
}
1728-
1729-
if (!p_prices) p_prices = &temp_prices;
1730-
1731-
(*p_prices)[neworder_price] = *p_indexes;
1732-
1733-
metadex[prop] = *p_prices;
1688+
if (OMNI_PROPERTY_BTC == prop_desired) {
1689+
const string combo = STR_SELLOFFER_ADDR_PROP_COMBO(sellerAddr);
1690+
CMPOffer newOffer(offerBlock, amountOriginal, prop, btcDesired, minFee, blocktimelimit, uint256(txidStr));
1691+
if (my_offers.insert(std::make_pair(combo, newOffer)).second) { return 0; } else { return -1; }
1692+
} else {
1693+
assert(10 == vstr.size());
1694+
left_forsale = boost::lexical_cast<uint64_t>(vstr[i++]);
1695+
CMPMetaDEx new_mdex(sellerAddr, offerBlock, prop, amountOriginal, prop_desired,
1696+
btcDesired, uint256(txidStr), blocktimelimit, (unsigned char) minFee, left_forsale );
1697+
if (MetaDEx_INSERT(new_mdex)) { return 0; } else { return -1; }
17341698
}
1735-
1736-
return 0;
17371699
}
17381700

17391701
// seller-address, property, buyer-address, amount, fee, block

src/mastercore_mdex.cpp

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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
318353
int 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

src/mastercore_mdex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int MetaDEx_ADD(const std::string& sender_addr, uint32_t, int64_t, int block, ui
128128
int MetaDEx_CANCEL_AT_PRICE(const uint256&, uint32_t, const std::string&, uint32_t, int64_t, uint32_t, int64_t);
129129
int MetaDEx_CANCEL_ALL_FOR_PAIR(const uint256&, uint32_t, const std::string&, uint32_t, uint32_t);
130130
int MetaDEx_CANCEL_EVERYTHING(const uint256& txid, uint32_t block, const std::string& sender_addr, unsigned char ecosystem);
131-
131+
bool MetaDEx_INSERT(CMPMetaDEx objMetaDEx);
132132
void MetaDEx_debug_print(bool bShowPriceLevel = false, bool bDisplay = false);
133133
}
134134

0 commit comments

Comments
 (0)