|
| 1 | +import { Page, expect, request, test } from "@playwright/test"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +import { UTXO } from "../../src/utils/blockchain"; |
| 6 | +import { |
| 7 | + createAndVerifySwap, |
| 8 | + decodeLiquidRawTransaction, |
| 9 | + elementsSendToAddress, |
| 10 | + generateLiquidBlock, |
| 11 | + getLiquidAddress, |
| 12 | + setFailedToPay, |
| 13 | +} from "../utils"; |
| 14 | + |
| 15 | +const getCurrentSwapId = (page: Page) => { |
| 16 | + const url = new URL(page.url()); |
| 17 | + return url.pathname.split("/").pop(); |
| 18 | +}; |
| 19 | + |
| 20 | +const waitForUTXOsInMempool = async (address: string, amount: number) => { |
| 21 | + const requestContext = request.newContext(); |
| 22 | + await expect |
| 23 | + .poll( |
| 24 | + async () => { |
| 25 | + const res = await ( |
| 26 | + await requestContext |
| 27 | + ).get(`http://localhost:4003/api/address/${address}/utxo`); |
| 28 | + |
| 29 | + const utxos = (await res.json()) as UTXO[]; |
| 30 | + return utxos.length === amount; |
| 31 | + }, |
| 32 | + { timeout: 10_000 }, |
| 33 | + ) |
| 34 | + .toBe(true); |
| 35 | +}; |
| 36 | + |
| 37 | +const navigateToSwapDetails = async (page: Page, swapId: string) => { |
| 38 | + await page.getByRole("link", { name: "Refund" }).click(); |
| 39 | + const swapItem = page.locator(`div[data-testid='swaplist-item-${swapId}']`); |
| 40 | + await expect(page.getByTestId("loading-spinner")).not.toBeVisible(); |
| 41 | + await expect(swapItem.getByRole("link", { name: "Refund" })).toBeVisible(); |
| 42 | + await swapItem.click(); |
| 43 | +}; |
| 44 | + |
| 45 | +const validateRefundTxInputs = async (page: Page, expectedInputs: number) => { |
| 46 | + const refundRequest = await page.waitForRequest((req) => |
| 47 | + req.url().includes("/refund"), |
| 48 | + ); |
| 49 | + const broadcastedTx = JSON.parse(refundRequest.postData() || "{}"); |
| 50 | + |
| 51 | + const decodedTx = await decodeLiquidRawTransaction( |
| 52 | + broadcastedTx.transaction, |
| 53 | + ); |
| 54 | + const tx = JSON.parse(decodedTx); |
| 55 | + |
| 56 | + expect(tx.vin.length).toBe(expectedInputs); |
| 57 | +}; |
| 58 | + |
| 59 | +test.describe("Refund", () => { |
| 60 | + const refundFileJson = path.join(__dirname, "rescue.json"); |
| 61 | + |
| 62 | + test.beforeEach(async () => { |
| 63 | + await generateLiquidBlock(); |
| 64 | + }); |
| 65 | + |
| 66 | + test.afterEach(() => { |
| 67 | + if (fs.existsSync(refundFileJson)) { |
| 68 | + fs.unlinkSync(refundFileJson); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + test("Refunds all UTXOs of `invoice.failedToPay`", async ({ page }) => { |
| 73 | + await createAndVerifySwap(page, refundFileJson); |
| 74 | + |
| 75 | + const swapId = getCurrentSwapId(page); |
| 76 | + |
| 77 | + const address = await page.evaluate(() => { |
| 78 | + return navigator.clipboard.readText(); |
| 79 | + }); |
| 80 | + const amount = 0.005; |
| 81 | + const utxoCount = 3; |
| 82 | + |
| 83 | + await setFailedToPay(swapId); |
| 84 | + |
| 85 | + await elementsSendToAddress(address, amount); |
| 86 | + await elementsSendToAddress(address, amount); |
| 87 | + await elementsSendToAddress(address, amount); |
| 88 | + |
| 89 | + await waitForUTXOsInMempool(address, utxoCount); |
| 90 | + |
| 91 | + await navigateToSwapDetails(page, swapId); |
| 92 | + |
| 93 | + await expect(page.getByText("invoice.failedToPay")).toBeVisible(); |
| 94 | + await page.getByTestId("refundAddress").fill(await getLiquidAddress()); |
| 95 | + await page.getByTestId("refundButton").click(); |
| 96 | + |
| 97 | + // Validate that the UTXOs are refunded on the same transaction |
| 98 | + await validateRefundTxInputs(page, utxoCount); |
| 99 | + |
| 100 | + const refundTxLink = page.getByText("open refund transaction"); |
| 101 | + const txId = (await refundTxLink.getAttribute("href")).split("/").pop(); |
| 102 | + |
| 103 | + expect(txId).toBeDefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + test("Refunds all UTXOs of `transaction.lockupFailed`", async ({ |
| 107 | + page, |
| 108 | + }) => { |
| 109 | + await createAndVerifySwap(page, refundFileJson); |
| 110 | + |
| 111 | + const swapId = getCurrentSwapId(page); |
| 112 | + |
| 113 | + const address = await page.evaluate(() => { |
| 114 | + return navigator.clipboard.readText(); |
| 115 | + }); |
| 116 | + const amount = 0.01; |
| 117 | + const utxoCount = 2; |
| 118 | + |
| 119 | + // Pay swap with incorrect amount & pay additional UTXO |
| 120 | + await elementsSendToAddress(address, amount); |
| 121 | + await elementsSendToAddress(address, amount); |
| 122 | + |
| 123 | + await waitForUTXOsInMempool(address, utxoCount); |
| 124 | + |
| 125 | + await navigateToSwapDetails(page, swapId); |
| 126 | + |
| 127 | + await expect(page.getByText("transaction.lockupFailed")).toBeVisible(); |
| 128 | + await page.getByTestId("refundAddress").fill(await getLiquidAddress()); |
| 129 | + await page.getByTestId("refundButton").click(); |
| 130 | + |
| 131 | + // Validate that the UTXOs are refunded on the same transaction |
| 132 | + await validateRefundTxInputs(page, utxoCount); |
| 133 | + |
| 134 | + const refundTxLink = page.getByText("open refund transaction"); |
| 135 | + const txId = (await refundTxLink.getAttribute("href")).split("/").pop(); |
| 136 | + |
| 137 | + expect(txId).toBeDefined(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments