Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: safety check before calculating fees #786

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions src/status/TransactionLockupFailed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ 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
4 changes: 4 additions & 0 deletions src/utils/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const createAdjustedClaim = async <
liquidNetwork?: LiquidNetwork,
blindingKey?: Buffer,
) => {
if (swap.receiveAmount === 0) {
throw "amount to be received is 0";
}

const asset = getRelevantAssetForSwap(swap);

let inputSum = 0;
Expand Down
Loading