Skip to content

Commit cfe5aeb

Browse files
committed
rpc: add minconf and maxconf options to sendall
1 parent a07a413 commit cfe5aeb

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

doc/release-notes-25375.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ added to the following RPCs:
88
- `fundrawtransaction`
99
- `send`
1010
- `walletcreatefundedpsbt`
11+
- `sendall`

src/wallet/rpc/spend.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ RPCHelpMan sendall()
12921292
{"include_watching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch-only.\n"
12931293
"Only solvable inputs can be used. Watch-only destinations are solvable if the public key and/or output script was imported,\n"
12941294
"e.g. with 'importpubkey' or 'importmulti' with the 'pubkeys' or 'desc' field."},
1295-
{"inputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Use exactly the specified inputs to build the transaction. Specifying inputs is incompatible with send_max.",
1295+
{"inputs", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Use exactly the specified inputs to build the transaction. Specifying inputs is incompatible with the send_max, minconf, and maxconf options.",
12961296
{
12971297
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
12981298
{
@@ -1307,6 +1307,8 @@ RPCHelpMan sendall()
13071307
{"lock_unspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"},
13081308
{"psbt", RPCArg::Type::BOOL, RPCArg::DefaultHint{"automatic"}, "Always return a PSBT, implies add_to_wallet=false."},
13091309
{"send_max", RPCArg::Type::BOOL, RPCArg::Default{false}, "When true, only use UTXOs that can pay for their own fees to maximize the output amount. When 'false' (default), no UTXO is left behind. send_max is incompatible with providing specific inputs."},
1310+
{"minconf", RPCArg::Type::NUM, RPCArg::Default{0}, "Require inputs with at least this many confirmations."},
1311+
{"maxconf", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "Require inputs with at most this many confirmations."},
13101312
},
13111313
FundTxDoc()
13121314
),
@@ -1381,6 +1383,23 @@ RPCHelpMan sendall()
13811383

13821384
coin_control.fAllowWatchOnly = ParseIncludeWatchonly(options["include_watching"], *pwallet);
13831385

1386+
if (options.exists("minconf")) {
1387+
if (options["minconf"].getInt<int>() < 0)
1388+
{
1389+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid minconf (minconf cannot be negative): %s", options["minconf"].getInt<int>()));
1390+
}
1391+
1392+
coin_control.m_min_depth = options["minconf"].getInt<int>();
1393+
}
1394+
1395+
if (options.exists("maxconf")) {
1396+
coin_control.m_max_depth = options["maxconf"].getInt<int>();
1397+
1398+
if (coin_control.m_max_depth < coin_control.m_min_depth) {
1399+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("maxconf can't be lower than minconf: %d < %d", coin_control.m_max_depth, coin_control.m_min_depth));
1400+
}
1401+
}
1402+
13841403
const bool rbf{options.exists("replaceable") ? options["replaceable"].get_bool() : pwallet->m_signal_rbf};
13851404

13861405
FeeCalculation fee_calc_out;
@@ -1402,6 +1421,8 @@ RPCHelpMan sendall()
14021421
bool send_max{options.exists("send_max") ? options["send_max"].get_bool() : false};
14031422
if (options.exists("inputs") && options.exists("send_max")) {
14041423
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot combine send_max with specific inputs.");
1424+
} else if (options.exists("inputs") && (options.exists("minconf") || options.exists("maxconf"))) {
1425+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot combine minconf or maxconf with specific inputs.");
14051426
} else if (options.exists("inputs")) {
14061427
for (const CTxIn& input : rawTx.vin) {
14071428
if (pwallet->IsSpent(input.prevout)) {

test/functional/wallet_sendall.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,68 @@ def sendall_watchonly_specific_inputs(self):
317317
assert_equal(decoded["tx"]["vin"][0]["vout"], utxo["vout"])
318318
assert_equal(decoded["tx"]["vout"][0]["scriptPubKey"]["address"], self.remainder_target)
319319

320+
@cleanup
321+
def sendall_with_minconf(self):
322+
# utxo of 17 bicoin has 6 confirmations, utxo of 4 has 3
323+
self.add_utxos([17])
324+
self.generate(self.nodes[0], 2)
325+
self.add_utxos([4])
326+
self.generate(self.nodes[0], 2)
327+
328+
self.log.info("Test sendall fails because minconf is negative")
329+
330+
assert_raises_rpc_error(-8,
331+
"Invalid minconf (minconf cannot be negative): -2",
332+
self.wallet.sendall,
333+
recipients=[self.remainder_target],
334+
options={"minconf": -2})
335+
self.log.info("Test sendall fails because minconf is used while specific inputs are provided")
336+
337+
utxo = self.wallet.listunspent()[0]
338+
assert_raises_rpc_error(-8,
339+
"Cannot combine minconf or maxconf with specific inputs.",
340+
self.wallet.sendall,
341+
recipients=[self.remainder_target],
342+
options={"inputs": [utxo], "minconf": 2})
343+
344+
self.log.info("Test sendall fails because there are no utxos with enough confirmations specified by minconf")
345+
346+
assert_raises_rpc_error(-6,
347+
"Total value of UTXO pool too low to pay for transaction. Try using lower feerate or excluding uneconomic UTXOs with 'send_max' option.",
348+
self.wallet.sendall,
349+
recipients=[self.remainder_target],
350+
options={"minconf": 7})
351+
352+
self.log.info("Test sendall only spends utxos with a specified number of confirmations when minconf is used")
353+
self.wallet.sendall(recipients=[self.remainder_target], fee_rate=300, options={"minconf": 6})
354+
355+
assert_equal(len(self.wallet.listunspent()), 1)
356+
assert_equal(self.wallet.listunspent()[0]['confirmations'], 3)
357+
358+
# decrease minconf and show the remaining utxo is picked up
359+
self.wallet.sendall(recipients=[self.remainder_target], fee_rate=300, options={"minconf": 3})
360+
assert_equal(self.wallet.getbalance(), 0)
361+
362+
@cleanup
363+
def sendall_with_maxconf(self):
364+
# utxo of 17 bicoin has 6 confirmations, utxo of 4 has 3
365+
self.add_utxos([17])
366+
self.generate(self.nodes[0], 2)
367+
self.add_utxos([4])
368+
self.generate(self.nodes[0], 2)
369+
370+
self.log.info("Test sendall fails because there are no utxos with enough confirmations specified by maxconf")
371+
assert_raises_rpc_error(-6,
372+
"Total value of UTXO pool too low to pay for transaction. Try using lower feerate or excluding uneconomic UTXOs with 'send_max' option.",
373+
self.wallet.sendall,
374+
recipients=[self.remainder_target],
375+
options={"maxconf": 1})
376+
377+
self.log.info("Test sendall only spends utxos with a specified number of confirmations when maxconf is used")
378+
self.wallet.sendall(recipients=[self.remainder_target], fee_rate=300, options={"maxconf":4})
379+
assert_equal(len(self.wallet.listunspent()), 1)
380+
assert_equal(self.wallet.listunspent()[0]['confirmations'], 6)
381+
320382
# This tests needs to be the last one otherwise @cleanup will fail with "Transaction too large" error
321383
def sendall_fails_with_transaction_too_large(self):
322384
self.log.info("Test that sendall fails if resulting transaction is too large")
@@ -392,6 +454,12 @@ def run_test(self):
392454
# Sendall succeeds with watchonly wallets spending specific UTXOs
393455
self.sendall_watchonly_specific_inputs()
394456

457+
# Sendall only uses outputs with at least a give number of confirmations when using minconf
458+
self.sendall_with_minconf()
459+
460+
# Sendall only uses outputs with less than a given number of confirmation when using minconf
461+
self.sendall_with_maxconf()
462+
395463
# Sendall fails when many inputs result to too large transaction
396464
self.sendall_fails_with_transaction_too_large()
397465

0 commit comments

Comments
 (0)