Skip to content

Commit

Permalink
feat: renegotiate chain swap amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 26, 2024
1 parent c624d2a commit 3ee2de7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 27 deletions.
23 changes: 11 additions & 12 deletions src/context/Global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type GlobalContextType = {
audio?: boolean,
) => void;
playNotificationSound: () => void;
fetchPairs: () => void;
fetchPairs: () => Promise<void>;

getLogs: () => Promise<Record<string, string[]>>;
clearLogs: () => Promise<void>;
Expand Down Expand Up @@ -191,17 +191,16 @@ const GlobalProvider = (props: { children: any }) => {
audio.play();
};

const fetchPairs = () => {
getPairs()
.then((data) => {
log.debug("getpairs", data);
setOnline(true);
setPairs(data);
})
.catch((error) => {
log.debug(error);
setOnline(false);
});
const fetchPairs = async () => {
try {
const data = await getPairs();
log.debug("getpairs", data);
setOnline(true);
setPairs(data);
} catch (error) {
log.debug(error);
setOnline(false);
}
};

// Use IndexedDB if available; fallback to LocalStorage
Expand Down
1 change: 1 addition & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const dict = {
switch_network: "Switch network",
block: "block",
logs_scan_progress: "Scan progress {{ value }}%",
accept: "Accept",
},
de: {
language: "Deutsch",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/RefundEvm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const RefundEvm = () => {
const { signer, getEtherSwap } = useWeb3Signer();

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

Expand Down
121 changes: 107 additions & 14 deletions src/status/TransactionLockupFailed.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,121 @@
import { Accessor, Show } from "solid-js";
import BigNumber from "bignumber.js";
import log from "loglevel";
import {
Accessor,
Match,
Show,
Switch,
createResource,
createSignal,
} from "solid-js";

import RefundButton from "../components/RefundButton";
import { SwapType } from "../consts/Enums";
import { useGlobalContext } from "../context/Global";
import { usePayContext } from "../context/Pay";
import NotFound from "../pages/NotFound";
import {
acceptChainSwapNewQuote,
getChainSwapNewQuote,
} from "../utils/boltzClient";
import { formatAmount } from "../utils/denomination";
import { formatError } from "../utils/errors";
import { ChainSwap, SubmarineSwap } from "../utils/swapCreator";

const TransactionLockupFailed = () => {
const { failureReason, swap } = usePayContext();
const { t } = useGlobalContext();
const { t, denomination, separator, fetchPairs, setSwapStorage, pairs } =
useGlobalContext();
const { failureReason, swap, setSwap } = usePayContext();

const [newQuote] = createResource<
{ quote: number; receiveAmount: number } | undefined
>(async () => {
if (swap() === null || swap().type !== SwapType.Chain) {
return undefined;
}

try {
await fetchPairs();
const quote = await getChainSwapNewQuote(swap().id);

const claimFee =
pairs()[SwapType.Chain][swap().assetSend][swap().assetReceive]
.fees.minerFees.user.claim + 1;

return {
quote: quote.quote,
receiveAmount: quote.quote - claimFee,
};
} catch (e) {
log.warn(
`Getting new quote for swap ${swap().id} failed: ${formatError(e)}`,
);
}

return undefined;
});

const [quoteRejected, setQuoteRejected] = createSignal<boolean>(false);

return (
<Show when={swap() !== null} fallback={<NotFound />}>
<div>
<h2>{t("lockup_failed")}</h2>
<p>
{t("failure_reason")}: {failureReason()}
</p>
<hr />
<RefundButton
swap={swap as Accessor<SubmarineSwap | ChainSwap>}
/>
<hr />
</div>
<Switch
fallback={
<div>
<h2>{t("lockup_failed")}</h2>
<p>
{t("failure_reason")}: {failureReason()}
</p>
<hr />
<RefundButton
swap={swap as Accessor<SubmarineSwap | ChainSwap>}
/>
<hr />
</div>
}>
<Match
when={
newQuote.state === "ready" &&
newQuote() !== undefined &&
!quoteRejected()
}>
<h2>
New quote:{" "}
{formatAmount(
BigNumber(newQuote().receiveAmount),
denomination(),
separator(),
)}
</h2>
<p>
{t("failure_reason")}: {failureReason()}
</p>
<div class="btns btns-space-between">
<button
class="btn btn-success"
onClick={async () => {
const newSwap = swap();
newSwap.receiveAmount =
newQuote().receiveAmount;
await setSwapStorage(newSwap);
setSwap(newSwap);

await acceptChainSwapNewQuote(
swap().id,
newQuote().quote,
);
}}>
{t("accept")}
</button>
<button
class="btn btn-danger"
onClick={() => setQuoteRejected(true)}>
{t("refund")}
</button>
</div>
<hr />
</Match>
</Switch>
</Show>
);
};
Expand Down
6 changes: 6 additions & 0 deletions src/utils/boltzClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ export const getChainSwapTransactions = (id: string) =>
serverLock: ChainSwapTransaction;
}>(`/v2/swap/chain/${id}/transactions`);

export const getChainSwapNewQuote = (id: string) =>
fetcher<{ quote: number }>(`/v2/swap/chain/${id}/renegotiate`);

export const acceptChainSwapNewQuote = (id: string, quote: number) =>
fetcher<{}>(`/v2/swap/chain/${id}/renegotiate`, { quote });

export {
Pairs,
Contracts,
Expand Down

0 comments on commit 3ee2de7

Please sign in to comment.