Skip to content

Commit

Permalink
Merge pull request #395 from stakwork/fi/cln-err-msg
Browse files Browse the repository at this point in the history
Fi/cln err msg
  • Loading branch information
Evanfeenstra authored Nov 14, 2024
2 parents 04788b0 + 36a6d75 commit 339d2b0
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 44 deletions.
17 changes: 12 additions & 5 deletions app/src/api/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,21 @@ export async function send_cmd(type: CmdType, data: CmdData, tag?: string) {
},
}
);

ret = await r.text();
const jj = JSON.parse(ret);
if (jj && jj["stack_error"]) {
console.warn("=> cmd err:", jj["stack_error"]);
return jj["stack_error"];
try {
const jj = JSON.parse(ret);
if (jj && jj["stack_error"]) {
console.warn("=> cmd err:", jj["stack_error"]);
return jj["stack_error"];
}
return jj;
} catch (error) {
console.log("error parsing json: ", error);
return ret;
}
return jj;
} catch (e) {
console.warn("=> cmd error:", ret, e);
console.log(e);
}
}
27 changes: 26 additions & 1 deletion app/src/helpers/cln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export function parseClnListPeerChannelsRes(res: {
export function parseClnListPeerRes(res: {
peers: { id: Buffer; netaddr; channels }[];
}): LndPeer[] {
if (typeof res !== "object") {
return [];
}
// pub_key: string;
// address: string;
// bytes_sent: number;
Expand Down Expand Up @@ -76,6 +79,12 @@ export function parseClnListPeerRes(res: {
}

function parseClnPeerChannelList(channels: any): LndChannel[] {
if (!channels) {
return [];
}
if (!Array.isArray(channels)) {
return [];
}
const parsedChannels = channels.map((channel, index: number) => {
return <LndChannel>{
remote_pubkey: bufferToHexString(channel.peer_id),
Expand Down Expand Up @@ -123,8 +132,18 @@ function getChannelStatus(status) {
}

function convertPeerChannelArrayToObj(peerChanObj) {
// console.log("=>", peerChanObj);
console.log("=>", peerChanObj);

const obj = {};

if (typeof peerChanObj !== "object") {
return obj;
}

if (!peerChanObj) {
return obj;
}

for (let i = 0; i < peerChanObj.channels.length; i++) {
const channel = peerChanObj.channels[i];
obj[channel.short_channel_id] = channel;
Expand All @@ -135,6 +154,9 @@ function convertPeerChannelArrayToObj(peerChanObj) {
export function parseClnListFunds(res, peersChans): number {
let balance = 0;
let channelBal = 0;
if (typeof res !== "object") {
return 0;
}
const channelsObj = convertPeerChannelArrayToObj(peersChans);

for (let i = 0; i < res.channels.length; i++) {
Expand All @@ -155,6 +177,9 @@ export function parseClnListFunds(res, peersChans): number {
}

export function parseUnconfirmedClnBalance(res): number {
if (typeof res !== "object") {
return 0;
}
let balance = 0;
for (let i = 0; i < res.outputs.length; i++) {
let output = res.outputs[i];
Expand Down
3 changes: 3 additions & 0 deletions app/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export function convertBtcToSats(num) {
}

export function bufferToHexString(byteArray) {
if (!byteArray) {
return "";
}
return Array.from(byteArray, function (byte: any) {
return ("0" + (byte & 0xff).toString(16)).slice(-2);
}).join("");
Expand Down
20 changes: 16 additions & 4 deletions app/src/lnd/Peers.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@
$: peers = $peersStore && $peersStore[tag];
let show_notification = false;
let message = "";
async function addPeer() {
if (type === "Cln") {
const peer = await CLN.add_peer(tag, pubkey, host);
show_notification = true;
if (typeof peer === "string") {
message = peer;
return;
}
if (typeof peer !== "object") {
message = "unexpected error";
console.log(peer);
return;
}
if (peer) {
show_notification = true;
pubkey = "";
host = "";
const peersData = await CLN.list_peers(tag);
Expand Down Expand Up @@ -101,9 +113,9 @@
{#if show_notification}
<InlineNotification
lowContrast
kind="success"
title="Success:"
subtitle="Pair has been added."
kind={message ? "error" : "success"}
title={message ? "Error" : "Success:"}
subtitle={message || "Pair has been added."}
timeout={3000}
on:close={(e) => {
e.preventDefault();
Expand Down
36 changes: 34 additions & 2 deletions app/src/lnd/invoices/AddInvoice.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script lang="ts">
import { Button, TextInput } from "carbon-components-svelte";
import {
Button,
InlineNotification,
TextInput,
} from "carbon-components-svelte";
import Add from "carbon-icons-svelte/lib/Add.svelte";
import * as LND from "../../api/lnd";
import * as CLN from "../../api/cln";
Expand All @@ -15,14 +19,29 @@
$: invDisabled = !amount;
$: invoice = $activeInvoice[tag] || "";
let message = "";
let show_notification = false;
let success = false;
async function newInvoice() {
if (type === "Cln") {
const invoiceRes = await CLN.add_invoice(
tag,
convertSatsToMilliSats(amount)
);
if (invoiceRes) {
show_notification = true;
if (typeof invoiceRes === "string") {
message = invoiceRes;
return;
}
if (typeof invoiceRes !== "object") {
message = "invalid response";
console.log(invoiceRes);
return;
}
if (invoiceRes && invoiceRes.bolt11) {
success = true;
message = "Invoice created successfully";
activeInvoice.update((inv) => {
return { ...inv, [tag]: invoiceRes.bolt11 };
});
Expand All @@ -43,6 +62,19 @@
</script>

<main>
{#if show_notification}
<InlineNotification
lowContrast
kind={success ? "success" : "error"}
title={success ? "Success:" : "Error:"}
subtitle={message}
timeout={9000}
on:close={(e) => {
e.preventDefault();
show_notification = false;
}}
/>
{/if}
<section class="invoice-wrap">
<TextInput
labelText={"Amount (satoshis)"}
Expand Down
10 changes: 9 additions & 1 deletion app/src/lnd/invoices/PayInvoice.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
if (type === "Cln") {
const payRes = await CLN.pay_invoice(tag, pay_req);
show_notification = true;
if (typeof payRes === "string") {
message = payRes;
return;
}
if (typeof payRes !== "object") {
message = "an unexpected error occurred";
return;
}
if (payRes.status === 0) {
success = true;
message = "Invoice payment has been made.";
Expand Down Expand Up @@ -79,7 +87,7 @@
kind={success ? "success" : "error"}
title={success ? "Success:" : "Error:"}
subtitle={message}
timeout={3000}
timeout={9000}
on:close={(e) => {
e.preventDefault();
show_notification = false;
Expand Down
13 changes: 11 additions & 2 deletions app/src/lnd/invoices/PayKeysend.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@
window.maxfeepercent,
window.exemptfee
);
show_notification = true;
if (typeof payRes === "string") {
payment_error = payRes;
return;
}
if (typeof payRes !== "object") {
payment_error = "unexpected error occured";
console.log(payRes);
return;
}
if (payRes) {
show_notification = true;
payment_error = "";
dest = "";
amount = 0;
Expand Down Expand Up @@ -85,7 +94,7 @@
kind={payment_error ? "error" : "success"}
title={payment_error ? "Failure:" : "Success:"}
subtitle={payment_error || "Keysend payment has been made."}
timeout={4000}
timeout={9000}
on:close={(e) => {
e.preventDefault();
show_notification = false;
Expand Down
Loading

0 comments on commit 339d2b0

Please sign in to comment.