Skip to content

Commit

Permalink
show more info
Browse files Browse the repository at this point in the history
  • Loading branch information
thal0x committed Dec 26, 2023
1 parent 5ed78b9 commit 6381711
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/components/PriceImpactWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { useDisclosureKey } from "@/context/disclosures";

interface Props {
onGoBack: () => void;
warningMessage?: string;
}

export const PriceImpactWarning = ({ onGoBack }: Props) => {
export const PriceImpactWarning = ({
onGoBack,
warningMessage = "",
}: Props) => {
const [isOpen, control] = useDisclosureKey("priceImpactWarning");

if (!isOpen) return null;
Expand All @@ -31,8 +35,8 @@ export const PriceImpactWarning = ({ onGoBack }: Props) => {
<p className="font-bold text-lg text-center text-red-500 mb-2">
Price Impact Warning
</p>
<p className="text-center text-lg max-w-72 mx-auto leading-snug text-gray-500">
This route has a high price impact. Do you want to continue?
<p className="text-center text-lg px-4 leading-snug text-gray-500">
{warningMessage} Do you want to continue?
</p>
</div>
<div className="flex items-end gap-2">
Expand Down
2 changes: 2 additions & 0 deletions src/components/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const SwapWidget: FC = () => {
swapPriceImpactPercent,
priceImpactThresholdReached,
routeError,
routeWarning,
} = useSwapWidget();

let usdDiffPercent = 0.0;
Expand Down Expand Up @@ -269,6 +270,7 @@ export const SwapWidget: FC = () => {
priceImpactThresholdReached ||
Math.abs(usdDiffPercent * 100) > PRICE_IMPACT_THRESHOLD
}
routeWarning={routeWarning}
/>
{insufficientBalance && (
<p className="text-center font-semibold text-sm text-red-500">
Expand Down
68 changes: 67 additions & 1 deletion src/components/SwapWidget/useSwapWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useBalancesByChain } from "@/utils/utils";

export const LAST_SOURCE_CHAIN_KEY = "IBC_DOT_FUN_LAST_SOURCE_CHAIN";

export const PRICE_IMPACT_THRESHOLD = 0.01;
export const PRICE_IMPACT_THRESHOLD = 0.1;

export function useSwapWidget() {
const {
Expand Down Expand Up @@ -186,6 +186,71 @@ export function useSwapWidget() {
return swapPriceImpactPercent > PRICE_IMPACT_THRESHOLD;
}, [swapPriceImpactPercent]);

const usdDiffPercent = useMemo(() => {
// if (route?.usdAmountIn && route?.usdAmountOut) {
// usdDiffPercent =
// (parseFloat(route.usdAmountOut) - parseFloat(route.usdAmountIn)) /
// parseFloat(route.usdAmountIn);
// }

if (!routeResponse) {
return undefined;
}

if (!routeResponse.usdAmountIn || !routeResponse.usdAmountOut) {
return undefined;
}

const usdAmountIn = parseFloat(routeResponse.usdAmountIn);
const usdAmountOut = parseFloat(routeResponse.usdAmountOut);

return (usdAmountOut - usdAmountIn) / usdAmountIn;
}, [routeResponse]);

console.log(usdDiffPercent);

const routeWarning = useMemo(() => {
if (!routeResponse) {
return undefined;
}

if (
!routeResponse.swapPriceImpactPercent &&
(!routeResponse.usdAmountIn || !routeResponse.usdAmountOut)
) {
return "We were unable to calculate the price impact of this route.";
}

if (usdDiffPercent && Math.abs(usdDiffPercent) > PRICE_IMPACT_THRESHOLD) {
const amountInUSD = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(parseFloat(routeResponse.usdAmountIn ?? "0"));

const amountOutUSD = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(parseFloat(routeResponse.usdAmountOut ?? "0"));

const formattedUsdDiffPercent = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 2,
}).format(Math.abs(usdDiffPercent));
return `Your estimated output value (${amountOutUSD}) is ${formattedUsdDiffPercent} lower than your estimated input value (${amountInUSD}).`;
}

if (
swapPriceImpactPercent &&
swapPriceImpactPercent > PRICE_IMPACT_THRESHOLD
) {
const formattedPriceImpact = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 2,
}).format(swapPriceImpactPercent);
return `Your swap is expected to execute at a ${formattedPriceImpact} worse price than the current estimated on-chain price. It's likely there's not much liquidity available for this swap.`;
}
}, [routeResponse, swapPriceImpactPercent, usdDiffPercent]);

return {
amountIn,
amountOut,
Expand All @@ -207,6 +272,7 @@ export function useSwapWidget() {
routeError: errorMessage,
swapPriceImpactPercent,
priceImpactThresholdReached,
routeWarning,
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/components/TransactionDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Props {
transactionCount: number;
insufficientBalance?: boolean;
shouldShowPriceImpactWarning?: boolean;
routeWarning?: string;
}

const TransactionDialog: FC<Props> = ({
Expand All @@ -22,6 +23,7 @@ const TransactionDialog: FC<Props> = ({
insufficientBalance,
transactionCount,
shouldShowPriceImpactWarning,
routeWarning,
}) => {
const [hasDisplayedWarning, setHasDisplayedWarning] = useState(false);
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -73,7 +75,10 @@ const TransactionDialog: FC<Props> = ({
</div>
)}
</div>
<PriceImpactWarning onGoBack={() => setIsOpen(false)} />
<PriceImpactWarning
onGoBack={() => setIsOpen(false)}
warningMessage={routeWarning}
/>
</Fragment>
);
};
Expand Down

0 comments on commit 6381711

Please sign in to comment.