Browse Source

pay: sendpay and waitsendpay print similar to listpayments

ppa-0.6.1
ZmnSCPxj 7 years ago
committed by Rusty Russell
parent
commit
022cfccddd
  1. 76
      lightningd/pay.c

76
lightningd/pay.c

@ -778,21 +778,56 @@ send_payment(const tal_t *ctx,
return true; return true;
} }
/*-----------------------------------------------------------------------------
Utility
-----------------------------------------------------------------------------*/
/* Outputs fields, not a separate object*/
static void
json_add_payment_fields(struct json_result *response,
const struct wallet_payment *t)
{
json_add_u64(response, "id", t->id);
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
json_add_pubkey(response, "destination", &t->destination);
json_add_u64(response, "msatoshi", t->msatoshi);
if (deprecated_apis)
json_add_u64(response, "timestamp", t->timestamp);
json_add_u64(response, "created_at", t->timestamp);
switch (t->status) {
case PAYMENT_PENDING:
json_add_string(response, "status", "pending");
break;
case PAYMENT_COMPLETE:
json_add_string(response, "status", "complete");
break;
case PAYMENT_FAILED:
json_add_string(response, "status", "failed");
break;
}
if (t->payment_preimage)
json_add_hex(response, "payment_preimage",
t->payment_preimage,
sizeof(*t->payment_preimage));
}
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
JSON-RPC sendpay interface JSON-RPC sendpay interface
-----------------------------------------------------------------------------*/ -----------------------------------------------------------------------------*/
static void static void
json_sendpay_success(struct command *cmd, json_sendpay_success(struct command *cmd,
const struct preimage *payment_preimage) const struct sendpay_result *r)
{ {
struct json_result *response; struct json_result *response;
assert(r->payment->status == PAYMENT_COMPLETE);
response = new_json_result(cmd); response = new_json_result(cmd);
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_bool(response, "completed", true); json_add_payment_fields(response, r->payment);
json_add_hex(response, "payment_preimage",
payment_preimage, sizeof(*payment_preimage));
json_object_end(response); json_object_end(response);
command_success(cmd, response); command_success(cmd, response);
} }
@ -807,10 +842,13 @@ static void json_waitsendpay_on_resolve(const struct sendpay_result *r,
struct routing_failure *fail; struct routing_failure *fail;
if (r->succeeded) if (r->succeeded)
json_sendpay_success(cmd, &r->preimage); json_sendpay_success(cmd, r);
else { else {
switch (r->errorcode) { switch (r->errorcode) {
/* We will never handle this case */
case PAY_IN_PROGRESS: case PAY_IN_PROGRESS:
abort();
case PAY_RHASH_ALREADY_USED: case PAY_RHASH_ALREADY_USED:
case PAY_UNSPECIFIED_ERROR: case PAY_UNSPECIFIED_ERROR:
case PAY_NO_SUCH_PAYMENT: case PAY_NO_SUCH_PAYMENT:
@ -880,7 +918,7 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_string(response, "message", json_add_string(response, "message",
"Monitor status with listpayments or waitsendpay"); "Monitor status with listpayments or waitsendpay");
json_add_bool(response, "completed", false); json_add_payment_fields(response, r->payment);
json_object_end(response); json_object_end(response);
command_success(cmd, response); command_success(cmd, response);
} else } else
@ -1090,32 +1128,8 @@ static void json_listpayments(struct command *cmd, const char *buffer,
json_object_start(response, NULL); json_object_start(response, NULL);
json_array_start(response, "payments"); json_array_start(response, "payments");
for (int i=0; i<tal_count(payments); i++) { for (int i=0; i<tal_count(payments); i++) {
const struct wallet_payment *t = payments[i];
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_u64(response, "id", t->id); json_add_payment_fields(response, payments[i]);
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
json_add_pubkey(response, "destination", &t->destination);
json_add_u64(response, "msatoshi", t->msatoshi);
if (deprecated_apis)
json_add_u64(response, "timestamp", t->timestamp);
json_add_u64(response, "created_at", t->timestamp);
switch (t->status) {
case PAYMENT_PENDING:
json_add_string(response, "status", "pending");
break;
case PAYMENT_COMPLETE:
json_add_string(response, "status", "complete");
break;
case PAYMENT_FAILED:
json_add_string(response, "status", "failed");
break;
}
if (t->payment_preimage)
json_add_hex(response, "payment_preimage",
t->payment_preimage,
sizeof(*t->payment_preimage));
json_object_end(response); json_object_end(response);
} }
json_array_end(response); json_array_end(response);

Loading…
Cancel
Save