Skip to content

[FRE-1641] Improve Bad Price Error Handling #1070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-ants-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@skip-go/widget": minor
---

fixed bug in displaying bad price warnings
77 changes: 65 additions & 12 deletions packages/widget/src/pages/ErrorPage/ErrorPageBadPriceWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RouteResponse } from "@skip-go/client";
import { useTheme } from "styled-components";
import { SwapPageHeader } from "../SwapPage/SwapPageHeader";
import { track } from "@amplitude/analytics-browser";
import { useMemo } from "react";

export type ErrorPageBadPriceWarningProps = {
onClickContinue: () => void;
Expand All @@ -32,11 +33,21 @@ export const ErrorPageBadPriceWarning = ({
destAssetChainID,
} = route;

const swapDifferencePercentage = `${calculatePercentageChange(
usdAmountIn ?? 0,
usdAmountOut ?? 0,
true,
)}%`;
const hasUsdValues =
usdAmountIn &&
usdAmountOut &&
parseFloat(usdAmountIn) > 0 &&
parseFloat(usdAmountOut) > 0;

const swapDifferencePercentage = hasUsdValues
? `${calculatePercentageChange(usdAmountIn, usdAmountOut, true)}%`
: null;

const priceImpactPercentage = useMemo(() => {
const impactString = route.swapPriceImpactPercent;
if (!impactString) return null;
return `${parseFloat(impactString).toFixed(2)}%`;
}, [route.swapPriceImpactPercent]);

const sourceDetails = useGetAssetDetails({
assetDenom: sourceAssetDenom,
Expand All @@ -49,6 +60,53 @@ export const ErrorPageBadPriceWarning = ({
tokenAmount: amountOut,
});

const { title, descriptionContent } = useMemo(() => {
if (hasUsdValues && swapDifferencePercentage) {
return {
title: `Warning: Bad trade (-${swapDifferencePercentage})`,
descriptionContent: (
<>
You will lose {swapDifferencePercentage} of your input value with this trade
<br />
Input: {sourceDetails?.amount} {sourceDetails?.symbol} ({usdAmountIn})
<br />
Estimated output: {destinationDetails?.amount} {destinationDetails?.symbol} ({usdAmountOut})
</>
),
};
} else if (priceImpactPercentage) {
return {
title: `Warning: High Price Impact (${priceImpactPercentage})`,
descriptionContent: (
<>
Executing this trade is expected to impact the price by {priceImpactPercentage}. Please verify the amounts.
<br />

</>
),
};
} else {
return {
title: "Warning: Bad Trade",
descriptionContent: (
<>
This trade may result in a poor execution price. Please verify the amounts carefully.
<br />

</>
),
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is purely a stylistic thing, but in the cases where each if condition has an early return I prefer this:

if () {
  return 1;
}
if () {
  return 2;
}
return 3;

over

if () {
  return 1;
} else if () {
  return 2;
} else {
  return 3;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh you updated it before I finished writing my comment 😆

}, [
hasUsdValues,
swapDifferencePercentage,
priceImpactPercentage,
sourceDetails,
destinationDetails,
usdAmountIn,
usdAmountOut,
]);

return (
<>
<SwapPageHeader
Expand All @@ -62,16 +120,11 @@ export const ErrorPageBadPriceWarning = ({
}}
/>
<ErrorPageContent
title={`Warning: Bad trade (-${swapDifferencePercentage})`}
title={title}
description={
<>
<SmallText color={theme.error.text} textAlign="center" textWrap="balance">
You will lose {swapDifferencePercentage} of your input value with this trade
<br />
Input: {sourceDetails?.amount} {sourceDetails?.symbol} ({usdAmountIn})
<br />
Estimated output: {destinationDetails?.amount} {destinationDetails?.symbol} (
{usdAmountOut})
{descriptionContent}
</SmallText>
<SmallTextButton
onClick={() => {
Expand Down
3 changes: 1 addition & 2 deletions packages/widget/src/pages/SwapPage/SwapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export const SwapPage = () => {
});
return;
}
if (route?.warning?.type === "BAD_PRICE_WARNING" && Number(priceChangePercentage ?? 0) < 0) {
if (route?.warning?.type === "BAD_PRICE_WARNING") {
track("error page: bad price warning", { route });
setError({
errorType: ErrorType.BadPriceWarning,
Expand Down Expand Up @@ -323,7 +323,6 @@ export const SwapPage = () => {
routePreference,
slippage,
showCosmosLedgerWarning,
priceChangePercentage,
showGoFastWarning,
isGoFast,
setChainAddresses,
Expand Down