-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathErrorPageBadPriceWarning.tsx
140 lines (129 loc) · 4 KB
/
ErrorPageBadPriceWarning.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ErrorPageContent } from "@/pages/ErrorPage/ErrorPageContent";
import { MainButton } from "@/components/MainButton";
import { SmallText, SmallTextButton } from "@/components/Typography";
import { useGetAssetDetails } from "@/hooks/useGetAssetDetails";
import { ICONS } from "@/icons";
import { calculatePercentageChange } from "@/utils/number";
import { RouteResponse } from "@skip-go/client";
import { useTheme } from "styled-components";
import { SwapPageHeader } from "../SwapPage/SwapPageHeader";
import { track } from "@amplitude/analytics-browser";
export type ErrorPageBadPriceWarningProps = {
onClickContinue: () => void;
onClickBack: () => void;
route: RouteResponse;
};
export const ErrorPageBadPriceWarning = ({
onClickContinue,
onClickBack,
route,
}: ErrorPageBadPriceWarningProps) => {
const theme = useTheme();
const {
amountIn,
amountOut,
usdAmountIn,
usdAmountOut,
sourceAssetDenom,
sourceAssetChainID,
destAssetDenom,
destAssetChainID,
} = route;
const hasUsdValues =
usdAmountIn &&
usdAmountOut &&
parseFloat(usdAmountIn) > 0 &&
parseFloat(usdAmountOut) > 0;
const swapDifferencePercentage = hasUsdValues
? `${calculatePercentageChange(usdAmountIn, usdAmountOut, true)}%`
: null;
const priceImpactPercentage = (() => {
const impactString = route.swapPriceImpactPercent;
if (!impactString) return null;
return `${parseFloat(impactString).toFixed(2)}%`;
})();
const sourceDetails = useGetAssetDetails({
assetDenom: sourceAssetDenom,
chainId: sourceAssetChainID,
tokenAmount: amountIn,
});
const destinationDetails = useGetAssetDetails({
assetDenom: destAssetDenom,
chainId: destAssetChainID,
tokenAmount: amountOut,
});
let title = "Warning: Bad Trade";
let descriptionContent: React.ReactNode = null;
if (hasUsdValues && swapDifferencePercentage) {
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) {
title = `Warning: High Price Impact (${priceImpactPercentage})`;
descriptionContent = (
<>
Executing this trade is expected to impact the price by {priceImpactPercentage}. Please verify the amounts.
<br />
</>
);
} else {
descriptionContent = (
<>
This trade may result in a poor execution price. Please verify the amounts carefully.
<br />
</>
);
}
return (
<>
<SwapPageHeader
leftButton={{
label: "Back",
icon: ICONS.thinArrow,
onClick: () => {
track("error page: bad price warning - header back button clicked");
onClickBack();
},
}}
/>
<ErrorPageContent
title={title}
description={
<>
<SmallText color={theme.error.text} textAlign="center" textWrap="balance">
{descriptionContent}
</SmallText>
<SmallTextButton
onClick={() => {
track("error page: bad price warning - continue anyway button clicked");
onClickContinue();
}}
color={theme.primary.text.lowContrast}
>
I know the risk, continue anyway
</SmallTextButton>
</>
}
icon={ICONS.triangleWarning}
backgroundColor={theme.error.background}
textColor={theme.error.text}
/>
<MainButton
label="Back"
icon={ICONS.leftArrow}
onClick={() => {
track("error page: bad price warning - main back button clicked");
onClickBack();
}}
backgroundColor={theme.error.text}
/>
</>
);
};