From 25cf6f3d41b33c4d8523b6613f0169772fd3937f Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Tue, 9 Dec 2014 14:50:01 +0100 Subject: minor rework of SendMoney in rpcwallet - rework the function to not log errors but use throw JSONRPCError - remove a check for IsLocked() that is done in sendtoaddress and sendfrom RPC calls already - cache GetBalance() return value, because it's possibly used twice --- src/rpcwallet.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 1afc3c910e..d097b6a0fa 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -19,6 +19,7 @@ #include #include + #include "json/json_spirit_utils.h" #include "json/json_spirit_value.h" @@ -316,35 +317,29 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) return ret; } -void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew) +static void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew) { + CAmount curBalance = pwalletMain->GetBalance(); + // Check amount if (nValue <= 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount"); - if (nValue > pwalletMain->GetBalance()) + if (nValue > curBalance) throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds"); - string strError; - if (pwalletMain->IsLocked()) - { - strError = "Error: Wallet locked, unable to create transaction!"; - LogPrintf("SendMoney(): %s", strError); - throw JSONRPCError(RPC_WALLET_ERROR, strError); - } - // Parse Bitcoin address CScript scriptPubKey = GetScriptForDestination(address); // Create and send the transaction CReserveKey reservekey(pwalletMain); CAmount nFeeRequired; - if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) - { - if (nValue + nFeeRequired > pwalletMain->GetBalance()) - 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)); - LogPrintf("SendMoney(): %s\n", strError); - throw JSONRPCError(RPC_WALLET_ERROR, strError); + std::string strError; + if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) { + if (nValue + nFeeRequired > curBalance) + 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))); + else + throw JSONRPCError(RPC_WALLET_ERROR, strError); } if (!pwalletMain->CommitTransaction(wtxNew, reservekey)) 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."); -- cgit v1.2.3