-
Notifications
You must be signed in to change notification settings - Fork 100
/
MigrateScreen.tsx
193 lines (183 loc) · 6.65 KB
/
MigrateScreen.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import React, { useContext, useState } from "react";
import { Platform, View } from "react-native";
import useAsyncEffect from "use-async-effect";
import ApproveButton from "../components/ApproveButton";
import BackgroundImage from "../components/BackgroundImage";
import Border from "../components/Border";
import Button from "../components/Button";
import ChangeNetwork from "../components/ChangeNetwork";
import Container from "../components/Container";
import Content from "../components/Content";
import ErrorMessage from "../components/ErrorMessage";
import FetchingButton from "../components/FetchingButton";
import Heading from "../components/Heading";
import InfoBox from "../components/InfoBox";
import InsufficientBalanceButton from "../components/InsufficientBalanceButton";
import LPTokenSelect, { LPTokenItem } from "../components/LPTokenSelect";
import Meta from "../components/Meta";
import Select, { Option } from "../components/Select";
import Text from "../components/Text";
import Title from "../components/Title";
import TokenInput from "../components/TokenInput";
import WebFooter from "../components/web/WebFooter";
import { MigrateSubMenu } from "../components/web/WebSubMenu";
import { SUSHI_ROLL } from "../constants/contracts";
import { Spacing } from "../constants/dimension";
import { EthersContext } from "../context/EthersContext";
import useLinker from "../hooks/useLinker";
import useMigrateState, { MigrateMode, MigrateState } from "../hooks/useMigrateState";
import useTranslation from "../hooks/useTranslation";
import MetamaskError from "../types/MetamaskError";
import { isEmptyValue, parseBalance } from "../utils";
import Screen from "./Screen";
const MigrateScreen = () => {
const t = useTranslation();
return (
<Screen>
<Container>
<BackgroundImage />
<Content>
<Title text={t("migrate-liquidity")} />
<Text light={true}>{t("migrate-liquidity-desc")}</Text>
<Migrate />
</Content>
{Platform.OS === "web" && <WebFooter />}
</Container>
<MigrateSubMenu />
</Screen>
);
};
const Migrate = () => {
const { ethereum, chainId } = useContext(EthersContext);
const state = useMigrateState();
if (chainId !== 1) return <ChangeNetwork />;
return (
<View style={{ marginTop: Spacing.large }}>
{!ethereum?.isWalletConnect && (
<>
<MigrateModeSelect state={state} />
<Border />
</>
)}
<UniswapLiquidityScreen state={state} />
<Border />
<AmountInput state={state} />
<AmountInfo state={state} />
</View>
);
};
const MigrateModeSelect = ({ state }: { state: MigrateState }) => {
const t = useTranslation();
const options: Option[] = [
{
key: "permit",
title: t("non-hardware-wallet"),
description: t("non-hardware-wallet-desc")
},
{
key: "approve",
title: t("hardware-wallet"),
description: t("hardware-wallet-desc")
}
];
return (
<Select
title={t("wallet-type")}
options={options}
option={options.find(option => option.key === state.mode)}
setOption={option => state.setMode(option?.key as MigrateMode | undefined)}
/>
);
};
const UniswapLiquidityScreen = ({ state }: { state: MigrateState }) => {
const t = useTranslation();
if (!state.mode) {
return <Heading text={t("your-uniswap-liquidity")} disabled={true} />;
}
return (
<LPTokenSelect
state={state}
title={t("your-uniswap-liquidity")}
emptyText={t("you-dont-have-any-liquidity-on-uniswap")}
Item={LPTokenItem}
/>
);
};
const AmountInput = ({ state }: { state: MigrateState }) => {
const t = useTranslation();
if (!state.selectedLPToken) {
return <Heading text={t("amount-of-tokens")} disabled={true} />;
}
return (
<TokenInput
title={t("amount-of-tokens")}
token={state.selectedLPToken}
amount={state.amount}
onAmountChanged={state.setAmount}
/>
);
};
const AmountInfo = ({ state }: { state: MigrateState }) => {
const disabled = !state.selectedLPToken || isEmptyValue(state.amount);
return (
<InfoBox>
<Meta label={state.selectedLPToken?.symbol || "SushiSwap LP"} text={state.amount} disabled={disabled} />
<Controls state={state} />
</InfoBox>
);
};
const Controls = ({ state }: { state: MigrateState }) => {
const [error, setError] = useState<MetamaskError>({});
useAsyncEffect(() => setError({}), [state.amount]);
return (
<View style={{ marginTop: Spacing.normal }}>
{!state.selectedLPToken || isEmptyValue(state.amount) ? (
<MigrateButton state={state} onError={setError} disabled={true} />
) : parseBalance(state.amount, state.selectedLPToken.decimals).gt(state.selectedLPToken.balance) ? (
<InsufficientBalanceButton symbol={state.selectedLPToken.symbol} />
) : state.loading ? (
<FetchingButton />
) : (
<>
{state.mode === "approve" && !state.selectedLPTokenAllowed && (
<ApproveButton
token={state.selectedLPToken}
spender={SUSHI_ROLL}
onSuccess={() => state.setSelectedLPTokenAllowed(true)}
onError={setError}
/>
)}
<MigrateButton
state={state}
onError={setError}
disabled={state.mode === "approve" && !state.selectedLPTokenAllowed}
/>
</>
)}
{error.message && error.code !== 4001 && <ErrorMessage error={error} />}
</View>
);
};
const MigrateButton = ({
state,
onError,
disabled
}: {
state: MigrateState;
onError: (e) => void;
disabled: boolean;
}) => {
const t = useTranslation();
const goToFarm = useLinker("/farming", "Farming");
const onPress = async () => {
onError({});
try {
await state.onMigrate();
goToFarm();
} catch (e) {
onError(e);
}
};
return <Button title={t("migrate-liquidity")} loading={state.migrating} onPress={onPress} disabled={disabled} />;
};
export default MigrateScreen;