try-catch potentially fallible mutiny_wallet calls

This commit is contained in:
Paul Miller
2023-06-20 13:54:52 -05:00
parent 5a7e67ea05
commit 6c4939603c
10 changed files with 139 additions and 87 deletions

View File

@@ -1,13 +1,5 @@
import { NiceP } from "./layout";
import {
For,
Match,
Show,
Switch,
createEffect,
createSignal,
onMount
} from "solid-js";
import { For, Match, Show, Switch, createEffect, createSignal } from "solid-js";
import { useMegaStore } from "~/state/megaStore";
import { ActivityItem as MutinyActivity } from "@mutinywallet/mutiny-wasm";
import { ActivityItem, HackActivityType } from "./ActivityItem";

View File

@@ -162,14 +162,19 @@ export const AmountEditable: ParentComponent<{
let fiatInputRef!: HTMLInputElement;
const [inboundCapacity] = createResource(async () => {
const channels = await state.mutiny_wallet?.list_channels();
let inbound = 0;
try {
const channels = await state.mutiny_wallet?.list_channels();
let inbound = 0;
for (const channel of channels) {
inbound += channel.size - (channel.balance + channel.reserve);
for (const channel of channels) {
inbound += channel.size - (channel.balance + channel.reserve);
}
return inbound;
} catch (e) {
console.error(e);
return 0;
}
return inbound;
});
const warningText = () => {

View File

@@ -353,33 +353,44 @@ export function DetailsIdModal(props: {
// TODO: is there a cleaner way to do refetch when id changes?
const [data, { refetch }] = createResource(async () => {
if (kind() === "Lightning") {
console.debug("reading invoice: ", id());
const invoice = await state.mutiny_wallet?.get_invoice_by_hash(
id()
);
return invoice;
} else if (kind() === "ChannelClose") {
console.debug("reading channel close: ", id());
const closeItem = await state.mutiny_wallet?.get_channel_closure(
id()
);
try {
if (kind() === "Lightning") {
console.debug("reading invoice: ", id());
const invoice = await state.mutiny_wallet?.get_invoice_by_hash(
id()
);
return invoice;
} else if (kind() === "ChannelClose") {
console.debug("reading channel close: ", id());
const closeItem =
await state.mutiny_wallet?.get_channel_closure(id());
return closeItem;
} else {
console.debug("reading tx: ", id());
const tx = await state.mutiny_wallet?.get_transaction(id());
return closeItem;
} else {
console.debug("reading tx: ", id());
const tx = await state.mutiny_wallet?.get_transaction(id());
return tx;
return tx;
}
} catch (e) {
console.error(e);
return undefined;
}
});
const tags = createMemo(() => {
if (data() && data().labels && data().labels.length > 0) {
const contact = state.mutiny_wallet?.get_contact(data().labels[0]);
if (contact) {
return [tagToMutinyTag(contact)];
} else {
try {
const contact = state.mutiny_wallet?.get_contact(
data().labels[0]
);
if (contact) {
return [tagToMutinyTag(contact)];
} else {
return [];
}
} catch (e) {
console.error(e);
return [];
}
} else {

View File

@@ -36,17 +36,22 @@ export function LiquidityMonitor() {
const [state, _actions] = useMegaStore();
const [channelInfo] = createResource(async () => {
const channels = await state.mutiny_wallet?.list_channels();
let inbound = 0n;
try {
const channels = await state.mutiny_wallet?.list_channels();
let inbound = 0n;
for (const channel of channels) {
inbound =
inbound +
BigInt(channel.size) -
BigInt(channel.balance + channel.reserve);
for (const channel of channels) {
inbound =
inbound +
BigInt(channel.size) -
BigInt(channel.balance + channel.reserve);
}
return { inbound, channelCount: channels?.length };
} catch (e) {
console.error(e);
return { inbound: 0, channelCount: 0 };
}
return { inbound, channelCount: channels?.length };
});
return (

View File

@@ -6,8 +6,16 @@ export function Logs() {
const [state, _] = useMegaStore();
async function handleSave() {
const logs = await state.mutiny_wallet?.get_logs();
downloadTextFile(logs.join("") || "", "mutiny-logs.txt", "text/plain");
try {
const logs = await state.mutiny_wallet?.get_logs();
downloadTextFile(
logs.join("") || "",
"mutiny-logs.txt",
"text/plain"
);
} catch (e) {
console.error(e);
}
}
return (

View File

@@ -7,12 +7,16 @@ export function Restart() {
const [hasStopped, setHasStopped] = createSignal(false);
async function toggle() {
if (hasStopped()) {
await state.mutiny_wallet?.start();
setHasStopped(false);
} else {
await state.mutiny_wallet?.stop();
setHasStopped(true);
try {
if (hasStopped()) {
await state.mutiny_wallet?.start();
setHasStopped(false);
} else {
await state.mutiny_wallet?.stop();
setHasStopped(true);
}
} catch (e) {
console.error(e);
}
}

View File

@@ -26,23 +26,32 @@ import { LoadingShimmer } from "~/components/BalanceBox";
function ContactRow() {
const [state, _actions] = useMegaStore();
const [contacts, { refetch }] = createResource(async () => {
const contacts = state.mutiny_wallet?.get_contacts();
try {
const contacts = state.mutiny_wallet?.get_contacts();
// FIXME: this is just types shenanigans I believe
const c: Contact[] = [];
if (contacts) {
for (const contact in contacts) {
c.push(contacts[contact]);
// FIXME: this is just types shenanigans I believe
const c: Contact[] = [];
if (contacts) {
for (const contact in contacts) {
c.push(contacts[contact]);
}
}
return c || [];
} catch (e) {
console.error(e);
return [];
}
return c || [];
});
const [gradients] = createResource(contacts, gradientsPerContact);
async function createContact(contact: ContactFormValues) {
// FIXME: npub not valid? other undefineds
const c = new Contact(contact.name, undefined, undefined, undefined);
await state.mutiny_wallet?.create_new_contact(c);
try {
await state.mutiny_wallet?.create_new_contact(c);
} catch (e) {
console.error(e);
}
refetch();
}

View File

@@ -188,10 +188,14 @@ export default function Receive() {
undefined,
undefined
);
const newContactId =
await state.mutiny_wallet?.create_new_contact(c);
if (newContactId) {
return [newContactId];
try {
const newContactId =
await state.mutiny_wallet?.create_new_contact(c);
if (newContactId) {
return [newContactId];
}
} catch (e) {
console.error(e);
}
}
@@ -221,7 +225,6 @@ export default function Receive() {
return `bitcoin:${raw?.address}?${params}`;
} catch (e) {
showToast(new Error("Failed to create invoice. Please try again."));
console.error(e);
}
@@ -245,27 +248,33 @@ export default function Receive() {
const lightning = bip21.invoice;
const address = bip21.address;
const invoice = await state.mutiny_wallet?.get_invoice(lightning);
try {
const invoice = await state.mutiny_wallet?.get_invoice(
lightning
);
// If the invoice has a fees amount that's probably the LSP fee
if (invoice?.fees_paid) {
setLspFee(invoice.fees_paid);
}
// If the invoice has a fees amount that's probably the LSP fee
if (invoice?.fees_paid) {
setLspFee(invoice.fees_paid);
}
if (invoice && invoice.paid) {
setReceiveState("paid");
setPaymentInvoice(invoice);
return "lightning_paid";
}
if (invoice && invoice.paid) {
setReceiveState("paid");
setPaymentInvoice(invoice);
return "lightning_paid";
}
const tx = (await state.mutiny_wallet?.check_address(address)) as
| OnChainTx
| undefined;
const tx = (await state.mutiny_wallet?.check_address(
address
)) as OnChainTx | undefined;
if (tx) {
setReceiveState("paid");
setPaymentTx(tx);
return "onchain_paid";
if (tx) {
setReceiveState("paid");
setPaymentTx(tx);
return "onchain_paid";
}
} catch (e) {
console.error(e);
}
}
}

View File

@@ -408,10 +408,14 @@ export default function Send() {
undefined,
undefined
);
const newContactId =
await state.mutiny_wallet?.create_new_contact(c);
if (newContactId) {
return [newContactId];
try {
const newContactId =
await state.mutiny_wallet?.create_new_contact(c);
if (newContactId) {
return [newContactId];
}
} catch (e) {
console.error(e);
}
}

View File

@@ -208,7 +208,12 @@ export const Provider: ParentComponent = (props) => {
setState({ dismissed_restore_prompt: true });
},
async listTags(): Promise<MutinyTagItem[]> {
return state.mutiny_wallet?.get_tag_items() as MutinyTagItem[];
try {
return state.mutiny_wallet?.get_tag_items() as MutinyTagItem[];
} catch (e) {
console.error(e);
return [];
}
},
setNwc(enabled: boolean) {
localStorage.setItem("nwc_enabled", enabled.toString());