Skip to content

Commit ea65637

Browse files
X_Trade : make code more readable - no functional change
1 parent 3cbf3c3 commit ea65637

File tree

1 file changed

+134
-180
lines changed

1 file changed

+134
-180
lines changed

src/mastercore_dex.cpp

Lines changed: 134 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -113,190 +113,144 @@ const bool bOK = (left == right);
113113
// RETURN:
114114
static MatchReturnType x_Trade(CMPMetaDEx *newo)
115115
{
116-
const CMPMetaDEx *p_older = NULL;
117-
md_PricesMap *prices = NULL;
118-
const unsigned int prop = newo->getProperty();
119-
const unsigned int desprop = newo->getDesProperty();
120-
MatchReturnType NewReturn = NOTHING;
121-
bool bBuyerSatisfied = false;
122-
const XDOUBLE buyersprice = newo->effectivePrice();
123-
const XDOUBLE desprice = (1/buyersprice); // inverse, to be matched against that of the existing older order
124-
125-
if (msc_debug_metadex1)
126-
{
116+
const CMPMetaDEx *p_older = NULL;
117+
md_PricesMap *prices = NULL;
118+
const unsigned int prop = newo->getProperty();
119+
const unsigned int desprop = newo->getDesProperty();
120+
MatchReturnType NewReturn = NOTHING;
121+
bool bBuyerSatisfied = false;
122+
const XDOUBLE buyersprice = newo->effectivePrice();
123+
const XDOUBLE desprice = (1/buyersprice); // inverse, to be matched against that of the existing older order
124+
125+
if (msc_debug_metadex1) {
127126
file_log("%s(%s: prop=%u, desprop=%u, desprice= %s);newo: %s\n",
128-
__FUNCTION__, newo->getAddr(), prop, desprop, desprice.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), newo->ToString());
129-
}
130-
131-
prices = get_Prices(desprop);
132-
133-
// nothing for the desired property exists in the market, sorry!
134-
if (!prices)
135-
{
136-
file_log("%s()=%u:%s NOT FOUND ON THE MARKET\n", __FUNCTION__, NewReturn, getTradeReturnType(NewReturn));
137-
return NewReturn;
138-
}
139-
140-
// within the desired property map (given one property) iterate over the items looking at prices
141-
for (md_PricesMap::iterator my_it = prices->begin(); my_it != prices->end(); ++my_it)
142-
{ // check all prices
143-
XDOUBLE sellers_price = (my_it->first);
144-
145-
if (msc_debug_metadex2) file_log("comparing prices: desprice %s needs to be GREATER THAN OR EQUAL TO %s\n",
146-
desprice.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed));
147-
148-
// Is the desired price check satisfied? The buyer's inverse price must be larger than that of the seller.
149-
if (desprice < sellers_price) continue;
150-
151-
md_Set *indexes = &(my_it->second);
152-
153-
// at good (single) price level and property iterate over offers looking at all parameters to find the match
154-
md_Set::iterator iitt;
155-
for (iitt = indexes->begin(); iitt != indexes->end();)
156-
{ // specific price, check all properties
157-
p_older = &(*iitt);
158-
159-
if (msc_debug_metadex1) file_log("Looking at existing: %s (its prop= %u, its des prop= %u) = %s\n",
160-
sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), p_older->getProperty(), p_older->getDesProperty(), p_older->ToString());
161-
162-
// is the desired property correct?
163-
if (p_older->getDesProperty() != prop)
164-
{
165-
++iitt;
166-
continue;
167-
}
168-
169-
if (msc_debug_metadex1) file_log("MATCH FOUND, Trade: %s = %s\n", sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), p_older->ToString());
170-
171-
// All Matched ! Trade now.
172-
// p_older is the old order pointer
173-
// newo is the new order pointer
174-
// the price in the older order is used
175-
const int64_t seller_amountForSale = p_older->getAmountForSale();
176-
const int64_t seller_amountWanted = p_older->getAmountDesired();
177-
const int64_t buyer_amountOffered = newo->getAmountForSale();
178-
179-
if (msc_debug_metadex1) file_log("$$ trading using price: %s; seller: forsale= %ld, wanted= %ld, buyer amount offered= %ld\n",
180-
sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), seller_amountForSale, seller_amountWanted, buyer_amountOffered);
181-
182-
if (msc_debug_metadex1) file_log("$$ old: %s\n", p_older->ToString());
183-
if (msc_debug_metadex1) file_log("$$ new: %s\n", newo->ToString());
184-
185-
int64_t seller_amountGot = seller_amountWanted;
186-
187-
if (buyer_amountOffered < seller_amountWanted)
188-
{
189-
seller_amountGot = buyer_amountOffered;
190-
}
191-
192-
const int64_t buyer_amountStillForSale = buyer_amountOffered - seller_amountGot;
193-
194-
///////////////////////////
195-
XDOUBLE x_buyer_got = (XDOUBLE) seller_amountGot / sellers_price;
196-
197-
x_buyer_got += (XDOUBLE) 0.5; // ROUND UP
198-
199-
std::string str_buyer_got = x_buyer_got.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
200-
std::string str_buyer_got_int_part = str_buyer_got.substr(0, str_buyer_got.find_first_of("."));
201-
const int64_t buyer_amountGot = boost::lexical_cast<int64_t>( str_buyer_got_int_part );
202-
203-
const int64_t seller_amountLeft = p_older->getAmountForSale() - buyer_amountGot;
204-
205-
if (msc_debug_metadex1) file_log("$$ buyer_got= %ld, seller_got= %ld, seller_left_for_sale= %ld, buyer_still_for_sale= %ld\n",
206-
buyer_amountGot, seller_amountGot, seller_amountLeft, buyer_amountStillForSale);
207-
208-
XDOUBLE seller_amount_stilldesired = (XDOUBLE) seller_amountLeft * sellers_price;
209-
210-
seller_amount_stilldesired += (XDOUBLE) 0.5; // ROUND UP
211-
212-
std::string str_amount_stilldesired = seller_amount_stilldesired.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
213-
std::string str_stilldesired_int_part = str_amount_stilldesired.substr(0, str_amount_stilldesired.find_first_of("."));
214-
215-
///////////////////////////
216-
CMPMetaDEx seller_replacement = *p_older;
217-
218-
seller_replacement.setAmountForSale(seller_amountLeft, "seller_replacement");
219-
seller_replacement.setAmountDesired(boost::lexical_cast<int64_t>( str_stilldesired_int_part ), "seller_replacement");
220-
221-
// transfer the payment property from buyer to seller
222-
// TODO: do something when failing here............
223-
// FIXME
224-
// ...
225-
if (update_tally_map(newo->getAddr(), newo->getProperty(), - seller_amountGot, BALANCE))
226-
{
227-
if (update_tally_map(p_older->getAddr(), p_older->getDesProperty(), seller_amountGot, BALANCE))
228-
{
229-
}
230-
}
231-
232-
// transfer the market (the one being sold) property from seller to buyer
233-
// TODO: do something when failing here............
234-
// FIXME
235-
// ...
236-
if (update_tally_map(p_older->getAddr(), p_older->getProperty(), - buyer_amountGot, METADEX_RESERVE))
237-
{
238-
update_tally_map(newo->getAddr(), newo->getDesProperty(), buyer_amountGot, BALANCE);
239-
}
240-
241-
NewReturn = TRADED;
242-
243-
XDOUBLE will_pay = (XDOUBLE) buyer_amountStillForSale * newo->effectivePrice();
244-
245-
will_pay += (XDOUBLE) 0.5; // ROUND UP
246-
247-
std::string str_will_pay = will_pay.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
248-
std::string str_will_pay_int_part = str_will_pay.substr(0, str_will_pay.find_first_of("."));
249-
250-
newo->setAmountForSale(buyer_amountStillForSale, "buyer");
251-
newo->setAmountDesired(boost::lexical_cast<int64_t>( str_will_pay_int_part ), "buyer");
252-
253-
if (0 < buyer_amountStillForSale)
254-
{
255-
NewReturn = TRADED_MOREINBUYER;
256-
257-
PriceCheck(getTradeReturnType(NewReturn), buyersprice, newo->effectivePrice());
258-
}
259-
else
260-
{
261-
bBuyerSatisfied = true;
262-
}
263-
264-
if (0 < seller_amountLeft) // done with all loops, update the seller, buyer is fully satisfied
265-
{
266-
NewReturn = TRADED_MOREINSELLER;
267-
bBuyerSatisfied = true;
268-
269-
PriceCheck(getTradeReturnType(NewReturn), p_older->effectivePrice(), seller_replacement.effectivePrice());
270-
}
271-
272-
if (msc_debug_metadex1) file_log("==== TRADED !!! %u=%s\n", NewReturn, getTradeReturnType(NewReturn));
273-
274-
// record the trade in MPTradeList
275-
t_tradelistdb->recordTrade(p_older->getHash(), newo->getHash(),
276-
p_older->getAddr(), newo->getAddr(), p_older->getDesProperty(), newo->getDesProperty(), seller_amountGot, buyer_amountGot, newo->getBlock());
127+
__FUNCTION__, newo->getAddr(), prop, desprop, desprice.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), newo->ToString());
128+
}
277129

278-
if (msc_debug_metadex1) file_log("++ erased old: %s\n", iitt->ToString());
279-
// erase the old seller element
280-
indexes->erase(iitt++);
130+
prices = get_Prices(desprop);
281131

282-
if (bBuyerSatisfied)
283-
{
284-
// insert the updated one in place of the old
285-
if (0 < seller_replacement.getAmountForSale())
286-
{
287-
file_log("++ inserting seller_replacement: %s\n", seller_replacement.ToString());
288-
indexes->insert(seller_replacement);
289-
}
290-
break;
291-
}
292-
} // specific price, check all properties
293-
294-
if (bBuyerSatisfied) break;
295-
} // check all prices
296-
297-
file_log("%s()=%u:%s\n", __FUNCTION__, NewReturn, getTradeReturnType(NewReturn));
132+
// nothing for the desired property exists in the market, sorry!
133+
if (!prices) {
134+
file_log("%s()=%u:%s NOT FOUND ON THE MARKET\n", __FUNCTION__, NewReturn, getTradeReturnType(NewReturn));
135+
return NewReturn;
136+
}
298137

299-
return NewReturn;
138+
// within the desired property map (given one property) iterate over the items looking at prices
139+
for (md_PricesMap::iterator my_it = prices->begin(); my_it != prices->end(); ++my_it) { // check all prices
140+
XDOUBLE sellers_price = (my_it->first);
141+
if (msc_debug_metadex2) file_log("comparing prices: desprice %s needs to be GREATER THAN OR EQUAL TO %s\n",
142+
desprice.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed));
143+
144+
// Is the desired price check satisfied? The buyer's inverse price must be larger than that of the seller.
145+
if (desprice < sellers_price) continue;
146+
md_Set *indexes = &(my_it->second);
147+
148+
// at good (single) price level and property iterate over offers looking at all parameters to find the match
149+
md_Set::iterator iitt;
150+
for (iitt = indexes->begin(); iitt != indexes->end();) { // specific price, check all properties
151+
p_older = &(*iitt);
152+
if (msc_debug_metadex1) file_log("Looking at existing: %s (its prop= %u, its des prop= %u) = %s\n",
153+
sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), p_older->getProperty(), p_older->getDesProperty(), p_older->ToString());
154+
155+
// is the desired property correct?
156+
if (p_older->getDesProperty() != prop) {
157+
++iitt;
158+
continue;
159+
}
160+
if (msc_debug_metadex1) file_log("MATCH FOUND, Trade: %s = %s\n", sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), p_older->ToString());
161+
162+
// All Matched ! Trade now.
163+
// p_older is the old order pointer
164+
// newo is the new order pointer
165+
// the price in the older order is used
166+
const int64_t seller_amountForSale = p_older->getAmountForSale();
167+
const int64_t seller_amountWanted = p_older->getAmountDesired();
168+
const int64_t buyer_amountOffered = newo->getAmountForSale();
169+
if (msc_debug_metadex1) file_log("$$ trading using price: %s; seller: forsale= %ld, wanted= %ld, buyer amount offered= %ld\n",
170+
sellers_price.str(DISPLAY_PRECISION_LEN, std::ios_base::fixed), seller_amountForSale, seller_amountWanted, buyer_amountOffered);
171+
if (msc_debug_metadex1) file_log("$$ old: %s\n", p_older->ToString());
172+
if (msc_debug_metadex1) file_log("$$ new: %s\n", newo->ToString());
173+
int64_t seller_amountGot = seller_amountWanted;
174+
if (buyer_amountOffered < seller_amountWanted) {
175+
seller_amountGot = buyer_amountOffered;
176+
}
177+
const int64_t buyer_amountStillForSale = buyer_amountOffered - seller_amountGot;
178+
179+
///////////////////////////
180+
XDOUBLE x_buyer_got = (XDOUBLE) seller_amountGot / sellers_price;
181+
x_buyer_got += (XDOUBLE) 0.5; // ROUND UP
182+
std::string str_buyer_got = x_buyer_got.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
183+
std::string str_buyer_got_int_part = str_buyer_got.substr(0, str_buyer_got.find_first_of("."));
184+
const int64_t buyer_amountGot = boost::lexical_cast<int64_t>( str_buyer_got_int_part );
185+
const int64_t seller_amountLeft = p_older->getAmountForSale() - buyer_amountGot;
186+
if (msc_debug_metadex1) file_log("$$ buyer_got= %ld, seller_got= %ld, seller_left_for_sale= %ld, buyer_still_for_sale= %ld\n",
187+
buyer_amountGot, seller_amountGot, seller_amountLeft, buyer_amountStillForSale);
188+
XDOUBLE seller_amount_stilldesired = (XDOUBLE) seller_amountLeft * sellers_price;
189+
seller_amount_stilldesired += (XDOUBLE) 0.5; // ROUND UP
190+
std::string str_amount_stilldesired = seller_amount_stilldesired.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
191+
std::string str_stilldesired_int_part = str_amount_stilldesired.substr(0, str_amount_stilldesired.find_first_of("."));
192+
193+
///////////////////////////
194+
CMPMetaDEx seller_replacement = *p_older;
195+
seller_replacement.setAmountForSale(seller_amountLeft, "seller_replacement");
196+
seller_replacement.setAmountDesired(boost::lexical_cast<int64_t>( str_stilldesired_int_part ), "seller_replacement");
197+
198+
// transfer the payment property from buyer to seller
199+
// TODO: do something when failing here............
200+
// FIXME
201+
// ...
202+
if (update_tally_map(newo->getAddr(), newo->getProperty(), - seller_amountGot, BALANCE)) {
203+
if (update_tally_map(p_older->getAddr(), p_older->getDesProperty(), seller_amountGot, BALANCE)) { } // ???
204+
}
205+
206+
// transfer the market (the one being sold) property from seller to buyer
207+
// TODO: do something when failing here............
208+
// FIXME
209+
// ...
210+
if (update_tally_map(p_older->getAddr(), p_older->getProperty(), - buyer_amountGot, METADEX_RESERVE)) {
211+
update_tally_map(newo->getAddr(), newo->getDesProperty(), buyer_amountGot, BALANCE);
212+
}
213+
NewReturn = TRADED;
214+
XDOUBLE will_pay = (XDOUBLE) buyer_amountStillForSale * newo->effectivePrice();
215+
will_pay += (XDOUBLE) 0.5; // ROUND UP
216+
std::string str_will_pay = will_pay.str(INTERNAL_PRECISION_LEN, std::ios_base::fixed);
217+
std::string str_will_pay_int_part = str_will_pay.substr(0, str_will_pay.find_first_of("."));
218+
newo->setAmountForSale(buyer_amountStillForSale, "buyer");
219+
newo->setAmountDesired(boost::lexical_cast<int64_t>( str_will_pay_int_part ), "buyer");
220+
if (0 < buyer_amountStillForSale) {
221+
NewReturn = TRADED_MOREINBUYER;
222+
PriceCheck(getTradeReturnType(NewReturn), buyersprice, newo->effectivePrice());
223+
} else {
224+
bBuyerSatisfied = true;
225+
}
226+
if (0 < seller_amountLeft) { // done with all loops, update the seller, buyer is fully satisfied
227+
NewReturn = TRADED_MOREINSELLER;
228+
bBuyerSatisfied = true;
229+
PriceCheck(getTradeReturnType(NewReturn), p_older->effectivePrice(), seller_replacement.effectivePrice());
230+
}
231+
if (msc_debug_metadex1) file_log("==== TRADED !!! %u=%s\n", NewReturn, getTradeReturnType(NewReturn));
232+
233+
// record the trade in MPTradeList
234+
t_tradelistdb->recordTrade(p_older->getHash(), newo->getHash(),
235+
p_older->getAddr(), newo->getAddr(), p_older->getDesProperty(), newo->getDesProperty(), seller_amountGot, buyer_amountGot, newo->getBlock());
236+
if (msc_debug_metadex1) file_log("++ erased old: %s\n", iitt->ToString());
237+
238+
// erase the old seller element
239+
indexes->erase(iitt++);
240+
if (bBuyerSatisfied) {
241+
242+
// insert the updated one in place of the old
243+
if (0 < seller_replacement.getAmountForSale()) {
244+
file_log("++ inserting seller_replacement: %s\n", seller_replacement.ToString());
245+
indexes->insert(seller_replacement);
246+
}
247+
break;
248+
}
249+
} // specific price, check all properties
250+
if (bBuyerSatisfied) break;
251+
} // check all prices
252+
file_log("%s()=%u:%s\n", __FUNCTION__, NewReturn, getTradeReturnType(NewReturn));
253+
return NewReturn;
300254
}
301255

302256
void mastercore::MetaDEx_debug_print(bool bShowPriceLevel, bool bDisplay)

0 commit comments

Comments
 (0)