Skip to content

Commit db4a1d5

Browse files
committed
Merge pull request #5694
25cf6f3 minor rework of SendMoney in rpcwallet (Philip Kaufmann)
2 parents 59310f1 + 25cf6f3 commit db4a1d5

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/rpcwallet.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdint.h>
2020

2121
#include <boost/assign/list_of.hpp>
22+
2223
#include "json/json_spirit_utils.h"
2324
#include "json/json_spirit_value.h"
2425

@@ -316,35 +317,29 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
316317
return ret;
317318
}
318319

319-
void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew)
320+
static void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew)
320321
{
322+
CAmount curBalance = pwalletMain->GetBalance();
323+
321324
// Check amount
322325
if (nValue <= 0)
323326
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");
324327

325-
if (nValue > pwalletMain->GetBalance())
328+
if (nValue > curBalance)
326329
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");
327330

328-
string strError;
329-
if (pwalletMain->IsLocked())
330-
{
331-
strError = "Error: Wallet locked, unable to create transaction!";
332-
LogPrintf("SendMoney(): %s", strError);
333-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
334-
}
335-
336331
// Parse Bitcoin address
337332
CScript scriptPubKey = GetScriptForDestination(address);
338333

339334
// Create and send the transaction
340335
CReserveKey reservekey(pwalletMain);
341336
CAmount nFeeRequired;
342-
if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
343-
{
344-
if (nValue + nFeeRequired > pwalletMain->GetBalance())
345-
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));
346-
LogPrintf("SendMoney(): %s\n", strError);
347-
throw JSONRPCError(RPC_WALLET_ERROR, strError);
337+
std::string strError;
338+
if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) {
339+
if (nValue + nFeeRequired > curBalance)
340+
throw JSONRPCError(RPC_WALLET_ERROR, 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)));
341+
else
342+
throw JSONRPCError(RPC_WALLET_ERROR, strError);
348343
}
349344
if (!pwalletMain->CommitTransaction(wtxNew, reservekey))
350345
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.");

0 commit comments

Comments
 (0)