diff options
author | Pavel Janík <Pavel@Janik.cz> | 2014-12-01 20:06:45 +0100 |
---|---|---|
committer | Pavel Janík <Pavel@Janik.cz> | 2014-12-02 08:32:10 +0100 |
commit | b93173dee9b233409daca201dd7ed0c19576e895 (patch) | |
tree | 315c7f8e97d7180db5fe63c872e28c48c304932f /src/rpcwallet.cpp | |
parent | f0877f8b6200dcf6df12df5e69d498d365f81f8b (diff) |
Move SendMoney() to rpcwallet.cpp.
Diffstat (limited to 'src/rpcwallet.cpp')
-rw-r--r-- | src/rpcwallet.cpp | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index d2d14ad9f4..a03ec7cee1 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -12,6 +12,7 @@ #include "netbase.h" #include "timedata.h" #include "util.h" +#include "utilmoneystr.h" #include "wallet.h" #include "walletdb.h" @@ -309,6 +310,42 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) return ret; } +void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew) +{ + // Check amount + if (nValue <= 0) + throw JSONRPCError(RPC_WALLET_ERROR, "Invalid amount"); + + if (nValue > pwalletMain->GetBalance()) + 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); + } + 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."); + + return; +} + Value sendtoaddress(const Array& params, bool fHelp) { if (fHelp || params.size() < 2 || params.size() > 4) @@ -348,9 +385,7 @@ Value sendtoaddress(const Array& params, bool fHelp) EnsureWalletIsUnlocked(); - string strError = pwalletMain->SendMoney(address.Get(), nAmount, wtx); - if (strError != "") - throw JSONRPCError(RPC_WALLET_ERROR, strError); + SendMoney(address.Get(), nAmount, wtx); return wtx.GetHash().GetHex(); } @@ -791,10 +826,7 @@ Value sendfrom(const Array& params, bool fHelp) if (nAmount > nBalance) throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds"); - // Send - string strError = pwalletMain->SendMoney(address.Get(), nAmount, wtx); - if (strError != "") - throw JSONRPCError(RPC_WALLET_ERROR, strError); + SendMoney(address.Get(), nAmount, wtx); return wtx.GetHash().GetHex(); } |