Skip to content

Commit 21bbd92

Browse files
committed
Add fundrawtransaction RPC method
1 parent 1e0d1a2 commit 21bbd92

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
7878
{ "signrawtransaction", 1 },
7979
{ "signrawtransaction", 2 },
8080
{ "sendrawtransaction", 1 },
81+
{ "fundrawtransaction", 1 },
8182
{ "gettxout", 1 },
8283
{ "gettxout", 2 },
8384
{ "gettxoutproof", 0 },

src/rpcserver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ static const CRPCCommand vRPCCommands[] =
316316
{ "rawtransactions", "getrawtransaction", &getrawtransaction, true },
317317
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false },
318318
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false }, /* uses wallet if enabled */
319+
#ifdef ENABLE_WALLET
320+
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false },
321+
#endif
319322

320323
/* Utility functions */
321324
{ "util", "createmultisig", &createmultisig, true },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ extern UniValue listlockunspent(const UniValue& params, bool fHelp);
217217
extern UniValue createrawtransaction(const UniValue& params, bool fHelp);
218218
extern UniValue decoderawtransaction(const UniValue& params, bool fHelp);
219219
extern UniValue decodescript(const UniValue& params, bool fHelp);
220+
extern UniValue fundrawtransaction(const UniValue& params, bool fHelp);
220221
extern UniValue signrawtransaction(const UniValue& params, bool fHelp);
221222
extern UniValue sendrawtransaction(const UniValue& params, bool fHelp);
222223
extern UniValue gettxoutproof(const UniValue& params, bool fHelp);

src/test/rpc_wallet_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
217217
UniValue arr = retValue.get_array();
218218
BOOST_CHECK(arr.size() > 0);
219219
BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
220+
221+
/*********************************
222+
* fundrawtransaction
223+
*********************************/
224+
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 28z"), runtime_error);
225+
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 01000000000180969800000000001976a91450ce0a4b0ee0ddeb633da85199728b940ac3fe9488ac00000000"), runtime_error);
220226
}
221227

222228
BOOST_AUTO_TEST_SUITE_END()

src/wallet/rpcwallet.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,3 +2359,57 @@ UniValue listunspent(const UniValue& params, bool fHelp)
23592359

23602360
return results;
23612361
}
2362+
2363+
UniValue fundrawtransaction(const UniValue& params, bool fHelp)
2364+
{
2365+
if (!EnsureWalletIsAvailable(fHelp))
2366+
return NullUniValue;
2367+
2368+
if (fHelp || params.size() != 1)
2369+
throw runtime_error(
2370+
"fundrawtransaction \"hexstring\"\n"
2371+
"\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2372+
"This will not modify existing inputs, and will add one change output to the outputs.\n"
2373+
"Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2374+
"The inputs added will not be signed, use signrawtransaction for that.\n"
2375+
"\nArguments:\n"
2376+
"1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2377+
"\nResult:\n"
2378+
"{\n"
2379+
" \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2380+
" \"fee\": n, (numeric) The fee added to the transaction\n"
2381+
" \"changepos\": n (numeric) The position of the added change output, or -1\n"
2382+
"}\n"
2383+
"\"hex\" \n"
2384+
"\nExamples:\n"
2385+
"\nCreate a transaction with no inputs\n"
2386+
+ HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2387+
"\nAdd sufficient unsigned inputs to meet the output value\n"
2388+
+ HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2389+
"\nSign the transaction\n"
2390+
+ HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2391+
"\nSend the transaction\n"
2392+
+ HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2393+
);
2394+
2395+
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR));
2396+
2397+
// parse hex string from parameter
2398+
CTransaction origTx;
2399+
if (!DecodeHexTx(origTx, params[0].get_str()))
2400+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2401+
2402+
CMutableTransaction tx(origTx);
2403+
CAmount nFee;
2404+
string strFailReason;
2405+
int nChangePos = -1;
2406+
if(!pwalletMain->FundTransaction(tx, nFee, nChangePos, strFailReason))
2407+
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);
2408+
2409+
UniValue result(UniValue::VOBJ);
2410+
result.push_back(Pair("hex", EncodeHexTx(tx)));
2411+
result.push_back(Pair("changepos", nChangePos));
2412+
result.push_back(Pair("fee", ValueFromAmount(nFee)));
2413+
2414+
return result;
2415+
}

0 commit comments

Comments
 (0)