Rewards cc
This commit is contained in:
@@ -23,8 +23,9 @@
|
||||
|
||||
bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx);
|
||||
|
||||
std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit);
|
||||
std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount);
|
||||
std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid);
|
||||
std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit);
|
||||
std::string RewardsAddfunds(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount);
|
||||
std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount);
|
||||
std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -67,19 +67,19 @@ uint8_t DecodeRewardsFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,ui
|
||||
return(0);
|
||||
}
|
||||
|
||||
CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits)
|
||||
CScript EncodeRewardsOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid)
|
||||
{
|
||||
CScript opret; uint8_t evalcode = EVAL_REWARDS;
|
||||
opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits);
|
||||
opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid);
|
||||
return(opret);
|
||||
}
|
||||
|
||||
uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits)
|
||||
uint8_t DecodeRewardsOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid)
|
||||
{
|
||||
std::vector<uint8_t> vopret; uint8_t *script,e,f;
|
||||
GetOpReturnData(scriptPubKey, vopret);
|
||||
script = (uint8_t *)vopret.data();
|
||||
if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits) != 0 )
|
||||
if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 )
|
||||
{
|
||||
if ( e == EVAL_REWARDS && (f == 'L' || f == 'U') )
|
||||
return(f);
|
||||
@@ -215,25 +215,28 @@ uint64_t RewardsPlanFunds(uint64_t &refsbits,struct CCcontract_info *cp,CPubKey
|
||||
return(totalinputs);
|
||||
}
|
||||
|
||||
std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid)
|
||||
std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid)
|
||||
{
|
||||
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount,inputs,CCchange=0; struct CCcontract_info *cp,C;
|
||||
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t funding,sbits,reward,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C;
|
||||
cp = CCinit(&C,EVAL_REWARDS);
|
||||
if ( txfee == 0 )
|
||||
txfee = 10000;
|
||||
rewardspk = GetUnspendable(cp,0);
|
||||
mypk = pubkey2pk(Mypubkey());
|
||||
funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr);
|
||||
if ( (amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee )
|
||||
funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid);
|
||||
if ( locktxid == zeroid )
|
||||
amount= AddRewardsInputs(cp,mtx,mypk,(1LL << 30),1);
|
||||
//else amount = value...;
|
||||
if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash)) > txfee )
|
||||
{
|
||||
if ( (inputs= AddRewardsInputs(cp,mtx,mypk,reward+amount+txfee,30)) > 0 )
|
||||
{
|
||||
if ( inputs > (amount + reward + 2*txfee) )
|
||||
if ( inputs >= (amount + reward + 2*txfee) )
|
||||
CCchange = (inputs - amount - reward - txfee);
|
||||
if ( CCchange != 0 )
|
||||
mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk));
|
||||
mtx.vout.push_back(CTxOut(amount+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
|
||||
return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits)));
|
||||
return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid)));
|
||||
}
|
||||
fprintf(stderr,"cant find enough rewards inputs\n");
|
||||
}
|
||||
@@ -241,7 +244,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 txid)
|
||||
return(0);
|
||||
}
|
||||
|
||||
std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit)
|
||||
std::string RewardsCreateFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit)
|
||||
{
|
||||
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C;
|
||||
cp = CCinit(&C,EVAL_REWARDS);
|
||||
@@ -249,7 +252,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR
|
||||
txfee = 10000;
|
||||
mypk = pubkey2pk(Mypubkey());
|
||||
rewardspk = GetUnspendable(cp,0);
|
||||
funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr);
|
||||
funding = RewardsPlanFunds(sbits,cp,rewardspk,planstr,zeroid);
|
||||
if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 )
|
||||
{
|
||||
mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,rewardspk));
|
||||
@@ -260,7 +263,7 @@ std::string RewardsFund(uint64_t txfee,char *planstr,uint64_t funds,uint64_t APR
|
||||
return(0);
|
||||
}
|
||||
|
||||
std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount)
|
||||
std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint64_t amount)
|
||||
{
|
||||
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t sbits,funding; struct CCcontract_info *cp,C;
|
||||
cp = CCinit(&C,EVAL_REWARDS);
|
||||
@@ -268,12 +271,12 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint64_t amount)
|
||||
txfee = 10000;
|
||||
mypk = pubkey2pk(Mypubkey());
|
||||
rewardspk = GetUnspendable(cp,0);
|
||||
if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr)) >= amount )
|
||||
if ( (funding= RewardsPlanFunds(sbits,cp,rewardspk,planstr,fundingtxid)) >= amount )
|
||||
{
|
||||
if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 )
|
||||
{
|
||||
mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,rewardspk));
|
||||
return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits)));
|
||||
return(FinalizeCCTx(cp,mtx,mypk,txfee,EncodeRewardsOpRet('L',sbits,fundingtxid)));
|
||||
} else fprintf(stderr,"cant find enough inputs\n");
|
||||
}
|
||||
fprintf(stderr,"cant find rewards inputs\n");
|
||||
|
||||
@@ -354,7 +354,8 @@ static const CRPCCommand vRPCCommands[] =
|
||||
{ "ponzi", "ponziaddress", &ponziaddress, true },
|
||||
|
||||
/* rewards */
|
||||
{ "rewards", "rewardsfund", &rewardsfund, true },
|
||||
{ "rewards", "rewardscreatefunding", &rewardscreatefunding, true },
|
||||
{ "rewards", "rewardsaddfunding", &rewardsaddfunding, true },
|
||||
{ "rewards", "rewardslock", &rewardslock, true },
|
||||
{ "rewards", "rewardsunlock", &rewardsunlock, true },
|
||||
{ "rewards", "rewardsaddress", &rewardsaddress, true },
|
||||
|
||||
@@ -225,7 +225,8 @@ extern UniValue faucetfund(const UniValue& params, bool fHelp);
|
||||
extern UniValue faucetget(const UniValue& params, bool fHelp);
|
||||
extern UniValue faucetaddress(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardsaddress(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardsfund(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardscreatefund(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardsaddfunding(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardslock(const UniValue& params, bool fHelp);
|
||||
extern UniValue rewardsunlock(const UniValue& params, bool fHelp);
|
||||
extern UniValue diceaddress(const UniValue& params, bool fHelp);
|
||||
|
||||
@@ -4957,7 +4957,7 @@ UniValue tokenaddress(const UniValue& params, bool fHelp)
|
||||
return(CCaddress(cp,(char *)"Assets",pubkey));
|
||||
}
|
||||
|
||||
UniValue rewardsfund(const UniValue& params, bool fHelp)
|
||||
UniValue rewardscreatefund(const UniValue& params, bool fHelp)
|
||||
{
|
||||
UniValue result(UniValue::VOBJ); char *name; uint64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex;
|
||||
if ( fHelp || params.size() > 6 || params.size() < 2 )
|
||||
@@ -4995,14 +4995,34 @@ UniValue rewardsfund(const UniValue& params, bool fHelp)
|
||||
|
||||
UniValue rewardslock(const UniValue& params, bool fHelp)
|
||||
{
|
||||
UniValue result(UniValue::VOBJ); char *name; uint64_t amount; std::string hex;
|
||||
if ( fHelp || params.size() > 2 )
|
||||
throw runtime_error("rewardslock name amount\n");
|
||||
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex;
|
||||
if ( fHelp || params.size() != 3 )
|
||||
throw runtime_error("rewardslock name fundingtxid amount\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();
|
||||
amount = atof(params[1].get_str().c_str()) * COIN;
|
||||
hex = RewardsLock(0,name,amount);
|
||||
fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
|
||||
amount = atof(params[2].get_str().c_str()) * COIN;
|
||||
hex = RewardsLock(0,name,fundingtxid,amount);
|
||||
if ( hex.size() > 0 )
|
||||
{
|
||||
result.push_back(Pair("result", "success"));
|
||||
result.push_back(Pair("hex", hex));
|
||||
} else result.push_back(Pair("error", "couldnt create rewards lock transaction"));
|
||||
return(result);
|
||||
}
|
||||
|
||||
UniValue rewardsaddfunding(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("rewardsaddfunding name fundingtxid amount\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());
|
||||
amount = atof(params[2].get_str().c_str()) * COIN;
|
||||
hex = RewardsAddFunds(0,name,fundingtxid,amount);
|
||||
if ( hex.size() > 0 )
|
||||
{
|
||||
result.push_back(Pair("result", "success"));
|
||||
@@ -5013,16 +5033,17 @@ UniValue rewardslock(const UniValue& params, bool fHelp)
|
||||
|
||||
UniValue rewardsunlock(const UniValue& params, bool fHelp)
|
||||
{
|
||||
UniValue result(UniValue::VOBJ); std::string hex; char *name; uint256 txid;
|
||||
if ( fHelp || params.size() > 2 )
|
||||
throw runtime_error("rewardsunlock name [txid]\n");
|
||||
UniValue result(UniValue::VOBJ); std::string hex; char *name; uint256 fundingtxid,txid;
|
||||
if ( fHelp || params.size() > 3 )
|
||||
throw runtime_error("rewardsunlock name fundingtxid [txid]\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();
|
||||
if ( params.size() > 1 )
|
||||
txid = Parseuint256((char *)params[1].get_str().c_str());
|
||||
fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
|
||||
if ( params.size() > 2 )
|
||||
txid = Parseuint256((char *)params[2].get_str().c_str());
|
||||
else memset(&txid,0,sizeof(txid));
|
||||
hex = RewardsUnlock(0,name,txid);
|
||||
hex = RewardsUnlock(0,name,fundingtxid,txid);
|
||||
if ( hex.size() > 0 )
|
||||
{
|
||||
result.push_back(Pair("result", "success"));
|
||||
|
||||
Reference in New Issue
Block a user