Skip to content

Commit

Permalink
Fixes for olympus swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jun 6, 2024
1 parent bc38a87 commit fa2223d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
29 changes: 17 additions & 12 deletions src/routes/SwapLightning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -69,15 +70,11 @@ export function SwapLightning() {
setLoading(true);
setFeeEstimateWarning(undefined);

if (isMax()) {
const result = await sw.sweep_federation_balance(undefined);

setSweepResult({ result: result });
} else {
const result = await sw.sweep_federation_balance(amountSats());

setSweepResult({ result: result });
}
const result = await sw.sweep_federation_balance_to_invoice(
undefined,
invoice()
);
setSweepResult({ result: result });

await vibrateSuccess();
} catch (e) {
Expand Down Expand Up @@ -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());

Expand Down
32 changes: 16 additions & 16 deletions src/routes/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 });
const mutinyInvoice = await sw.create_sweep_federation_invoice(
isMax() ? undefined : amountSats(),
fromFed()?.federation_id,
toFed()?.federation_id
);

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);
Expand Down
34 changes: 21 additions & 13 deletions src/workers/walletWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,31 +1533,39 @@ 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(

Check failure on line 1544 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / code_quality

Property 'sweep_federation_balance_to_invoice' does not exist on type 'MutinyWallet'. Did you mean 'sweep_federation_balance'?

Check failure on line 1544 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / Build APK

Property 'sweep_federation_balance_to_invoice' does not exist on type 'MutinyWallet'. Did you mean 'sweep_federation_balance'?

Check failure on line 1544 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / Build iOS

Property 'sweep_federation_balance_to_invoice' does not exist on type 'MutinyWallet'. Did you mean 'sweep_federation_balance'?
from_federation_id,
to_federation_id
invoice
);
return { ...result.value } as FedimintSweepResult;
}

/**
* 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(

Check failure on line 1563 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / code_quality

Property 'create_sweep_federation_invoice' does not exist on type 'MutinyWallet'.

Check failure on line 1563 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / Build APK

Property 'create_sweep_federation_invoice' does not exist on type 'MutinyWallet'.

Check failure on line 1563 in src/workers/walletWorker.ts

View workflow job for this annotation

GitHub Actions / Build iOS

Property 'create_sweep_federation_invoice' does not exist on type 'MutinyWallet'.
amount,
from_federation_id,
to_federation_id
);
return destructureInvoice(invoice);
}

export async function parse_params(params: string): Promise<PaymentParams> {
Expand Down

0 comments on commit fa2223d

Please sign in to comment.