diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index f88a5fe55..a32264a88 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -40,7 +40,7 @@ bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx // CCcustom UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector synthetic); UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount); -UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid); +UniValue PricesSetcostbasis(int64_t txfee,uint256 bettxid); UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight); UniValue PricesCashout(uint64_t txfee,uint256 bettxid); UniValue PricesInfo(uint256 bettxid,int32_t refheight); diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index a8542df6b..525a42cda 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -362,12 +362,13 @@ int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t m pricestack[depth] = 0; if ( prices_extract(pricedata,height,1,value) == 0 ) { - // push to the prices stack + // push price to the prices stack if ( minmax == 0 ) - pricestack[depth] = pricedata[2]; + pricestack[depth] = pricedata[2]; // use smoothed value if we are over 24h else { - if ( leverage > 0 ) + // if we are within 24h use min or max price + if ( leverage > 0 ) pricestack[depth] = (pricedata[1] > pricedata[2]) ? pricedata[1] : pricedata[2]; // MAX else pricestack[depth] = (pricedata[1] < pricedata[2]) ? pricedata[1] : pricedata[2]; // MIN @@ -495,20 +496,29 @@ int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t m int64_t prices_syntheticprofits(int64_t &costbasis,int32_t firstheight,int32_t height,int16_t leverage,std::vector vec,int64_t positionsize,int64_t addedbets) { - int64_t price,profits = 0; int32_t minmax; - minmax = (height > firstheight+PRICES_DAYWINDOW); - if ( (price= prices_syntheticprice(vec,height,minmax,leverage)) < 0 ) - { - fprintf(stderr,"unexpected zero synthetic price at height.%d\n",height); - return(0); - } - if ( minmax != 0 ) - { - if ( leverage > 0 && price > costbasis ) - costbasis = price; - else if ( leverage < 0 && (costbasis == 0 || price < costbasis) ) - costbasis = price; + int64_t price, profits = 0; + + if (firstheight >= 0) { // >=0 means request to find costbase + int32_t minmax = (height < firstheight + PRICES_DAYWINDOW); // use minmax value if we are within 24h + + if ((price = prices_syntheticprice(vec, height, minmax, leverage)) < 0) + { + fprintf(stderr, "unexpected zero synthetic price at height.%d\n", height); + return(0); + } + if (minmax /*!= 0*/) // if we are within day window, use bigger or lesser value + { + if (leverage > 0 && price > costbasis) + costbasis = price; // set costbasis + else if (leverage < 0 && (costbasis == 0 || price < costbasis)) + costbasis = price; + // else -> use the previous value + } + else + costbasis = price; //?? } + // else < 0 then use the passed costbase + profits = costbasis > 0 ? ((price * SATOSHIDEN) / costbasis) - SATOSHIDEN : 0; profits *= leverage * positionsize; return(positionsize + addedbets + profits); @@ -567,10 +577,10 @@ UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector= amount+5*txfee ) { betamount = (amount * 199) / 200; - mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); // marker mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,mypk)); // baton for total funding mtx.vout.push_back(MakeCC1vout(cp->evalcode,(amount-betamount)+2*txfee,pricespk)); mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,betamount,pricespk,mypk)); + mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); // marker rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_betopret(mypk,nextheight-1,amount,leverage,firstprice,vec,zeroid)); return(prices_rawtxresult(result,rawtx,0)); } @@ -612,7 +622,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) return(result); } -UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) +UniValue PricesSetcostbasis(int64_t txfee,uint256 bettxid) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); @@ -626,12 +636,12 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) { if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) { - addedbets = prices_batontxid(batontxid,bettx,bettxid); + addedbets = prices_batontxid(batontxid, bettx, bettxid); mtx.vin.push_back(CTxIn(bettxid,1,CScript())); - for (i=0; ievalcode,bettx.vout[1].nValue-myfee-txfee,pricespk)); @@ -669,7 +680,7 @@ UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight) { costbasis = prices_costbasis(bettx); addedbets = prices_batontxid(batontxid,bettx,bettxid); - if ( (profits= prices_syntheticprofits(ignore,firstheight,rektheight,leverage,vec,positionsize,addedbets)) < 0 ) + if ( (profits= prices_syntheticprofits(costbasis /*ignore*/, -1, rektheight, leverage, vec, positionsize, addedbets)) < 0 ) { myfee = (positionsize + addedbets) / 500; } diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 581676763..d132dabd5 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -465,6 +465,7 @@ static const CRPCCommand vRPCCommands[] = { "prices", "priceslist", &priceslist, true }, { "prices", "pricesinfo", &pricesinfo, true }, { "prices", "pricesbet", &pricesbet, true }, + { "prices", "pricesbet", &pricessetcostbasis, true }, // Pegs { "pegs", "pegsaddress", &pegsaddress, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index 3df78de9f..94d16ecdc 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -498,6 +498,7 @@ extern UniValue paxwithdraw(const UniValue& params, bool fHelp); extern UniValue prices(const UniValue& params, bool fHelp); extern UniValue pricesbet(const UniValue& params, bool fHelp); +extern UniValue pricessetcostbasis(const UniValue& params, bool fHelp); // test rpc: extern UniValue test_ac(const UniValue& params, bool fHelp);