diff --git a/contrib/pylightning/lightning/lightning.py b/contrib/pylightning/lightning/lightning.py index 792cc230e..1233b2624 100644 --- a/contrib/pylightning/lightning/lightning.py +++ b/contrib/pylightning/lightning/lightning.py @@ -499,15 +499,11 @@ class LightningRpc(UnixDomainSocketRpc): } return self.call("feerates", payload) - def fundchannel(self, node_id, satoshi, feerate=None, announce=True, minconf=None, utxos=None): - """ - Fund channel with {id} using {satoshi} satoshis with feerate - of {feerate} (uses default feerate if unset). - If {announce} is False, don't send channel announcements. - Only select outputs with {minconf} confirmations. - If {utxos} is specified (as a list of 'txid:vout' strings), - fund a channel from these specifics utxos. - """ + def _deprecated_fundchannel(self, node_id, satoshi, feerate=None, announce=True, minconf=None, utxos=None): + warnings.warn("fundchannel: the 'satoshi' field is renamed 'amount' : expect removal" + " in Mid-2020", + DeprecationWarning) + payload = { "id": node_id, "satoshi": satoshi, @@ -518,6 +514,32 @@ class LightningRpc(UnixDomainSocketRpc): } return self.call("fundchannel", payload) + def fundchannel(self, node_id, *args, **kwargs): + """ + Fund channel with {id} using {amount} satoshis with feerate + of {feerate} (uses default feerate if unset). + If {announce} is False, don't send channel announcements. + Only select outputs with {minconf} confirmations. + If {utxos} is specified (as a list of 'txid:vout' strings), + fund a channel from these specifics utxos. + """ + + if 'satoshi' in kwargs: + return self._deprecated_fundchannel(node_id, *args, **kwargs) + + def _fundchannel(node_id, amount, feerate=None, announce=True, minconf=None, utxos=None): + payload = { + "id": node_id, + "amount": amount, + "feerate": feerate, + "announce": announce, + "minconf": minconf, + "utxos": utxos + } + return self.call("fundchannel", payload) + + return _fundchannel(node_id, *args, **kwargs) + def fundchannel_start(self, node_id, satoshi, feerate=None, announce=True): """ Start channel funding with {id} for {satoshi} satoshis diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index 456f07505..86ef2be04 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -381,6 +381,27 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd, return fundchannel_start(cmd, fr); } +/* We will use 'id' and 'amount' to build a output: {id: amount}. + * For array type, if we miss 'amount', next parameter will be + * mistaken for 'amount'. + * Note the check for 'output' in 'txprepare' is behind of the checks + * for other parameter, so doing a simply check for 'amount' here can + * help us locate error correctly. + */ +static struct command_result *param_string_check_sat(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + const char **str) +{ + struct command_result *res; + struct amount_sat *amount; + + res = param_sat_or_all(cmd, name, buffer, tok, &amount); + if (res) + return res; + + return param_string(cmd, name, buffer, tok, str); +} + static struct command_result *json_fundchannel(struct command *cmd, const char *buf, const jsmntok_t *params) @@ -388,15 +409,38 @@ static struct command_result *json_fundchannel(struct command *cmd, struct funding_req *fr = tal(cmd, struct funding_req); struct json_out *ret; - if (!param(cmd, buf, params, - p_req("id", param_node_id, &fr->id), - p_req("satoshi", param_string, &fr->funding_str), - p_opt("feerate", param_string, &fr->feerate_str), - p_opt_def("announce", param_bool, &fr->announce_channel, true), - p_opt_def("minconf", param_number, &fr->minconf, 1), - p_opt("utxos", param_string, &fr->utxo_str), - NULL)) - return command_param_failed(); + /* For generating help, give new-style. */ + if (!params || !deprecated_apis || params->type == JSMN_ARRAY) { + if (!param(cmd, buf, params, + p_req("id", param_node_id, &fr->id), + p_req("amount", param_string_check_sat, &fr->funding_str), + p_opt("feerate", param_string, &fr->feerate_str), + p_opt_def("announce", param_bool, &fr->announce_channel, true), + p_opt_def("minconf", param_number, &fr->minconf, 1), + p_opt("utxos", param_string, &fr->utxo_str), + NULL)) + return command_param_failed(); + } else { + const char *satoshi_str; + if (!param(cmd, buf, params, + p_req("id", param_node_id, &fr->id), + p_opt("amount", param_string, &fr->funding_str), + p_opt("satoshi", param_string, &satoshi_str), + p_opt("feerate", param_string, &fr->feerate_str), + p_opt_def("announce", param_bool, &fr->announce_channel, true), + p_opt_def("minconf", param_number, &fr->minconf, 1), + p_opt("utxos", param_string, &fr->utxo_str), + NULL)) + return command_param_failed(); + + if (!fr->funding_str) { + if (satoshi_str) + fr->funding_str = satoshi_str; + else + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "Need set 'amount' field"); + } + } fr->funding_all = streq(fr->funding_str, "all");