Skip to content

Commit

Permalink
add status page tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Aug 5, 2024
1 parent d2e13d1 commit 3a38d50
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 59 deletions.
7 changes: 1 addition & 6 deletions src/consts/Types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export type ButtonLabelParams = {
key: string;
params?: Record<string, string>;
};

export type SwapStatusTransaction = {
hex?: string;
id?: string;
params?: Record;

Check failure on line 3 in src/consts/Types.ts

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Record' requires 2 type argument(s).

Check failure on line 3 in src/consts/Types.ts

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Record' requires 2 type argument(s).
};
20 changes: 10 additions & 10 deletions src/context/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
useContext,
} from "solid-js";

import { SwapStatusTransaction } from "../consts/Types";
import { SwapStatusTransaction } from "../utils/boltzClient";
import { SomeSwap } from "../utils/swapCreator";

export type PayContextType = {
failureReason: Accessor<string>;
setFailureReason: Setter<string>;
swap: Accessor<SomeSwap | null>;
setSwap: Setter<SomeSwap | null>;
swapStatus: Accessor<string>;
setSwapStatus: Setter<string>;
swapStatusTransaction: Accessor<SwapStatusTransaction>;
setSwapStatusTransaction: Setter<SwapStatusTransaction>;
failureReason: Accessor;

Check failure on line 13 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).

Check failure on line 13 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).
setFailureReason: Setter;

Check failure on line 14 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).

Check failure on line 14 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).
swap: Accessor;

Check failure on line 15 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).

Check failure on line 15 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).
setSwap: Setter;

Check failure on line 16 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).

Check failure on line 16 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).
swapStatus: Accessor;

Check failure on line 17 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).

Check failure on line 17 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).
setSwapStatus: Setter;

Check failure on line 18 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).

Check failure on line 18 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).
swapStatusTransaction: Accessor;

Check failure on line 19 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).

Check failure on line 19 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).
setSwapStatusTransaction: Setter;

Check failure on line 20 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).

Check failure on line 20 in src/context/Pay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Setter' requires 1 type argument(s).
};

const PayContext = createContext<PayContextType>();
Expand All @@ -30,7 +30,7 @@ const PayProvider = (props: { children: any }) => {
});
const [swapStatus, setSwapStatus] = createSignal<string>("");
const [swapStatusTransaction, setSwapStatusTransaction] =
createSignal<SwapStatusTransaction>({});
createSignal<SwapStatusTransaction | null>(null);

return (
<PayContext.Provider
Expand Down
6 changes: 3 additions & 3 deletions src/status/InvoiceFailedToPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const InvoiceFailedToPay = () => {
{t("failure_reason")}: {failureReason()}
</p>
<hr />
<RefundButton swap={swap as Accessor<SubmarineSwap | ChainSwap>} />
<RefundButton swap={swap as Accessor} />

Check failure on line 25 in src/status/InvoiceFailedToPay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).

Check failure on line 25 in src/status/InvoiceFailedToPay.tsx

View workflow job for this annotation

GitHub Actions / ci

Generic type 'Accessor' requires 1 type argument(s).
<Show when={swap().assetSend !== RBTC && !isTaproot}>
<DownloadRefund />
</Show>
<hr />
<BlockExplorer
asset={swap().assetSend}
address={getLockupAddress(swap() as SubmarineSwap)}
asset={swap()?.assetSend}
address={getLockupAddress(swap() as SubmarineSwap | ChainSwap)}
/>
</div>
);
Expand Down
13 changes: 6 additions & 7 deletions src/status/SwapExpired.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const SwapExpired = () => {
swap().id,
swap().type,
);
log.debug(`got swap transaction for ${swap().id}`);
log.debug(`got swap transaction for ${swap()?.id}`);
setTransactionToRefund(res);
} catch (error: any) {
log.warn(`no swap transaction for: ${swap().id}`, error);
log.warn(`no swap transaction for: ${swap()?.id}`, error);
}
});

Expand All @@ -39,13 +39,12 @@ const SwapExpired = () => {
</p>
<hr />
<Show when={transactionToRefund() !== null}>
<RefundButton
swap={swap as Accessor<SubmarineSwap | ChainSwap>}
/>
<RefundButton swap={swap as Accessor} />
<hr />
<BlockExplorer
asset={swap().assetSend}
txId={transactionToRefund().id}
typeLabel="lockup_tx"
asset={swap()?.assetSend}
txId={transactionToRefund()?.id}
/>
</Show>
<button class="btn" onClick={() => navigate("/swap")}>
Expand Down
4 changes: 2 additions & 2 deletions src/status/TransactionMempool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const TransactionMempool = () => {
<LoadingSpinner />
<Show when={swapStatusTransaction()}>
<BlockExplorer
asset={swap().assetSend}
txId={swapStatusTransaction().id}
asset={swap()?.assetSend}
txId={swapStatusTransaction()?.id}
typeLabel="lockup_tx"
/>
</Show>
Expand Down
42 changes: 19 additions & 23 deletions src/utils/boltzClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,11 @@ type ChainPairTypeTaproot = PairType & {
};
};

type SubmarinePairsTaproot = Record<
string,
Record<string, SubmarinePairTypeTaproot>
>;
type SubmarinePairsTaproot = Record;

type ReversePairsTaproot = Record<
string,
Record<string, ReversePairTypeTaproot>
>;
type ReversePairsTaproot = Record;

type ChainPairsTaproot = Record<string, Record<string, ChainPairTypeTaproot>>;
type ChainPairsTaproot = Record;

type Pairs = {
[SwapType.Submarine]: SubmarinePairsTaproot;
Expand All @@ -90,7 +84,7 @@ type Contracts = {
network: {
chainId: number;
};
tokens: Record<string, string>;
tokens: Record;
swapContracts: {
EtherSwap: string;
ERC20Swap: string;
Expand Down Expand Up @@ -168,7 +162,7 @@ type ChainSwapTransaction = {

type TransactionInterface = Transaction | LiquidTransaction;

export const getPairs = async (asset: string): Promise<Pairs> => {
export const getPairs = async (asset: string): Promise => {
const [submarine, reverse, chain] = await Promise.all([
fetcher<SubmarinePairsTaproot>("/v2/swap/submarine", asset),
fetcher<ReversePairsTaproot>("/v2/swap/reverse", asset),
Expand Down Expand Up @@ -196,7 +190,7 @@ export const createSubmarineSwap = (
pairHash: string,
referralId: string,
refundPublicKey?: string,
): Promise<SubmarineCreatedResponse> =>
): Promise =>
fetcher("/v2/swap/submarine", to, {
from,
to,
Expand All @@ -215,7 +209,7 @@ export const createReverseSwap = (
referralId: string,
claimPublicKey?: string,
claimAddress?: string,
): Promise<ReverseCreatedResponse> =>
): Promise =>
fetcher("/v2/swap/reverse", to, {
from,
to,
Expand All @@ -237,7 +231,7 @@ export const createChainSwap = (
claimAddress: string | undefined,
pairHash: string,
referralId: string,
): Promise<ChainSwapCreatedResponse> =>
): Promise =>
fetcher("/v2/swap/chain", to, {
from,
to,
Expand All @@ -257,7 +251,7 @@ export const getPartialRefundSignature = async (
pubNonce: Buffer,
transaction: TransactionInterface,
index: number,
): Promise<PartialSignature> => {
): Promise => {
checkCooperative();
const res = await fetcher(
`/v2/swap/${
Expand All @@ -283,7 +277,7 @@ export const getPartialReverseClaimSignature = async (
pubNonce: Buffer,
transaction: TransactionInterface,
index: number,
): Promise<PartialSignature> => {
): Promise => {
checkCooperative();
const res = await fetcher(`/v2/swap/reverse/${id}/claim`, asset, {
index,
Expand Down Expand Up @@ -332,7 +326,7 @@ export const getEipRefundSignature = (
};

export const getFeeEstimations = (asset: string) =>
fetcher<Record<string, number>>("/v2/chain/fees", asset);
fetcher<Record>("/v2/chain/fees", asset);

export const getNodes = (asset: string) =>
fetcher<{
Expand All @@ -355,7 +349,7 @@ export const getNodeStats = (asset: string) =>
}>("/v2/nodes/stats", asset);

export const getContracts = (asset: string) =>
fetcher<Record<string, Contracts>>("/v2/chain/contracts", asset);
fetcher<Record>("/v2/chain/contracts", asset);

export const broadcastTransaction = (asset: string, txHex: string) =>
fetcher<{ id: string }>(`/v2/chain/${asset}/transaction`, asset, {
Expand All @@ -373,7 +367,7 @@ export const getLockupTransaction = async (
asset: string,
id: string,
type: SwapType,
): Promise<LockupTransaction> => {
): Promise => {
switch (type) {
case SwapType.Submarine:
return fetcher<{
Expand Down Expand Up @@ -404,14 +398,16 @@ export const getReverseTransaction = (asset: string, id: string) =>
timeoutBlockHeight: number;
}>(`/v2/swap/reverse/${id}/transaction`, asset);

export type SwapStatusTransaction = {
hex: string;
id: string;
};

export type SwapStatus = {
status: string;
failureReason?: string;
zeroConfRejected?: boolean;
transaction?: {
id: string;
hex: string;
};
transaction?: SwapStatusTransaction;
};

export const getSwapStatus = (asset: string, id: string) =>
Expand Down
48 changes: 48 additions & 0 deletions tests/status/SwapExpired.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { render, screen } from "@solidjs/testing-library";

import { BTC } from "../../src/consts/Assets";
import i18n from "../../src/i18n/i18n";
import SwapExpired from "../../src/status/SwapExpired";
import { TestComponent, contextWrapper, payContext } from "../helper";

jest.mock("../../src/utils/boltzClient", () => {
const originalModule = jest.requireActual("../../src/utils/boltzClient");

return {
__esModule: true,
...originalModule,
getLockupTransaction: jest.fn(() => {
return { id: "txid" };
}),
};
});

describe("SwapExpired", () => {
test("should show blockexplorer lockup transaction button", async () => {
render(
() => (
<>
<TestComponent />
<SwapExpired />
</>
),
{
wrapper: contextWrapper,
},
);
payContext.setSwap({
assetSend: BTC,
id: "swapid",
} as any);

const type_label = i18n.en[`blockexplorer_lockup_tx`];
const label = i18n.en.blockexplorer.replace(
"{{ typeLabel }}",
type_label,
);

const button = await screen.findByText(label);
expect(button).not.toBeUndefined();
expect(button.getAttribute("href")).toMatch("txid");
});
});
26 changes: 19 additions & 7 deletions tests/status/TransactionClaimed.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { render, screen } from "@solidjs/testing-library";

import { BTC, LBTC, LN, RBTC } from "../../src/consts/Assets";
import { SwapType } from "../../src/consts/Enums";
import i18n from "../../src/i18n/i18n";
import TransactionClaimed from "../../src/status/TransactionClaimed";
import { TestComponent, contextWrapper, payContext } from "../helper";

describe("TransactionClaimed", () => {
test.each`
name | swap
${"normal swaps"} | ${{ assetSend: RBTC, assetReceive: LN, address: "address" }}
${"reverse swaps to RBTC"} | ${{ assetSend: LN, assetReceive: RBTC, claimTx: "txid" }}
${"reverse swaps to BTC with claim transactions"} | ${{ assetSend: LN, assetReceive: LBTC, claimTx: "txid" }}
${"reverse swaps to L-BTC with claim transactions"} | ${{ assetSend: LN, assetReceive: BTC, claimTx: "txid" }}
${"chain swaps BTC to L-BTC with claim transactions"} | ${{ assetSend: BTC, assetReceive: LBTC, claimTx: "txid" }}
`("should show success for $name", async ({ swap }) => {
name | label | swap
${"normal swaps"} | ${"lockup_address"} | ${{ type: SwapType.Submarine, assetSend: RBTC, assetReceive: LN, address: "bc12" }}
${"reverse swaps to RBTC"} | ${"claim_tx"} | ${{ type: SwapType.Reverse, assetSend: LN, assetReceive: RBTC, claimTx: "txid" }}
${"reverse swaps to BTC with claim transactions"} | ${"claim_tx"} | ${{ type: SwapType.Reverse, assetSend: LN, assetReceive: LBTC, claimTx: "txid" }}
${"reverse swaps to L-BTC with claim transactions"} | ${"claim_tx"} | ${{ type: SwapType.Reverse, assetSend: LN, assetReceive: BTC, claimTx: "txid" }}
${"chain swaps BTC to L-BTC with claim transactions"} | ${"claim_tx"} | ${{ type: SwapType.Chain, assetSend: BTC, assetReceive: LBTC, claimTx: "txid" }}
`("should show success for $name", async ({ label, swap }) => {
render(
() => (
<>
Expand All @@ -30,5 +31,16 @@ describe("TransactionClaimed", () => {
await expect(
screen.findByText(i18n.en.congrats),
).resolves.not.toBeUndefined();

const type_label = i18n.en[`blockexplorer_${label}`];
const _label = i18n.en.blockexplorer.replace(
"{{ typeLabel }}",
type_label,
);
const address_or_tx =
swap.type === SwapType.Submarine ? swap.address : swap.claimTx;
const button = screen.getByText(_label);
expect(button).not.toBeUndefined();
expect(button.getAttribute("href")).toMatch(address_or_tx);
});
});
35 changes: 34 additions & 1 deletion tests/status/TransactionLockupFailed.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { render, screen } from "@solidjs/testing-library";
import { OutputType } from "boltz-core";

import { BTC, RBTC } from "../../src/consts/Assets";
import { BTC, LBTC, RBTC } from "../../src/consts/Assets";
import { SwapType } from "../../src/consts/Enums";
import i18n from "../../src/i18n/i18n";
import TransactionLockupFailed from "../../src/status/TransactionLockupFailed";
import { TestComponent, contextWrapper, payContext } from "../helper";
Expand Down Expand Up @@ -84,4 +85,36 @@ describe("TransactionLockupFailed", () => {
screen.findByText(i18n.en.refund),
).resolves.not.toBeUndefined();
});

test.each`
swap
${{ assetSend: BTC, type: SwapType.Submarine, address: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq" }}
${{ assetSend: LBTC, type: SwapType.Chain, lockupDetails: { lockupAddress: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq" } }}
`("should show blockexplorer lockup address button", async ({ swap }) => {
render(
() => (
<>
<TestComponent />
<TransactionLockupFailed />
</>
),
{
wrapper: contextWrapper,
},
);
const lockup_address =
swap.type === SwapType.Submarine
? swap.address
: swap.lockupDetails.lockupAddress;
payContext.setSwap(swap);

const type_label = i18n.en.blockexplorer_lockup_address;
const label = i18n.en.blockexplorer.replace(
"{{ typeLabel }}",
type_label,
);
const button = screen.getByText(label);
expect(button).not.toBeUndefined();
expect(button.getAttribute("href")).toMatch(lockup_address);
});
});
Loading

0 comments on commit 3a38d50

Please sign in to comment.