Skip to content

Commit

Permalink
Add Min Max to S4C
Browse files Browse the repository at this point in the history
  • Loading branch information
Tranz5 committed Aug 30, 2014
1 parent 29f4b2f commit 2ef0266
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "loadwallet" && n > 3) ConvertTo<bool>(params[3]);
if (strMethod == "loadwallet" && n > 4) ConvertTo<boost::int64_t>(params[4]);
if (strMethod == "stakeforcharity" && n > 1) ConvertTo<int>(params[1]);

if (strMethod == "stakeforcharity" && n > 2) ConvertTo<double>(params[2]);
if (strMethod == "stakeforcharity" && n > 3) ConvertTo<double>(params[3]);

return params;
}
Expand Down
60 changes: 46 additions & 14 deletions src/qt/charitydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "walletmodel.h"
#include "base58.h"
#include "addressbookpage.h"

#include "init.h"


StakeForCharityDialog::StakeForCharityDialog(QWidget *parent) :
Expand All @@ -15,7 +15,6 @@ StakeForCharityDialog::StakeForCharityDialog(QWidget *parent) :
ui->setupUi(this);

ui->label_2->setFocus();

}

StakeForCharityDialog::~StakeForCharityDialog()
Expand Down Expand Up @@ -49,42 +48,75 @@ void StakeForCharityDialog::on_addressBookButton_clicked()

void StakeForCharityDialog::on_enableButton_clicked()
{
CBitcoinAddress address = ui->charityAddressEdit->text().toStdString();

if(model->getEncryptionStatus() == WalletModel::Locked)
{
ui->message->setStyleSheet("QLabel { color: black; }");
ui->message->setText(tr("Please unlock wallet before starting stake for charity."));
return;
}

bool fValidConversion = false;
int64 nMinAmount = MIN_TXOUT_AMOUNT;
int64 nMaxAmount = MAX_MONEY;

CBitcoinAddress address = ui->charityAddressEdit->text().toStdString();
if (!address.IsValid())
{
ui->message->setStyleSheet("QLabel { color: red; }");
ui->message->setText(tr("The entered address: ") + ui->charityAddressEdit->text() + tr(" is invalid.\nPlease check the address and try again."));
ui->charityAddressEdit->setFocus();
return;
}

QString str = ui->charityPercentEdit->text();
bool fIntConversion;
int nCharityPercent = str.toInt(&fIntConversion, 10);
if (!fIntConversion || nCharityPercent > 50 || nCharityPercent <= 0)
int nCharityPercent = ui->charityPercentEdit->text().toInt(&fValidConversion, 10);
if (!fValidConversion || nCharityPercent > 50 || nCharityPercent <= 0)
{
ui->message->setStyleSheet("QLabel { color: red; }");
ui->message->setText(tr("Please Enter 1 - 50 for percent."));
ui->charityPercentEdit->clear();
ui->charityPercentEdit->setFocus();
return;
}

model->setStakeForCharity(true, nCharityPercent, address);
ui->message->setStyleSheet("QLabel { color: green; }");
ui->message->setText(tr("Thank you for giving to\n") + QString(address.ToString().c_str()) + tr("."));
return;
if (!ui->charityMinEdit->text().isEmpty())
{
nMinAmount = ui->charityMinEdit->text().toDouble(&fValidConversion) * COIN;
if(!fValidConversion || nMinAmount <= MIN_TXOUT_AMOUNT || nMinAmount >= MAX_MONEY )
{
ui->message->setStyleSheet("QLabel { color: red; }");
ui->message->setText(tr("Min Amount out of Range, please re-enter."));
ui->charityMinEdit->setFocus();
return;
}
}

if (!ui->charityMaxEdit->text().isEmpty())
{
nMaxAmount = ui->charityMaxEdit->text().toDouble(&fValidConversion) * COIN;
if(!fValidConversion || nMaxAmount <= MIN_TXOUT_AMOUNT || nMaxAmount >= MAX_MONEY )
{
ui->message->setStyleSheet("QLabel { color: red; }");
ui->message->setText(tr("Max Amount out of Range, please re-enter."));
ui->charityMaxEdit->setFocus();
return;
}
}

model->setStakeForCharity(true, nCharityPercent, address, nMinAmount, nMaxAmount);
if(!fGlobalStakeForCharity)
fGlobalStakeForCharity = true;
ui->message->setStyleSheet("QLabel { color: green; }");
ui->message->setText(tr("Thank you for giving to\n") + QString(address.ToString().c_str()) + tr("."));
return;
}

void StakeForCharityDialog::on_disableButton_clicked()
{
model->setStakeForCharity(false,0,"");
int nCharityPercent = 0;
CBitcoinAddress address = "";
int64 nMinAmount = MIN_TXOUT_AMOUNT;
int64 nMaxAmount = MAX_MONEY;

model->setStakeForCharity(false, nCharityPercent, address, nMinAmount, nMaxAmount);
ui->charityAddressEdit->clear();
ui->charityMaxEdit->clear();
ui->charityMinEdit->clear();
Expand Down
9 changes: 6 additions & 3 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,19 @@ bool WalletModel::backupAllWallets(const QString &filename)
return mretval;
}

void WalletModel::setStakeForCharity(bool fStakeForCharity, int nStakeForCharityPercent, CBitcoinAddress strStakeForCharityAddress)
void WalletModel::setStakeForCharity(bool fStakeForCharity, int& nStakeForCharityPercent,
CBitcoinAddress& strStakeForCharityAddress,
int64& nStakeForCharityMinAmout,
int64& nStakeForCharityMaxAmount)
{
// This function assumes the values were checked before being called
{
LOCK(wallet->cs_wallet);
wallet->fStakeForCharity = fStakeForCharity;
wallet->nStakeForCharityPercent = nStakeForCharityPercent;
wallet->strStakeForCharityAddress = strStakeForCharityAddress;
wallet->nStakeForCharityMin = nStakeForCharityMinAmout;
wallet->nStakeForCharityMax = nStakeForCharityMaxAmount;
}
}

Expand Down Expand Up @@ -427,8 +432,6 @@ void WalletModel::getStakeWeight(uint64& nMinWeight, uint64& nMaxWeight, uint64&
wallet->GetStakeWeight(*wallet, nMinWeight, nMaxWeight, nWeight);
}



quint64 WalletModel::getReserveBalance()
{
return wallet->nReserveBalance;
Expand Down
5 changes: 4 additions & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ class WalletModel : public QObject
int getStakeForCharityPercent();
QString getStakeForCharityAddress();
// setStakeForCharity Wallet Settings
void setStakeForCharity(bool fStakeForCharity, int nStakeForCharityPercent, CBitcoinAddress strStakeForCharityAddress);
void setStakeForCharity(bool fStakeForCharity, int& nStakeForCharityPercent,
CBitcoinAddress& strStakeForCharityAddress,
qint64& nStakeForCharityMinAmount,
qint64& nStakeForCharityMaxAmount);

// RAI object for unlocking wallet, returned by requestUnlock()
class UnlockContext
Expand Down
33 changes: 31 additions & 2 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ Value getaccountaddress(CWallet* pWallet, const Array& params, bool fHelp)
Value stakeforcharity(CWallet *pWallet, const Array &params, bool fHelp)
{

if (fHelp || params.size() != 2)
if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error(
"stakeforcharity <HoboNickelsaddress> <percent>\n"
"stakeforcharity <HoboNickelsaddress> <percent> [min amount] [max amount]\n"
"Gives a percentage of a found stake to a different address, after stake matures\n"
"Percent is a whole number 1 to 50.\n"
"Min and Max Amount are optional\n"
"Set percentage to zero to turn off"
+ HelpRequiringPassphrase(pWallet));

Expand All @@ -241,6 +242,30 @@ Value stakeforcharity(CWallet *pWallet, const Array &params, bool fHelp)

unsigned int nPer = (unsigned int) params[1].get_int();

int64 nMinAmount = MIN_TXOUT_AMOUNT;
int64 nMaxAmount = MAX_MONEY;

// Optional Min Amount
if (params.size() > 2)
{
int64 nAmount = AmountFromValue(params[2]);
if (nAmount < MIN_TXOUT_AMOUNT)
throw JSONRPCError(-101, "Send amount too small");
else
nMinAmount = nAmount;
}

// Optional Max Amount
if (params.size() > 3)
{
int64 nAmount = AmountFromValue(params[3]);

if (nAmount < MIN_TXOUT_AMOUNT)
throw JSONRPCError(-101, "Send amount too small");
else
nMaxAmount = nAmount;
}

LOCK(pWallet->cs_wallet);

// Turn off if we set to zero.
Expand All @@ -250,6 +275,8 @@ Value stakeforcharity(CWallet *pWallet, const Array &params, bool fHelp)
pWallet->fStakeForCharity = false;
pWallet->strStakeForCharityAddress = "";
pWallet->nStakeForCharityPercent = 0;
pWallet->nStakeForCharityMin = nMinAmount;
pWallet->nStakeForCharityMax = nMaxAmount;
return Value::null;
}

Expand All @@ -260,6 +287,8 @@ Value stakeforcharity(CWallet *pWallet, const Array &params, bool fHelp)
// Future: These will be an array of addr/per/wallet
pWallet->strStakeForCharityAddress = address;
pWallet->nStakeForCharityPercent = nPer;
pWallet->nStakeForCharityMin = nMinAmount;
pWallet->nStakeForCharityMax = nMaxAmount;
pWallet->fStakeForCharity = true;
fGlobalStakeForCharity = true;

Expand Down
8 changes: 4 additions & 4 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ int64 CWallet::GetNewMint() const

bool fGlobalStakeForCharity = false;

bool CWallet::StakeForCharity ()
bool CWallet::StakeForCharity()
{
if ( IsInitialBlockDownload() || IsLocked() )
return false;
Expand All @@ -1236,10 +1236,10 @@ bool CWallet::StakeForCharity ()
// Calculate Amount for Charity
nNet = ( ( pcoin->GetCredit() - pcoin->GetDebit() ) * nStakeForCharityPercent )/100;

// Do not send if amount is too low
if (nNet < MIN_TXOUT_AMOUNT )
// Do not send if amount is too low/high
if (nNet <= nStakeForCharityMin || nNet >= nStakeForCharityMax )
{
printf("StakeForCharity: Amount: %s is below MIN_TXOUT_AMOUNT: %s\n",FormatMoney(nNet).c_str(),FormatMoney(MIN_TXOUT_AMOUNT).c_str());
printf("StakeForCharity: Amount: %s is not in range of Min: %s and Max:%s\n",FormatMoney(nNet).c_str(),FormatMoney(nStakeForCharityMin).c_str(),FormatMoney(nStakeForCharityMax).c_str());
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class CWallet : public CCryptoKeyStore
bool fWalletUnlockMintOnly;
bool fStakeForCharity;
int nStakeForCharityPercent;
int64 nStakeForCharityMin;
int64 nStakeForCharityMax;
CBitcoinAddress strStakeForCharityAddress;
std::string strWalletFile;
int64 nReserveBalance;
Expand Down Expand Up @@ -157,6 +159,8 @@ class CWallet : public CCryptoKeyStore
fWalletUnlockMintOnly = false;
fStakeForCharity = false;
nStakeForCharityPercent = 0;
nStakeForCharityMin = MIN_TXOUT_AMOUNT;
nStakeForCharityMax = MAX_MONEY;
strStakeForCharityAddress = "";
nReserveBalance = 0;
}
Expand Down

0 comments on commit 2ef0266

Please sign in to comment.