Fixes for olympus swaps

This commit is contained in:
benthecarman
2024-06-06 12:14:54 -05:00
committed by Tony Giorgio
parent 549a810dfd
commit 737280ad37
3 changed files with 53 additions and 40 deletions

View File

@@ -47,6 +47,7 @@ export function SwapLightning() {
);
const [amountSats, setAmountSats] = createSignal(0n);
const [invoice, setInvoice] = createSignal("");
const [feeSats, setFeeSats] = createSignal(0n);
const [maxFederationBalanceBeforeSwap, setMaxFederationBalanceBeforeSwap] =
createSignal(0n);
@@ -69,15 +70,11 @@ export function SwapLightning() {
setLoading(true);
setFeeEstimateWarning(undefined);
if (isMax()) {
const result = await sw.sweep_federation_balance(undefined);
const result = await sw.sweep_federation_balance_to_invoice(
undefined,
invoice()
);
setSweepResult({ result: result });
} else {
const result = await sw.sweep_federation_balance(amountSats());
setSweepResult({ result: result });
}
await vibrateSuccess();
} catch (e) {
@@ -129,12 +126,20 @@ export function SwapLightning() {
setLoading(true);
setFeeEstimateWarning(undefined);
const fee = await sw.estimate_sweep_federation_fee(
const mutinyInvoice = await sw.create_sweep_federation_invoice(
isMax() ? undefined : amountSats()
);
if (fee) {
setFeeSats(fee);
if (mutinyInvoice.bolt11) {
setInvoice(mutinyInvoice.bolt11);
} else {
console.error("No invoice created");
// this should never happen
throw new Error("No invoice created");
}
if (mutinyInvoice.fees_paid) {
setFeeSats(mutinyInvoice.fees_paid);
}
setMaxFederationBalanceBeforeSwap(calculateMaxFederation());

View File

@@ -85,24 +85,24 @@ export function Transfer() {
if (!fromFed()) throw new Error("No from federation");
if (!toFed()) throw new Error("No to federation");
if (isMax()) {
const result = await sw.sweep_federation_balance(
undefined,
const mutinyInvoice = await sw.create_sweep_federation_invoice(
isMax() ? undefined : amountSats(),
fromFed()?.federation_id,
toFed()?.federation_id
);
setTransferResult({ result: result });
} else {
const result = await sw.sweep_federation_balance(
amountSats(),
fromFed()?.federation_id,
toFed()?.federation_id
);
setTransferResult({ result: result });
if (!mutinyInvoice.bolt11) {
// this should never happen
throw new Error("No invoice created");
}
const result = await sw.sweep_federation_balance_to_invoice(
fromFed()?.federation_id,
mutinyInvoice.bolt11
);
setTransferResult({ result: result });
await vibrateSuccess();
} catch (e) {
const error = eify(e);

View File

@@ -1588,18 +1588,17 @@ export async function estimate_sweep_channel_open_fee(
/**
* Sweep the federation balance into a lightning channel
* @param {bigint | undefined} [amount]
* @param {string | undefined} [from_federation_id]
* @param {string} [invoice]
* @returns {Promise<FedimintSweepResult>}
*/
export async function sweep_federation_balance(
amount?: bigint,
from_federation_id?: string,
to_federation_id?: string
export async function sweep_federation_balance_to_invoice(
from_federation_id: string | undefined,
invoice: string
): Promise<FedimintSweepResult> {
const result = await wallet!.sweep_federation_balance(
amount,
const result = await wallet!.sweep_federation_balance_to_invoice(
from_federation_id,
to_federation_id
invoice
);
return { ...result.value } as FedimintSweepResult;
}
@@ -1607,12 +1606,21 @@ export async function sweep_federation_balance(
/**
* Estimate the fee before trying to sweep from federation
* @param {bigint | undefined} [amount]
* @returns {Promise<bigint | undefined>}
* @param {string | undefined} [from_federation_id]
* @param {string | undefined} [to_federation_id]
* @returns {Promise<MutinyInvoice>}
*/
export async function estimate_sweep_federation_fee(
amount?: bigint
): Promise<bigint | undefined> {
return await wallet!.estimate_sweep_federation_fee(amount);
export async function create_sweep_federation_invoice(
amount?: bigint,
from_federation_id?: string,
to_federation_id?: string
): Promise<MutinyInvoice> {
const invoice = await wallet!.create_sweep_federation_invoice(
amount,
from_federation_id,
to_federation_id
);
return destructureInvoice(invoice);
}
export async function parse_params(params: string): Promise<PaymentParams> {