diff --git a/src/cc/CCassetsCore.cpp b/src/cc/CCassetsCore.cpp index e837b0fb2..88ebf0feb 100644 --- a/src/cc/CCassetsCore.cpp +++ b/src/cc/CCassetsCore.cpp @@ -185,13 +185,13 @@ bool SetAssetOrigpubkey(std::vector &origpubkey,uint64_t &price,const C else return(false); } -bool GetAssetorigaddrs(char *CCaddr,char *destaddr,const CTransaction& tx) +bool GetAssetorigaddrs(struct CCcontract_info *cp,char *CCaddr,char *destaddr,const CTransaction& tx) { uint256 assetid,assetid2; uint64_t price,nValue=0; int32_t n; uint8_t funcid; std::vector origpubkey; CScript script; n = tx.vout.size(); if ( n == 0 || (funcid= DecodeAssetOpRet(tx.vout[n-1].scriptPubKey,assetid,assetid2,price,origpubkey)) == 0 ) return(false); - if ( GetCCaddress(EVAL_ASSETS,CCaddr,pubkey2pk(origpubkey)) != 0 && Getscriptaddress(destaddr,CScript() << origpubkey << OP_CHECKSIG) != 0 ) + if ( GetCCaddress(cp,CCaddr,pubkey2pk(origpubkey)) != 0 && Getscriptaddress(destaddr,CScript() << origpubkey << OP_CHECKSIG) != 0 ) return(true); else return(false); } diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 29fb299c0..d961d1713 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -15,11 +15,11 @@ #include "CCassets.h" -uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) +uint64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint64_t total,int32_t maxinputs) { char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_ASSETS,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); //std::sort(unspentOutputs.begin(), unspentOutputs.end(), heightSort); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) @@ -44,13 +44,15 @@ uint64_t AddAssetInputs(CMutableTransaction &mtx,CPubKey pk,uint256 assetid,uint uint64_t GetAssetBalance(CPubKey pk,uint256 tokenid) { - CMutableTransaction mtx; - return(AddAssetInputs(mtx,pk,tokenid,0,0)); + CMutableTransaction mtx; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); + return(AddAssetInputs(cp,mtx,pk,tokenid,0,0)); } UniValue AssetOrders(uint256 refassetid) { - uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; + uint64_t price; uint256 txid,hashBlock,assetid,assetid2; std::vector origpubkey; CTransaction vintx; UniValue result(UniValue::VARR); std::vector > unspentOutputs; uint8_t funcid; char funcidstr[16],origaddr[64],assetidstr[65]; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); SetCCunspents(unspentOutputs,(char *)AssetsCCaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { @@ -71,7 +73,7 @@ UniValue AssetOrders(uint256 refassetid) else item.push_back(Pair("askamount",(double)vintx.vout[0].nValue)); if ( origpubkey.size() == 33 ) { - GetCCaddress(EVAL_ASSETS,origaddr,pubkey2pk(origpubkey)); + GetCCaddress(cp,origaddr,pubkey2pk(origpubkey)); item.push_back(Pair("origaddress",origaddr)); } if ( assetid != zeroid ) @@ -93,7 +95,8 @@ UniValue AssetOrders(uint256 refassetid) std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std::string description) { - CMutableTransaction mtx; CPubKey mypk; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( name.size() > 32 || description.size() > 4096 ) { fprintf(stderr,"name.%d or description.%d is too big\n",(int32_t)name.size(),(int32_t)description.size()); @@ -106,14 +109,15 @@ std::string CreateAsset(uint64_t txfee,uint64_t assetsupply,std::string name,std { mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,assetsupply,mypk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(AssetsCChexstr) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetCreateOpRet('c',Mypubkey(),name,description))); } return(0); } std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector destpubkey,uint64_t total) { - CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; //int32_t i,n; + CMutableTransaction mtx; CPubKey mypk; uint64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -124,7 +128,7 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de { for (i=0; i 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,total,60)) > 0 ) { if ( inputs > total ) CCchange = (inputs - total); @@ -132,7 +136,7 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,total,pubkey2pk(destpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); } else fprintf(stderr,"not enough CC asset inputs for %.8f\n",(double)total/COIN); //} else fprintf(stderr,"numoutputs.%d != numamounts.%d\n",n,(int32_t)amounts.size()); } @@ -141,29 +145,31 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; + CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,bidamount+txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount,GetUnspendable(EVAL_ASSETS,0))); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount,GetUnspendable(cp,0))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('b',assetid,zeroid,pricetotal,Mypubkey()))); } return(0); } std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) { - CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; + CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) { - if ( (inputs= AddAssetInputs(mtx,mypk,assetid,askamount,60)) > 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); if ( inputs > askamount ) CCchange = (inputs - askamount); if ( CCchange != 0 ) @@ -171,7 +177,7 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 if ( assetid2 == zeroid ) opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); else opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } return(0); @@ -179,7 +185,8 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t bidamount; CPubKey mypk; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t bidamount; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -190,7 +197,7 @@ std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) bidamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(bidtxid,0,CScript())); mtx.vout.push_back(CTxOut(bidamount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('o',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -198,7 +205,8 @@ std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid) std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) { - CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t askamount; CPubKey mypk; + CMutableTransaction mtx; CTransaction vintx; uint256 hashBlock; uint64_t askamount; CPubKey mypk; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -209,7 +217,7 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) askamount = vintx.vout[0].nValue; mtx.vin.push_back(CTxIn(asktxid,0,CScript())); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,mypk)); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('x',assetid,zeroid,0,Mypubkey()))); } } return(0); @@ -217,7 +225,8 @@ std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid) std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount) { - CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; + CTransaction vintx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t bidvout=0; uint64_t origprice,bidamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -228,18 +237,18 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t bidamount = vintx.vout[bidvout].nValue; SetAssetOrigpubkey(origpubkey,origprice,vintx); mtx.vin.push_back(CTxIn(bidtxid,bidvout,CScript())); - if ( (inputs= AddAssetInputs(mtx,mypk,assetid,fillamount,60)) > 0 ) + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,fillamount,60)) > 0 ) { if ( inputs > fillamount ) CCchange = (inputs - fillamount); SetAssetFillamounts(0,paid_amount,remaining_required,bidamount,fillamount,origprice); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,bidamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(CTxOut(paid_amount,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet('B',assetid,zeroid,remaining_required,origpubkey))); } else fprintf(stderr,"filltx wasnt for assetid\n"); } } @@ -248,7 +257,8 @@ std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount) { - CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t totalunits,askamount,paid_amount,remaining_required,inputs,CCchange=0; + CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector origpubkey; int32_t askvout=0; uint64_t totalunits,askamount,paid_amount,remaining_required,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -260,20 +270,20 @@ std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 ask SetAssetOrigpubkey(origpubkey,totalunits,vintx); mtx.vin.push_back(CTxIn(asktxid,askvout,CScript())); if ( assetid2 == zeroid ) - inputs = AddAssetInputs(mtx,mypk,assetid2,fillamount,60); + inputs = AddAssetInputs(cp,mtx,mypk,assetid2,fillamount,60); else inputs = AddNormalinputs(mtx,mypk,fillamount,60); if ( inputs > 0 ) { if ( assetid2 == zeroid && inputs > fillamount ) CCchange = (inputs - fillamount); SetAssetFillamounts(1,paid_amount,remaining_required,askamount,fillamount,totalunits); - mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(EVAL_ASSETS,0))); + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount - paid_amount,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_amount,mypk)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,fillamount,pubkey2pk(origpubkey))); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); fprintf(stderr,"remaining %llu -> origpubkey\n",(long long)remaining_required); - return(FinalizeCCTx(EVAL_ASSETS,mtx,mypk,txfee,EncodeAssetOpRet(assetid2==zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); + return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2==zeroid?'E':'S',assetid,assetid2,remaining_required,origpubkey))); } else fprintf(stderr,"filltx not enough utxos\n"); } } diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 539a9b123..1b262f301 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -47,7 +47,7 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector pubkey); -bool GetCCaddress(uint8_t evalcode,char *destaddr,CPubKey pk); +bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk); bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,uint64_t nValue); bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t numvins,int32_t preventCCvouts,int32_t numvouts); bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 5a8b3607a..b37e6f061 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -38,7 +38,7 @@ bool SignTx(CMutableTransaction &mtx,int32_t vini,uint64_t utxovalue,const CScri #endif } -std::string FinalizeCCTx(uint8_t evalcode,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) +std::string FinalizeCCTx(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret) { auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); CTransaction vintx; std::string hex; uint256 hashBlock; uint64_t vinimask=0,utxovalues[64],change,totaloutputs=0,totalinputs=0; int32_t i,utxovout,n,err = 0; char myaddr[64],destaddr[64],unspendable[64]; uint8_t *privkey,myprivkey[32],unspendablepriv[32],*msg32 = 0; CC *mycond=0,*othercond=0,*cond; CPubKey unspendablepk; @@ -54,11 +54,11 @@ std::string FinalizeCCTx(uint8_t evalcode,CMutableTransaction &mtx,CPubKey mypk, return(0); } Myprivkey(myprivkey); - unspendablepk = GetUnspendable(evalcode,unspendablepriv); - GetCCaddress(evalcode,myaddr,mypk); - mycond = MakeCCcond1(evalcode,mypk); - GetCCaddress(evalcode,unspendable,unspendablepk); - othercond = MakeCCcond1(evalcode,unspendablepk); + unspendablepk = GetUnspendable(cp,unspendablepriv); + GetCCaddress(cp,myaddr,mypk); + mycond = MakeCCcond1(cp->evalcode,mypk); + GetCCaddress(cp,unspendable,unspendablepk); + othercond = MakeCCcond1(cp->evalcode,unspendablepk); //fprintf(stderr,"myCCaddr.(%s) %p vs unspendable.(%s) %p\n",myaddr,mycond,unspendable,othercond); memset(utxovalues,0,sizeof(utxovalues)); for (i=0; ievalcode,pk)) != 0 ) { Getscriptaddress(destaddr,CCPubKey(payoutCond)); cc_free(payoutCond); diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 24cd6f612..0882e0a6b 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -120,7 +120,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu { char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_FAUCET,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { @@ -148,7 +148,7 @@ std::string FaucetGet(uint64_t txfee) cp = CCinit(&C,EVAL_FAUCET); if ( txfee == 0 ) txfee = 10000; - faucetpk = GetUnspendable(EVAL_FAUCET,0); + faucetpk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); if ( (inputs= AddFaucetInputs(cp,mtx,faucetpk,nValue+txfee,60)) > 0 ) { @@ -157,22 +157,23 @@ std::string FaucetGet(uint64_t txfee) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,CCchange,faucetpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_FAUCET,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"cant find faucet inputs\n"); return(0); } std::string FaucetFund(uint64_t txfee,uint64_t funds) { - CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; + CMutableTransaction mtx; CPubKey mypk,faucetpk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FAUCET); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); - faucetpk = GetUnspendable(EVAL_FAUCET,0); + faucetpk = GetUnspendable(cp,0); if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,funds,faucetpk)); - return(FinalizeCCTx(EVAL_FAUCET,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } return(0); } diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index b08cd6bd7..6dfae6839 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -24,12 +24,12 @@ uint64_t RewardsCalc(uint64_t claim,uint256 txid) return(reward); } -uint64_t IsRewardsvout(const CTransaction& tx,int32_t v) +uint64_t IsRewardsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,RewardsCCaddr) == 0 ) + if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) return(tx.vout[v].nValue); } return(0); @@ -51,7 +51,7 @@ bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransactio { if ( hashBlock == zerohash ) return eval->Invalid("cant rewards from mempool"); - if ( (assetoshis= IsRewardsvout(vinTx,tx.vin[i].prevout.n)) != 0 ) + if ( (assetoshis= IsRewardsvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) inputs += assetoshis; } } @@ -59,7 +59,7 @@ bool RewardsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransactio for (i=0; i origpubkey; CTransaction vintx; int32_t n = 0; std::vector > unspentOutputs; - GetCCaddress(EVAL_REWARDS,coinaddr,pk); + GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsRewardsvout(vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsRewardsvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); @@ -150,50 +150,13 @@ uint64_t AddRewardsInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int return(totalinputs); } -// 0.834% every 60 days, min 100, capped at 0.834% - -std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) -{ - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - rewardspk = GetUnspendable(EVAL_REWARDS,0); - if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,funds,rewardspk)); - mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); - } - return(0); -} - -std::string RewardsLock(uint64_t txfee,uint64_t amount) -{ - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; - if ( txfee == 0 ) - txfee = 10000; - rewardspk = GetUnspendable(EVAL_REWARDS,0); - mypk = pubkey2pk(Mypubkey()); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,amount,rewardspk)); - // specify destination pubkey, funding txid - //opret = ;// - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); - } else fprintf(stderr,"cant find rewards inputs\n"); - return(0); -} - std::string RewardsUnlock(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0; + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; - rewardspk = GetUnspendable(EVAL_REWARDS,0); + rewardspk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); if ( (claim= AddRewardsInputs(mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee ) { @@ -204,9 +167,50 @@ std::string RewardsUnlock(uint64_t txfee) if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,CCchange,rewardspk)); mtx.vout.push_back(CTxOut(claim+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } } else fprintf(stderr,"cant find rewards inputs\n"); return(0); } +// 0.834% every 60 days, min 100, capped at 0.834% + +std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + rewardspk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,funds,rewardspk)); + mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG)); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } + return(0); +} + +std::string RewardsLock(uint64_t txfee,uint64_t amount) +{ + CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); + if ( txfee == 0 ) + txfee = 10000; + rewardspk = GetUnspendable(cp,0); + mypk = pubkey2pk(Mypubkey()); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(EVAL_REWARDS,amount,rewardspk)); + // specify destination pubkey, funding txid + //opret = ;// + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"cant find rewards inputs\n"); + return(0); +} + + diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ac202298d..5d92a585f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4846,21 +4846,22 @@ int32_t ensure_CCrequirements() UniValue rewardsaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_REWARDS); if ( fHelp || params.size() > 1 ) throw runtime_error("rewardsaddress [pubkey]\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"); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("RewardsCCaddress",destaddr)); if ( params.size() == 1 ) { pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_REWARDS,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); } @@ -4935,21 +4936,22 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp) UniValue faucetaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FAUCET); if ( fHelp || params.size() > 1 ) throw runtime_error("faucetaddress [pubkey]\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"); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("FaucetCCaddress",destaddr)); if ( params.size() == 1 ) { pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_FAUCET,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); } @@ -5002,7 +5004,8 @@ UniValue tokenorders(const UniValue& params, bool fHelp) UniValue tokenbalance(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char destaddr[64]; uint256 tokenid; uint64_t balance; std::vector pubkey; + UniValue result(UniValue::VOBJ); char destaddr[64]; uint256 tokenid; uint64_t balance; std::vector pubkey; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( fHelp || params.size() > 2 ) throw runtime_error("tokenbalance tokenid [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) @@ -5012,7 +5015,7 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) pubkey = ParseHex(params[1].get_str().c_str()); else pubkey = Mypubkey(); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); balance = GetAssetBalance(pubkey2pk(pubkey),tokenid); result.push_back(Pair("tokenid", params[0].get_str())); @@ -5022,21 +5025,22 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) UniValue tokenaddress(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; + UniValue result(UniValue::VOBJ); std::vector pubkey; char destaddr[64]; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_ASSETS); if ( fHelp || params.size() > 1 ) throw runtime_error("tokenaddress [pubkey]\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"); result.push_back(Pair("result", "success")); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("AssetsCCaddress",destaddr)); if ( params.size() == 1 ) { pubkey = ParseHex(params[0].get_str().c_str()); - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(pubkey)) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) result.push_back(Pair("CCaddress",destaddr)); } - if ( GetCCaddress(EVAL_ASSETS,destaddr,pubkey2pk(Mypubkey())) != 0 ) + if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 ) result.push_back(Pair("myCCaddress",destaddr)); return(result); }