Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added spent option to listfunds #4296

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions contrib/pyln-client/pyln/client/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,16 @@ def listforwards(self):
"""
return self.call("listforwards")

def listfunds(self):
def listfunds(self, spent=False):
"""
Show funds available for opening channels.
Show funds available for opening channels
or both unspent and spent funds if {spent} is True.
"""
return self.call("listfunds")

payload = {
"spent": spent
}
return self.call("listfunds", payload)

def listtransactions(self):
"""
Expand Down
8 changes: 6 additions & 2 deletions doc/lightning-listfunds.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion doc/lightning-listfunds.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-listfunds -- Command showing all funds currently managed by the c-ligh
SYNOPSIS
--------

**listfunds**
**listfunds** \[*spent*\]

DESCRIPTION
-----------
Expand All @@ -13,6 +13,9 @@ The **listfunds** RPC command displays all funds available, either in
unspent outputs (UTXOs) in the internal wallet or funds locked in
currently open channels.

*spent* is a boolean: if true, then the *outputs* will include spent outputs
in addition to the unspent ones. Default is false.

RETURN VALUE
------------

Expand Down
21 changes: 21 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2410,3 +2410,24 @@ def test_listtransactions(node_factory):
# The txid of the transaction funding the channel is present, and
# represented as little endian (like bitcoind and explorers).
assert wallettxid in txids


def test_listfunds(node_factory):
"""Test listfunds command."""
l1, l2 = node_factory.get_nodes(2, opts=[{}, {}])

open_txid = l1.openchannel(l2, 10**5)["wallettxid"]

# unspent outputs
utxos = l1.rpc.listfunds()["outputs"]

# only 1 unspent output should be available
assert len(utxos) == 1

# both unspent and spent outputs
all_outputs = l1.rpc.listfunds(spent=True)["outputs"]
txids = [output['txid'] for output in all_outputs]

# 1 spent output (channel opening) and 1 unspent output
assert len(all_outputs) == 2
assert open_txid in txids
19 changes: 16 additions & 3 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,28 @@ static struct command_result *json_listfunds(struct command *cmd,
{
struct json_stream *response;
struct peer *p;
struct utxo **utxos, **reserved_utxos;
struct utxo **utxos, **reserved_utxos, **spent_utxos;
bool *spent;

if (!param(cmd, buffer, params, NULL))
if (!param(cmd, buffer, params,
p_opt_def("spent", param_bool, &spent, false),
NULL))
return command_param_failed();

response = json_stream_success(cmd);

utxos = wallet_get_utxos(cmd, cmd->ld->wallet, OUTPUT_STATE_AVAILABLE);
reserved_utxos = wallet_get_utxos(cmd, cmd->ld->wallet, OUTPUT_STATE_RESERVED);

json_array_start(response, "outputs");
json_add_utxos(response, cmd->ld->wallet, utxos);
json_add_utxos(response, cmd->ld->wallet, reserved_utxos);

if (*spent) {
spent_utxos = wallet_get_utxos(cmd, cmd->ld->wallet, OUTPUT_STATE_SPENT);
json_add_utxos(response, cmd->ld->wallet, spent_utxos);
}

json_array_end(response);

/* Add funds that are allocated to channels */
Expand Down Expand Up @@ -364,7 +374,10 @@ static const struct json_command listfunds_command = {
json_listfunds,
"Show available funds from the internal wallet",
false,
"Returns a list of funds (outputs) that can be used by the internal wallet to open new channels or can be withdrawn, using the `withdraw` command, to another wallet."
"Returns a list of funds (outputs) that can be used "
"by the internal wallet to open new channels "
"or can be withdrawn, using the `withdraw` command, to another wallet. "
"Includes spent outputs if {spent} is set to true."
};
AUTODATA(json_command, &listfunds_command);

Expand Down