Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisend RPC refactoring #68

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 57 additions & 42 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,6 @@ Value autocombinerewards(const Array & params, bool fHelp)
return "Auto Combine Rewards Threshold Set";
}

//presstab
Array printMultiSend()
{
Array ret;
Expand Down Expand Up @@ -2280,7 +2279,6 @@ Array printMultiSend()
return ret;
}

//presstab HyperStake
Array printAddresses()
{
std::vector<COutput> vCoins;
Expand Down Expand Up @@ -2312,7 +2310,6 @@ Array printAddresses()
return ret;
}

//presstab HyperStake
unsigned int sumMultiSend()
{
unsigned int sum = 0;
Expand Down Expand Up @@ -2342,59 +2339,77 @@ Value multisend(const Array &params, bool fHelp)
{
LOCK(pwalletMain->cs_wallet);
{
fFileBacked = pwalletMain->fFileBacked;
string strRet;
if(fFileBacked)
bool erased = false;
if(pwalletMain->fFileBacked)
{
if(walletdb.EraseMultiSend(pwalletMain->vMultiSend))
strRet += "erased MultiSend vector from database & ";
erased = true;
}

pwalletMain->vMultiSend.clear();
pwalletMain->setMultiSendDisabled();
strRet += "cleared MultiSend vector from RAM";
return strRet;

Object obj;
obj.push_back(Pair("Erased from database", erased));
obj.push_back(Pair("Erased from RAM", true));

return obj;
}
}
else if (strCommand == "enablestake" || strCommand == "activatestake" )
{
if(pwalletMain->vMultiSend.size() < 1)
return "Unable to activate MultiSend, check MultiSend vector";
throw JSONRPCError(RPC_INVALID_REQUEST, "Unable to activate MultiSend, check MultiSend vector");

if(CBitcoinAddress(pwalletMain->vMultiSend[0].first).IsValid())
{
pwalletMain->fMultiSendStake = true;

if(!walletdb.WriteMSettings(true, pwalletMain->fMultiSendMasternodeReward, pwalletMain->nLastMultiSendHeight))
return "MultiSend activated but writing settings to DB failed";
{
Object obj;
obj.push_back(Pair("error", "MultiSend activated but writing settings to DB failed"));
Array arr;
arr.push_back(obj);
arr.push_back(printMultiSend());
return arr;
}
else
return "MultiSend activated";
return printMultiSend();
}
else
return "Unable to activate MultiSend, check MultiSend vector";

throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to activate MultiSend, check MultiSend vector");
}
else if(strCommand == "enablemasternode" || strCommand == "activatemasternode")
{
if(pwalletMain->vMultiSend.size() < 1)
return "Unable to activate MultiSend, check MultiSend vector";
throw JSONRPCError(RPC_INVALID_REQUEST, "Unable to activate MultiSend, check MultiSend vector");

if(CBitcoinAddress(pwalletMain->vMultiSend[0].first).IsValid())
{
pwalletMain->fMultiSendMasternodeReward = true;

if(!walletdb.WriteMSettings(pwalletMain->fMultiSendStake, true, pwalletMain->nLastMultiSendHeight))
return "MultiSend activated but writing settings to DB failed";
{
Object obj;
obj.push_back(Pair("error", "MultiSend activated but writing settings to DB failed"));
Array arr;
arr.push_back(obj);
arr.push_back(printMultiSend());
return arr;
}
else
return "MultiSend activated";
return printMultiSend();
}
else
return "Unable to activate MultiSend, check MultiSend vector";

throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to activate MultiSend, check MultiSend vector");
}
else if (strCommand == "disable" || strCommand == "deactivate" )
{
pwalletMain->setMultiSendDisabled();
if(!walletdb.WriteMSettings(false, false, pwalletMain->nLastMultiSendHeight))
return "MultiSend deactivated but writing settings to DB failed";
throw JSONRPCError(RPC_DATABASE_ERROR, "MultiSend deactivated but writing settings to DB failed");

return "MultiSend deactivated";
return printMultiSend();
}
else if(strCommand == "enableall")
{
Expand All @@ -2403,36 +2418,37 @@ Value multisend(const Array &params, bool fHelp)
else
{
pwalletMain->vDisabledAddresses.clear();
return "all addresses will now send MultiSend transactions";
return printMultiSend();
}
}
}
if(params.size() == 2 && params[0].get_str() == "delete")
{
int del = boost::lexical_cast<int>(params[1].get_str());
if(!walletdb.EraseMultiSend(pwalletMain->vMultiSend))
return "failed to delete old MultiSend vector from database";
throw JSONRPCError(RPC_DATABASE_ERROR, "failed to delete old MultiSend vector from database");

pwalletMain->vMultiSend.erase(pwalletMain->vMultiSend.begin() + del);
if(!walletdb.WriteMultiSend(pwalletMain->vMultiSend))
return "walletdb WriteMultiSend failed!";
throw JSONRPCError(RPC_DATABASE_ERROR, "walletdb WriteMultiSend failed!");

return printMultiSend();
}
if(params.size() == 2 && params[0].get_str() == "disable")
{
std::string disAddress = params[1].get_str();
if(!CBitcoinAddress(disAddress).IsValid())
return "address you want to disable is not valid";
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "address you want to disable is not valid");
else
{
pwalletMain->vDisabledAddresses.push_back(disAddress);
if(!walletdb.EraseMSDisabledAddresses(pwalletMain->vDisabledAddresses))
return "disabled address from sending, but failed to clear old vector from walletDB";
throw JSONRPCError(RPC_DATABASE_ERROR, "disabled address from sending, but failed to clear old vector from walletDB");

if(!walletdb.WriteMSDisabledAddresses(pwalletMain->vDisabledAddresses))
return "disabled address from sending, but failed to store it to walletDB";
throw JSONRPCError(RPC_DATABASE_ERROR, "disabled address from sending, but failed to store it to walletDB");
else
return "disabled address from sending MultiSend transactions";
return printMultiSend();
}
}

Expand All @@ -2442,25 +2458,24 @@ Value multisend(const Array &params, bool fHelp)
"multisend <command>\n"
"****************************************************************\n"
"WHAT IS MULTISEND?\n"
"MultiSend is a rebuild of what used to be called Stake For Charity (s4c)\n"
"MultiSend allows a user to automatically send a percent of their stake reward to as many addresses as you would like\n"
"The MultiSend transaction is sent when the staked coins mature (30 confirmations)\n"
"The only current restriction is that you cannot choose to send more than 100% of your stake using MultiSend\n"
"The MultiSend transaction is sent when the staked coins mature (100 confirmations)\n"
"****************************************************************\n"
"TO CREATE OR ADD TO THE MULTISEND VECTOR:\n"
"multisend <PIVX Address> <percent>\n"
"This will add a new address to the MultiSend vector\n"
"Percent is a whole number 1 to 100.\n"
"****************************************************************\n"
"MULTISEND COMMANDS (usage: multisend <command>)\n"
" print - displays the current MultiSend vector \n"
" clear - deletes the current MultiSend vector \n"
" enable/activate - activates the current MultiSend vector \n"
" enablestake/activatestake - activates the current MultiSend vector to be activated on stake rewards\n"
" enablemasternode/activatemasternode - activates the current MultiSend vector to be activated on masternode rewards\n"
" disable/deactivate - disables the current MultiSend vector \n"
" delete <Address #> - deletes an address from the MultiSend vector \n"
" disable <address> - prevents a specific address from sending MultiSend transactions\n"
" enableall - enables all addresses to be eligible to send MultiSend transactions\n"
"****************************************************************\n"
"TO CREATE OR ADD TO THE MULTISEND VECTOR:\n"
"multisend <PIV Address> <percent>\n"
"This will add a new address to the MultiSend vector\n"
"Percent is a whole number 1 to 100.\n"
"****************************************************************\n"
);

//if the user is entering a new MultiSend item
Expand All @@ -2480,17 +2495,17 @@ Value multisend(const Array &params, bool fHelp)
//Error if 0 is entered
if(nPercent == 0)
{
return "Sending 0% of stake is not valid";
throw JSONRPCError(RPC_INVALID_PARAMETER, "Sending 0% of stake is not valid");
}

//MultiSend can only send 100% of your stake
if (nPercent + sumMultiSend() > 100)
return "Failed to add to MultiSend vector, the sum of your MultiSend is greater than 100%";
throw JSONRPCError(RPC_INVALID_PARAMETER, "Failed to add to MultiSend vector, the sum of your MultiSend is greater than 100%");

for(unsigned int i = 0; i < pwalletMain->vMultiSend.size(); i++)
{
if(pwalletMain->vMultiSend[i].first == strAddress)
return "Failed to add to MultiSend vector, cannot use the same address twice";
throw JSONRPCError(RPC_INVALID_PARAMETER, "Failed to add to MultiSend vector, cannot use the same address twice");
}

if(fFileBacked)
Expand All @@ -2503,7 +2518,7 @@ Value multisend(const Array &params, bool fHelp)
if(fFileBacked)
{
if(!walletdb.WriteMultiSend(pwalletMain->vMultiSend))
return "walletdb WriteMultiSend failed!";
throw JSONRPCError(RPC_DATABASE_ERROR, "walletdb WriteMultiSend failed!");
}
}
return printMultiSend();
Expand Down