Skip to content

Commit 3446806

Browse files
committed
Merge pull request #5399
4be639e Use RPC_INVALID_PARAMETER instead of RPC_WALLET_ERROR for invalid amount. No return at the end of void function. (Pavel Janík) b93173d Move SendMoney() to rpcwallet.cpp. (Pavel Janík)
2 parents 12c05ee + 4be639e commit 3446806

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

src/rpcwallet.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "netbase.h"
1313
#include "timedata.h"
1414
#include "util.h"
15+
#include "utilmoneystr.h"
1516
#include "wallet.h"
1617
#include "walletdb.h"
1718

@@ -309,6 +310,40 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
309310
return ret;
310311
}
311312

313+
void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew)
314+
{
315+
// Check amount
316+
if (nValue <= 0)
317+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");
318+
319+
if (nValue > pwalletMain->GetBalance())
320+
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");
321+
322+
string strError;
323+
if (pwalletMain->IsLocked())
324+
{
325+
strError = "Error: Wallet locked, unable to create transaction!";
326+
LogPrintf("SendMoney() : %s", strError);
327+
throw JSONRPCError(RPC_WALLET_ERROR, strError);
328+
}
329+
330+
// Parse Bitcoin address
331+
CScript scriptPubKey = GetScriptForDestination(address);
332+
333+
// Create and send the transaction
334+
CReserveKey reservekey(pwalletMain);
335+
CAmount nFeeRequired;
336+
if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
337+
{
338+
if (nValue + nFeeRequired > pwalletMain->GetBalance())
339+
strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired));
340+
LogPrintf("SendMoney() : %s\n", strError);
341+
throw JSONRPCError(RPC_WALLET_ERROR, strError);
342+
}
343+
if (!pwalletMain->CommitTransaction(wtxNew, reservekey))
344+
throw JSONRPCError(RPC_WALLET_ERROR, "Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
345+
}
346+
312347
Value sendtoaddress(const Array& params, bool fHelp)
313348
{
314349
if (fHelp || params.size() < 2 || params.size() > 4)
@@ -348,9 +383,7 @@ Value sendtoaddress(const Array& params, bool fHelp)
348383

349384
EnsureWalletIsUnlocked();
350385

351-
string strError = pwalletMain->SendMoney(address.Get(), nAmount, wtx);
352-
if (strError != "")
353-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
386+
SendMoney(address.Get(), nAmount, wtx);
354387

355388
return wtx.GetHash().GetHex();
356389
}
@@ -791,10 +824,7 @@ Value sendfrom(const Array& params, bool fHelp)
791824
if (nAmount > nBalance)
792825
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
793826

794-
// Send
795-
string strError = pwalletMain->SendMoney(address.Get(), nAmount, wtx);
796-
if (strError != "")
797-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
827+
SendMoney(address.Get(), nAmount, wtx);
798828

799829
return wtx.GetHash().GetHex();
800830
}

src/wallet.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,46 +1587,6 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
15871587
return true;
15881588
}
15891589

1590-
1591-
1592-
1593-
string CWallet::SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew)
1594-
{
1595-
// Check amount
1596-
if (nValue <= 0)
1597-
return _("Invalid amount");
1598-
if (nValue > GetBalance())
1599-
return _("Insufficient funds");
1600-
1601-
string strError;
1602-
if (IsLocked())
1603-
{
1604-
strError = _("Error: Wallet locked, unable to create transaction!");
1605-
LogPrintf("SendMoney() : %s", strError);
1606-
return strError;
1607-
}
1608-
1609-
// Parse Bitcoin address
1610-
CScript scriptPubKey = GetScriptForDestination(address);
1611-
1612-
// Create and send the transaction
1613-
CReserveKey reservekey(this);
1614-
CAmount nFeeRequired;
1615-
if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
1616-
{
1617-
if (nValue + nFeeRequired > GetBalance())
1618-
strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!"), FormatMoney(nFeeRequired));
1619-
LogPrintf("SendMoney() : %s\n", strError);
1620-
return strError;
1621-
}
1622-
if (!CommitTransaction(wtxNew, reservekey))
1623-
return _("Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
1624-
1625-
return "";
1626-
}
1627-
1628-
1629-
16301590
CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool)
16311591
{
16321592
// payTxFee is user-set "I want to pay this much"

src/wallet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
288288
bool CreateTransaction(CScript scriptPubKey, const CAmount& nValue,
289289
CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet, std::string& strFailReason, const CCoinControl *coinControl = NULL);
290290
bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
291-
std::string SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew);
292291

293292
static CFeeRate minTxFee;
294293
static CAmount GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool);

0 commit comments

Comments
 (0)