mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
invoice: common routine for invoice to json.
Different commands (listinvoice, delinvoice, waitinvoice, waitanyinvoice) returned different fields, as not all were updated. This makes them uniform. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
26d1c35a01
commit
b56473c735
@ -74,19 +74,26 @@ struct invoices *invoices_init(const tal_t *ctx)
|
|||||||
return invs;
|
return invs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void json_add_invoice(struct json_result *response,
|
||||||
|
const struct invoice *inv)
|
||||||
|
{
|
||||||
|
json_object_start(response, NULL);
|
||||||
|
json_add_string(response, "label", inv->label);
|
||||||
|
json_add_hex(response, "rhash", &inv->rhash, sizeof(inv->rhash));
|
||||||
|
if (inv->msatoshi)
|
||||||
|
json_add_u64(response, "msatoshi", *inv->msatoshi);
|
||||||
|
json_add_bool(response, "complete", inv->state == PAID);
|
||||||
|
if (inv->state == PAID)
|
||||||
|
json_add_u64(response, "pay_index", inv->pay_index);
|
||||||
|
json_add_u64(response, "expiry_time", inv->expiry_time);
|
||||||
|
json_object_end(response);
|
||||||
|
}
|
||||||
|
|
||||||
static void tell_waiter(struct command *cmd, const struct invoice *paid)
|
static void tell_waiter(struct command *cmd, const struct invoice *paid)
|
||||||
{
|
{
|
||||||
struct json_result *response = new_json_result(cmd);
|
struct json_result *response = new_json_result(cmd);
|
||||||
|
|
||||||
json_object_start(response, NULL);
|
json_add_invoice(response, paid);
|
||||||
json_add_string(response, "label", paid->label);
|
|
||||||
json_add_hex(response, "rhash", &paid->rhash, sizeof(paid->rhash));
|
|
||||||
if (paid->msatoshi)
|
|
||||||
json_add_u64(response, "msatoshi", *paid->msatoshi);
|
|
||||||
json_add_bool(response, "complete", paid->state == PAID);
|
|
||||||
if (paid->state == PAID)
|
|
||||||
json_add_u64(response, "pay_index", paid->pay_index);
|
|
||||||
json_object_end(response);
|
|
||||||
command_success(cmd, response);
|
command_success(cmd, response);
|
||||||
}
|
}
|
||||||
static void tell_waiter_deleted(struct command *cmd, const struct invoice *paid)
|
static void tell_waiter_deleted(struct command *cmd, const struct invoice *paid)
|
||||||
@ -291,7 +298,7 @@ static const struct json_command invoice_command = {
|
|||||||
"invoice",
|
"invoice",
|
||||||
json_invoice,
|
json_invoice,
|
||||||
"Create invoice for {msatoshi} with {label} and {description} with optional {expiry} seconds (default 1 hour)",
|
"Create invoice for {msatoshi} with {label} and {description} with optional {expiry} seconds (default 1 hour)",
|
||||||
"Returns the {rhash}, {expiry_time} and {bolt11} on success, and {description} if too alrge for {bolt11}. "
|
"Returns the {rhash}, {expiry_time} and {bolt11} on success, and {description} if too large for {bolt11}. "
|
||||||
};
|
};
|
||||||
AUTODATA(json_command, &invoice_command);
|
AUTODATA(json_command, &invoice_command);
|
||||||
|
|
||||||
@ -307,14 +314,7 @@ static void json_add_invoices(struct json_result *response,
|
|||||||
list_for_each(list, i, list) {
|
list_for_each(list, i, list) {
|
||||||
if (lbl && !streq(i->label, lbl))
|
if (lbl && !streq(i->label, lbl))
|
||||||
continue;
|
continue;
|
||||||
json_object_start(response, NULL);
|
json_add_invoice(response, i);
|
||||||
json_add_string(response, "label", i->label);
|
|
||||||
json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash));
|
|
||||||
if (i->msatoshi)
|
|
||||||
json_add_u64(response, "msatoshi", *i->msatoshi);
|
|
||||||
json_add_bool(response, "complete", i->state == PAID);
|
|
||||||
json_add_u64(response, "expiry_time", i->expiry_time);
|
|
||||||
json_object_end(response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,7 +343,7 @@ static const struct json_command listinvoice_command = {
|
|||||||
"listinvoice",
|
"listinvoice",
|
||||||
json_listinvoice,
|
json_listinvoice,
|
||||||
"Show invoice {label} (or all, if no {label}))",
|
"Show invoice {label} (or all, if no {label}))",
|
||||||
"Returns an array of {label}, {rhash}, {msatoshi} and {complete} on success. "
|
"Returns an array of {label}, {rhash}, {msatoshi} (if set), {complete}, {pay_index} (if paid) and {expiry_time} on success. "
|
||||||
};
|
};
|
||||||
AUTODATA(json_command, &listinvoice_command);
|
AUTODATA(json_command, &listinvoice_command);
|
||||||
|
|
||||||
@ -374,12 +374,7 @@ static void json_delinvoice(struct command *cmd,
|
|||||||
|
|
||||||
/* Get invoice details before attempting to delete, as
|
/* Get invoice details before attempting to delete, as
|
||||||
* otherwise the invoice will be freed. */
|
* otherwise the invoice will be freed. */
|
||||||
json_object_start(response, NULL);
|
json_add_invoice(response, i);
|
||||||
json_add_string(response, "label", i->label);
|
|
||||||
json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash));
|
|
||||||
if (i->msatoshi)
|
|
||||||
json_add_u64(response, "msatoshi", *i->msatoshi);
|
|
||||||
json_object_end(response);
|
|
||||||
|
|
||||||
error = delete_invoice(cmd, cmd->ld->wallet, invs, i);
|
error = delete_invoice(cmd, cmd->ld->wallet, invs, i);
|
||||||
|
|
||||||
@ -400,7 +395,7 @@ static const struct json_command delinvoice_command = {
|
|||||||
"delinvoice",
|
"delinvoice",
|
||||||
json_delinvoice,
|
json_delinvoice,
|
||||||
"Delete unpaid invoice {label}))",
|
"Delete unpaid invoice {label}))",
|
||||||
"Returns {label}, {rhash} and {msatoshi} on success. "
|
"Returns {label}, {rhash}, {msatoshi} (if set), {complete}, {pay_index} (if paid) and {expiry_time} on success. "
|
||||||
};
|
};
|
||||||
AUTODATA(json_command, &delinvoice_command);
|
AUTODATA(json_command, &delinvoice_command);
|
||||||
|
|
||||||
@ -440,15 +435,7 @@ static void json_waitanyinvoice(struct command *cmd,
|
|||||||
if (inv) {
|
if (inv) {
|
||||||
response = new_json_result(cmd);
|
response = new_json_result(cmd);
|
||||||
|
|
||||||
json_object_start(response, NULL);
|
json_add_invoice(response, inv);
|
||||||
json_add_string(response, "label", inv->label);
|
|
||||||
json_add_hex(response, "rhash", &inv->rhash, sizeof(inv->rhash));
|
|
||||||
if (inv->msatoshi)
|
|
||||||
json_add_u64(response, "msatoshi", *inv->msatoshi);
|
|
||||||
json_add_bool(response, "complete", true);
|
|
||||||
json_add_u64(response, "pay_index", inv->pay_index);
|
|
||||||
json_object_end(response);
|
|
||||||
|
|
||||||
command_success(cmd, response);
|
command_success(cmd, response);
|
||||||
|
|
||||||
/* inv is freed when cmd is freed, and command_success
|
/* inv is freed when cmd is freed, and command_success
|
||||||
@ -468,7 +455,7 @@ static const struct json_command waitanyinvoice_command = {
|
|||||||
"waitanyinvoice",
|
"waitanyinvoice",
|
||||||
json_waitanyinvoice,
|
json_waitanyinvoice,
|
||||||
"Wait for the next invoice to be paid, after {lastpay_index} (if supplied)))",
|
"Wait for the next invoice to be paid, after {lastpay_index} (if supplied)))",
|
||||||
"Returns {label}, {rhash}, {msatoshi}, and {pay_index} on success. "
|
"Returns {label}, {rhash}, {msatoshi} (if set), {complete}, {pay_index} and {expiry_time} on success. "
|
||||||
};
|
};
|
||||||
AUTODATA(json_command, &waitanyinvoice_command);
|
AUTODATA(json_command, &waitanyinvoice_command);
|
||||||
|
|
||||||
@ -515,7 +502,7 @@ static const struct json_command waitinvoice_command = {
|
|||||||
"waitinvoice",
|
"waitinvoice",
|
||||||
json_waitinvoice,
|
json_waitinvoice,
|
||||||
"Wait for an incoming payment matching the invoice with {label}",
|
"Wait for an incoming payment matching the invoice with {label}",
|
||||||
"Returns {label}, {rhash} and {msatoshi} on success"
|
"Returns {label}, {rhash}, {msatoshi} (if set), {complete}, {pay_index} and {expiry_time} on success"
|
||||||
};
|
};
|
||||||
AUTODATA(json_command, &waitinvoice_command);
|
AUTODATA(json_command, &waitinvoice_command);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user