Skip to content

Commit c9022a1

Browse files
committed
refactor: refund address input
1 parent 2b820b6 commit c9022a1

File tree

1 file changed

+55
-66
lines changed

1 file changed

+55
-66
lines changed

src/components/RefundButton.tsx

+55-66
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import { OutputType } from "boltz-core";
33
import { Signature, TransactionResponse } from "ethers";
44
import { Network as LiquidNetwork } from "liquidjs-lib/src/networks";
55
import log from "loglevel";
6-
import {
7-
Accessor,
8-
Match,
9-
Setter,
10-
Show,
11-
Switch,
12-
createResource,
13-
createSignal,
14-
} from "solid-js";
6+
import { Accessor, Setter, Show, createResource, createSignal } from "solid-js";
157
import { ChainSwap, SubmarineSwap } from "src/utils/swapCreator";
168

179
import RefundEta from "../components/RefundEta";
@@ -136,14 +128,15 @@ export const RefundBtc = (props: {
136128
const input = evt.currentTarget as HTMLInputElement;
137129
const inputValue = input.value.trim();
138130

131+
setRefundAddress(inputValue);
132+
139133
const lockupAddress =
140134
props.swap().type === SwapType.Submarine
141135
? (props.swap() as SubmarineSwap).address
142136
: (props.swap() as ChainSwap).lockupDetails.lockupAddress;
143137

144138
if (inputValue === lockupAddress) {
145139
log.debug("refunds to lockup address are blocked");
146-
input.setCustomValidity("lockup address");
147140
return false;
148141
}
149142
try {
@@ -152,14 +145,12 @@ export const RefundBtc = (props: {
152145
getNetwork(asset) as LiquidNetwork,
153146
);
154147
input.setCustomValidity("");
155-
setRefundAddress(inputValue);
156148
return true;
157149
} catch (e) {
158150
log.debug("parsing refund address failed", e);
159151
input.setCustomValidity("invalid address");
152+
return false;
160153
}
161-
162-
return false;
163154
};
164155

165156
const refundAction = async () => {
@@ -235,62 +226,60 @@ export const RefundBtc = (props: {
235226
return transactionToRefund;
236227
});
237228

229+
const message = () =>
230+
props.buttonOverride ??
231+
(lockupTransaction.state == "errored"
232+
? t("no_lockup_transaction")
233+
: valid() || !refundAddress() || !props.swap()
234+
? t("refund")
235+
: t("invalid_address", { asset: props.swap()?.assetSend }));
236+
238237
return (
239-
<Switch>
240-
<Match
241-
when={
242-
lockupTransaction.state === "ready" ||
243-
lockupTransaction.state == "unresolved"
244-
}>
245-
<Show when={timeoutEta() > 0 || timeoutBlockheight() > 0}>
246-
<RefundEta
247-
timeoutEta={timeoutEta}
248-
timeoutBlockHeight={timeoutBlockheight}
249-
/>
250-
</Show>
251-
<h3 style={{ color: "#fff" }}>
252-
{props.swap()
253-
? t("refund_address_header", {
238+
<Show
239+
when={
240+
lockupTransaction.state === "ready" ||
241+
lockupTransaction.state == "unresolved" ||
242+
lockupTransaction.state == "errored"
243+
}
244+
fallback={<LoadingSpinner />}>
245+
<Show when={timeoutEta() > 0 || timeoutBlockheight() > 0}>
246+
<RefundEta
247+
timeoutEta={timeoutEta}
248+
timeoutBlockHeight={timeoutBlockheight}
249+
/>
250+
</Show>
251+
<h3 style={{ color: "#fff" }}>
252+
{props.swap()
253+
? t("refund_address_header", {
254+
asset: props.swap()?.assetSend,
255+
})
256+
: t("refund_address_header_no_asset")}
257+
</h3>
258+
<input
259+
data-testid="refundAddress"
260+
id="refundAddress"
261+
disabled={lockupTransaction.state == "errored"}
262+
onInput={(e) =>
263+
setValid(refundAddressChange(e, props.swap()?.assetSend))
264+
}
265+
type="text"
266+
name="refundAddress"
267+
placeholder={
268+
props.swap()
269+
? t("onchain_address", {
254270
asset: props.swap()?.assetSend,
255271
})
256-
: t("refund_address_header_no_asset")}
257-
</h3>
258-
<input
259-
data-testid="refundAddress"
260-
id="refundAddress"
261-
disabled={props.swap() === null}
262-
onInput={(e) =>
263-
setValid(
264-
refundAddressChange(e, props.swap()?.assetSend),
265-
)
266-
}
267-
type="text"
268-
name="refundAddress"
269-
placeholder={
270-
props.swap()
271-
? t("onchain_address", {
272-
asset: props.swap()?.assetSend,
273-
})
274-
: t("onchain_address_no_asset")
275-
}
276-
/>
277-
<button
278-
data-testid="refundButton"
279-
class="btn"
280-
disabled={!valid() || refundRunning()}
281-
onClick={() => refundAction()}>
282-
{props.buttonOverride ?? t("refund")}
283-
</button>
284-
</Match>
285-
<Match when={lockupTransaction.state === "pending"}>
286-
<LoadingSpinner />
287-
</Match>
288-
<Match when={lockupTransaction.state === "errored"}>
289-
<button class="btn" disabled={true}>
290-
{t("no_lockup_transaction")}
291-
</button>
292-
</Match>
293-
</Switch>
272+
: t("onchain_address_no_asset")
273+
}
274+
/>
275+
<button
276+
data-testid="refundButton"
277+
class="btn"
278+
disabled={!valid() || refundRunning()}
279+
onClick={() => refundAction()}>
280+
{message()}
281+
</button>
282+
</Show>
294283
);
295284
};
296285

0 commit comments

Comments
 (0)