Skip to content

Commit

Permalink
refactor: nicer screen for logs refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Nov 28, 2024
1 parent 37420ad commit df6751b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/components/ContractTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ConnectWallet, { ConnectAddress, SwitchNetwork } from "./ConnectWallet";
import LoadingSpinner from "./LoadingSpinner";

const ContractTransaction = (props: {
disabled?: boolean;
onClick: () => Promise<unknown>;
children?: JSX.Element;
showHr?: boolean;
Expand Down Expand Up @@ -74,7 +75,7 @@ const ContractTransaction = (props: {
</Show>
<button
class="btn"
disabled={clicked()}
disabled={props.disabled || clicked()}
onClick={async () => {
setClicked(true);
try {
Expand Down
2 changes: 2 additions & 0 deletions src/components/RefundButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import ContractTransaction from "./ContractTransaction";
import LoadingSpinner from "./LoadingSpinner";

export const RefundEvm = (props: {
disabled?: boolean;
swapId?: string;
amount: number;
preimageHash: string;
Expand All @@ -47,6 +48,7 @@ export const RefundEvm = (props: {

return (
<ContractTransaction
disabled={props.disabled}
/* eslint-disable-next-line solid/reactivity */
onClick={async () => {
const contract = getEtherSwap();
Expand Down
1 change: 1 addition & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ const dict = {
no_browser_wallet: "No browser wallet detected",
sent: "Sent",
will_receive: "Will receive",
refund_available_in: "Refund will be available in {{ blocks }} blocks",
},
de: {
language: "Deutsch",
Expand Down
10 changes: 2 additions & 8 deletions src/pages/Refund.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { useNavigate } from "@solidjs/router";
import log from "loglevel";
import QrScanner from "qr-scanner";
import {
Show,
createResource,
createSignal,
onCleanup,
onMount,
} from "solid-js";
import { Show, createEffect, createSignal, onCleanup, onMount } from "solid-js";

import BlockExplorer from "../components/BlockExplorer";
import ConnectWallet from "../components/ConnectWallet";
Expand Down Expand Up @@ -121,7 +115,7 @@ const Refund = () => {
}
});

createResource(async () => {
createEffect(async () => {

Check warning on line 118 in src/pages/Refund.tsx

View workflow job for this annotation

GitHub Actions / ci

This tracked scope should not be async. Solid's reactivity only tracks synchronously
setLogRefundableSwaps([]);

if (refundScanAbort !== undefined) {
Expand Down
118 changes: 94 additions & 24 deletions src/pages/RefundEvm.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,99 @@
import { useParams } from "@solidjs/router";
import { Match, Show, Switch, createResource, createSignal } from "solid-js";
import BigNumber from "bignumber.js";
import {
Match,
Setter,
Show,
Switch,
createResource,
createSignal,
} from "solid-js";

import BlockExplorer from "../components/BlockExplorer";
import LoadingSpinner from "../components/LoadingSpinner";
import { RefundEvm as RefundButton } from "../components/RefundButton";
import { useGlobalContext } from "../context/Global";
import { useWeb3Signer } from "../context/Web3";
import { LogRefundData, getLogsFromReceipt } from "../utils/contractLogs";
import { formatAmount } from "../utils/denomination";
import { formatError } from "../utils/errors";
import { cropString } from "../utils/helper";

type RefundData = LogRefundData & { currentHeight: bigint };

const RefundState = (props: {
asset: string;
lockupTxHash: string;
refundData: RefundData;
setRefundTxHash: Setter<string | undefined>;
}) => {
const { t, denomination, separator } = useGlobalContext();

const timelockExpired = () =>
props.refundData.timelock <= props.refundData.currentHeight;

return (
<>
<p>
{t("refund")}{" "}
{formatAmount(
new BigNumber(props.refundData.amount.toString()),
denomination(),
separator(),
)}{" "}
{props.asset}
</p>

<Show when={!timelockExpired()}>
<h3>
{t("refund_available_in", {
blocks: (
props.refundData.timelock -
props.refundData.currentHeight
).toString(),
})}
</h3>
</Show>

<RefundButton
disabled={!timelockExpired()}
setRefundTxHash={props.setRefundTxHash}
amount={Number(props.refundData.amount)}
preimageHash={props.refundData.preimageHash}
claimAddress={props.refundData.claimAddress}
signerAddress={props.refundData.refundAddress}
timeoutBlockHeight={Number(props.refundData.timelock)}
/>
<hr />
<BlockExplorer
typeLabel={"lockup_tx"}
asset={props.asset}
txId={props.lockupTxHash}
/>
</>
);
};

const RefundEvm = () => {
const params = useParams<{ asset: string; txHash: string }>();

const { t } = useGlobalContext();
const { signer, getEtherSwap } = useWeb3Signer();

const [refundData] = createResource<LogRefundData>(async () => {
const [refundData] = createResource<RefundData>(async () => {
if (signer() === undefined) {
return undefined;
}

return await getLogsFromReceipt(
signer(),
getEtherSwap(),
params.txHash,
);
const [logData, currentHeight] = await Promise.all([
getLogsFromReceipt(signer(), getEtherSwap(), params.txHash),
signer().provider.getBlockNumber(),
]);

return {
...logData,
currentHeight: BigInt(currentHeight),
};
});

const [refundTxHash, setRefundTxHash] = createSignal<string | undefined>(
Expand All @@ -37,27 +107,27 @@ const RefundEvm = () => {
fallback={<h2>{t("no_wallet")}</h2>}>
<Switch>
<Match when={refundData.state === "ready"}>
<h2>{t("refund")}</h2>
<h2 style={{ "margin-bottom": "6px" }}>
{t("refund")} {cropString(params.txHash, 15, 5)}
</h2>
<Show
when={refundTxHash() !== undefined}
when={refundTxHash() === undefined}
fallback={
<RefundButton
setRefundTxHash={setRefundTxHash}
amount={Number(refundData().amount)}
preimageHash={refundData().preimageHash}
claimAddress={refundData().claimAddress}
signerAddress={refundData().refundAddress}
timeoutBlockHeight={Number(
refundData().timelock,
)}
/>
<>
<p>{t("refunded")}</p>
<hr />
<BlockExplorer
typeLabel={"refund_tx"}
asset={params.asset}
txId={refundTxHash()}
/>
</>
}>
<p>{t("refunded")}</p>
<hr />
<BlockExplorer
typeLabel={"refund_tx"}
<RefundState
asset={params.asset}
txId={refundTxHash()}
lockupTxHash={params.txHash}
refundData={refundData()}
setRefundTxHash={setRefundTxHash}
/>
</Show>
</Match>
Expand Down

0 comments on commit df6751b

Please sign in to comment.