Skip to content

Commit

Permalink
feat: safety check before calculating fees
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 6, 2025
1 parent fa0679c commit 51e311c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
10 changes: 9 additions & 1 deletion e2e/chainSwaps/chainSwaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
generateBitcoinBlock,
generateLiquidBlock,
getBitcoinAddress,
getBitcoinWalletTx,
} from "../utils";

test.describe("Chain swap", () => {
Expand Down Expand Up @@ -56,6 +57,13 @@ test.describe("Chain swap", () => {

await elementsSendToAddress(sendAddress, sendAmount);
await generateLiquidBlock();
// TODO: verify amounts

const txIdLink = page.getByText("open claim transaction");

const txId = (await txIdLink.getAttribute("href")).split("/").pop();
expect(txId).toBeDefined();

const txInfo = JSON.parse(await getBitcoinWalletTx(txId));
expect(txInfo.amount.toString()).toEqual(receiveAmount);
});
});
53 changes: 52 additions & 1 deletion e2e/chainSwaps/zeroAmount.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
import { expect, test } from "@playwright/test";

import { getBitcoinAddress } from "../utils";
import {
bitcoinSendToAddress,
generateBitcoinBlock,
getBitcoinAddress,
getElementsWalletTx,
getLiquidAddress,
} from "../utils";

test.describe("Chain Swap 0-amount", () => {
test("BTC/L-BTC", async ({ page }) => {
await page.goto("/");

await page
.locator(
"div:nth-child(3) > .asset-wrap > .asset > .asset-selection > .arrow-down",
)
.click();
await page.getByTestId("select-L-BTC").click();

await page.locator(".arrow-down").first().click();
await page.getByTestId("select-BTC").click();

await page.getByTestId("onchainAddress").click();

const liquidAddress = await getLiquidAddress();
await page.getByTestId("onchainAddress").fill(liquidAddress);
await page.getByTestId("create-swap-button").click();

await page.getByRole("button", { name: "Skip download" }).click();

const buttons = page.locator("div[data-testid='pay-onchain-buttons']");
const copyAddressButton = buttons.getByText("address");
expect(copyAddressButton).toBeDefined();
await copyAddressButton.click();

const sendAddress = await page.evaluate(() => {
return navigator.clipboard.readText();
});
expect(sendAddress).toBeDefined();

await bitcoinSendToAddress(sendAddress, "0.01");

await page.getByRole("button", { name: "Accept" }).click();
await generateBitcoinBlock();

const txIdLink = page.getByText("open claim transaction");

const txId = (await txIdLink.getAttribute("href")).split("/").pop();
expect(txId).toBeDefined();

const txInfo = JSON.parse(await getElementsWalletTx(txId));
expect(txInfo.amount.bitcoin.toString()).toEqual("0.0099865");
});

test("should allow 0-amount chain swaps", async ({ page }) => {
await page.goto("/");

Expand Down
3 changes: 3 additions & 0 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const generateLiquidBlock = (): Promise<string> =>
export const getBitcoinWalletTx = (txId: string): Promise<string> =>
execCommand(`bitcoin-cli-sim-client gettransaction ${txId}`);

export const getElementsWalletTx = (txId: string): Promise<string> =>
execCommand(`elements-cli-sim-client gettransaction ${txId}`);

export const payInvoiceLnd = (invoice: string): Promise<string> =>
execCommand(`lncli-sim 1 payinvoice -f ${invoice}`);

Expand Down
2 changes: 1 addition & 1 deletion regtest
2 changes: 1 addition & 1 deletion src/components/SwapChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export const SwapChecker = () => {
);
} catch (e) {
const msg = t("claim_fail", { id: currentSwap.id });
log.warn(msg, formatError(e));
log.warn(msg, await handleRequestError(e));
notify("error", msg);
}
} else if (
Expand Down
4 changes: 4 additions & 0 deletions src/status/TransactionLockupFailed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ const TransactionLockupFailed = (props: {
const newSwap = swap() as ChainSwap;

const { quote, receiveAmount } = newQuote();
log.info(
`Accepting new quote for swap ${newSwap.id}`,
{ quote, receiveAmount },
);
newSwap.receiveAmount = receiveAmount;
newSwap.claimDetails.amount = quote;

Expand Down

0 comments on commit 51e311c

Please sign in to comment.