-
Notifications
You must be signed in to change notification settings - Fork 294
/
index.tsx
386 lines (359 loc) · 11.2 KB
/
index.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import {
getAccountCurrency,
getAccountUnit,
getMainAccount,
} from "@ledgerhq/live-common/account/index";
import { formatCurrencyUnit } from "@ledgerhq/live-common/currencies/index";
import { getAddressExplorer, getDefaultExplorerView } from "@ledgerhq/live-common/explorers";
import { stakeActions, stakeActivePercent } from "@ledgerhq/live-common/families/solana/logic";
import { useSolanaStakesWithMeta } from "@ledgerhq/live-common/families/solana/react";
import {
SolanaAccount,
SolanaStakeWithMeta,
StakeAction,
} from "@ledgerhq/live-common/families/solana/types";
import {
assertUnreachable,
sweetch,
tupleOfUnion,
} from "@ledgerhq/live-common/families/solana/logic";
import { AccountLike } from "@ledgerhq/types-live";
import { Box, Text } from "@ledgerhq/native-ui";
import { useNavigation, useTheme } from "@react-navigation/native";
import { BigNumber } from "bignumber.js";
import invariant from "invariant";
import { capitalize } from "lodash/fp";
import React, { useCallback, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Linking, StyleSheet, View } from "react-native";
import { StackNavigationProp } from "@react-navigation/stack";
import { rgba } from "../../../colors";
import AccountDelegationInfo from "~/components/AccountDelegationInfo";
import AccountSectionLabel from "~/components/AccountSectionLabel";
import Circle from "~/components/Circle";
import DelegationDrawer, { IconProps } from "~/components/DelegationDrawer";
import Touchable from "~/components/Touchable";
import { urls } from "~/utils/urls";
import { NavigatorName, ScreenName } from "~/const";
import ClaimRewardIcon from "~/icons/ClaimReward";
import DelegateIcon from "~/icons/Delegate";
import IlluRewards from "~/icons/images/Rewards";
import UndelegateIcon from "~/icons/Undelegate";
import ValidatorImage from "../shared/ValidatorImage";
import DelegationLabelRight from "./LabelRight";
import DelegationRow from "./Row";
type Props = {
account: SolanaAccount;
};
type DelegationDrawerProps = Parameters<typeof DelegationDrawer>[0];
type DelegationDrawerActions = DelegationDrawerProps["actions"];
function Delegations({ account }: Props) {
const { colors } = useTheme();
const { t } = useTranslation();
const mainAccount = getMainAccount(account, undefined) as SolanaAccount;
const currency = getAccountCurrency(mainAccount);
invariant(currency.type === "CryptoCurrency", "expected crypto currency");
const stakesWithMeta: SolanaStakeWithMeta[] = useSolanaStakesWithMeta(
currency,
mainAccount.solanaResources?.stakes ?? [],
);
const unit = getAccountUnit(mainAccount);
const navigation = useNavigation();
const [selectedStakeWithMeta, setSelectedStakeWithMeta] = useState<
SolanaStakeWithMeta | undefined
>();
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const onNavigate = useCallback(
({
route,
screen,
params,
}: {
route: string;
screen?: string;
params?: { [key: string]: unknown };
}) => {
setSelectedStakeWithMeta(undefined);
(navigation as StackNavigationProp<{ [key: string]: object | undefined }>).navigate(route, {
screen,
params: { ...params, accountId: account.id },
});
},
[account.id, navigation],
);
const onCloseDrawer = () => {
setIsDrawerOpen(false);
};
const openValidatorUrl = useCallback(
({ stake, meta }: SolanaStakeWithMeta) => {
const { delegation } = stake;
const url =
meta.validator?.url ??
(delegation?.voteAccAddr &&
getAddressExplorer(getDefaultExplorerView(account.currency), delegation.voteAccAddr));
if (url) {
Linking.openURL(url);
}
},
[account.currency],
);
const formatAmount = useCallback(
(amount: number) =>
formatCurrencyUnit(unit, new BigNumber(amount), {
disableRounding: true,
alwaysShowSign: false,
showCode: true,
}),
[unit],
);
const data = useMemo<DelegationDrawerProps["data"]>(() => {
if (selectedStakeWithMeta === undefined) {
return [];
}
const { stake, meta } = selectedStakeWithMeta;
return [
{
label: t("delegation.validator"),
Component: (
<Touchable
onPress={() => openValidatorUrl(selectedStakeWithMeta)}
event="DelegationOpenExplorer"
>
<Text
numberOfLines={1}
fontWeight="semiBold"
ellipsizeMode="middle"
style={[styles.valueText]}
color="live"
>
{meta.validator?.name ?? stake.delegation?.voteAccAddr ?? "N/A"}
</Text>
</Touchable>
),
},
{
label: t("solana.delegation.stakeActivationState"),
Component: (
<Text
numberOfLines={1}
fontWeight="semiBold"
ellipsizeMode="middle"
style={[styles.valueText]}
color="live"
>
{t(`solana.delegation.states.${stake.activation.state}`)}
</Text>
),
},
{
label: t("solana.delegation.stakeActivePercent"),
Component: (
<Text
numberOfLines={1}
fontWeight="semiBold"
ellipsizeMode="middle"
style={[styles.valueText]}
color="live"
>
{stake.delegation === undefined ? 0 : stakeActivePercent(stake).toFixed(2)} %
</Text>
),
},
{
label: t("solana.delegation.stakeAvailableBalance"),
Component: (
<Text
numberOfLines={1}
fontWeight="semiBold"
ellipsizeMode="middle"
style={[styles.valueText]}
color="live"
>
{formatAmount(stake.withdrawable)}
</Text>
),
},
];
}, [selectedStakeWithMeta, t, formatAmount, openValidatorUrl]);
const delegationActions = useMemo<DelegationDrawerActions>(() => {
const allStakeActions = tupleOfUnion<StakeAction>()([
"activate",
"deactivate",
"reactivate",
"withdraw",
]);
const availableActions =
(selectedStakeWithMeta && stakeActions(selectedStakeWithMeta.stake)) ?? [];
return allStakeActions.map(action => {
const actionEnabled = availableActions.includes(action);
const enabledActionCircleBgColor = sweetch(action, {
activate: colors.fog,
deactivate: rgba(colors.alert, 0.2),
reactivate: colors.fog,
withdraw: rgba(colors.yellow, 0.2),
});
const drawerAction: DelegationDrawerActions[number] = {
label: t(`solana.delegation.actions.${action}`),
Icon: (props: IconProps) => (
<Circle {...props} bg={actionEnabled ? enabledActionCircleBgColor : colors.lightFog}>
<DrawerStakeActionIcon stakeAction={action} enabled={actionEnabled} />
</Circle>
),
event: `DelegationAction${capitalize(action)}`,
onPress: () => {
onNavigate({
route: NavigatorName.SolanaDelegationFlow,
screen: ScreenName.DelegationSummary,
params: {
delegationAction: {
kind: "change",
stakeWithMeta: selectedStakeWithMeta,
stakeAction: action,
},
},
});
},
disabled: !availableActions.includes(action),
};
return drawerAction;
});
}, [
selectedStakeWithMeta,
colors.fog,
colors.alert,
colors.yellow,
colors.lightFog,
onNavigate,
t,
]);
const onDelegate = () => {
onNavigate({
route: NavigatorName.SolanaDelegationFlow,
screen: ScreenName.DelegationSummary,
params: {
delegationAction: {
kind: "new",
},
},
});
};
const onDelegationSelected = (stakeWithMeta: SolanaStakeWithMeta) => {
setIsDrawerOpen(true);
setSelectedStakeWithMeta(stakeWithMeta);
};
return (
<View style={styles.root}>
<DelegationDrawer
isOpen={data && data.length > 0 && isDrawerOpen}
onClose={onCloseDrawer}
account={account}
ValidatorImage={({ size }) => (
<ValidatorImage
imgUrl={selectedStakeWithMeta?.meta?.validator?.img}
name={
selectedStakeWithMeta?.meta?.validator?.name ??
selectedStakeWithMeta?.stake.delegation?.voteAccAddr
}
size={size}
/>
)}
amount={new BigNumber(selectedStakeWithMeta?.stake?.delegation?.stake ?? 0)}
data={data}
actions={delegationActions}
/>
{stakesWithMeta.length === 0 ? (
<AccountDelegationInfo
title={t("account.delegation.info.title")}
image={<IlluRewards style={styles.illustration} />}
description={t("solana.delegation.delegationEarn", {
name: account.currency.name,
})}
infoUrl={urls.solana.stakingPage}
infoTitle={t("solana.delegation.info")}
onPress={onDelegate}
ctaTitle={t("account.delegation.info.cta")}
/>
) : (
<View style={styles.wrapper}>
<AccountSectionLabel
name={t("account.delegation.sectionLabel")}
RightComponent={<DelegationLabelRight disabled={false} onPress={onDelegate} />}
/>
<Box mt={6}>
{stakesWithMeta.map((stakeWithMeta, i) => (
<View
key={stakeWithMeta.stake.stakeAccAddr}
style={[styles.delegationsWrapper, { backgroundColor: colors.card }]}
>
<DelegationRow
stakeWithMeta={stakeWithMeta}
currency={currency}
unit={unit}
onPress={onDelegationSelected}
isLast={i === stakesWithMeta.length - 1}
/>
</View>
))}
</Box>
</View>
)}
</View>
);
}
function DrawerStakeActionIcon({
enabled,
stakeAction,
}: {
enabled: boolean;
stakeAction: StakeAction;
}) {
const { colors } = useTheme();
const iconColor = enabled ? undefined : colors.grey;
switch (stakeAction) {
case "activate":
case "reactivate":
return <DelegateIcon color={iconColor} />;
case "deactivate":
return <UndelegateIcon color={iconColor} />;
case "withdraw":
return <ClaimRewardIcon color={iconColor} />;
default:
return assertUnreachable(stakeAction);
}
}
export default function SolanaDelegations({ account }: { account: AccountLike }) {
if (!(account as SolanaAccount).solanaResources) return null;
return <Delegations account={account as SolanaAccount} />;
}
const styles = StyleSheet.create({
root: {
marginHorizontal: 16,
},
illustration: { alignSelf: "center", marginBottom: 16 },
rewardsWrapper: {
flexDirection: "row",
justifyContent: "space-between",
alignContent: "center",
padding: 16,
marginBottom: 16,
borderRadius: 4,
},
label: {
fontSize: 20,
flex: 1,
},
subLabel: {
fontSize: 14,
flex: 1,
},
column: {
flexDirection: "column",
},
wrapper: {},
delegationsWrapper: {
borderRadius: 4,
},
valueText: {
fontSize: 14,
},
});