From 4d72ae985a6d2e7a8d0ddae9b3733cc6ef92713e Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 15:10:28 +0500 Subject: [PATCH 01/87] added totals to PriceGetOrderbook --- src/cc/prices.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 2116f2e8e..3ac334109 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -62,6 +62,7 @@ typedef struct OneBetData { } onebetdata; typedef struct BetInfo { + uint256 txid; int64_t averageCostbasis, firstprice, lastprice, liquidationprice, equity; int64_t rektfee; int32_t lastheight; @@ -69,7 +70,7 @@ typedef struct BetInfo { bool isOpen, isRekt; uint256 tokenid; - std::vector parsed; + std::vector vecparsed; std::vector bets; CPubKey pk; @@ -1599,13 +1600,13 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) return -2; OneBetData bet1; - if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betinfo.pk, bet1.firstheight, bet1.positionsize, betinfo.leverage, betinfo.firstprice, betinfo.parsed, betinfo.tokenid) == 'B') + if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betinfo.pk, bet1.firstheight, bet1.positionsize, betinfo.leverage, betinfo.firstprice, betinfo.vecparsed, betinfo.tokenid) == 'B') { uint256 finaltxid; int32_t vini; int32_t finaltxheight; //, endheight; //std::vector bets; - + betinfo.txid = bettxid; if (CCgetspenttxid(finaltxid, vini, finaltxheight, bettxid, NVOUT_CCMARKER) == 0) betinfo.isOpen = false; @@ -1618,7 +1619,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) prices_enumaddedbets(batontxid, betinfo.bets, bettxid); - if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.parsed, betinfo.lastprice, betinfo.lastheight) < 0) { + if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.vecparsed, betinfo.lastprice, betinfo.lastheight) < 0) { return -4; } @@ -1921,7 +1922,7 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) result.push_back(Pair("rektheight", betinfo.lastheight)); } - std::string expr = prices_getsourceexpression(betinfo.parsed); + std::string expr = prices_getsourceexpression(betinfo.vecparsed); result.push_back(Pair("expression", expr)); result.push_back(Pair("reduced", prices_getreducedexpr(expr))); // result.push_back(Pair("batontxid", batontxid.GetHex())); @@ -2002,11 +2003,52 @@ UniValue PricesList(uint32_t filter, CPubKey mypk) } -void prices_addbookentry(uint256 txid) +static bool prices_addbookentry(uint256 txid, std::vector &book) { BetInfo betinfo; - //if( prices_getbetinfo(txid, betinfo) == 0 ) + if (prices_getbetinfo(txid, betinfo) == 0) { + book.push_back(betinfo); + return true; + } + return false; } +/* +static bool prices_isopposite(BetInfo p1, BetInfo p2) { + if (p1.vecparsed.size() <= 3 && p2.vecparsed.size() <= 3) { // simple synthetic exp + + uint16_t opcode1 = p1.vecparsed[0]; + uint16_t opcode2 = p2.vecparsed[0]; + + int32_t value1 = (opcode1 & (KOMODO_MAXPRICES - 1)); // index or weight + int32_t value2 = (opcode2 & (KOMODO_MAXPRICES - 1)); // index or weight + + if ( (opcode1 & KOMODO_PRICEMASK) && (opcode2 & KOMODO_PRICEMASK) ) { + char name1[65]; + char name2[65]; + if (komodo_pricename(name1, value1) && komodo_pricename(name2, value2)) { + + std::string upperquote1, bottomquote1, upperquote2, bottomquote2; + prices_splitpair(std::string(name1), upperquote1, bottomquote1); + prices_splitpair(std::string(name2), upperquote2, bottomquote2); + + if (upperquote == "BTC") + isTop = true; + else if (bottomquote == "BTC") + isTop = false; + else + continue; + + if (!bottomquote.empty()) { + + for (int j = i + 1; j < book.size(); j++) { + + } + } + } + } + } +} +*/ // walk through uxtos on the global address // calculate the balance: @@ -2016,16 +2058,50 @@ void prices_addbookentry(uint256 txid) UniValue PricesGetOrderbook() { UniValue result(UniValue::VARR); - std::vector > addressIndex, addressIndexCC; + std::vector book, opposits; + std::vector > addressIndex; struct CCcontract_info *cp, C; cp = CCinit(&C, EVAL_PRICES); + // add all bets: SetCCtxids(addressIndex, cp->normaladdr, false); // old normal marker for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) { if (it->first.txindex == NVOUT_NORMALMARKER) - prices_addbookentry(it->first.txhash); + prices_addbookentry(it->first.txhash, book); } + + + // calc total fund amount + int64_t totalfund = 0; + std::vector > addressCCunspents; + SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker + for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) + { + totalfund += it->second.satoshis; + } + + // extract out opposit bets: + int lastbtcpos = -1; + int lastleverage = 0; + for (int i = 0; i < book.size() - 1; i++) { + for (int j = 0; j < book.size(); j++) { + if (book[i].isOpen && book[j].isOpen) { + //if (prices_isopposite(book[i], book[j])) { + //} + } + } + } + + int64_t totalliabilities = 0; + for (int i = 0; i < book.size() - 1; i++) { + if (book[i].isOpen) { + totalliabilities += book[i].equity; + } + } + + result.push_back(Pair("TotalFund", ValueFromAmount(totalfund))); + result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalliabilities))); return result; } From 5a688c226de0021777d880e6286125406106fd6b Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 15:16:14 +0500 Subject: [PATCH 02/87] corr vobj --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 3ac334109..fb87427a8 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2057,7 +2057,7 @@ static bool prices_isopposite(BetInfo p1, BetInfo p2) { // - unbalanced positions UniValue PricesGetOrderbook() { - UniValue result(UniValue::VARR); + UniValue result(UniValue::VOBJ); std::vector book, opposits; std::vector > addressIndex; struct CCcontract_info *cp, C; From 2b97d588bed159f7938d2ec650a935c38040a7a8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 15:30:50 +0500 Subject: [PATCH 03/87] logging book vars --- src/cc/prices.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index fb87427a8..d193df54d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2097,6 +2097,9 @@ UniValue PricesGetOrderbook() int64_t totalliabilities = 0; for (int i = 0; i < book.size() - 1; i++) { if (book[i].isOpen) { + int64_t t = 0; + for (auto b : book[i].bets) t += b.positionsize; + std::cerr << "book[i].exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos" << t << " equity=" << book[i].equity << std::endl; totalliabilities += book[i].equity; } } From d2c343a56ddafbf2947bca1c29b530dc822075d2 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 15:41:38 +0500 Subject: [PATCH 04/87] corr tx list selection --- src/cc/prices.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index d193df54d..d4e7701cf 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1988,7 +1988,7 @@ UniValue PricesList(uint32_t filter, CPubKey mypk) SetCCtxids(addressIndex, cp->normaladdr, false); // old normal marker for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) { - if( it->first.txindex == NVOUT_NORMALMARKER ) + if( it->first.index == NVOUT_NORMALMARKER ) AddBetToList(it->first.txhash); } @@ -2068,7 +2068,7 @@ UniValue PricesGetOrderbook() SetCCtxids(addressIndex, cp->normaladdr, false); // old normal marker for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) { - if (it->first.txindex == NVOUT_NORMALMARKER) + if (it->first.index == NVOUT_NORMALMARKER) prices_addbookentry(it->first.txhash, book); } @@ -2095,11 +2095,11 @@ UniValue PricesGetOrderbook() } int64_t totalliabilities = 0; - for (int i = 0; i < book.size() - 1; i++) { + for (int i = 0; i < book.size(); i++) { if (book[i].isOpen) { int64_t t = 0; for (auto b : book[i].bets) t += b.positionsize; - std::cerr << "book[i].exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos" << t << " equity=" << book[i].equity << std::endl; + std::cerr << "book[i].exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; totalliabilities += book[i].equity; } } From 8bfb3ad4563059067795e35f9056a9b35f16450b Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 15:53:34 +0500 Subject: [PATCH 05/87] commented logging to speed up --- src/cc/prices.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index d4e7701cf..aebf6e476 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -882,7 +882,7 @@ int32_t prices_syntheticvec(std::vector &vec, std::vector return(-3); } depth -= need; - std::cerr << "prices_syntheticvec() opcode=" << opcode << " opstr=" << opstr << " need=" << need << " depth=" << depth << std::endl; + //std::cerr << "prices_syntheticvec() opcode=" << opcode << " opstr=" << opstr << " need=" << need << " depth=" << depth << std::endl; if ((opcode & KOMODO_PRICEMASK) != PRICES_WEIGHT) { // skip weight depth++; // increase operands count std::cerr << "depth++=" << depth << std::endl; @@ -931,14 +931,14 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t mpz_set_ui(mpzResult, 0); // clear result to test overflow (see below) - std::cerr << "prices_syntheticprice" << " i=" << i << " mpzTotalPrice=" << mpz_get_si(mpzTotalPrice) << " value=" << value << " depth=" << depth << " opcode&KOMODO_PRICEMASK=" << (opcode & KOMODO_PRICEMASK) < 0 ) + // std::cerr << "prices_syntheticprice top pricestack[depth-1=" << depth-1 << "]=" << pricestack[depth-1] << std::endl; + // else + // std::cerr << "prices_syntheticprice pricestack empty" << std::endl; } free(pricedata); @@ -1155,7 +1155,7 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t std::cerr << "prices_syntheticprice err=" << errcode << std::endl; return(errcode); } - std::cerr << "prices_syntheticprice priceIndex=" << priceIndex << " den=" << den << std::endl; + //std::cerr << "prices_syntheticprice priceIndex=" << priceIndex << " den=" << den << std::endl; return priceIndex; } @@ -1192,11 +1192,11 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t if (minmax) { // if we are within day window, set temp costbasis to max (or min) price value if (leverage > 0 && price > costbasis) { costbasis = price; // set temp costbasis - std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } else if (leverage < 0 && (costbasis == 0 || price < costbasis)) { costbasis = price; - std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } //else { //-> use the previous value // std::cerr << "prices_syntheticprofits() unchanged costbasis=" << costbasis << " price=" << price << " leverage=" << leverage << std::endl; @@ -1208,7 +1208,7 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t //costbasis = price; // use calculated minmax costbasis - std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; + //std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; } } @@ -1266,7 +1266,7 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t else profits = 0; - std::cerr << "prices_syntheticprofits() profits=" << profits << std::endl; + //std::cerr << "prices_syntheticprofits() profits=" << profits << std::endl; return 0; // (positionsize + addedbets + profits); } @@ -1366,7 +1366,7 @@ int64_t prices_enumaddedbets(uint256 &batontxid, std::vector &bets, added.positionsize = amount; added.firstheight = blockIdx.GetHeight(); bets.push_back(added); - std::cerr << "prices_batontxid() added amount=" << amount << std::endl; + //std::cerr << "prices_batontxid() added amount=" << amount << std::endl; } else { std::cerr << "prices_batontxid() cannot load or decode add bet tx, isLoaded=" << isLoaded << " funcId=" << (int)funcId << std::endl; @@ -1494,7 +1494,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v int32_t retcode = prices_syntheticprofits(bets[i].costbasis, bets[i].firstheight, height, leverage, vec, bets[i].positionsize, bets[i].profits, lastprice); if (retcode < 0) { - std::cerr << "prices_scanchain() prices_syntheticprofits returned -1, breaking" << std::endl; + std::cerr << "prices_scanchain() prices_syntheticprofits returned -1, finishing..." << std::endl; stop = true; break; } From b25371ef01fee71ec5f05a61b8abd9eb0e123fde Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 16:00:28 +0500 Subject: [PATCH 06/87] more logging commented out --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index aebf6e476..634a781c7 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1103,7 +1103,7 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t break; } - std::cerr << "prices_syntheticprice test mpzResult=" << mpz_get_si(mpzResult) << std::endl; + // std::cerr << "prices_syntheticprice test mpzResult=" << mpz_get_si(mpzResult) << std::endl; // check overflow: if (mpz_cmp_si(mpzResult, std::numeric_limits::max()) > 0) { errcode = -13; @@ -1155,7 +1155,7 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t std::cerr << "prices_syntheticprice err=" << errcode << std::endl; return(errcode); } - //std::cerr << "prices_syntheticprice priceIndex=" << priceIndex << " den=" << den << std::endl; +// std::cerr << "prices_syntheticprice priceIndex=totalprice/den=" << priceIndex << " den=" << den << std::endl; return priceIndex; } From 0730775cad04f445206e70ac8f6357371967482a Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 16:03:35 +0500 Subject: [PATCH 07/87] added totalfund logging --- src/cc/prices.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 634a781c7..36954001e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2079,6 +2079,7 @@ UniValue PricesGetOrderbook() SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) { + std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis; totalfund += it->second.satoshis; } From 3a563f72561fa96171e064d510ab7f7de89e1ddd Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 16:07:10 +0500 Subject: [PATCH 08/87] add endl --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 36954001e..b2853d142 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2079,7 +2079,7 @@ UniValue PricesGetOrderbook() SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) { - std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis; + std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis << std::endl; totalfund += it->second.satoshis; } From 95b5d42fbe8501b74fda2aeb41e68e0f7cf9f23d Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 16:10:52 +0500 Subject: [PATCH 09/87] added txid to log --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index b2853d142..4b52a364d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2100,7 +2100,7 @@ UniValue PricesGetOrderbook() if (book[i].isOpen) { int64_t t = 0; for (auto b : book[i].bets) t += b.positionsize; - std::cerr << "book[i].exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; + std::cerr << "book[i].=" << book[i].txid.GetHex() << " exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; totalliabilities += book[i].equity; } } From 55c334885126eded2a1fd4cda540c44ca679749f Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 16:25:07 +0500 Subject: [PATCH 10/87] added isopen to pricesinfo --- src/cc/prices.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 4b52a364d..401a8190c 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1922,6 +1922,8 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) result.push_back(Pair("rektheight", betinfo.lastheight)); } + result.push_back(Pair("open", betinfo.isOpen ? 1 : 0 )); + std::string expr = prices_getsourceexpression(betinfo.vecparsed); result.push_back(Pair("expression", expr)); result.push_back(Pair("reduced", prices_getreducedexpr(expr))); @@ -2100,7 +2102,7 @@ UniValue PricesGetOrderbook() if (book[i].isOpen) { int64_t t = 0; for (auto b : book[i].bets) t += b.positionsize; - std::cerr << "book[i].=" << book[i].txid.GetHex() << " exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; + std::cerr << "book[i].txid=" << book[i].txid.GetHex() << " exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; totalliabilities += book[i].equity; } } From 5db0a5c38131ae212f55e05aeb035c14abe522a0 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 22:02:21 +0500 Subject: [PATCH 11/87] added orderbooks to result --- src/cc/prices.cpp | 62 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 401a8190c..de1242d0a 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2014,7 +2014,7 @@ static bool prices_addbookentry(uint256 txid, std::vector &book) } return false; } -/* + static bool prices_isopposite(BetInfo p1, BetInfo p2) { if (p1.vecparsed.size() <= 3 && p2.vecparsed.size() <= 3) { // simple synthetic exp @@ -2033,24 +2033,15 @@ static bool prices_isopposite(BetInfo p1, BetInfo p2) { prices_splitpair(std::string(name1), upperquote1, bottomquote1); prices_splitpair(std::string(name2), upperquote2, bottomquote2); - if (upperquote == "BTC") - isTop = true; - else if (bottomquote == "BTC") - isTop = false; - else - continue; - - if (!bottomquote.empty()) { - - for (int j = i + 1; j < book.size(); j++) { - - } - } + if (upperquote1 == bottomquote2 && bottomquote1 == upperquote2 && (p1.leverage > 0 == p2.leverage > 0) || + upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && (p1.leverage > 0 != p2.leverage > 0)) + return true; } } } + return false; } -*/ + // walk through uxtos on the global address // calculate the balance: @@ -2085,18 +2076,49 @@ UniValue PricesGetOrderbook() totalfund += it->second.satoshis; } - // extract out opposit bets: - int lastbtcpos = -1; - int lastleverage = 0; + // extract out opposite bets: + std::map > bookmatched; for (int i = 0; i < book.size() - 1; i++) { for (int j = 0; j < book.size(); j++) { if (book[i].isOpen && book[j].isOpen) { - //if (prices_isopposite(book[i], book[j])) { - //} + if (prices_isopposite(book[i], book[j])) { + char name[65]; + komodo_pricename(name, (book[i].vecparsed[0] & (KOMODO_MAXPRICES - 1))); + std::string sname = name; + bookmatched[sname].push_back(book[i]); + bookmatched[sname].push_back(book[j]); + book.erase(book.begin() + j); + book.erase(book.begin() + i); + } } } } + UniValue resbook (UniValue::VARR); + for (int i = 0; i < book.size(); i++) { + UniValue entry(UniValue::VOBJ); + entry.push_back(Pair("expression", prices_getsourceexpression(book[i].vecparsed))); + entry.push_back(Pair("costbasis", book[i].averageCostbasis)); + entry.push_back(Pair("leverage", book[i].leverage)); + entry.push_back(Pair("equity", book[i].equity)); + resbook.push_back(entry); + } + result.push_back(Pair("unmatched", resbook)); + + for (auto m : bookmatched) { + UniValue resbook(UniValue::VARR); + for (int i = 0; i < m.second.size(); i++) { + UniValue entry(UniValue::VOBJ); + entry.push_back(Pair("expression", prices_getsourceexpression(m.second[i].vecparsed))); + entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); + entry.push_back(Pair("leverage", m.second[i].leverage)); + entry.push_back(Pair("equity", m.second[i].equity)); + resbook.push_back(entry); + } + result.push_back(Pair(m.first, resbook)); + } + + int64_t totalliabilities = 0; for (int i = 0; i < book.size(); i++) { if (book[i].isOpen) { From fececb0e6b1ecc479547163b7124cd9618c925d8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 22:58:54 +0500 Subject: [PATCH 12/87] corr isopposite corr put into map --- src/cc/prices.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index de1242d0a..3e1adfafb 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2024,17 +2024,20 @@ static bool prices_isopposite(BetInfo p1, BetInfo p2) { int32_t value1 = (opcode1 & (KOMODO_MAXPRICES - 1)); // index or weight int32_t value2 = (opcode2 & (KOMODO_MAXPRICES - 1)); // index or weight - if ( (opcode1 & KOMODO_PRICEMASK) && (opcode2 & KOMODO_PRICEMASK) ) { + if ( (opcode1 & KOMODO_PRICEMASK) == 0 && (opcode2 & KOMODO_PRICEMASK) == 0 ) { char name1[65]; char name2[65]; if (komodo_pricename(name1, value1) && komodo_pricename(name2, value2)) { + uint16_t opcode1 = p1.vecparsed[1]; + uint16_t opcode2 = p2.vecparsed[1]; + std::string upperquote1, bottomquote1, upperquote2, bottomquote2; prices_splitpair(std::string(name1), upperquote1, bottomquote1); prices_splitpair(std::string(name2), upperquote2, bottomquote2); - if (upperquote1 == bottomquote2 && bottomquote1 == upperquote2 && (p1.leverage > 0 == p2.leverage > 0) || - upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && (p1.leverage > 0 != p2.leverage > 0)) + if (/*upperquote1 == bottomquote2 && bottomquote1 == upperquote2 && (p1.leverage > 0 == p2.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV == (opcode2 & KOMODO_PRICEMASK) == PRICES_INV) ||*/ + upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && ((p1.leverage > 0) != (p2.leverage > 0) || ((opcode1 & KOMODO_PRICEMASK) == PRICES_INV) != ((opcode2 & KOMODO_PRICEMASK) == PRICES_INV)) ) return true; } } @@ -2078,20 +2081,22 @@ UniValue PricesGetOrderbook() // extract out opposite bets: std::map > bookmatched; - for (int i = 0; i < book.size() - 1; i++) { - for (int j = 0; j < book.size(); j++) { - if (book[i].isOpen && book[j].isOpen) { - if (prices_isopposite(book[i], book[j])) { - char name[65]; - komodo_pricename(name, (book[i].vecparsed[0] & (KOMODO_MAXPRICES - 1))); - std::string sname = name; - bookmatched[sname].push_back(book[i]); + while (book.size() > 0) { + char name[65]; + komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); + std::string sname = name; + bookmatched[sname].push_back(book[0]); + + for (int j = 1; j < book.size(); j++) { + if (book[0].isOpen && book[j].isOpen) { + if (prices_isopposite(book[0], book[j])) { + bookmatched[sname].push_back(book[j]); book.erase(book.begin() + j); - book.erase(book.begin() + i); } } } + book.erase(book.begin()); } UniValue resbook (UniValue::VARR); From 1330bde5c84382522b1222fa912198200d6ab3d7 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 23:13:59 +0500 Subject: [PATCH 13/87] corr store in map --- src/cc/prices.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 3e1adfafb..5d7125541 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2082,17 +2082,20 @@ UniValue PricesGetOrderbook() // extract out opposite bets: std::map > bookmatched; while (book.size() > 0) { - char name[65]; - komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); - std::string sname = name; + //char name[65]; + //komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); + std::string sname = prices_getsourceexpression(book[0].vecparsed); bookmatched[sname].push_back(book[0]); - for (int j = 1; j < book.size(); j++) { - if (book[0].isOpen && book[j].isOpen) { - if (prices_isopposite(book[0], book[j])) { - - bookmatched[sname].push_back(book[j]); - book.erase(book.begin() + j); + if (book[0].vecparsed.size() <= 3) { // only short expr matched: "BTC_USD,1" or "BTC_USD,!,1" + + for (int j = 1; j < book.size(); j++) { + if (book[0].isOpen && book[j].isOpen) { + if (prices_isopposite(book[0], book[j])) { + + bookmatched[sname].push_back(book[j]); + book.erase(book.begin() + j); + } } } } From 114def2ab7cf18bcffaafb92c0c102942d227f71 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 23:21:59 +0500 Subject: [PATCH 14/87] corr more put into map --- src/cc/prices.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 5d7125541..95658d6d5 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2082,12 +2082,14 @@ UniValue PricesGetOrderbook() // extract out opposite bets: std::map > bookmatched; while (book.size() > 0) { - //char name[65]; - //komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); std::string sname = prices_getsourceexpression(book[0].vecparsed); bookmatched[sname].push_back(book[0]); if (book[0].vecparsed.size() <= 3) { // only short expr matched: "BTC_USD,1" or "BTC_USD,!,1" + char name[65]; + komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); + std::string sname = name; + bookmatched[sname].push_back(book[0]); for (int j = 1; j < book.size(); j++) { if (book[0].isOpen && book[j].isOpen) { @@ -2099,6 +2101,11 @@ UniValue PricesGetOrderbook() } } } + else { + // store as is + std::string sname = prices_getsourceexpression(book[0].vecparsed); + bookmatched[sname].push_back(book[0]); + } book.erase(book.begin()); } From 9928edbb0df9e0f4a3ead2904f0348c09299ccb9 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 8 May 2019 23:28:52 +0500 Subject: [PATCH 15/87] extra stmt removed --- src/cc/prices.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 95658d6d5..81074e40d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2082,8 +2082,6 @@ UniValue PricesGetOrderbook() // extract out opposite bets: std::map > bookmatched; while (book.size() > 0) { - std::string sname = prices_getsourceexpression(book[0].vecparsed); - bookmatched[sname].push_back(book[0]); if (book[0].vecparsed.size() <= 3) { // only short expr matched: "BTC_USD,1" or "BTC_USD,!,1" char name[65]; From b2f170ca951c9c0a496ab2ec6d09cf56961ccd60 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 15:28:45 +0500 Subject: [PATCH 16/87] added more totals --- src/cc/prices.cpp | 137 ++++++++++++++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 47 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 81074e40d..55bccf686 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -11,7 +11,34 @@ * * * Removal or modification of this copyright notice is prohibited. * * * - ***************************************************************************** + *****************************************************************************/ + + /* + CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; + + 0.5% fee based on betamount, NOT leveraged betamount!! + 0.1% collected by price basis determinant + 0.2% collected by rekt tx + + PricesBet -> +/-leverage, amount, synthetic -> opreturn includes current price + funds are locked into 1of2 global CC address + for first day, long basis is MAX(correlated,smoothed), short is MIN() + reference price is the smoothed of the previous block + if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price + original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund + 0.5% of bet -> globalfund + + PricesStatus -> bettxid maxsamples returns initial params, cost basis, amount left, rekt:true/false, rektheight, initial synthetic price, current synthetic price, net gain + + PricesRekt -> bettxid height -> 0.1% to miner, rest to global CC + + PricesClose -> bettxid returns (synthetic value + amount) + + PricesList -> all bettxid -> list [bettxid, netgain] + + */ + +/* To create payments plan start a chain with the following ac_params: -ac_snapshot=1440 (or for test chain something smaller, if you like.) - this enables the payments airdrop cc to work. @@ -83,31 +110,11 @@ typedef struct BetInfo { } } BetInfo; -/* -CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; - - 0.5% fee based on betamount, NOT leveraged betamount!! - 0.1% collected by price basis determinant - 0.2% collected by rekt tx - - PricesBet -> +/-leverage, amount, synthetic -> opreturn includes current price - funds are locked into 1of2 global CC address - for first day, long basis is MAX(correlated,smoothed), short is MIN() - reference price is the smoothed of the previous block - if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price - original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund - 0.5% of bet -> globalfund - - PricesStatus -> bettxid maxsamples returns initial params, cost basis, amount left, rekt:true/false, rektheight, initial synthetic price, current synthetic price, net gain - - PricesRekt -> bettxid height -> 0.1% to miner, rest to global CC - - PricesClose -> bettxid returns (synthetic value + amount) - - PricesList -> all bettxid -> list [bettxid, netgain] - - -*/ +typedef struct MatchedBookTotal { + + int64_t diffLeveragedPosition; + +} MatchedBookTotal; int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t height, int16_t leverage, std::vector vec, int64_t positionsize, int64_t &profits, int64_t &outprice); @@ -2081,33 +2088,64 @@ UniValue PricesGetOrderbook() // extract out opposite bets: std::map > bookmatched; + int64_t totalBets = 0; + int64_t totalRekt = 0; + int64_t totalEquity = 0; while (book.size() > 0) { - if (book[0].vecparsed.size() <= 3) { // only short expr matched: "BTC_USD,1" or "BTC_USD,!,1" - char name[65]; - komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); - std::string sname = name; - bookmatched[sname].push_back(book[0]); + int64_t betspos = 0; + for (auto bet : book[0].bets) betspos += bet.positionsize; + + if (!book[0].isRekt) { + + totalBets += betspos; + totalEquity += book[0].equity; - for (int j = 1; j < book.size(); j++) { - if (book[0].isOpen && book[j].isOpen) { - if (prices_isopposite(book[0], book[j])) { + if (book[0].vecparsed.size() <= 3) { // only short expr check for match: "BTC_USD,1" or "BTC_USD,!,1" + char name[65]; + komodo_pricename(name, (book[0].vecparsed[0] & (KOMODO_MAXPRICES - 1))); + std::string sname = name; + bookmatched[sname].push_back(book[0]); - bookmatched[sname].push_back(book[j]); - book.erase(book.begin() + j); + for (int j = 1; j < book.size(); j++) { + if (book[0].isOpen && book[j].isOpen) { + if (prices_isopposite(book[0], book[j])) { + + bookmatched[sname].push_back(book[j]); + book.erase(book.begin() + j); + } } } } + else { + // store as is + std::string sname = prices_getsourceexpression(book[0].vecparsed); + bookmatched[sname].push_back(book[0]); + } } - else { - // store as is - std::string sname = prices_getsourceexpression(book[0].vecparsed); - bookmatched[sname].push_back(book[0]); + else { + totalRekt += betspos; } book.erase(book.begin()); } - UniValue resbook (UniValue::VARR); + // calculate cancelling amount + std::map matchedTotals; + for (auto m : bookmatched) { + int64_t totalLeveragedPositionUp = 0; + int64_t totalLeveragedPositionDown = 0; + int64_t totalLeverageUp = 0; + int64_t totalLeverageDown = 0; + + + for (int i = 0; i < m.second.size(); i++) { + + } + matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp / totalLeverageUp - totalLeveragedPositionDown / totalLeverageDown; + } + + + /*UniValue resbook (UniValue::VARR); for (int i = 0; i < book.size(); i++) { UniValue entry(UniValue::VOBJ); entry.push_back(Pair("expression", prices_getsourceexpression(book[i].vecparsed))); @@ -2116,7 +2154,7 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("equity", book[i].equity)); resbook.push_back(entry); } - result.push_back(Pair("unmatched", resbook)); + result.push_back(Pair("unmatched", resbook)); */ for (auto m : bookmatched) { UniValue resbook(UniValue::VARR); @@ -2131,18 +2169,23 @@ UniValue PricesGetOrderbook() result.push_back(Pair(m.first, resbook)); } - - int64_t totalliabilities = 0; + int64_t totalLiabilities = 0; + /* empty for (int i = 0; i < book.size(); i++) { if (book[i].isOpen) { int64_t t = 0; for (auto b : book[i].bets) t += b.positionsize; std::cerr << "book[i].txid=" << book[i].txid.GetHex() << " exp=" << prices_getsourceexpression(book[i].vecparsed) << " totalpos=" << t << " equity=" << book[i].equity << std::endl; - totalliabilities += book[i].equity; + totalLiabilities += book[i].equity; } - } + } */ result.push_back(Pair("TotalFund", ValueFromAmount(totalfund))); - result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalliabilities))); + result.push_back(Pair("TotalRekt", ValueFromAmount(totalRekt))); + result.push_back(Pair("TotalBets", ValueFromAmount(totalBets))); + result.push_back(Pair("TotalEquity", ValueFromAmount(totalEquity))); + + +// result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalLiabilities))); return result; } From b8b80c934107334a83a5909c14f0d9fa71d94282 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 16:12:33 +0500 Subject: [PATCH 17/87] corr calc --- src/cc/prices.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 55bccf686..dc4f09127 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -889,10 +889,10 @@ int32_t prices_syntheticvec(std::vector &vec, std::vector return(-3); } depth -= need; - //std::cerr << "prices_syntheticvec() opcode=" << opcode << " opstr=" << opstr << " need=" << need << " depth=" << depth << std::endl; + ///std::cerr << "prices_syntheticvec() opcode=" << opcode << " opstr=" << opstr << " need=" << need << " depth=" << depth << std::endl; if ((opcode & KOMODO_PRICEMASK) != PRICES_WEIGHT) { // skip weight depth++; // increase operands count - std::cerr << "depth++=" << depth << std::endl; + ///std::cerr << "depth++=" << depth << std::endl; } if (depth > 3) { std::cerr << "prices_syntheticvec() too many operands, last=" << opstr << std::endl; @@ -1620,8 +1620,9 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) else betinfo.isOpen = true; - //bet1.amount = betinfo.positionsize; - //bet1.firstheight = firstheight; + // override with real amount (TODO: check this) + bet1.positionsize = bettx.vout[2].nValue; + betinfo.bets.push_back(bet1); prices_enumaddedbets(batontxid, betinfo.bets, bettxid); From 0ca3bbdb12c5c1ea70b4e2f140b73a76ce0639df Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 16:16:28 +0500 Subject: [PATCH 18/87] corr totals calc --- src/cc/prices.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index dc4f09127..763e3b038 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1883,8 +1883,6 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) } - - // pricesinfo rpc impl UniValue PricesInfo(uint256 bettxid, int32_t refheight) { @@ -2125,7 +2123,7 @@ UniValue PricesGetOrderbook() } } else { - totalRekt += betspos; + totalRekt += (betspos - book[0].rektfee); } book.erase(book.begin()); } From 6a51c67aeb21f26f8b7d3ae98882e0d0cfbdedfb Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 17:08:10 +0500 Subject: [PATCH 19/87] added lev position calc --- src/cc/prices.cpp | 59 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 763e3b038..e44e30cb7 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -72,6 +72,7 @@ GetKomodoEarlytxidScriptPub is on line #2080 of komodo_bitcoind.h #include "CCassets.h" #include "CCPrices.h" +#include #include #define IS_CHARINSTR(c, str) (std::string(str).find((char)(c)) != std::string::npos) @@ -1142,8 +1143,13 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t mpz_clear(mpzTotalPrice); mpz_clear(mpzPriceValue); - if (errcode != 0) + if (errcode != 0) std::cerr << "prices_syntheticprice errcode in switch=" << errcode << std::endl; + + if( errcode = -1 ) { + std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; + return errcode; + } if (errcode == -13) { std::cerr << "prices_syntheticprice overflow in price" << std::endl; @@ -2021,6 +2027,40 @@ static bool prices_addbookentry(uint256 txid, std::vector &book) return false; } + +static bool prices_ispositionup(BetInfo p) { + if (p.vecparsed.size() <= 3) { + uint16_t opcode = p.vecparsed[0]; + + int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // index or weight + + if ((opcode & KOMODO_PRICEMASK)) { + char name[65]; + if (komodo_pricename(name, value)) { + std::string upperquote, bottomquote; + prices_splitpair(std::string(name), upperquote, bottomquote); + + if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc + if (upperquote == "BTC" && (p.leverage > 0 || (opcode & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "BTC" && (p.leverage < 0 || (opcode & KOMODO_PRICEMASK) == PRICES_INV)) + return true; + else + return false; + } + + if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd + if (upperquote == "USD" && (p.leverage > 0 || (opcode & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "USD" && (p.leverage < 0 || (opcode & KOMODO_PRICEMASK) == PRICES_INV)) + return true; + else + return false; + } + } + } + } + return false; +} + static bool prices_isopposite(BetInfo p1, BetInfo p2) { if (p1.vecparsed.size() <= 3 && p2.vecparsed.size() <= 3) { // simple synthetic exp @@ -2133,14 +2173,16 @@ UniValue PricesGetOrderbook() for (auto m : bookmatched) { int64_t totalLeveragedPositionUp = 0; int64_t totalLeveragedPositionDown = 0; - int64_t totalLeverageUp = 0; - int64_t totalLeverageDown = 0; - for (int i = 0; i < m.second.size(); i++) { - + int64_t betspos = 0; + for (auto bet : m.second[i].bets) betspos += bet.positionsize; + if (prices_ispositionup(m.second[i])) + totalLeveragedPositionUp += betspos * abs(m.second[i].leverage); + else + totalLeveragedPositionDown += betspos * abs(m.second[i].leverage); } - matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp / totalLeverageUp - totalLeveragedPositionDown / totalLeverageDown; + matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp - totalLeveragedPositionDown; } @@ -2156,6 +2198,7 @@ UniValue PricesGetOrderbook() result.push_back(Pair("unmatched", resbook)); */ for (auto m : bookmatched) { + UniValue mathedBookHeader(UniValue::VOBJ); UniValue resbook(UniValue::VARR); for (int i = 0; i < m.second.size(); i++) { UniValue entry(UniValue::VOBJ); @@ -2165,7 +2208,9 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("equity", m.second[i].equity)); resbook.push_back(entry); } - result.push_back(Pair(m.first, resbook)); + mathedBookHeader.push_back(Pair("positions", resbook)); + mathedBookHeader.push_back(Pair("DiffLeveragedPosition", matchedTotals[m.first].diffLeveragedPosition)); + result.push_back(Pair(m.first, mathedBookHeader)); } int64_t totalLiabilities = 0; From cd58050445f584e7d8ffa40c007a4a96383a6e78 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 17:46:37 +0500 Subject: [PATCH 20/87] changed back bet.positionsize calc --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index e44e30cb7..a30f50994 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1627,7 +1627,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) betinfo.isOpen = true; // override with real amount (TODO: check this) - bet1.positionsize = bettx.vout[2].nValue; + //bet1.positionsize = bettx.vout[2].nValue; betinfo.bets.push_back(bet1); From 4d9de1ef923efb7e24716756ad401b274a37e088 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 18:05:53 +0500 Subject: [PATCH 21/87] returned back err check --- src/cc/prices.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index a30f50994..11ad87353 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1146,10 +1146,10 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t if (errcode != 0) std::cerr << "prices_syntheticprice errcode in switch=" << errcode << std::endl; - if( errcode = -1 ) { - std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; - return errcode; - } + //if( errcode = -1 ) { + // std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; + // return errcode; + //} if (errcode == -13) { std::cerr << "prices_syntheticprice overflow in price" << std::endl; From ab7d088b254e9a7c060a70e5cf4122ade19ec0f1 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 18:18:04 +0500 Subject: [PATCH 22/87] corr errcode check in syntheticprice --- src/cc/prices.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 11ad87353..d71a3363c 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1146,10 +1146,10 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t if (errcode != 0) std::cerr << "prices_syntheticprice errcode in switch=" << errcode << std::endl; - //if( errcode = -1 ) { - // std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; - // return errcode; - //} + if( errcode == -1 ) { + std::cerr << "prices_syntheticprice error getting price (could be end of chain)" << std::endl; + return errcode; + } if (errcode == -13) { std::cerr << "prices_syntheticprice overflow in price" << std::endl; From c8f56fb60973788c62510b358a0d0a44c3b83478 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 20:08:57 +0500 Subject: [PATCH 23/87] isup pos flag added --- src/cc/prices.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index d71a3363c..8380a2010 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -95,7 +95,7 @@ typedef struct BetInfo { int64_t rektfee; int32_t lastheight; int16_t leverage; - bool isOpen, isRekt; + bool isOpen, isRekt, isUp; uint256 tokenid; std::vector vecparsed; @@ -107,7 +107,7 @@ typedef struct BetInfo { lastheight = 0; leverage = 0; rektfee = 0; - isOpen = isRekt = false; + isOpen = isRekt = isUp = false; } } BetInfo; @@ -1633,6 +1633,15 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) prices_enumaddedbets(batontxid, betinfo.bets, bettxid); + if (!betinfo.isOpen) { + CTransaction finaltx; + uint256 hashBlock; + if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && prices_finalopretdecode) + { + } + } + + if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.vecparsed, betinfo.lastprice, betinfo.lastheight) < 0) { return -4; } @@ -1793,7 +1802,7 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); /// mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[2].nValue - myfee - txfee, pricespk)); // change - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, rektheight, mypk, betinfo.firstprice, 0, totalbets /*- positionsize*/, 0/*positionsize*/, betinfo.leverage)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, rektheight, mypk, betinfo.firstprice, betinfo.averageCostbasis, totalbets, 0, betinfo.leverage)); return(prices_rawtxresult(result, rawtx, 0)); } else @@ -1877,13 +1886,13 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) } mtx.vin.push_back(CTxIn(bettxid, NVOUT_CCMARKER, CScript())); // spend cc marker - if ((inputsum = AddPricesInputs(cp, mtx, destaddr, betinfo.equity + txfee, 64)) > betinfo.equity + txfee) + if ((inputsum = AddPricesInputs(cp, mtx, destaddr, betinfo.equity + txfee, 64)) > betinfo.equity + txfee) // TODO: why txfee from the fund? CCchange = (inputsum - betinfo.equity); mtx.vout.push_back(CTxOut(betinfo.equity, CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); if (CCchange >= txfee) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); // TODO: what should the opret param be: - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, nextheight - 1, mypk, betinfo.firstprice, 0, totalbets/*- betinfo.positionsize*/, 0/*betinfo.positionsize*/, betinfo.leverage)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, nextheight - 1, mypk, betinfo.firstprice, betinfo.averageCostbasis, totalbets, 0, betinfo.leverage)); return(prices_rawtxresult(result, rawtx, 0)); } @@ -2177,10 +2186,12 @@ UniValue PricesGetOrderbook() for (int i = 0; i < m.second.size(); i++) { int64_t betspos = 0; for (auto bet : m.second[i].bets) betspos += bet.positionsize; - if (prices_ispositionup(m.second[i])) + m.second[i].isUp = prices_ispositionup(m.second[i]); + if (m.second[i].isUp) totalLeveragedPositionUp += betspos * abs(m.second[i].leverage); else totalLeveragedPositionDown += betspos * abs(m.second[i].leverage); + } matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp - totalLeveragedPositionDown; } @@ -2206,6 +2217,7 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); entry.push_back(Pair("equity", m.second[i].equity)); + entry.push_back(Pair("isUpPosition", (m.second[i].isUp ? 1 : 0))); resbook.push_back(entry); } mathedBookHeader.push_back(Pair("positions", resbook)); From b297400d6c700bb09d9bbce772458ad96455396f Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 20:23:51 +0500 Subject: [PATCH 24/87] corr isup calc --- src/cc/prices.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 8380a2010..18a496a54 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2043,23 +2043,25 @@ static bool prices_ispositionup(BetInfo p) { int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // index or weight - if ((opcode & KOMODO_PRICEMASK)) { + if ((opcode & KOMODO_PRICEMASK) == 0) { char name[65]; if (komodo_pricename(name, value)) { std::string upperquote, bottomquote; prices_splitpair(std::string(name), upperquote, bottomquote); + + uint16_t opcode1 = p.vecparsed[1]; if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc - if (upperquote == "BTC" && (p.leverage > 0 || (opcode & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "BTC" && (p.leverage < 0 || (opcode & KOMODO_PRICEMASK) == PRICES_INV)) + if (upperquote == "BTC" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "BTC" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) return true; else return false; } if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd - if (upperquote == "USD" && (p.leverage > 0 || (opcode & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "USD" && (p.leverage < 0 || (opcode & KOMODO_PRICEMASK) == PRICES_INV)) + if (upperquote == "USD" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "USD" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) return true; else return false; From 909d3cbafbff82853196d896427ff036be7be4c7 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 20:44:17 +0500 Subject: [PATCH 25/87] logging added --- src/cc/prices.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 18a496a54..2430dd714 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2038,10 +2038,10 @@ static bool prices_addbookentry(uint256 txid, std::vector &book) static bool prices_ispositionup(BetInfo p) { - if (p.vecparsed.size() <= 3) { + if (p.vecparsed.size() > 1 && p.vecparsed.size() <= 3) { uint16_t opcode = p.vecparsed[0]; - int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // index or weight + int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // filter index or weight = opcode & (2048-1) if ((opcode & KOMODO_PRICEMASK) == 0) { char name[65]; @@ -2051,18 +2051,24 @@ static bool prices_ispositionup(BetInfo p) { uint16_t opcode1 = p.vecparsed[1]; + std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; + if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc if (upperquote == "BTC" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "BTC" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) + bottomquote == "BTC" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + std::cerr << "prices_ispositionup return true for BTC" << std::endl; return true; + } else return false; } if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd if (upperquote == "USD" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "USD" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) + bottomquote == "USD" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + std::cerr << "prices_ispositionup return true for USD" << std::endl; return true; + } else return false; } From 6489732013bdb45375c4cead6b06c6804b23d764 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 21:01:28 +0500 Subject: [PATCH 26/87] more loggin --- src/cc/prices.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 2430dd714..55dfe706f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2199,6 +2199,7 @@ UniValue PricesGetOrderbook() totalLeveragedPositionUp += betspos * abs(m.second[i].leverage); else totalLeveragedPositionDown += betspos * abs(m.second[i].leverage); + std::cerr << "PricesGetOrderbook 0 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; } matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp - totalLeveragedPositionDown; @@ -2216,6 +2217,7 @@ UniValue PricesGetOrderbook() } result.push_back(Pair("unmatched", resbook)); */ + // copy to rpc UniResult for (auto m : bookmatched) { UniValue mathedBookHeader(UniValue::VOBJ); UniValue resbook(UniValue::VARR); @@ -2225,6 +2227,7 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); entry.push_back(Pair("equity", m.second[i].equity)); + std::cerr << "PricesGetOrderbook 1 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; entry.push_back(Pair("isUpPosition", (m.second[i].isUp ? 1 : 0))); resbook.push_back(entry); } From 7cb1794d9148cc5b1b6fe77c9f4618a556fbf033 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 21:28:39 +0500 Subject: [PATCH 27/87] test --- src/cc/prices.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 55dfe706f..69ee376aa 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -95,13 +95,15 @@ typedef struct BetInfo { int64_t rektfee; int32_t lastheight; int16_t leverage; - bool isOpen, isRekt, isUp; + bool isOpen, isRekt; uint256 tokenid; std::vector vecparsed; std::vector bets; CPubKey pk; + bool isUp; + BetInfo() { averageCostbasis = firstprice = lastprice = liquidationprice = equity = 0; lastheight = 0; @@ -2181,6 +2183,7 @@ UniValue PricesGetOrderbook() } else { totalRekt += (betspos - book[0].rektfee); + //TODO: store rekt } book.erase(book.begin()); } From 60d585913618891174be0cdb3b8ea8746c040e18 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 9 May 2019 21:37:04 +0500 Subject: [PATCH 28/87] test --- src/cc/prices.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 69ee376aa..ffd9d04e2 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2190,7 +2190,7 @@ UniValue PricesGetOrderbook() // calculate cancelling amount std::map matchedTotals; - for (auto m : bookmatched) { + for (auto &m : bookmatched) { int64_t totalLeveragedPositionUp = 0; int64_t totalLeveragedPositionDown = 0; @@ -2221,11 +2221,12 @@ UniValue PricesGetOrderbook() result.push_back(Pair("unmatched", resbook)); */ // copy to rpc UniResult - for (auto m : bookmatched) { + for (auto &m : bookmatched) { UniValue mathedBookHeader(UniValue::VOBJ); UniValue resbook(UniValue::VARR); for (int i = 0; i < m.second.size(); i++) { UniValue entry(UniValue::VOBJ); + entry.push_back(Pair("isOpen", (m.second[i].isOpen ? 1 : 0 ))); entry.push_back(Pair("expression", prices_getsourceexpression(m.second[i].vecparsed))); entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); From 039a3128f2d3e3abc4e5955605d4475f50eb2342 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 01:50:33 +0500 Subject: [PATCH 29/87] added check amount acceptable --- src/cc/prices.cpp | 160 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 112 insertions(+), 48 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index ffd9d04e2..87fc17e4e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -119,7 +119,16 @@ typedef struct MatchedBookTotal { } MatchedBookTotal; +typedef struct TotalFund { + int64_t totalFund; + int64_t totalBets; + int64_t totalRekt; + int64_t totalEquity; + +} TotalFund; + int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t height, int16_t leverage, std::vector vec, int64_t positionsize, int64_t &profits, int64_t &outprice); +static bool prices_isacceptableamount(const std::vector &vecparsed, int64_t amount, int16_t leverage); // helpers: @@ -1423,6 +1432,13 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< result.push_back(Pair("error", "invalid synthetic")); return(result); } + + if (prices_isacceptableamount(vec, amount, leverage)) { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "too large amount and leverage")); + return(result); + } + if (AddNormalinputs(mtx, mypk, amount + 4 * txfee, 64) >= amount + 4 * txfee) { betamount = (amount * 199) / 200; @@ -1431,14 +1447,14 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, pricespk)); // vout1 cc marker (NVOUT_CCMARKER) mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout2 betamount mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); // vout3 normal marker NVOUT_NORMALMARKER - TODO: remove it as we have cc marker now, when move to the new chain - if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) + /*if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) { // Lock here, as in validation we cannot call lock in the function itself. // may not be needed as the validation call to update the global, is called in a LOCK already, and it can only update there and here. LOCK(cs_main); GetKomodoEarlytxidScriptPub(); } - mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); + mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); */ rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); return(prices_rawtxresult(result, rawtx, 0)); @@ -1635,13 +1651,13 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) prices_enumaddedbets(batontxid, betinfo.bets, bettxid); - if (!betinfo.isOpen) { + /*if (!betinfo.isOpen) { CTransaction finaltx; uint256 hashBlock; if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && prices_finalopretdecode) { } - } + }*/ if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.vecparsed, betinfo.lastprice, betinfo.lastheight) < 0) { @@ -2039,9 +2055,9 @@ static bool prices_addbookentry(uint256 txid, std::vector &book) } -static bool prices_ispositionup(BetInfo p) { - if (p.vecparsed.size() > 1 && p.vecparsed.size() <= 3) { - uint16_t opcode = p.vecparsed[0]; +static bool prices_ispositionup(const std::vector &vecparsed, int16_t leverage) { + if (vecparsed.size() > 1 && vecparsed.size() <= 3) { + uint16_t opcode = vecparsed[0]; int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // filter index or weight = opcode & (2048-1) @@ -2051,13 +2067,13 @@ static bool prices_ispositionup(BetInfo p) { std::string upperquote, bottomquote; prices_splitpair(std::string(name), upperquote, bottomquote); - uint16_t opcode1 = p.vecparsed[1]; + uint16_t opcode1 = vecparsed[1]; - std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; + //std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc - if (upperquote == "BTC" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "BTC" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + if (upperquote == "BTC" && (leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "BTC" && (leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { std::cerr << "prices_ispositionup return true for BTC" << std::endl; return true; } @@ -2066,8 +2082,8 @@ static bool prices_ispositionup(BetInfo p) { } if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd - if (upperquote == "USD" && (p.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "USD" && (p.leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + if (upperquote == "USD" && (leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || + bottomquote == "USD" && (leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { std::cerr << "prices_ispositionup return true for USD" << std::endl; return true; } @@ -2111,15 +2127,30 @@ static bool prices_isopposite(BetInfo p1, BetInfo p2) { } -// walk through uxtos on the global address -// calculate the balance: -// + rekt positions -// = opposite positions -// - unbalanced positions -UniValue PricesGetOrderbook() + +static std::string findMatchedBook(const std::vector &vecparsed, const std::map > & bookmatched) { + + if (vecparsed.size() > 1 && vecparsed.size() <= 3) { + uint16_t opcode = vecparsed[0]; + + int32_t value = (opcode & (KOMODO_MAXPRICES - 1)); // filter index or weight = opcode & (2048-1) + + if ((opcode & KOMODO_PRICEMASK) == 0) { + char name[65]; + if (komodo_pricename(name, value)) { + auto it = bookmatched.find(std::string(name)); + if (it != bookmatched.end()) + return it->first; + } + } + } + return std::string(""); +} + + +void prices_getorderbook(std::map > & bookmatched, std::map &matchedTotals, TotalFund &fundTotals) { - UniValue result(UniValue::VOBJ); - std::vector book, opposits; + std::vector book; std::vector > addressIndex; struct CCcontract_info *cp, C; @@ -2135,29 +2166,25 @@ UniValue PricesGetOrderbook() // calc total fund amount - int64_t totalfund = 0; + fundTotals.totalFund = 0; std::vector > addressCCunspents; SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) { std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis << std::endl; - totalfund += it->second.satoshis; + fundTotals.totalFund += it->second.satoshis; } - + // extract out opposite bets: - std::map > bookmatched; - int64_t totalBets = 0; - int64_t totalRekt = 0; - int64_t totalEquity = 0; while (book.size() > 0) { int64_t betspos = 0; for (auto bet : book[0].bets) betspos += bet.positionsize; - + if (!book[0].isRekt) { - - totalBets += betspos; - totalEquity += book[0].equity; + + fundTotals.totalBets += betspos; + fundTotals.totalEquity += book[0].equity; if (book[0].vecparsed.size() <= 3) { // only short expr check for match: "BTC_USD,1" or "BTC_USD,!,1" char name[65]; @@ -2182,14 +2209,13 @@ UniValue PricesGetOrderbook() } } else { - totalRekt += (betspos - book[0].rektfee); + fundTotals.totalRekt += (betspos - book[0].rektfee); //TODO: store rekt } book.erase(book.begin()); } // calculate cancelling amount - std::map matchedTotals; for (auto &m : bookmatched) { int64_t totalLeveragedPositionUp = 0; int64_t totalLeveragedPositionDown = 0; @@ -2197,17 +2223,55 @@ UniValue PricesGetOrderbook() for (int i = 0; i < m.second.size(); i++) { int64_t betspos = 0; for (auto bet : m.second[i].bets) betspos += bet.positionsize; - m.second[i].isUp = prices_ispositionup(m.second[i]); + m.second[i].isUp = prices_ispositionup(m.second[i].vecparsed, m.second[i].leverage); if (m.second[i].isUp) totalLeveragedPositionUp += betspos * abs(m.second[i].leverage); else totalLeveragedPositionDown += betspos * abs(m.second[i].leverage); - std::cerr << "PricesGetOrderbook 0 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; - + //std::cerr << "PricesGetOrderbook 0 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; + } matchedTotals[m.first].diffLeveragedPosition = totalLeveragedPositionUp - totalLeveragedPositionDown; } +} +static bool prices_isacceptableamount(const std::vector &vecparsed, int64_t amount, int16_t leverage) { + + std::map > matchedBook; + std::map matchedTotals; + TotalFund fundTotals; + + prices_getorderbook(matchedBook, matchedTotals, fundTotals); + std::string pricename = findMatchedBook(vecparsed, matchedBook); + if (!pricename.empty()) { + std::cerr << "IsAcceptableAmount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << std::endl; + // could fit into leveraged amount + if ((prices_ispositionup(vecparsed, leverage) || leverage > 0) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { + std::cerr << "IsAcceptableAmount() could fit into opposite negative lev amount" << std::endl; + return true; + } + if ((!prices_ispositionup(vecparsed, leverage) || leverage < 0) && -amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition >= 0) { + std::cerr << "IsAcceptableAmount() could fit into opposite positive lev amount" << std::endl; + return true; + } + } + return false; +} + + +// walk through uxtos on the global address +// calculate the balance: +// + rekt positions +// = opposite positions +// - unbalanced positions +UniValue PricesGetOrderbook() +{ + UniValue result(UniValue::VOBJ); + std::map > matchedBook; + std::map matchedTotals; + TotalFund fundTotals; + + prices_getorderbook(matchedBook, matchedTotals, fundTotals); /*UniValue resbook (UniValue::VARR); for (int i = 0; i < book.size(); i++) { @@ -2221,8 +2285,8 @@ UniValue PricesGetOrderbook() result.push_back(Pair("unmatched", resbook)); */ // copy to rpc UniResult - for (auto &m : bookmatched) { - UniValue mathedBookHeader(UniValue::VOBJ); + for (auto &m : matchedBook) { + UniValue resheader(UniValue::VOBJ); UniValue resbook(UniValue::VARR); for (int i = 0; i < m.second.size(); i++) { UniValue entry(UniValue::VOBJ); @@ -2231,16 +2295,16 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); entry.push_back(Pair("equity", m.second[i].equity)); - std::cerr << "PricesGetOrderbook 1 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; + //std::cerr << "PricesGetOrderbook 1 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; entry.push_back(Pair("isUpPosition", (m.second[i].isUp ? 1 : 0))); resbook.push_back(entry); } - mathedBookHeader.push_back(Pair("positions", resbook)); - mathedBookHeader.push_back(Pair("DiffLeveragedPosition", matchedTotals[m.first].diffLeveragedPosition)); - result.push_back(Pair(m.first, mathedBookHeader)); + resheader.push_back(Pair("positions", resbook)); + resheader.push_back(Pair("DiffLeveragedPosition", matchedTotals[m.first].diffLeveragedPosition)); + result.push_back(Pair(m.first, resheader)); } - int64_t totalLiabilities = 0; + //int64_t totalLiabilities = 0; /* empty for (int i = 0; i < book.size(); i++) { if (book[i].isOpen) { @@ -2251,10 +2315,10 @@ UniValue PricesGetOrderbook() } } */ - result.push_back(Pair("TotalFund", ValueFromAmount(totalfund))); - result.push_back(Pair("TotalRekt", ValueFromAmount(totalRekt))); - result.push_back(Pair("TotalBets", ValueFromAmount(totalBets))); - result.push_back(Pair("TotalEquity", ValueFromAmount(totalEquity))); + result.push_back(Pair("TotalFund", ValueFromAmount(fundTotals.totalFund))); + result.push_back(Pair("TotalRekt", ValueFromAmount(fundTotals.totalRekt))); + result.push_back(Pair("TotalBets", ValueFromAmount(fundTotals.totalBets))); + result.push_back(Pair("TotalEquity", ValueFromAmount(fundTotals.totalEquity))); // result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalLiabilities))); From e286938a2215c68d8ded3b3bd9f668982d7d23c2 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 01:59:45 +0500 Subject: [PATCH 30/87] corr check amount and lev in pricebet --- src/cc/prices.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 87fc17e4e..edc71867b 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1433,9 +1433,9 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< return(result); } - if (prices_isacceptableamount(vec, amount, leverage)) { + if (!prices_isacceptableamount(vec, amount, leverage)) { result.push_back(Pair("result", "error")); - result.push_back(Pair("error", "too large amount and leverage")); + result.push_back(Pair("error", "too big amount and leverage")); return(result); } @@ -2244,14 +2244,14 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in prices_getorderbook(matchedBook, matchedTotals, fundTotals); std::string pricename = findMatchedBook(vecparsed, matchedBook); if (!pricename.empty()) { - std::cerr << "IsAcceptableAmount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << std::endl; + std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << std::endl; // could fit into leveraged amount if ((prices_ispositionup(vecparsed, leverage) || leverage > 0) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { - std::cerr << "IsAcceptableAmount() could fit into opposite negative lev amount" << std::endl; + std::cerr << "prices_isacceptableamount() could fit into opposite negative lev amount" << std::endl; return true; } if ((!prices_ispositionup(vecparsed, leverage) || leverage < 0) && -amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition >= 0) { - std::cerr << "IsAcceptableAmount() could fit into opposite positive lev amount" << std::endl; + std::cerr << "prices_isacceptableamount() could fit into opposite positive lev amount" << std::endl; return true; } } From fa1a0875fc0272fef2fd42e1ba30c5cfe304ebc4 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 10:24:45 +0500 Subject: [PATCH 31/87] logging added --- src/cc/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index edc71867b..916528fc4 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2069,7 +2069,7 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t uint16_t opcode1 = vecparsed[1]; - //std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; + std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc if (upperquote == "BTC" && (leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || @@ -2246,11 +2246,11 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in if (!pricename.empty()) { std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << std::endl; // could fit into leveraged amount - if ((prices_ispositionup(vecparsed, leverage) || leverage > 0) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { + if (prices_ispositionup(vecparsed, leverage) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { std::cerr << "prices_isacceptableamount() could fit into opposite negative lev amount" << std::endl; return true; } - if ((!prices_ispositionup(vecparsed, leverage) || leverage < 0) && -amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition >= 0) { + if (!prices_ispositionup(vecparsed, leverage) && -amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition >= 0) { std::cerr << "prices_isacceptableamount() could fit into opposite positive lev amount" << std::endl; return true; } From 00d5328699be240d7883eab788046f193e6b5d2f Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 10:32:44 +0500 Subject: [PATCH 32/87] more logging --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 916528fc4..08cd20575 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2244,7 +2244,7 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in prices_getorderbook(matchedBook, matchedTotals, fundTotals); std::string pricename = findMatchedBook(vecparsed, matchedBook); if (!pricename.empty()) { - std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << std::endl; + std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << "expr=" << prices_getsourceexpression(vecparsed) << std::endl; // could fit into leveraged amount if (prices_ispositionup(vecparsed, leverage) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { std::cerr << "prices_isacceptableamount() could fit into opposite negative lev amount" << std::endl; From 8a910e5952382b26aaa86b8495ec881c0691a768 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 10:56:40 +0500 Subject: [PATCH 33/87] corr logic expr --- src/cc/prices.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 08cd20575..a902ef5e5 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2068,12 +2068,13 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t prices_splitpair(std::string(name), upperquote, bottomquote); uint16_t opcode1 = vecparsed[1]; + bool isInverted = ((opcode1 & KOMODO_PRICEMASK) == PRICES_INV); std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc - if (upperquote == "BTC" && (leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "BTC" && (leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + if (upperquote == "BTC" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || + bottomquote == "BTC" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { std::cerr << "prices_ispositionup return true for BTC" << std::endl; return true; } @@ -2082,8 +2083,8 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t } if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd - if (upperquote == "USD" && (leverage > 0 || (opcode1 & KOMODO_PRICEMASK) != PRICES_INV) || - bottomquote == "USD" && (leverage < 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV)) { + if (upperquote == "USD" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || + bottomquote == "USD" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { std::cerr << "prices_ispositionup return true for USD" << std::endl; return true; } From 31030492e5eb6d07222db9f631809932dac75a7b Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 12:04:36 +0500 Subject: [PATCH 34/87] added check amount for 10% --- src/cc/prices.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index a902ef5e5..edcd430f4 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2256,6 +2256,11 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in return true; } } + + // if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund + if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * 0.1) + return true; + return false; } From 771d5d8b84746dbcab5fd6b413e8c2b6161cc6b2 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 12:39:52 +0500 Subject: [PATCH 35/87] added > 5342 height --- src/cc/prices.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index edcd430f4..fb5bd363b 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1630,6 +1630,12 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) if (hashBlock.IsNull()) return -2; + + // TODO: forget old tx + CBlockIndex *bi = komodo_getblockindex(hashBlock); + if (bi && bi->GetHeight() > 5342) + return -5; + OneBetData bet1; if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betinfo.pk, bet1.firstheight, bet1.positionsize, betinfo.leverage, betinfo.firstprice, betinfo.vecparsed, betinfo.tokenid) == 'B') { @@ -2002,6 +2008,12 @@ UniValue PricesList(uint32_t filter, CPubKey mypk) if (GetTransaction(txid, vintx, hashBlock, false) != 0) { + + // TODO: forget old tx + CBlockIndex *bi = komodo_getblockindex(hashBlock); + if (bi && bi->GetHeight() > 5342) + return; + bool bAppend = false; if (vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout.back().scriptPubKey, pk, height, amount, leverage, firstprice, vec, tokenid) == 'B' && (mypk == CPubKey() || mypk == pk)) // if only mypubkey to list From bb2ade543c52564bf94d9585ee8431a456abe583 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 12:47:25 +0500 Subject: [PATCH 36/87] corr height --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index fb5bd363b..4682df726 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1633,7 +1633,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) // TODO: forget old tx CBlockIndex *bi = komodo_getblockindex(hashBlock); - if (bi && bi->GetHeight() > 5342) + if (bi && bi->GetHeight() < 5342) return -5; OneBetData bet1; @@ -2011,7 +2011,7 @@ UniValue PricesList(uint32_t filter, CPubKey mypk) // TODO: forget old tx CBlockIndex *bi = komodo_getblockindex(hashBlock); - if (bi && bi->GetHeight() > 5342) + if (bi && bi->GetHeight() < 5342) return; bool bAppend = false; From 8975bbf9c9de2e92183a827b8a52e0c02c37dc06 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 15:01:44 +0500 Subject: [PATCH 37/87] added check amount for addbet rpc --- src/cc/prices.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 4682df726..a1f13122f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1388,7 +1388,7 @@ int64_t prices_enumaddedbets(uint256 &batontxid, std::vector &bets, addedBetsTotal += amount; added.positionsize = amount; - added.firstheight = blockIdx.GetHeight(); + added.firstheight = blockIdx.GetHeight(); //TODO: check if this is correct (to get height from the block not from the opret) bets.push_back(added); //std::cerr << "prices_batontxid() added amount=" << amount << std::endl; } @@ -1469,11 +1469,14 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp, C; CTransaction bettx; - CPubKey pricespk, mypk; - //int64_t addedbets = 0, betamount, firstprice; - std::vector vec; - uint256 batontxid; + struct CCcontract_info *cp, C; + CTransaction bettx; + CPubKey pricespk, mypk, pk; + int64_t positionsize, betamount, firstprice; + int32_t firstheight; + std::vector vecparsed; + uint256 batontxid, tokenid, hashBlock; + int16_t leverage; std::string rawtx; //char myaddr[64]; @@ -1482,7 +1485,22 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) txfee = PRICES_TXFEE; mypk = pubkey2pk(Mypubkey()); pricespk = GetUnspendable(cp, 0); - //GetCCaddress(cp, myaddr, mypk); + + if (!myGetTransaction(bettxid, bettx, hashBlock) || + bettx.vout.size() <= 3 || + hashBlock.IsNull() || + prices_betopretdecode(bettx.vout.back().scriptPubKey, pk, firstheight, positionsize, leverage, firstprice, vecparsed, tokenid) != 'B') { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid bet tx")); + return(result); + } + + if (!prices_isacceptableamount(vecparsed, amount, leverage)) { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "too big amount and leverage")); + return(result); + } + if (AddNormalinputs(mtx, mypk, amount + 2*txfee, 64) >= amount + 2*txfee) { std::vector bets; @@ -2229,7 +2247,7 @@ void prices_getorderbook(std::map > & bookmatc } // calculate cancelling amount - for (auto &m : bookmatched) { + for (auto &m : bookmatched) { // note: use reference &m otherwise isUp will be changed only in a copy int64_t totalLeveragedPositionUp = 0; int64_t totalLeveragedPositionDown = 0; From a6194b8c04eeb954a3d21a93058e752bed9cdbf1 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 15:23:45 +0500 Subject: [PATCH 38/87] added logging --- src/cc/prices.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index a1f13122f..657b389de 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1476,7 +1476,7 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) int32_t firstheight; std::vector vecparsed; uint256 batontxid, tokenid, hashBlock; - int16_t leverage; + int16_t leverage = 0; std::string rawtx; //char myaddr[64]; @@ -2287,6 +2287,7 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in } } + std::cerr << "prices_isacceptableamount() amount=" << amount << " leverage=" << leverage << " fundTotals.totalFund=" << fundTotals.totalFund << " fundTotals.totalEquity=" << fundTotals.totalEquity << std::endl; // if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * 0.1) return true; From 239cc223e49b4ec600d881669c1b8602454c33b9 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 15:40:00 +0500 Subject: [PATCH 39/87] init uninited total vars --- src/cc/prices.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 657b389de..26c2c887e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -125,6 +125,10 @@ typedef struct TotalFund { int64_t totalRekt; int64_t totalEquity; + TotalFund() { + totalFund = totalBets = totalRekt = totalEquity = 0; + } + } TotalFund; int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t height, int16_t leverage, std::vector vec, int64_t positionsize, int64_t &profits, int64_t &outprice); @@ -2198,6 +2202,10 @@ void prices_getorderbook(std::map > & bookmatc // calc total fund amount fundTotals.totalFund = 0; + fundTotals.totalRekt = 0; + fundTotals.totalEquity = 0; + fundTotals.totalBets = 0; + std::vector > addressCCunspents; SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) @@ -2275,7 +2283,7 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in prices_getorderbook(matchedBook, matchedTotals, fundTotals); std::string pricename = findMatchedBook(vecparsed, matchedBook); if (!pricename.empty()) { - std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << "expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_isacceptableamount() found matched book=" << pricename << " diffLeveragedPosition=" << matchedTotals[pricename].diffLeveragedPosition << " expr=" << prices_getsourceexpression(vecparsed) << std::endl; // could fit into leveraged amount if (prices_ispositionup(vecparsed, leverage) && amount*abs(leverage) + matchedTotals[pricename].diffLeveragedPosition <= 0) { std::cerr << "prices_isacceptableamount() could fit into opposite negative lev amount" << std::endl; From adcb0315514845a01456e7be367c1f81fd3b3126 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 16:42:01 +0500 Subject: [PATCH 40/87] logic expr optimized logging added --- src/cc/prices.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 26c2c887e..fe80e9bdd 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2109,7 +2109,7 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc if (upperquote == "BTC" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "BTC" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup return true for BTC" << std::endl; + std::cerr << "prices_ispositionup return true for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return true; } else @@ -2119,7 +2119,7 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd if (upperquote == "USD" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "USD" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup return true for USD" << std::endl; + std::cerr << "prices_ispositionup return true for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return true; } else @@ -2128,6 +2128,7 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t } } } + std::cerr << "prices_ispositionup returns false for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return false; } @@ -2152,8 +2153,11 @@ static bool prices_isopposite(BetInfo p1, BetInfo p2) { prices_splitpair(std::string(name1), upperquote1, bottomquote1); prices_splitpair(std::string(name2), upperquote2, bottomquote2); + bool isInverted1 = ((opcode1 & KOMODO_PRICEMASK) != PRICES_INV); + bool isInverted2 = ((opcode2 & KOMODO_PRICEMASK) != PRICES_INV); + if (/*upperquote1 == bottomquote2 && bottomquote1 == upperquote2 && (p1.leverage > 0 == p2.leverage > 0 || (opcode1 & KOMODO_PRICEMASK) == PRICES_INV == (opcode2 & KOMODO_PRICEMASK) == PRICES_INV) ||*/ - upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && ((p1.leverage > 0) != (p2.leverage > 0) || ((opcode1 & KOMODO_PRICEMASK) == PRICES_INV) != ((opcode2 & KOMODO_PRICEMASK) == PRICES_INV)) ) + upperquote1 == upperquote2 && bottomquote1 == bottomquote2 && ((p1.leverage > 0) != (p2.leverage > 0) || isInverted1 != isInverted2) ) return true; } } @@ -2210,7 +2214,7 @@ void prices_getorderbook(std::map > & bookmatc SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker for (std::vector >::const_iterator it = addressCCunspents.begin(); it != addressCCunspents.end(); it++) { - std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis << std::endl; + //std::cerr << "totalfund calc txid=" << it->first.txhash.GetHex() << " nvout=" << it->first.index << " satoshis=" << it->second.satoshis << std::endl; fundTotals.totalFund += it->second.satoshis; } From e85a7a6c045ae70b47bbbd29e02b3336934f4606 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 18:50:45 +0500 Subject: [PATCH 41/87] corr final opret --- src/cc/prices.cpp | 71 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index fe80e9bdd..9f001b959 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -92,7 +92,7 @@ typedef struct OneBetData { typedef struct BetInfo { uint256 txid; int64_t averageCostbasis, firstprice, lastprice, liquidationprice, equity; - int64_t rektfee; + int64_t exitfee; int32_t lastheight; int16_t leverage; bool isOpen, isRekt; @@ -108,7 +108,7 @@ typedef struct BetInfo { averageCostbasis = firstprice = lastprice = liquidationprice = equity = 0; lastheight = 0; leverage = 0; - rektfee = 0; + exitfee = 0; isOpen = isRekt = isUp = false; } } BetInfo; @@ -201,18 +201,18 @@ uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKe return(0); } -CScript prices_finalopret(uint256 bettxid,int64_t profits,int32_t height,CPubKey mypk,int64_t firstprice,int64_t costbasis,int64_t addedbets,int64_t positionsize,int16_t leverage) +CScript prices_finalopret(bool isRekt, uint256 bettxid, CPubKey pk, int32_t lastheight, int64_t costbasis, int64_t lastprice, int64_t liquidationprice, int64_t equity, int64_t exitfee) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'F' << bettxid << profits << height << mypk << firstprice << costbasis << addedbets << positionsize << leverage); + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'F' : 'R') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee); return(opret); } -uint8_t prices_finalopretdecode(CScript scriptPubKey,uint256 &bettxid,int64_t &profits,int32_t &height,CPubKey &pk,int64_t &firstprice,int64_t &costbasis,int64_t &addedbets,int64_t &positionsize,int16_t &leverage) +uint8_t prices_finalopretdecode(CScript scriptPubKey, uint256 &bettxid, CPubKey &pk, int32_t &lastheight, int64_t &costbasis, int64_t &lastprice, int64_t &liquidationprice, int64_t &equity, int64_t &exitfee) { std::vector vopret; uint8_t e,f; GetOpReturnData(scriptPubKey,vopret); - if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> profits; ss >> height; ss >> pk; ss >> firstprice; ss >> costbasis; ss >> addedbets; ss >> positionsize; ss >> leverage) != 0 && e == EVAL_PRICES && f == 'F' ) + if (vopret.size() > 2 && E_UNMARSHAL(vopret, ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> lastheight; ss >> costbasis; ss >> lastprice; ss >> liquidationprice; ss >> equity; ss >> exitfee) != 0 && e == EVAL_PRICES && (f == 'F' || f == 'R')) { return(f); } @@ -381,8 +381,8 @@ static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTrans int64_t amount; CPubKey pk, pricespk; int64_t profits; - int32_t height; - int64_t firstprice, costbasis, addedbets, positionsize; + int32_t lastheight; + int64_t firstprice, costbasis, lastprice, liquidationprice, equity, fee; int16_t leverage; if (finaltx.vout.size() < 3 || finaltx.vout.size() > 4) { @@ -391,7 +391,7 @@ static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTrans } vscript_t opret; - if (prices_finalopretdecode(finaltx.vout.back().scriptPubKey, bettxid, profits, height, pk, firstprice, costbasis, addedbets, positionsize, leverage) != 'F') + if (prices_finalopretdecode(finaltx.vout.back().scriptPubKey, bettxid, pk, lastheight, costbasis, lastprice, liquidationprice, equity, fee) == 0) return eval->Invalid("cannot decode opreturn for final tx"); if (bettx.GetHash() != bettxid) @@ -1679,13 +1679,19 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) prices_enumaddedbets(batontxid, betinfo.bets, bettxid); - /*if (!betinfo.isOpen) { + if (!betinfo.isOpen) { CTransaction finaltx; uint256 hashBlock; - if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && prices_finalopretdecode) - { + vscript_t vopret; + if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && PricesCheckOpret(finaltx, vopret) != 0) { + uint8_t funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, betinfo.txid, betinfo.pk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, betinfo.exitfee); + betinfo.isRekt = (funcId == 'R'); + + return 0; } - }*/ + else + return -6; + } if (prices_scanchain(betinfo.bets, betinfo.leverage, betinfo.vecparsed, betinfo.lastprice, betinfo.lastheight) < 0) { @@ -1756,7 +1762,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) else { betinfo.isRekt = true; - betinfo.rektfee = totalbets / 500; + betinfo.exitfee = totalbets / 500; } mpz_clear(mpzTotalbets); @@ -1776,18 +1782,13 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); struct CCcontract_info *cp, C; CTransaction bettx; -/* uint256 hashBlock, tokenid, batontxid; - int64_t firstprice, lastprice = 0, positionsize; - int32_t firstheight; - int16_t leverage; - std::vector vec; */ int64_t myfee = 0; CPubKey pk, mypk, pricespk; std::string rawtx; char destaddr[64]; cp = CCinit(&C, EVAL_PRICES); - if (txfee == 0) // TODO: what did we want tot do with txfee in prices? + if (txfee == 0) // TODO: what did we want to do with txfee in prices? txfee = PRICES_TXFEE; mypk = pubkey2pk(Mypubkey()); pricespk = GetUnspendable(cp, 0); @@ -1834,7 +1835,7 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) prices_betjson(result, betinfo.bets, betinfo.leverage, betinfo.lastheight, betinfo.lastprice); // fill output if (betinfo.isRekt) { - myfee = betinfo.rektfee; // consolation fee for loss + myfee = betinfo.exitfee; // consolation fee for loss } if (myfee != 0) { @@ -1848,7 +1849,7 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); /// mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[2].nValue - myfee - txfee, pricespk)); // change - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, rektheight, mypk, betinfo.firstprice, betinfo.averageCostbasis, totalbets, 0, betinfo.leverage)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee)); return(prices_rawtxresult(result, rawtx, 0)); } else @@ -1866,12 +1867,6 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); struct CCcontract_info *cp, C; char destaddr[64]; -/* CTransaction bettx; - uint256 hashBlock, batontxid, tokenid; - int64_t positionsize, firstprice, lastprice = 0; - int32_t firstheight; - int16_t leverage; - std::vector vec;*/ int64_t CCchange = 0, inputsum; CPubKey pk, mypk, pricespk; std::string rawtx; @@ -1938,7 +1933,7 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) if (CCchange >= txfee) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); // TODO: what should the opret param be: - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(bettxid, totalprofits, nextheight - 1, mypk, betinfo.firstprice, betinfo.averageCostbasis, totalbets, 0, betinfo.leverage)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, nextheight-1, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, txfee)); return(prices_rawtxresult(result, rawtx, 0)); } @@ -1985,7 +1980,7 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) else { result.push_back(Pair("rekt", (int64_t)1)); - result.push_back(Pair("rektfee", betinfo.rektfee)); + result.push_back(Pair("rektfee", betinfo.exitfee)); result.push_back(Pair("rektheight", betinfo.lastheight)); } @@ -2104,26 +2099,30 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t uint16_t opcode1 = vecparsed[1]; bool isInverted = ((opcode1 & KOMODO_PRICEMASK) == PRICES_INV); - std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; + //std::cerr << "prices_ispositionup upperquote=" << upperquote << " bottomquote=" << bottomquote << " opcode1=" << opcode1 << " (opcode1 & KOMODO_PRICEMASK)=" << (opcode1 & KOMODO_PRICEMASK) << std::endl; if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc if (upperquote == "BTC" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "BTC" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup return true for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns true for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return true; } - else + else { + std::cerr << "prices_ispositionup returns false for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return false; + } } if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd if (upperquote == "USD" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "USD" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup return true for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns true for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return true; } - else + else { + std::cerr << "prices_ispositionup returns false for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; return false; + } } } } @@ -2252,7 +2251,7 @@ void prices_getorderbook(std::map > & bookmatc } } else { - fundTotals.totalRekt += (betspos - book[0].rektfee); + fundTotals.totalRekt += (betspos - book[0].exitfee); //TODO: store rekt } book.erase(book.begin()); From 9386dd9830091833f31d4d35492f42000cd026d0 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 19:10:29 +0500 Subject: [PATCH 42/87] corr opreturn check --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 9f001b959..58544a6f0 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -222,7 +222,7 @@ uint8_t prices_finalopretdecode(CScript scriptPubKey, uint256 &bettxid, CPubKey // price opret basic validation and retrieval static uint8_t PricesCheckOpret(const CTransaction & tx, vscript_t &opret) { - if (tx.vout.size() > 0 && GetOpReturnData(tx.vout.back().scriptPubKey, opret) && opret.size() > 2 && opret.begin()[0] == EVAL_PRICES && IS_CHARINSTR(opret.begin()[1], "BACF")) + if (tx.vout.size() > 0 && GetOpReturnData(tx.vout.back().scriptPubKey, opret) && opret.size() > 2 && opret.begin()[0] == EVAL_PRICES && IS_CHARINSTR(opret.begin()[1], "BACFR")) return opret.begin()[1]; else return (uint8_t)0; From af56d295fcf31ed75bdda4c2cca0d8d78b5cc565 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 19:18:25 +0500 Subject: [PATCH 43/87] added funcid to switch --- src/cc/prices.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 58544a6f0..425462ce5 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -525,6 +525,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx break; case 'F': // final tx + case 'R': if (!ValidateFinalTx(cp, eval, tx, firstVinTx)) { ///return false; std::cerr << "PricesValidate() " << "ValidateFinalTx=false " << eval->state.GetRejectReason() << std::endl; From f2bb9b3b9c2d9d26d5783900d22320e9cd4b8c64 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 19:31:45 +0500 Subject: [PATCH 44/87] corr validation code for 'R' --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 425462ce5..2718be5ea 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -459,7 +459,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx std::cerr << "PricesValidate() " << "cannot find prices opret in vintx" << std::endl; } - if (funcId != 'F' && vintxOpret.begin()[1] == 'B' && prevoutN == 1) { + if (!IS_CHARINSTR(funcId, "FR") && vintxOpret.begin()[1] == 'B' && prevoutN == 1) { //return eval->Invalid("cannot spend bet marker"); std::cerr << "PricesValidate() " << " non-final tx cannot spend cc marker vout=" << prevoutN << std::endl; } @@ -476,7 +476,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if (!foundFirst) return eval->Invalid("prices cc vin not found"); - if (funcId != 'F' && ccVinCount > 1) {// for all prices tx except final tx only one cc vin is allowed + if (!IS_CHARINSTR(funcId, "FR") && ccVinCount > 1) {// for all prices tx except final tx only one cc vin is allowed //return eval->Invalid("only one prices cc vin allowed for this tx"); std::cerr << "PricesValidate() " << "only one prices cc vin allowed for this tx" << std::endl; } From 98c96012c482ba02a8fe74b601ce8ba9cee39ec0 Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 19:43:29 +0500 Subject: [PATCH 45/87] corr final funcid --- src/cc/prices.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 2718be5ea..fb92caf97 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -204,7 +204,7 @@ uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKe CScript prices_finalopret(bool isRekt, uint256 bettxid, CPubKey pk, int32_t lastheight, int64_t costbasis, int64_t lastprice, int64_t liquidationprice, int64_t equity, int64_t exitfee) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'F' : 'R') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee); + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'R' : 'F') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee); return(opret); } @@ -1686,6 +1686,9 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) vscript_t vopret; if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && PricesCheckOpret(finaltx, vopret) != 0) { uint8_t funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, betinfo.txid, betinfo.pk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, betinfo.exitfee); + if (funcId == 0) + return -3; + betinfo.isRekt = (funcId == 'R'); return 0; From 8dc85e9c27f7396a72ce5d4ae3ecb54f22815d4f Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 10 May 2019 20:03:53 +0500 Subject: [PATCH 46/87] corr total calc for cashout --- src/cc/prices.cpp | 73 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index fb92caf97..9e006f93f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -121,12 +121,13 @@ typedef struct MatchedBookTotal { typedef struct TotalFund { int64_t totalFund; - int64_t totalBets; + int64_t totalActiveBets; + int64_t totalCashout; int64_t totalRekt; int64_t totalEquity; TotalFund() { - totalFund = totalBets = totalRekt = totalEquity = 0; + totalFund = totalActiveBets = totalCashout = totalRekt = totalEquity = 0; } } TotalFund; @@ -1304,7 +1305,7 @@ void prices_betjson(UniValue &result, std::vector bets, int16_t leve { UniValue resultbets(UniValue::VARR); - int64_t totalbets = 0; + int64_t totalposition = 0; int64_t totalprofits = 0; for (auto b : bets) { @@ -1314,14 +1315,14 @@ void prices_betjson(UniValue &result, std::vector bets, int16_t leve entry.push_back(Pair("costbasis", ValueFromAmount(b.costbasis))); entry.push_back(Pair("firstheight", b.firstheight)); resultbets.push_back(entry); - totalbets += b.positionsize; + totalposition += b.positionsize; totalprofits += b.profits; } - int64_t equity = totalbets + totalprofits; + int64_t equity = totalposition + totalprofits; result.push_back(Pair("bets", resultbets)); result.push_back(Pair("leverage", (int64_t)leverage)); - result.push_back(Pair("TotalPositionSize", ValueFromAmount(totalbets))); + result.push_back(Pair("TotalPositionSize", ValueFromAmount(totalposition))); result.push_back(Pair("TotalProfits", ValueFromAmount(totalprofits))); result.push_back(Pair("equity", ValueFromAmount(equity))); result.push_back(Pair("LastPrice", ValueFromAmount(lastprice))); @@ -1538,7 +1539,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v bool stop = false; for (int32_t height = bets[0].firstheight; ; height++) // the last datum for 24h is the costbasis value { - int64_t totalbets = 0; + int64_t totalposition = 0; int64_t totalprofits = 0; // scan upto the chain tip @@ -1552,7 +1553,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v stop = true; break; } - totalbets += bets[i].positionsize; + totalposition += bets[i].positionsize; totalprofits += bets[i].profits; } } @@ -1561,7 +1562,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v break; endheight = height; - int64_t equity = totalbets + totalprofits; + int64_t equity = totalposition + totalprofits; if (equity < 0) { // we are in loss break; @@ -1702,15 +1703,15 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) return -4; } - mpz_t mpzTotalbets; + mpz_t mpzTotalPosition; mpz_t mpzTotalprofits; mpz_t mpzTotalcostbasis; - mpz_init(mpzTotalbets); + mpz_init(mpzTotalPosition); mpz_init(mpzTotalprofits); mpz_init(mpzTotalcostbasis); - int64_t totalbets = 0; + int64_t totalposition = 0; int64_t totalprofits = 0; for (auto b : betinfo.bets) { @@ -1729,26 +1730,26 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) mpz_mul_ui(mpzProduct, mpzProduct, (uint64_t)b.positionsize); // b.costbasis * b.amount mpz_add(mpzTotalcostbasis, mpzTotalcostbasis, mpzProduct); //averageCostbasis += b.costbasis * b.amount; - mpz_add_ui(mpzTotalbets, mpzTotalbets, (uint64_t)b.positionsize); //totalbets += b.amount; + mpz_add_ui(mpzTotalPosition, mpzTotalPosition, (uint64_t)b.positionsize); //totalposition += b.amount; mpz_add(mpzTotalprofits, mpzTotalprofits, mpzProfits); //totalprofits += b.profits; - totalbets += b.positionsize; + totalposition += b.positionsize; totalprofits += b.profits; mpz_clear(mpzProduct); mpz_clear(mpzProfits); } - betinfo.equity = totalbets + totalprofits; + betinfo.equity = totalposition + totalprofits; //int64_t averageCostbasis = 0; - if (mpz_get_ui(mpzTotalbets) != 0) { //prevent zero div + if (mpz_get_ui(mpzTotalPosition) != 0) { //prevent zero div mpz_t mpzAverageCostbasis; mpz_init(mpzAverageCostbasis); - //averageCostbasis = totalcostbasis / totalbets; + //averageCostbasis = totalcostbasis / totalposition; mpz_mul_ui(mpzTotalcostbasis, mpzTotalcostbasis, SATOSHIDEN); // profits *= SATOSHIDEN normalization to prevent loss of significance while division - mpz_tdiv_q(mpzAverageCostbasis, mpzTotalcostbasis, mpzTotalbets); + mpz_tdiv_q(mpzAverageCostbasis, mpzTotalcostbasis, mpzTotalPosition); mpz_tdiv_q_ui(mpzAverageCostbasis, mpzAverageCostbasis, SATOSHIDEN); // profits /= SATOSHIDEN de-normalization @@ -1766,10 +1767,10 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) else { betinfo.isRekt = true; - betinfo.exitfee = totalbets / 500; + betinfo.exitfee = totalposition / 500; } - mpz_clear(mpzTotalbets); + mpz_clear(mpzTotalPosition); mpz_clear(mpzTotalprofits); mpz_clear(mpzTotalcostbasis); return 0; @@ -1821,11 +1822,11 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) return(result); } - int64_t totalbets = 0; + int64_t totalposition = 0; int64_t totalprofits = 0; for (auto b : betinfo.bets) { - totalbets += b.positionsize; + totalposition += b.positionsize; totalprofits += b.profits; } @@ -1906,11 +1907,11 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) return(result); } - int64_t totalbets = 0; + int64_t totalposition = 0; int64_t totalprofits = 0; for (auto b : betinfo.bets) { - totalbets += b.positionsize; + totalposition += b.positionsize; totalprofits += b.profits; } @@ -2211,7 +2212,7 @@ void prices_getorderbook(std::map > & bookmatc fundTotals.totalFund = 0; fundTotals.totalRekt = 0; fundTotals.totalEquity = 0; - fundTotals.totalBets = 0; + fundTotals.totalActiveBets = 0; std::vector > addressCCunspents; SetCCunspents(addressCCunspents, cp->unspendableCCaddr, true); // cc marker @@ -2224,12 +2225,12 @@ void prices_getorderbook(std::map > & bookmatc // extract out opposite bets: while (book.size() > 0) { - int64_t betspos = 0; - for (auto bet : book[0].bets) betspos += bet.positionsize; + int64_t totalPos = 0; + for (auto bet : book[0].bets) totalPos += bet.positionsize; - if (!book[0].isRekt) { + if (book[0].isOpen) { - fundTotals.totalBets += betspos; + fundTotals.totalActiveBets += totalPos; fundTotals.totalEquity += book[0].equity; if (book[0].vecparsed.size() <= 3) { // only short expr check for match: "BTC_USD,1" or "BTC_USD,!,1" @@ -2255,7 +2256,11 @@ void prices_getorderbook(std::map > & bookmatc } } else { - fundTotals.totalRekt += (betspos - book[0].exitfee); + if( book[0].isRekt ) + fundTotals.totalRekt += (totalPos - book[0].exitfee); + else + fundTotals.totalCashout += (totalPos - book[0].exitfee); + //TODO: store rekt } book.erase(book.begin()); @@ -2368,11 +2373,11 @@ UniValue PricesGetOrderbook() } */ result.push_back(Pair("TotalFund", ValueFromAmount(fundTotals.totalFund))); - result.push_back(Pair("TotalRekt", ValueFromAmount(fundTotals.totalRekt))); - result.push_back(Pair("TotalBets", ValueFromAmount(fundTotals.totalBets))); result.push_back(Pair("TotalEquity", ValueFromAmount(fundTotals.totalEquity))); - - + result.push_back(Pair("TotalRekt", ValueFromAmount(fundTotals.totalRekt))); + result.push_back(Pair("TotalBets", ValueFromAmount(fundTotals.totalActiveBets))); + result.push_back(Pair("TotalCashoutBets", ValueFromAmount(fundTotals.totalCashout))); + // result.push_back(Pair("TotalLiabilities", ValueFromAmount(totalLiabilities))); return result; } From 74bf716aca711787fb7a5311523f8f2a502c43c8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 13:21:10 +0500 Subject: [PATCH 47/87] added position to orderbook output --- src/cc/prices.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 9e006f93f..e5fba6c3e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2272,13 +2272,13 @@ void prices_getorderbook(std::map > & bookmatc int64_t totalLeveragedPositionDown = 0; for (int i = 0; i < m.second.size(); i++) { - int64_t betspos = 0; - for (auto bet : m.second[i].bets) betspos += bet.positionsize; + int64_t totalPos = 0; + for (auto bet : m.second[i].bets) totalPos += bet.positionsize; m.second[i].isUp = prices_ispositionup(m.second[i].vecparsed, m.second[i].leverage); if (m.second[i].isUp) - totalLeveragedPositionUp += betspos * abs(m.second[i].leverage); + totalLeveragedPositionUp += totalPos * abs(m.second[i].leverage); else - totalLeveragedPositionDown += betspos * abs(m.second[i].leverage); + totalLeveragedPositionDown += totalPos * abs(m.second[i].leverage); //std::cerr << "PricesGetOrderbook 0 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; } @@ -2347,8 +2347,12 @@ UniValue PricesGetOrderbook() UniValue resbook(UniValue::VARR); for (int i = 0; i < m.second.size(); i++) { UniValue entry(UniValue::VOBJ); + + int64_t totalPos = 0; + for (auto bet : m.second[i].bets) totalPos += bet.positionsize; entry.push_back(Pair("isOpen", (m.second[i].isOpen ? 1 : 0 ))); entry.push_back(Pair("expression", prices_getsourceexpression(m.second[i].vecparsed))); + entry.push_back(Pair("positionsize", totalPos)); entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); entry.push_back(Pair("equity", m.second[i].equity)); From 38213f23a0295aeebee0981c4db89ca42ffba233 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 15:03:30 +0500 Subject: [PATCH 48/87] add vout with test revshare fee --- src/cc/CCPrices.h | 2 ++ src/cc/prices.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 238131d59..779f27a34 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -41,6 +41,8 @@ extern CScript KOMODO_EARLYTXID_SCRIPTPUB; #define PRICES_NORMFACTOR (int64_t)(SATOSHIDEN) #define PRICES_POINTFACTOR (int64_t)10000 +#define PRICES_SUBREVSHAREFEE(amount) ((amount) / 199 * 200) + bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); // CCcustom diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index e5fba6c3e..c8cf0e9d7 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1447,7 +1447,7 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< if (AddNormalinputs(mtx, mypk, amount + 4 * txfee, 64) >= amount + 4 * txfee) { - betamount = (amount * 199) / 200; + betamount = PRICES_SUBREVSHAREFEE(amount); mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding // mtx.vout.push_back(MakeCC1vout(cp->evalcode, (amount - betamount) + 2 * txfee, pricespk)); // vout1, when spent, costbasis is set mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, pricespk)); // vout1 cc marker (NVOUT_CCMARKER) @@ -1461,6 +1461,7 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< GetKomodoEarlytxidScriptPub(); } mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); */ + mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac") << OP_CHECKSIG)); // test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); return(prices_rawtxresult(result, rawtx, 0)); @@ -1509,12 +1510,16 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) if (AddNormalinputs(mtx, mypk, amount + 2*txfee, 64) >= amount + 2*txfee) { + betamount = PRICES_SUBREVSHAREFEE(amount); + std::vector bets; if (prices_enumaddedbets(batontxid, bets, bettxid) >= 0) { mtx.vin.push_back(CTxIn(batontxid, 0, CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding - mtx.vout.push_back(MakeCC1vout(cp->evalcode, amount, pricespk)); // vout1 added amount + mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout1 added amount + mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac") << OP_CHECKSIG)); //vout2 test revshare fee + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_addopret(bettxid, mypk, amount)); return(prices_rawtxresult(result, rawtx, 0)); } From 6a196be9cccadb8cd685d658942e38a1910e317b Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 15:19:39 +0500 Subject: [PATCH 49/87] corr test scriptpubkey --- src/cc/prices.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index c8cf0e9d7..b12a9a39f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -314,7 +314,8 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C return true; } -// validate costbasis tx helper +// validate costbasis tx helper (deprecated) +/* static bool ValidateCostbasisTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & costbasistx, const CTransaction & bettx) { uint256 bettxid; @@ -374,6 +375,7 @@ static bool ValidateCostbasisTx(struct CCcontract_info *cp, Eval *eval, const CT return true; } +*/ // validate final tx helper static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTransaction & finaltx, const CTransaction & bettx) @@ -509,6 +511,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } break; + /* not used: case 'C': // set costbasis if (!ValidateCostbasisTx(cp, eval, tx, firstVinTx)) { //return false; @@ -523,7 +526,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx std::cerr << "PricesValidate() " << "costbasis tx incorrect vout to spend=" << prevoutN << std::endl; } //return eval->Invalid("test: costbasis is good"); - break; + break; */ case 'F': // final tx case 'R': @@ -1461,7 +1464,7 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< GetKomodoEarlytxidScriptPub(); } mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); */ - mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac") << OP_CHECKSIG)); // test revshare fee + mtx.vout.push_back(CTxOut(amount - betamount, CScript(ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac")))); // vout4 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); return(prices_rawtxresult(result, rawtx, 0)); @@ -1518,7 +1521,7 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) mtx.vin.push_back(CTxIn(batontxid, 0, CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout1 added amount - mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac") << OP_CHECKSIG)); //vout2 test revshare fee + mtx.vout.push_back(CTxOut(amount - betamount, CScript(ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac")))); //vout2 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_addopret(bettxid, mypk, amount)); return(prices_rawtxresult(result, rawtx, 0)); From 7303f125cab0e5ff41bc736038a9425549164903 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 15:30:32 +0500 Subject: [PATCH 50/87] corr test scriptpubkey --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index b12a9a39f..e4c8452bc 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1464,7 +1464,7 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< GetKomodoEarlytxidScriptPub(); } mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); */ - mtx.vout.push_back(CTxOut(amount - betamount, CScript(ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac")))); // vout4 test revshare fee + mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); // vout4 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); return(prices_rawtxresult(result, rawtx, 0)); @@ -1521,7 +1521,7 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) mtx.vin.push_back(CTxIn(batontxid, 0, CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout1 added amount - mtx.vout.push_back(CTxOut(amount - betamount, CScript(ParseHex("76a91412046a2aaaa2d041740aa2b755757bed270541d888ac")))); //vout2 test revshare fee + mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); //vout2 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_addopret(bettxid, mypk, amount)); return(prices_rawtxresult(result, rawtx, 0)); From 2fe4bbf7f6d000932b05aa743605bd36d7e4d60b Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 15:42:46 +0500 Subject: [PATCH 51/87] corr macro calc revshare fee --- src/cc/CCPrices.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 779f27a34..18a33de20 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -41,7 +41,7 @@ extern CScript KOMODO_EARLYTXID_SCRIPTPUB; #define PRICES_NORMFACTOR (int64_t)(SATOSHIDEN) #define PRICES_POINTFACTOR (int64_t)10000 -#define PRICES_SUBREVSHAREFEE(amount) ((amount) / 199 * 200) +#define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); From 7933b7919fff46974c3071cad6431da05983ae7d Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 16:00:08 +0500 Subject: [PATCH 52/87] added dust check --- src/cc/CCPrices.h | 5 +++-- src/cc/prices.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 18a33de20..5e1699fde 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -38,9 +38,10 @@ extern CScript KOMODO_EARLYTXID_SCRIPTPUB; #define PRICES_MMM (KOMODO_MAXPRICES * 7) // 0011 1000 0000 0000 #define PRICES_DDD (KOMODO_MAXPRICES * 8) // 0100 0000 0000 0000 -#define PRICES_NORMFACTOR (int64_t)(SATOSHIDEN) -#define PRICES_POINTFACTOR (int64_t)10000 +//#define PRICES_NORMFACTOR (int64_t)(SATOSHIDEN) +//#define PRICES_POINTFACTOR (int64_t)10000 +#define PRICES_REVSHAREDUST 10000 #define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index e4c8452bc..f04496eba 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1451,6 +1451,14 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< if (AddNormalinputs(mtx, mypk, amount + 4 * txfee, 64) >= amount + 4 * txfee) { betamount = PRICES_SUBREVSHAREFEE(amount); + + if( amount - betamount < PRICES_REVSHAREDUST) { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "bet amount too small")); + return(result); + } + + mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding // mtx.vout.push_back(MakeCC1vout(cp->evalcode, (amount - betamount) + 2 * txfee, pricespk)); // vout1, when spent, costbasis is set mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, pricespk)); // vout1 cc marker (NVOUT_CCMARKER) @@ -1514,6 +1522,11 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) if (AddNormalinputs(mtx, mypk, amount + 2*txfee, 64) >= amount + 2*txfee) { betamount = PRICES_SUBREVSHAREFEE(amount); + if (amount - betamount < PRICES_REVSHAREDUST) { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "bet amount too small")); + return(result); + } std::vector bets; if (prices_enumaddedbets(batontxid, bets, bettxid) >= 0) From 96bc9641235d4e75458a572a33347c606da4fd2b Mon Sep 17 00:00:00 2001 From: dimxy Date: Sun, 12 May 2019 16:16:28 +0500 Subject: [PATCH 53/87] corr add funding tx validation added amount check --- src/cc/prices.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index f04496eba..c6df1ccb5 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -238,8 +238,11 @@ static bool ValidateBetTx(struct CCcontract_info *cp, Eval *eval, const CTransac int16_t leverage; CPubKey pk, pricespk; std::vector vec; + + // check payment cc config: if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) GetKomodoEarlytxidScriptPub(); + if (bettx.vout.size() < 5 || bettx.vout.size() > 6) return eval->Invalid("incorrect vout number for bet tx"); @@ -260,7 +263,7 @@ static bool ValidateBetTx(struct CCcontract_info *cp, Eval *eval, const CTransac return eval->Invalid("the fee was paid to wrong address."); int64_t betamount = bettx.vout[2].nValue; - if (betamount != (positionsize * 199) / 200) { + if (betamount != PRICES_SUBREVSHAREFEE(positionsize)) { return eval->Invalid("invalid position size in the opreturn"); } @@ -289,7 +292,11 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C CPubKey pk, pricespk; vscript_t vintxOpret; - if (addfundingtx.vout.size() < 3 || addfundingtx.vout.size() > 4) + // check payment cc config: + if (ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0) + GetKomodoEarlytxidScriptPub(); + + if (addfundingtx.vout.size() < 4 || addfundingtx.vout.size() > 5) return eval->Invalid("incorrect vout number for add funding tx"); vscript_t opret; @@ -311,6 +318,11 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C if (MakeCC1vout(cp->evalcode, addfundingtx.vout[1].nValue, pricespk) != addfundingtx.vout[1]) return eval->Invalid("cannot validate vout1 in add funding tx with global pk"); + int64_t betamount = addfundingtx.vout[2].nValue; + if (betamount != PRICES_SUBREVSHAREFEE(amount)) { + return eval->Invalid("invalid position size in the opreturn"); + } + return true; } From 822dd109b874acdabd24d1fd1aa1fa0d02d51103 Mon Sep 17 00:00:00 2001 From: dimxy Date: Mon, 13 May 2019 02:25:57 +0500 Subject: [PATCH 56/87] test removed --- src/cc/CCtokens.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/CCtokens.cpp b/src/cc/CCtokens.cpp index 5f5e56ebb..564fead37 100644 --- a/src/cc/CCtokens.cpp +++ b/src/cc/CCtokens.cpp @@ -632,6 +632,9 @@ int64_t AddTokenCCInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, C if (it->second.satoshis < threshold) // this should work also for non-fungible tokens (there should be only 1 satoshi for non-fungible token issue) continue; + if (std::find_if(mtx.vin.begin(), mtx.vin.end(), [&](auto v) {return (v.prevout.hash == vintxid && v.prevout.n == vout); }) == mtx.vin.end()) + continue; + int32_t ivin; for (ivin = 0; ivin < mtx.vin.size(); ivin ++) if (vintxid == mtx.vin[ivin].prevout.hash && vout == mtx.vin[ivin].prevout.n) From 5b359f1b31c3b5240c6aa230b2fe0f269db2ebf8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Mon, 13 May 2019 02:26:36 +0500 Subject: [PATCH 57/87] test removed --- src/cc/CCtokens.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/cc/CCtokens.cpp b/src/cc/CCtokens.cpp index 564fead37..5f5e56ebb 100644 --- a/src/cc/CCtokens.cpp +++ b/src/cc/CCtokens.cpp @@ -632,9 +632,6 @@ int64_t AddTokenCCInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, C if (it->second.satoshis < threshold) // this should work also for non-fungible tokens (there should be only 1 satoshi for non-fungible token issue) continue; - if (std::find_if(mtx.vin.begin(), mtx.vin.end(), [&](auto v) {return (v.prevout.hash == vintxid && v.prevout.n == vout); }) == mtx.vin.end()) - continue; - int32_t ivin; for (ivin = 0; ivin < mtx.vin.size(); ivin ++) if (vintxid == mtx.vin[ivin].prevout.hash && vout == mtx.vin[ivin].prevout.n) From 404070afea783c036ee019bd28bbb5a450541749 Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 14 May 2019 01:40:13 +0500 Subject: [PATCH 58/87] changed non validating height --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index c6df1ccb5..03188e6fd 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -443,7 +443,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx { vscript_t vopret; - if (strcmp(ASSETCHAINS_SYMBOL, "REKT0") == 0 && chainActive.Height() < 2965) + if (strcmp(ASSETCHAINS_SYMBOL, "REKT0") == 0 && chainActive.Height() < 5851) return true; // check basic opret rules: if (PricesCheckOpret(tx, vopret) == 0) From 7be3960b7fe320475f56b8970b67d5f663ca61c1 Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 14 May 2019 13:59:56 +0500 Subject: [PATCH 59/87] corr log msg --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 03188e6fd..c0e43784d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -519,7 +519,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if (prevoutN != 0) { // check spending rules // return eval->Invalid("incorrect vintx vout to spend"); - std::cerr << "PricesValidate() " << "add fund tx incorrect vout to spend=" << prevoutN << std::endl; + std::cerr << "PricesValidate() " << "addfunding tx incorrect vout to spend=" << prevoutN << std::endl; } break; From 63306290accafc2462a694aebe7271ce352b52f4 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 00:34:48 +0500 Subject: [PATCH 60/87] earlytxid scriptpubkey vout enabled validation enabled --- src/cc/prices.cpp | 52 ++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index c0e43784d..1905cd313 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -318,6 +318,10 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C if (MakeCC1vout(cp->evalcode, addfundingtx.vout[1].nValue, pricespk) != addfundingtx.vout[1]) return eval->Invalid("cannot validate vout1 in add funding tx with global pk"); + // This should be all you need to verify it, maybe also check amount? + if (addfundingtx.vout[2].scriptPubKey != KOMODO_EARLYTXID_SCRIPTPUB) + return eval->Invalid("the fee was paid to wrong address."); + int64_t betamount = addfundingtx.vout[2].nValue; if (betamount != PRICES_SUBREVSHAREFEE(amount)) { return eval->Invalid("invalid position size in the opreturn"); @@ -455,7 +459,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx vscript_t firstVinTxOpret; bool foundFirst = false; int32_t ccVinCount = 0; - uint32_t prevoutN = 0; + uint32_t prevCCoutN = 0; // check basic rules: @@ -474,13 +478,13 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx std::cerr << "PricesValidate() " << "cannot find prices opret in vintx" << std::endl; } - if (!IS_CHARINSTR(funcId, "FR") && vintxOpret.begin()[1] == 'B' && prevoutN == 1) { + if (!IS_CHARINSTR(funcId, "FR") && vintxOpret.begin()[1] == 'B' && prevCCoutN == 1) { //return eval->Invalid("cannot spend bet marker"); - std::cerr << "PricesValidate() " << " non-final tx cannot spend cc marker vout=" << prevoutN << std::endl; + std::cerr << "PricesValidate() " << " non-final tx cannot spend cc marker vout=" << prevCCoutN << std::endl; } if (!foundFirst) { - prevoutN = vin.prevout.n; + prevCCoutN = vin.prevout.n; firstVinTx = vintx; firstVinTxOpret = vintxOpret; foundFirst = true; @@ -503,23 +507,20 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx case 'A': // add funding // check tx structure: if (!ValidateAddFundingTx(cp, eval, tx, firstVinTx)) { - //return false; // invalid state is already set in the func std::cerr << "PricesValidate() " << "ValidateAddFundingTx = false " << eval->state.GetRejectReason() << std::endl; + return false; // invalid state is already set in the func } if (firstVinTxOpret.begin()[1] == 'B') { if (!ValidateBetTx(cp, eval, firstVinTx)) {// check tx structure - // return false; std::cerr << "PricesValidate() " << "funcId=A ValidatebetTx = false " << eval->state.GetRejectReason() << std::endl; + return false; // invalid state is already set in the func } } - else if (firstVinTxOpret.begin()[1] == 'A') { - // no need to validate the previous addfunding tx (it was validated when added) - } - if (prevoutN != 0) { // check spending rules - // return eval->Invalid("incorrect vintx vout to spend"); - std::cerr << "PricesValidate() " << "addfunding tx incorrect vout to spend=" << prevoutN << std::endl; + if (prevCCoutN != 0) { // check spending rules + std::cerr << "PricesValidate() " << "addfunding tx incorrect vout to spend=" << prevCCoutN << std::endl; + return eval->Invalid("incorrect vintx vout to spend"); } break; @@ -543,16 +544,16 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx case 'F': // final tx case 'R': if (!ValidateFinalTx(cp, eval, tx, firstVinTx)) { - ///return false; std::cerr << "PricesValidate() " << "ValidateFinalTx=false " << eval->state.GetRejectReason() << std::endl; + return false; } if (!ValidateBetTx(cp, eval, firstVinTx)) { - // return false; std::cerr << "PricesValidate() " << "ValidateBetTx=false " << eval->state.GetRejectReason() << std::endl; + return false; } - if (prevoutN != 1) { // check spending rules - // return eval->Invalid("incorrect vout to spend"); - std::cerr << "PricesValidate() "<< "final tx incorrect vout to spend=" << prevoutN << std::endl; + if (prevCCoutN != 1) { // check spending rules + std::cerr << "PricesValidate() "<< "final tx incorrect vout to spend=" << prevCCoutN << std::endl; + return eval->Invalid("incorrect vout to spend"); } break; @@ -1476,15 +1477,15 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, pricespk)); // vout1 cc marker (NVOUT_CCMARKER) mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout2 betamount mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); // vout3 normal marker NVOUT_NORMALMARKER - TODO: remove it as we have cc marker now, when move to the new chain - /*if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) + if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) { // Lock here, as in validation we cannot call lock in the function itself. // may not be needed as the validation call to update the global, is called in a LOCK already, and it can only update there and here. LOCK(cs_main); GetKomodoEarlytxidScriptPub(); } - mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); */ - mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); // vout4 test revshare fee + mtx.vout.push_back(CTxOut(amount-betamount, KOMODO_EARLYTXID_SCRIPTPUB)); + //test: mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); // vout4 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_betopret(mypk, nextheight - 1, amount, leverage, firstprice, vec, zeroid)); return(prices_rawtxresult(result, rawtx, 0)); @@ -1546,7 +1547,16 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) mtx.vin.push_back(CTxIn(batontxid, 0, CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding mtx.vout.push_back(MakeCC1vout(cp->evalcode, betamount, pricespk)); // vout1 added amount - mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); //vout2 test revshare fee + + if (ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0) + { + // Lock here, as in validation we cannot call lock in the function itself. + // may not be needed as the validation call to update the global, is called in a LOCK already, and it can only update there and here. + LOCK(cs_main); + GetKomodoEarlytxidScriptPub(); + } + mtx.vout.push_back(CTxOut(amount - betamount, KOMODO_EARLYTXID_SCRIPTPUB)); + // test: mtx.vout.push_back(CTxOut(amount - betamount, CScript() << ParseHex("037c803ec82d12da939ac04379bbc1130a9065c53d8244a61eece1db942cf0efa7") << OP_CHECKSIG)); //vout2 test revshare fee rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_addopret(bettxid, mypk, amount)); return(prices_rawtxresult(result, rawtx, 0)); From 4d02ec11e47e6c141f16800af46ae3266f2a3d46 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 00:43:56 +0500 Subject: [PATCH 61/87] disable check for min revshare --- src/cc/prices.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 1905cd313..d9a536424 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1465,11 +1465,11 @@ UniValue PricesBet(int64_t txfee, int64_t amount, int16_t leverage, std::vector< { betamount = PRICES_SUBREVSHAREFEE(amount); - if( amount - betamount < PRICES_REVSHAREDUST) { + /*if( amount - betamount < PRICES_REVSHAREDUST) { result.push_back(Pair("result", "error")); result.push_back(Pair("error", "bet amount too small")); return(result); - } + }*/ mtx.vout.push_back(MakeCC1vout(cp->evalcode, txfee, mypk)); // vout0 baton for total funding @@ -1535,11 +1535,11 @@ UniValue PricesAddFunding(int64_t txfee, uint256 bettxid, int64_t amount) if (AddNormalinputs(mtx, mypk, amount + 2*txfee, 64) >= amount + 2*txfee) { betamount = PRICES_SUBREVSHAREFEE(amount); - if (amount - betamount < PRICES_REVSHAREDUST) { + /*if (amount - betamount < PRICES_REVSHAREDUST) { result.push_back(Pair("result", "error")); result.push_back(Pair("error", "bet amount too small")); return(result); - } + }*/ std::vector bets; if (prices_enumaddedbets(batontxid, bets, bettxid) >= 0) From 273f574e64036bc885db59a93ce126fddb2dad1b Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 15:03:34 +0500 Subject: [PATCH 62/87] daywindow set to 7 --- src/komodo_defs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index cf15f0b19..6aa675bf8 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -//#define PRICES_DAYWINDOW (7) +#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From 3667b573fa62925a1e2398598f1e25977598cdcb Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 16:36:24 +0500 Subject: [PATCH 63/87] added refillfund rpc --- src/cc/CCPrices.h | 1 + src/cc/prices.cpp | 28 ++++++++++++++++++++++++++++ src/rpc/blockchain.cpp | 17 +++++++++++++++++ src/rpc/server.cpp | 2 +- src/rpc/server.h | 1 + 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 5e1699fde..b8c7a74e0 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -55,6 +55,7 @@ UniValue PricesCashout(int64_t txfee,uint256 bettxid); UniValue PricesInfo(uint256 bettxid,int32_t refheight); UniValue PricesList(uint32_t filter, CPubKey mypk); UniValue PricesGetOrderbook(); +UniValue PricesRefillFund(int64_t amount); #endif diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index d9a536424..952814761 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1687,6 +1687,34 @@ UniValue PricesSetcostbasis(int64_t txfee, uint256 bettxid) } +// pricesaddfunding rpc impl: add yet another bet +UniValue PricesRefillFund(int64_t amount) +{ + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp, C; + CPubKey pricespk, mypk, pk; + std::string rawtx; + //char myaddr[64]; + + cp = CCinit(&C, EVAL_PRICES); + const int64_t txfee = PRICES_TXFEE; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp, 0); + + if (AddNormalinputs(mtx, mypk, amount + txfee, 64) >= amount + txfee) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode, amount, pricespk)); // vout1 added amount + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, CScript()); + return(prices_rawtxresult(result, rawtx, 0)); + + } + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "not enough funds")); + return(result); +} + + int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) { CTransaction bettx; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 042b6591f..730ff7491 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1454,6 +1454,23 @@ UniValue pricesgetorderbook(const UniValue& params, bool fHelp) return PricesGetOrderbook(); } +// pricesrekt rpc implementation +UniValue pricesrefillfund(const UniValue& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw runtime_error("pricesrefillfund\n"); + LOCK(cs_main); + UniValue ret(UniValue::VOBJ); + + if (ASSETCHAINS_CBOPRET == 0) + throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); + + CAmount amount = atof(params[0].get_str().c_str()) * COIN; + + return PricesRefillFund(amount); +} + + UniValue gettxout(const UniValue& params, bool fHelp) { if (fHelp || params.size() < 2 || params.size() > 3) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index c35647f8c..500d175e0 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -471,7 +471,7 @@ static const CRPCCommand vRPCCommands[] = { "prices", "pricesrekt", &pricesrekt, true }, { "prices", "pricesaddfunding", &pricesaddfunding, true }, { "prices", "pricesgetorderbook", &pricesgetorderbook, true }, - + { "prices", "pricesrefillfund", &pricesrefillfund, true }, // Pegs { "pegs", "pegsaddress", &pegsaddress, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index c61f62dc3..b9fb33198 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -510,6 +510,7 @@ extern UniValue pricescashout(const UniValue& params, bool fHelp); extern UniValue pricesrekt(const UniValue& params, bool fHelp); extern UniValue pricesaddfunding(const UniValue& params, bool fHelp); extern UniValue pricesgetorderbook(const UniValue& params, bool fHelp); +extern UniValue pricesrefillfund(const UniValue& params, bool fHelp); From dc30041567c8f5f9df3e0189670ee6c80fedf160 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 16:54:43 +0500 Subject: [PATCH 64/87] corr rpc help --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 730ff7491..76d35ff61 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1458,7 +1458,7 @@ UniValue pricesgetorderbook(const UniValue& params, bool fHelp) UniValue pricesrefillfund(const UniValue& params, bool fHelp) { if (fHelp || params.size() != 1) - throw runtime_error("pricesrefillfund\n"); + throw runtime_error("pricesrefillfund amount\n"); LOCK(cs_main); UniValue ret(UniValue::VOBJ); From 51f32c8b8c0fadb5bc8f5b103d2484e42d3b1775 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 17:04:25 +0500 Subject: [PATCH 65/87] restored PRICES_DAYWINDOW to original --- src/komodo_defs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 6aa675bf8..cf15f0b19 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -#define PRICES_DAYWINDOW (7) +//#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From b80c8710f28dcab0724a5ef52f6a72b3933dabb7 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 17:16:26 +0500 Subject: [PATCH 66/87] added default err report in info --- src/cc/prices.cpp | 4 ++++ src/komodo_defs.h | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 952814761..5d63f9114 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2047,6 +2047,10 @@ UniValue PricesInfo(uint256 bettxid, int32_t refheight) else if (retcode == -4) { result.push_back(Pair("result", "error")); result.push_back(Pair("error", "error scanning chain")); + } + else { + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", retcode)); } return(result); } diff --git a/src/komodo_defs.h b/src/komodo_defs.h index cf15f0b19..6aa675bf8 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -//#define PRICES_DAYWINDOW (7) +#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From 7c31159a1d3416c3c20cc710e3c71b14a0292d5c Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 17:26:17 +0500 Subject: [PATCH 67/87] removed base chain height --- src/cc/prices.cpp | 12 ++++++------ src/komodo_defs.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 5d63f9114..7067465be 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1727,9 +1727,9 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) // TODO: forget old tx - CBlockIndex *bi = komodo_getblockindex(hashBlock); - if (bi && bi->GetHeight() < 5342) - return -5; + //CBlockIndex *bi = komodo_getblockindex(hashBlock); + //if (bi && bi->GetHeight() < 5342) + // return -5; OneBetData bet1; if (prices_betopretdecode(bettx.vout.back().scriptPubKey, betinfo.pk, bet1.firstheight, bet1.positionsize, betinfo.leverage, betinfo.firstprice, betinfo.vecparsed, betinfo.tokenid) == 'B') @@ -2107,9 +2107,9 @@ UniValue PricesList(uint32_t filter, CPubKey mypk) { // TODO: forget old tx - CBlockIndex *bi = komodo_getblockindex(hashBlock); - if (bi && bi->GetHeight() < 5342) - return; + //CBlockIndex *bi = komodo_getblockindex(hashBlock); + //if (bi && bi->GetHeight() < 5342) + // return; bool bAppend = false; if (vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout.back().scriptPubKey, pk, height, amount, leverage, firstprice, vec, tokenid) == 'B' && diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 6aa675bf8..cf15f0b19 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -#define PRICES_DAYWINDOW (7) +//#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From 0e61558b43e98726c5f5a87c3c45cee73b7992f3 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 15 May 2019 17:27:44 +0500 Subject: [PATCH 68/87] daywindow = 7 --- src/komodo_defs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index cf15f0b19..6aa675bf8 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -//#define PRICES_DAYWINDOW (7) +#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From e9fd323fb541e6b18449d472d82fd35cb4159d9d Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 16 May 2019 00:14:47 +0500 Subject: [PATCH 70/87] set PRICES_DAYWINDOW to orig --- src/komodo_defs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 6aa675bf8..cf15f0b19 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -192,9 +192,9 @@ static const char *notaries_elected[NUM_KMD_SEASONS][NUM_KMD_NOTARIES][2] = #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) //#ifndef TESTMODE -//#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) //#else -#define PRICES_DAYWINDOW (7) +//#define PRICES_DAYWINDOW (7) //#endif extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; From b932d0fab5e610f931adebe887fc03db44f55531 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 00:28:14 +0500 Subject: [PATCH 71/87] corr bet and add tx validation added min margin --- src/cc/CCPrices.h | 3 ++- src/cc/prices.cpp | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index b8c7a74e0..3779111a6 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -42,7 +42,8 @@ extern CScript KOMODO_EARLYTXID_SCRIPTPUB; //#define PRICES_POINTFACTOR (int64_t)10000 #define PRICES_REVSHAREDUST 10000 -#define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) +#define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) // revshare fee percentage == 0.005 +#define PRICES_MINAVAILFUNDFRACTION 0.1 // leveraged bet limit < fund fraction bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 7067465be..b063e4552 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -243,7 +243,7 @@ static bool ValidateBetTx(struct CCcontract_info *cp, Eval *eval, const CTransac if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) GetKomodoEarlytxidScriptPub(); - if (bettx.vout.size() < 5 || bettx.vout.size() > 6) + if (bettx.vout.size() < 6 || bettx.vout.size() > 7) return eval->Invalid("incorrect vout number for bet tx"); vscript_t opret; @@ -322,9 +322,9 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C if (addfundingtx.vout[2].scriptPubKey != KOMODO_EARLYTXID_SCRIPTPUB) return eval->Invalid("the fee was paid to wrong address."); - int64_t betamount = addfundingtx.vout[2].nValue; + int64_t betamount = addfundingtx.vout[1].nValue; if (betamount != PRICES_SUBREVSHAREFEE(amount)) { - return eval->Invalid("invalid position size in the opreturn"); + return eval->Invalid("invalid bet position size in the opreturn"); } return true; @@ -602,6 +602,19 @@ int64_t AddPricesInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, ch return(totalinputs); } +// return min equity percentage depending on leverage value +// for lev=1 2% +// for lev>=100 10% +double prices_minmarginpercent(int16_t leverage) +{ + int16_t absleverage = std::abs(leverage); + if (absleverage < 100) + return (absleverage * 0.080808 + 1.9191919) / 100.0; + else + return 0.01; +} + + UniValue prices_rawtxresult(UniValue &result, std::string rawtx, int32_t broadcastflag) { CTransaction tx; @@ -1606,7 +1619,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v endheight = height; int64_t equity = totalposition + totalprofits; - if (equity < 0) + if (equity < (double)totalposition * prices_minmarginpercent(leverage)) { // we are in loss break; } @@ -1833,7 +1846,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) betinfo.liquidationprice = betinfo.averageCostbasis - betinfo.averageCostbasis / betinfo.leverage; } - if (betinfo.equity >= 0) + if (betinfo.equity >= (double)totalposition * prices_minmarginpercent(betinfo.leverage)) betinfo.isRekt = false; else { @@ -2384,7 +2397,7 @@ static bool prices_isacceptableamount(const std::vector &vecparsed, in std::cerr << "prices_isacceptableamount() amount=" << amount << " leverage=" << leverage << " fundTotals.totalFund=" << fundTotals.totalFund << " fundTotals.totalEquity=" << fundTotals.totalEquity << std::endl; // if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund - if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * 0.1) + if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * PRICES_MINAVAILFUNDFRACTION) return true; return false; From 8914898d439214a805495ee34e45fd2822f6256c Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 01:36:02 +0500 Subject: [PATCH 72/87] corr liq price calc --- src/cc/prices.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index b063e4552..043882ef3 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -424,6 +424,9 @@ static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTrans if( finaltx.vout.size() == 3 && MakeCC1vout(cp->evalcode, finaltx.vout[1].nValue, pricespk) != finaltx.vout[1] ) return eval->Invalid("cannot validate vout1 in final tx with global pk"); + // TODO: validate exitfee for 'R' + // TODO: validate amount for 'F' + return true; } @@ -1262,13 +1265,13 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t //} } else { - if (height == firstheight + COSTBASIS_PERIOD) { + //if (height == firstheight + COSTBASIS_PERIOD) { // if costbasis not set, just set it //costbasis = price; // use calculated minmax costbasis //std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; - } + //} } // normalize to 10,000,000 to prevent underflow @@ -1619,7 +1622,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v endheight = height; int64_t equity = totalposition + totalprofits; - if (equity < (double)totalposition * prices_minmarginpercent(leverage)) + if (equity <= (int64_t)((double)totalposition * prices_minmarginpercent(leverage))) { // we are in loss break; } @@ -1843,15 +1846,15 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) betinfo.liquidationprice = 0; if (betinfo.leverage != 0) {// prevent zero div - betinfo.liquidationprice = betinfo.averageCostbasis - betinfo.averageCostbasis / betinfo.leverage; + betinfo.liquidationprice = betinfo.averageCostbasis - (betinfo.averageCostbasis * (1 - prices_minmarginpercent(betinfo.leverage)))/ betinfo.leverage; } - if (betinfo.equity >= (double)totalposition * prices_minmarginpercent(betinfo.leverage)) + if (betinfo.equity > (int64_t)((double)totalposition * prices_minmarginpercent(betinfo.leverage))) betinfo.isRekt = false; else { betinfo.isRekt = true; - betinfo.exitfee = totalposition / 500; + betinfo.exitfee = (totalposition - betinfo.equity) / 10; // was: totalposition / 500 } mpz_clear(mpzTotalPosition); From 7e57b812a8350fe99055427b5b3caa286bad8fbb Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 01:58:06 +0500 Subject: [PATCH 73/87] added zero prices err code --- src/cc/prices.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 043882ef3..7691dfb24 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1013,7 +1013,7 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t errcode = -1; if (pricestack[depth] == 0) - errcode = -1; + errcode = -14; depth++; break; @@ -1204,7 +1204,10 @@ int64_t prices_syntheticprice(std::vector vec, int32_t height, int32_t std::cerr << "prices_syntheticprice overflow in price" << std::endl; return errcode; } - + if (errcode == -14) { + std::cerr << "prices_syntheticprice price is zero, not enough historic data yet" << std::endl; + return errcode; + } if (den == 0) { std::cerr << "prices_syntheticprice den==0 return err=-11" << std::endl; return(-11); From 1a5f486916b1a53d06e9a98820ff6c5d57953753 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 02:36:46 +0500 Subject: [PATCH 74/87] logging added for cb --- src/cc/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 7691dfb24..a9aef8722 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1257,11 +1257,11 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t if (minmax) { // if we are within day window, set temp costbasis to max (or min) price value if (leverage > 0 && price > costbasis) { costbasis = price; // set temp costbasis - //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } else if (leverage < 0 && (costbasis == 0 || price < costbasis)) { costbasis = price; - //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } //else { //-> use the previous value // std::cerr << "prices_syntheticprofits() unchanged costbasis=" << costbasis << " price=" << price << " leverage=" << leverage << std::endl; @@ -1273,7 +1273,7 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t //costbasis = price; // use calculated minmax costbasis - //std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; + std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; //} } From 2117941bb3d0dbf0cbf5567e97579963eb4ac99f Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 02:58:34 +0500 Subject: [PATCH 75/87] corr start scan h --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index a9aef8722..463732e7c 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1599,7 +1599,7 @@ int32_t prices_scanchain(std::vector &bets, int16_t leverage, std::v return -1; bool stop = false; - for (int32_t height = bets[0].firstheight; ; height++) // the last datum for 24h is the costbasis value + for (int32_t height = bets[0].firstheight+1; ; height++) // the last datum for 24h is the costbasis value { int64_t totalposition = 0; int64_t totalprofits = 0; From 6a23133b4226d094834b04401b9479f533adda06 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 03:04:29 +0500 Subject: [PATCH 76/87] removed extra logging --- src/cc/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 463732e7c..380af303e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1257,11 +1257,11 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t if (minmax) { // if we are within day window, set temp costbasis to max (or min) price value if (leverage > 0 && price > costbasis) { costbasis = price; // set temp costbasis - std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } else if (leverage < 0 && (costbasis == 0 || price < costbasis)) { costbasis = price; - std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; + //std::cerr << "prices_syntheticprofits() minmax costbasis=" << costbasis << std::endl; } //else { //-> use the previous value // std::cerr << "prices_syntheticprofits() unchanged costbasis=" << costbasis << " price=" << price << " leverage=" << leverage << std::endl; @@ -1273,7 +1273,7 @@ int32_t prices_syntheticprofits(int64_t &costbasis, int32_t firstheight, int32_t //costbasis = price; // use calculated minmax costbasis - std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; + //std::cerr << "prices_syntheticprofits() use permanent costbasis=" << costbasis << " at height=" << height << std::endl; //} } From 8531278b3477cfdcf9d3af4db149d914a5798974 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 03:13:01 +0500 Subject: [PATCH 77/87] corr isrekt to true for'R' funcid --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 380af303e..803de625c 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1944,7 +1944,7 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); /// mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[2].nValue - myfee - txfee, pricespk)); // change - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(true, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee)); return(prices_rawtxresult(result, rawtx, 0)); } else From 0816d0e7c24e7042e7616c856af6a0c3db0a9cd9 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 03:27:56 +0500 Subject: [PATCH 78/87] returned scan chain for finalized for getinfo to work --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 803de625c..defcc567a 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1782,7 +1782,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) betinfo.isRekt = (funcId == 'R'); - return 0; + // return 0; } else return -6; From 3e43396242f2e22d379aa23ba31816f04c2e1cb8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 03:31:37 +0500 Subject: [PATCH 79/87] added check for isrekt set by final tx --- src/cc/prices.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index defcc567a..0fb23ac0e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1775,12 +1775,12 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) CTransaction finaltx; uint256 hashBlock; vscript_t vopret; - if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && PricesCheckOpret(finaltx, vopret) != 0) { + if (myGetTransaction(finaltxid, finaltx, hashBlock) && finaltx.vout.size() > 0 && PricesCheckOpret(finaltx, vopret) != 0) { uint8_t funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, betinfo.txid, betinfo.pk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, betinfo.exitfee); if (funcId == 0) return -3; - betinfo.isRekt = (funcId == 'R'); + betinfo.isRekt = (funcId == 'R'); // return 0; } @@ -1849,15 +1849,18 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) betinfo.liquidationprice = 0; if (betinfo.leverage != 0) {// prevent zero div - betinfo.liquidationprice = betinfo.averageCostbasis - (betinfo.averageCostbasis * (1 - prices_minmarginpercent(betinfo.leverage)))/ betinfo.leverage; + betinfo.liquidationprice = betinfo.averageCostbasis - (betinfo.averageCostbasis * (1 - prices_minmarginpercent(betinfo.leverage))) / betinfo.leverage; } - if (betinfo.equity > (int64_t)((double)totalposition * prices_minmarginpercent(betinfo.leverage))) - betinfo.isRekt = false; - else - { - betinfo.isRekt = true; - betinfo.exitfee = (totalposition - betinfo.equity) / 10; // was: totalposition / 500 + if (!betinfo.isRekt) { // not set by check for final tx + + if (betinfo.equity > (int64_t)((double)totalposition * prices_minmarginpercent(betinfo.leverage))) + betinfo.isRekt = false; + else + { + betinfo.isRekt = true; + betinfo.exitfee = (totalposition - betinfo.equity) / 10; // was: totalposition / 500 + } } mpz_clear(mpzTotalPosition); From 18452ff4a9453707a0c9f641b5ce3da122c8d598 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 18:13:47 +0500 Subject: [PATCH 80/87] updated rekt fee calculation --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 0fb23ac0e..01e43cada 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1859,7 +1859,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo) else { betinfo.isRekt = true; - betinfo.exitfee = (totalposition - betinfo.equity) / 10; // was: totalposition / 500 + betinfo.exitfee = (int64_t)(((double)totalposition * prices_minmarginpercent(betinfo.leverage)) / 10); // was: totalposition / 500 } } From c054a5abdcb7adb319aaf5066310037207e5cb05 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 18:34:26 +0500 Subject: [PATCH 81/87] logging added --- src/cc/prices.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 01e43cada..1d33c257e 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2206,11 +2206,11 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t if (upperquote == "BTC" || bottomquote == "BTC") { // it is relatively btc if (upperquote == "BTC" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "BTC" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup returns true for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns true for BTC for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return true; } else { - std::cerr << "prices_ispositionup returns false for BTC for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns false for BTC for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return false; } } @@ -2218,18 +2218,18 @@ static bool prices_ispositionup(const std::vector &vecparsed, int16_t if (upperquote == "USD" || bottomquote == "USD") { // it is relatively usd if (upperquote == "USD" && (leverage > 0 && !isInverted || leverage < 0 && isInverted) || bottomquote == "USD" && (leverage < 0 && !isInverted || leverage > 0 && isInverted)) { - std::cerr << "prices_ispositionup returns true for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns true for USD for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return true; } else { - std::cerr << "prices_ispositionup returns false for USD for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns false for USD for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return false; } } } } } - std::cerr << "prices_ispositionup returns false for expr=" << prices_getsourceexpression(vecparsed) << std::endl; + std::cerr << "prices_ispositionup returns false for expr=" << prices_getsourceexpression(vecparsed) << " lev=" << leverage << std::endl; return false; } From e1bd5163cb9b75c77cddb628906e28258cd52c3f Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 19:14:10 +0500 Subject: [PATCH 82/87] added lastprice to orderbook print --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 1d33c257e..10ecd246f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -2450,10 +2450,10 @@ UniValue PricesGetOrderbook() entry.push_back(Pair("isOpen", (m.second[i].isOpen ? 1 : 0 ))); entry.push_back(Pair("expression", prices_getsourceexpression(m.second[i].vecparsed))); entry.push_back(Pair("positionsize", totalPos)); - entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); entry.push_back(Pair("leverage", m.second[i].leverage)); + entry.push_back(Pair("costbasis", m.second[i].averageCostbasis)); + entry.push_back(Pair("lastprice", m.second[i].lastprice)); entry.push_back(Pair("equity", m.second[i].equity)); - //std::cerr << "PricesGetOrderbook 1 m.second[i].isUp=" << m.second[i].isUp << " i=" << i << std::endl; entry.push_back(Pair("isUpPosition", (m.second[i].isUp ? 1 : 0))); resbook.push_back(entry); } From 669645b4d4e160484252f73bd6952b456ec5c0ec Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 18 May 2019 21:11:10 +0500 Subject: [PATCH 83/87] corr rekt fee for big leverages --- src/cc/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 10ecd246f..246d02329 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -614,7 +614,7 @@ double prices_minmarginpercent(int16_t leverage) if (absleverage < 100) return (absleverage * 0.080808 + 1.9191919) / 100.0; else - return 0.01; + return 0.1; } From 661fe3457181241489a13479eb1cf773e81423d5 Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 21 May 2019 20:56:30 +0500 Subject: [PATCH 84/87] added pow to rekt txid --- src/cc/prices.cpp | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 246d02329..be4d68e7d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -202,18 +202,20 @@ uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKe return(0); } -CScript prices_finalopret(bool isRekt, uint256 bettxid, CPubKey pk, int32_t lastheight, int64_t costbasis, int64_t lastprice, int64_t liquidationprice, int64_t equity, int64_t exitfee) +CScript prices_finalopret(bool isRekt, uint256 bettxid, CPubKey pk, int32_t lastheight, int64_t costbasis, int64_t lastprice, int64_t liquidationprice, int64_t equity, int64_t exitfee, uint32_t nonce) { CScript opret; - opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'R' : 'F') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee); + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << (isRekt ? 'R' : 'F') << bettxid << pk << lastheight << costbasis << lastprice << liquidationprice << equity << exitfee << nonce); return(opret); } uint8_t prices_finalopretdecode(CScript scriptPubKey, uint256 &bettxid, CPubKey &pk, int32_t &lastheight, int64_t &costbasis, int64_t &lastprice, int64_t &liquidationprice, int64_t &equity, int64_t &exitfee) { std::vector vopret; uint8_t e,f; + uint32_t nonce; + GetOpReturnData(scriptPubKey,vopret); - if (vopret.size() > 2 && E_UNMARSHAL(vopret, ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> lastheight; ss >> costbasis; ss >> lastprice; ss >> liquidationprice; ss >> equity; ss >> exitfee) != 0 && e == EVAL_PRICES && (f == 'F' || f == 'R')) + if (vopret.size() > 2 && E_UNMARSHAL(vopret, ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> lastheight; ss >> costbasis; ss >> lastprice; ss >> liquidationprice; ss >> equity; ss >> exitfee; if (!ss.eof()) ss >> nonce; ) != 0 && e == EVAL_PRICES && (f == 'F' || f == 'R')) { return(f); } @@ -1947,8 +1949,35 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); /// mtx.vout.push_back(MakeCC1vout(cp->evalcode, bettx.vout[2].nValue - myfee - txfee, pricespk)); // change - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(true, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee)); - return(prices_rawtxresult(result, rawtx, 0)); + + // make some PoW to get txid=0x00.....00 to 'faucet' rekts + fprintf(stderr, "start PoW at %u\n", (uint32_t)time(NULL)); + uint32_t nonce = rand() & 0xfffffff; + for (int i = 0; i<1000000; i++, nonce++) + { + CMutableTransaction tmpmtx = mtx; + int32_t len; + uint8_t txbuf[32768]; + + rawtx = FinalizeCCTx(0, cp, tmpmtx, mypk, txfee, prices_finalopret(true, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee, nonce)); + if ((len = (int32_t)rawtx.size()) > 0 && len < sizeof(txbuf) / sizeof(txbuf[0]) * 2) + { + len >>= 1; // sizeof hex divide by 2 + decode_hex(txbuf, len, (char *)rawtx.c_str()); + bits256 hash = bits256_doublesha256(0, txbuf, len); + if ((hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0) + { + fprintf(stderr, "found valid txid after %d iterations %u\n", i, (uint32_t)time(NULL)); + return(prices_rawtxresult(result, rawtx, 0)); + } + //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]); + } + } + fprintf(stderr, "couldnt generate valid txid %u\n", (uint32_t)time(NULL)); + + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "could not generate valid txid")); + return(result); } else { @@ -2031,7 +2060,7 @@ UniValue PricesCashout(int64_t txfee, uint256 bettxid) if (CCchange >= txfee) mtx.vout.push_back(MakeCC1vout(cp->evalcode, CCchange, pricespk)); // TODO: what should the opret param be: - rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, nextheight-1, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, txfee)); + rawtx = FinalizeCCTx(0, cp, mtx, mypk, txfee, prices_finalopret(false, bettxid, mypk, nextheight-1, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, txfee, 0)); return(prices_rawtxresult(result, rawtx, 0)); } From bf3874ef765033955e4d38c8f0c5dd5c442f9bd8 Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 21 May 2019 21:21:52 +0500 Subject: [PATCH 85/87] simplified rekt hash check --- src/cc/prices.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index be4d68e7d..5ab1ff12f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1956,22 +1956,24 @@ UniValue PricesRekt(int64_t txfee, uint256 bettxid, int32_t rektheight) for (int i = 0; i<1000000; i++, nonce++) { CMutableTransaction tmpmtx = mtx; - int32_t len; - uint8_t txbuf[32768]; + //int32_t len; + //uint8_t txbuf[32768]; rawtx = FinalizeCCTx(0, cp, tmpmtx, mypk, txfee, prices_finalopret(true, bettxid, mypk, betinfo.lastheight, betinfo.averageCostbasis, betinfo.lastprice, betinfo.liquidationprice, betinfo.equity, myfee, nonce)); - if ((len = (int32_t)rawtx.size()) > 0 && len < sizeof(txbuf) / sizeof(txbuf[0]) * 2) - { - len >>= 1; // sizeof hex divide by 2 - decode_hex(txbuf, len, (char *)rawtx.c_str()); - bits256 hash = bits256_doublesha256(0, txbuf, len); - if ((hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0) + //if ((len = (int32_t)rawtx.size()) > 0 && len < sizeof(txbuf) / sizeof(txbuf[0]) * 2) + //{ + // len >>= 1; // sizeof hex divide by 2 + //decode_hex(txbuf, len, (char *)rawtx.c_str()); + //bits256 hash = bits256_doublesha256(0, txbuf, len); + uint256 hash = tmpmtx.GetHash(); + //if ((hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0) + if ((hash.begin()[0] & 0xff) == 0 && (hash.begin()[31] & 0xff) == 0) { fprintf(stderr, "found valid txid after %d iterations %u\n", i, (uint32_t)time(NULL)); return(prices_rawtxresult(result, rawtx, 0)); } //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]); - } + //} } fprintf(stderr, "couldnt generate valid txid %u\n", (uint32_t)time(NULL)); From 74c29d0df7a5d5ed0ec3460cad1e21886ad56100 Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 21 May 2019 22:12:46 +0500 Subject: [PATCH 86/87] added validation for rekt txid --- src/cc/prices.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 5ab1ff12f..218211324 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -412,9 +412,14 @@ static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTrans } vscript_t opret; - if (prices_finalopretdecode(finaltx.vout.back().scriptPubKey, bettxid, pk, lastheight, costbasis, lastprice, liquidationprice, equity, fee) == 0) + uint8_t funcId; + if ((funcId = prices_finalopretdecode(finaltx.vout.back().scriptPubKey, bettxid, pk, lastheight, costbasis, lastprice, liquidationprice, equity, fee)) == 0) return eval->Invalid("cannot decode opreturn for final tx"); + // check rekt txid mining: + if( funcId == 'R' && (finaltx.GetHash().begin()[0] != 0 || finaltx.GetHash().begin()[31] != 0) ) + return eval->Invalid("incorrect rekt txid"); + if (bettx.GetHash() != bettxid) return eval->Invalid("incorrect bettx id"); From 9553165287264dd6b24abaaeea8f07d5af40fbdc Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 21 May 2019 23:40:29 +0500 Subject: [PATCH 87/87] remove rekt txid validation --- src/cc/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 218211324..2c0a33b20 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -417,8 +417,8 @@ static bool ValidateFinalTx(struct CCcontract_info *cp, Eval *eval, const CTrans return eval->Invalid("cannot decode opreturn for final tx"); // check rekt txid mining: - if( funcId == 'R' && (finaltx.GetHash().begin()[0] != 0 || finaltx.GetHash().begin()[31] != 0) ) - return eval->Invalid("incorrect rekt txid"); +// if( funcId == 'R' && (finaltx.GetHash().begin()[0] != 0 || finaltx.GetHash().begin()[31] != 0) ) +// return eval->Invalid("incorrect rekt txid"); if (bettx.GetHash() != bettxid) return eval->Invalid("incorrect bettx id");