From 3ee2de724800875d95df6a61ba59b60356e914ff Mon Sep 17 00:00:00 2001 From: michael1011 Date: Mon, 26 Aug 2024 15:04:04 +0200 Subject: [PATCH] feat: renegotiate chain swap amounts --- src/context/Global.tsx | 23 +++-- src/i18n/i18n.ts | 1 + src/pages/RefundEvm.tsx | 2 +- src/status/TransactionLockupFailed.tsx | 121 ++++++++++++++++++++++--- src/utils/boltzClient.ts | 6 ++ 5 files changed, 126 insertions(+), 27 deletions(-) diff --git a/src/context/Global.tsx b/src/context/Global.tsx index 8c0cadae..33592289 100644 --- a/src/context/Global.tsx +++ b/src/context/Global.tsx @@ -75,7 +75,7 @@ export type GlobalContextType = { audio?: boolean, ) => void; playNotificationSound: () => void; - fetchPairs: () => void; + fetchPairs: () => Promise; getLogs: () => Promise>; clearLogs: () => Promise; @@ -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 diff --git a/src/i18n/i18n.ts b/src/i18n/i18n.ts index 900156e5..8f348f0a 100644 --- a/src/i18n/i18n.ts +++ b/src/i18n/i18n.ts @@ -211,6 +211,7 @@ const dict = { switch_network: "Switch network", block: "block", logs_scan_progress: "Scan progress {{ value }}%", + accept: "Accept", }, de: { language: "Deutsch", diff --git a/src/pages/RefundEvm.tsx b/src/pages/RefundEvm.tsx index 7f4f1ad7..df00ffd3 100644 --- a/src/pages/RefundEvm.tsx +++ b/src/pages/RefundEvm.tsx @@ -15,7 +15,7 @@ const RefundEvm = () => { const { signer, getEtherSwap } = useWeb3Signer(); const [refundData] = createResource(async () => { - if (signer === undefined) { + if (signer() === undefined) { return undefined; } diff --git a/src/status/TransactionLockupFailed.tsx b/src/status/TransactionLockupFailed.tsx index 8e20b7e7..c504c63f 100644 --- a/src/status/TransactionLockupFailed.tsx +++ b/src/status/TransactionLockupFailed.tsx @@ -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(false); return ( }> -
-

{t("lockup_failed")}

-

- {t("failure_reason")}: {failureReason()} -

-
- } - /> -
-
+ +

{t("lockup_failed")}

+

+ {t("failure_reason")}: {failureReason()} +

+
+ } + /> +
+ + }> + +

+ New quote:{" "} + {formatAmount( + BigNumber(newQuote().receiveAmount), + denomination(), + separator(), + )} +

+

+ {t("failure_reason")}: {failureReason()} +

+
+ + +
+
+
+
); }; diff --git a/src/utils/boltzClient.ts b/src/utils/boltzClient.ts index 4e5131cf..39c796c3 100644 --- a/src/utils/boltzClient.ts +++ b/src/utils/boltzClient.ts @@ -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,