From 8ce9e65a8dd76c6b11464ec013901ce12c4314d5 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 14 Jan 2020 18:26:14 -0400 Subject: [PATCH] better handle of sendpaymentsync response --- services/gunDB/contact-api/actions.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index e81d5ff5..a9c29cfb 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -921,18 +921,31 @@ const sendPayment = async (to, amount, memo, gun, user, SEA) => { services: { lightning } } = LightningServices + /** + * Partial + * https://api.lightning.community/#grpc-response-sendresponse-2 + * @typedef {object} SendResponse + * @prop {string|null} payment_error + * @prop {any[]|null} payment_route + */ + await new Promise((rej, resolve) => { lightning.sendPaymentSync( { payment_request: decInvoice }, - (/** @type {any} */ err, /** @type {any} */ res) => { - if (err) { - console.log(err) - rej(err) - } else { - console.log(res) + (_, /** @type {SendResponse} */ res) => { + if (res.payment_error) { + rej(new Error(res.payment_error)) + } else if (res.payment_route) { resolve() + } else { + rej( + new Error( + 'Unexpected response from sendPaymentSync() -> ' + + JSON.stringify(res) + ) + ) } } )