|
12 | 12 | #include "netbase.h"
|
13 | 13 | #include "timedata.h"
|
14 | 14 | #include "util.h"
|
| 15 | +#include "utilmoneystr.h" |
15 | 16 | #include "wallet.h"
|
16 | 17 | #include "walletdb.h"
|
17 | 18 |
|
@@ -309,6 +310,40 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
|
309 | 310 | return ret;
|
310 | 311 | }
|
311 | 312 |
|
| 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 | + |
312 | 347 | Value sendtoaddress(const Array& params, bool fHelp)
|
313 | 348 | {
|
314 | 349 | if (fHelp || params.size() < 2 || params.size() > 4)
|
@@ -348,9 +383,7 @@ Value sendtoaddress(const Array& params, bool fHelp)
|
348 | 383 |
|
349 | 384 | EnsureWalletIsUnlocked();
|
350 | 385 |
|
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); |
354 | 387 |
|
355 | 388 | return wtx.GetHash().GetHex();
|
356 | 389 | }
|
@@ -791,10 +824,7 @@ Value sendfrom(const Array& params, bool fHelp)
|
791 | 824 | if (nAmount > nBalance)
|
792 | 825 | throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
793 | 826 |
|
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); |
798 | 828 |
|
799 | 829 | return wtx.GetHash().GetHex();
|
800 | 830 | }
|
|
0 commit comments