diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 23adb0ffb..236a8652b 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,6 +24,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); +std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); +std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 4c74c6057..6d113b54a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -269,12 +269,24 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,forfeitblocks,entropy,hash); - fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); + //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); + if ( winnings == 0 ) + { + // queue 'L' losing tx + } + else + { + // queue 'W' winning tx + } } } } return eval->Invalid("dont confirm bet during debug"); break; + case 'L': + break; + case 'W': + break; case 'U': //vin.0: locked funds CC vout.0 from lock //vin.1+: funding CC vout.0 from 'F' and 'E' and 'U' @@ -524,6 +536,68 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 return(0); } +std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + sbits = stringbits(planstr); + if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) + { + fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + return(0); + } + sbits = stringbits(planstr); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + +std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + sbits = stringbits(planstr); + if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) + { + fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + return(0); + } + sbits = stringbits(planstr); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index aaa0f2a8e..97ade0db9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5169,6 +5169,44 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } +UniValue dicewinner(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("dicewinner name fundingtxid bettxid\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + name = (char *)params[0].get_str().c_str(); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + bettxid = Parseuint256((char *)params[2].get_str().c_str()); + hex = DiceWinner(0,name,fundingtxid,bettxid); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + return(result); +} + +UniValue diceloser(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("diceloser name fundingtxid bettxid\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + name = (char *)params[0].get_str().c_str(); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + bettxid = Parseuint256((char *)params[2].get_str().c_str()); + hex = DiceLoser(0,name,fundingtxid,bettxid); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + return(result); +} + UniValue dicelist(const UniValue& params, bool fHelp) { uint256 tokenid;