diff --git a/src/components/AmountCard.tsx b/src/components/AmountCard.tsx index c761f5a..010d8a8 100644 --- a/src/components/AmountCard.tsx +++ b/src/components/AmountCard.tsx @@ -77,7 +77,6 @@ export function AmountCard(props: { }) { // Normally we want to add the fee to the amount, but for max amount we just show the max const totalOrTotalLessFee = () => { - console.log(props.amountSats, props.fee, props.maxAmountSats); if ( props.fee && props.maxAmountSats && diff --git a/src/routes/Receive.tsx b/src/routes/Receive.tsx index 1e29b38..2b643c7 100644 --- a/src/routes/Receive.tsx +++ b/src/routes/Receive.tsx @@ -221,9 +221,8 @@ export default function Receive() { return `bitcoin:${raw?.address}?${params}`; } catch (e) { - showToast( - new Error("Couldn't create invoice. Are you asking for enough?") - ); + + showToast(new Error("Failed to create invoice. Please try again.")); console.error(e); } } diff --git a/src/routes/Swap.tsx b/src/routes/Swap.tsx index 4e4996d..54edfc7 100644 --- a/src/routes/Swap.tsx +++ b/src/routes/Swap.tsx @@ -5,6 +5,7 @@ import { Match, Show, Switch, + createMemo, createResource, createSignal } from "solid-js"; @@ -54,28 +55,13 @@ export default function Swap() { const [amountSats, setAmountSats] = createSignal(0n); const [isConnecting, setIsConnecting] = createSignal(false); + const [loading, setLoading] = createSignal(false); + const [selectedPeer, setSelectedPeer] = createSignal(""); const [channelOpenResult, setChannelOpenResult] = createSignal(); - const feeEstimate = () => { - if (amountSats()) { - try { - return state.mutiny_wallet?.estimate_tx_fee( - CHANNEL_FEE_ESTIMATE_ADDRESS, - amountSats(), - undefined - ); - } catch (e) { - console.error(e); - // showToast(eify(new Error("Unsufficient funds"))) - return undefined; - } - } - return undefined; - }; - const hasLsp = () => { return ( !!localStorage.getItem("MUTINY_SETTINGS_lsp") || @@ -136,21 +122,28 @@ export default function Swap() { const handleSwap = async () => { if (canSwap()) { try { + setLoading(true); const nodes = await state.mutiny_wallet?.list_nodes(); const firstNode = (nodes[0] as string) || ""; - if (hasLsp()) { - const new_channel = await state.mutiny_wallet?.open_channel( - firstNode, - undefined, - amountSats() - ); + let peer = undefined; + + if (!hasLsp()) { + peer = selectedPeer(); + } + + if (isMax()) { + const new_channel = + await state.mutiny_wallet?.sweep_all_to_channel( + firstNode, + peer + ); setChannelOpenResult({ channel: new_channel }); } else { const new_channel = await state.mutiny_wallet?.open_channel( firstNode, - selectedPeer(), + peer, amountSats() ); @@ -159,6 +152,8 @@ export default function Swap() { } catch (e) { setChannelOpenResult({ failure_reason: eify(e) }); // showToast(eify(e)) + } finally { + setLoading(false); } } }; @@ -185,6 +180,11 @@ export default function Swap() { }; const amountWarning = () => { + // Balance can go down during swap so... + if (loading() || !!channelOpenResult()) { + return undefined; + } + const network = state.mutiny_wallet?.get_network() as Network; if (network === "bitcoin" && amountSats() < 50000n) { @@ -207,6 +207,44 @@ export default function Swap() { return undefined; }; + const maxOnchain = createMemo(() => { + return ( + (state.balance?.confirmed ?? 0n) + + (state.balance?.unconfirmed ?? 0n) + ); + }); + + const isMax = createMemo(() => { + return amountSats() === maxOnchain(); + }); + + const feeEstimate = createMemo(() => { + // Balance can go down during swap so... + if (loading() || !!channelOpenResult()) { + return undefined; + } + + // If max we want to use the sweep fee estimator + if (amountSats() === maxOnchain()) { + return state.mutiny_wallet?.estimate_sweep_channel_open_fee(); + } + + if (amountSats()) { + try { + return state.mutiny_wallet?.estimate_tx_fee( + CHANNEL_FEE_ESTIMATE_ADDRESS, + amountSats(), + undefined + ); + } catch (e) { + console.error(e); + // showToast(eify(new Error("Unsufficient funds"))) + return undefined; + } + } + return undefined; + }); + const network = state.mutiny_wallet?.get_network() as Network; return ( @@ -366,6 +404,7 @@ export default function Swap() { fee={feeEstimate()?.toString()} isAmountEditable={true} skipWarnings={true} + maxAmountSats={maxOnchain()} /> 0n}> {amountWarning()} @@ -377,7 +416,7 @@ export default function Swap() { disabled={!canSwap()} intent="blue" onClick={handleSwap} - loading={false} + loading={loading()} > {"Confirm Swap"}