-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialWithdrawValidatorTable.tsx
More file actions
353 lines (327 loc) · 12.2 KB
/
PartialWithdrawValidatorTable.tsx
File metadata and controls
353 lines (327 loc) · 12.2 KB
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
import { DoDisturb as DoDisturbIcon } from "@mui/icons-material";
import {
Box,
Typography,
TableContainer,
Table,
TableHead,
TableRow,
TableBody,
TableSortLabel,
IconButton,
} from "@mui/material";
import BigNumber from "bignumber.js";
import React, { useState, useMemo, useEffect } from "react";
import {
CustomTableCell,
CustomTableHeaderCell,
CustomTableRow,
} from "@/components/CustomTable";
import { CustomTextField } from "@/components/CustomTextField";
import { ExplorerLink } from "@/components/ExplorerLink";
import { FilterInput } from "@/components/Input";
import { ValidatorsWrapper } from "@/components/ValidatorsWrapper";
import { useSelectedValidator } from "@/context/SelectedValidatorContext";
import { useValidators } from "@/hooks/useValidators";
import {
Credentials,
Validator,
ValidatorStatus,
WithdrawalEntry,
} from "@/types";
import { enforceGweiPrecision } from "@/utils/number";
interface PartialWithdrawValidatorTableParams {
entries: WithdrawalEntry[];
setEntries: React.Dispatch<React.SetStateAction<WithdrawalEntry[]>>;
}
const MIN_WITHDRAWAL_AMOUNT = 0;
const MINIMUM_BALANCE = 32;
export const PartialWithdrawValidatorTable = ({
entries,
setEntries,
}: PartialWithdrawValidatorTableParams) => {
const { selectedValidator, setSelectedValidator } = useSelectedValidator();
const { data: validatorData } = useValidators();
const [searchQuery, setSearchQuery] = useState("");
const [sortAscending, setSortAscending] = useState<boolean>(true);
const [hoveredRow, setHoveredRow] = useState<string | null>(null);
useEffect(() => {
if (selectedValidator) {
setSearchQuery(selectedValidator.pubkey);
setSelectedValidator(null);
}
}, [selectedValidator]);
const validators = useMemo(() => {
return validatorData?.validators || [];
}, [validatorData]);
useEffect(() => {
if (
entries.some(
(e) => !validators.find((v) => v.pubkey === e.validator.pubkey),
)
) {
setEntries((prev) =>
prev.filter(
(e) => !!validators.find((v) => v.pubkey === e.validator.pubkey),
),
);
}
}, [entries, validators]);
const filteredValidators = useMemo(() => {
return validators
.filter(
(validator) =>
validator.credentials === Credentials.compounding &&
validator.status === ValidatorStatus.active_ongoing &&
(validator.pubkey.toLowerCase().includes(searchQuery.toLowerCase()) ||
validator.index.toString().includes(searchQuery)),
)
.sort((a, b) => {
const aIndex = parseInt(a.index);
const bIndex = parseInt(b.index);
return sortAscending ? aIndex - bIndex : bIndex - aIndex;
});
}, [searchQuery, validators, sortAscending]);
const createEntry = (
pubkey: string,
amount: string,
): WithdrawalEntry | undefined => {
let changedValue = false;
let numericAmount = new BigNumber(amount === "" ? 0 : amount);
const validator = validators.find((v) => v.pubkey === pubkey);
if (!validator) {
return;
}
if (!numericAmount.eq(0) && numericAmount.isNaN()) {
changedValue = true;
numericAmount = new BigNumber(0);
}
const maxWithdrawal = Math.max(
0,
new BigNumber(validator.totalBalance).minus(MINIMUM_BALANCE).toNumber(),
);
if (numericAmount.gt(maxWithdrawal)) {
changedValue = true;
numericAmount = new BigNumber(maxWithdrawal);
}
if (numericAmount.lt(MIN_WITHDRAWAL_AMOUNT)) {
numericAmount = new BigNumber(0);
changedValue = true;
}
return {
validator,
withdrawalAmount: changedValue ? numericAmount.toString() : amount,
};
};
const handleWithdrawalAmountChange = (pubkey: string, amount: string) => {
const gweiAmount = enforceGweiPrecision(amount);
const newEntry = createEntry(pubkey, gweiAmount.toString());
if (!newEntry) {
return;
}
setEntries((prev) => {
const prevEntry = prev.find((e) => e.validator.pubkey === pubkey);
const withdrawalAmount = new BigNumber(newEntry.withdrawalAmount || "-1");
if (!prevEntry && withdrawalAmount.gte(MIN_WITHDRAWAL_AMOUNT)) {
return [...prev, newEntry];
} else if (
withdrawalAmount.isNaN() ||
withdrawalAmount.lt(MIN_WITHDRAWAL_AMOUNT)
) {
return prev.filter((entry) => entry.validator.pubkey !== pubkey);
} else {
return prev.map((entry) => ({
validator: entry.validator,
withdrawalAmount:
entry.validator.pubkey === newEntry.validator.pubkey
? newEntry.withdrawalAmount
: entry.withdrawalAmount,
}));
}
});
};
const onInputBlur = (validator: Validator) => {
const entry = entries.find((e) => e.validator.pubkey === validator.pubkey);
if (entry && new BigNumber(entry.withdrawalAmount).lte(0)) {
setEntries((prev) =>
prev.filter((e) => e.validator.pubkey !== validator.pubkey),
);
}
};
const getRemainingBalance = (validator: Validator) => {
const entry = entries.find((e) => e.validator.pubkey === validator.pubkey);
return new BigNumber(validator.totalBalance)
.minus(entry?.withdrawalAmount || 0)
.toNumber();
};
const formatBalance = (balance: number) => {
return balance.toFixed(4);
};
return (
<Box className="rounded bg-background p-6">
<Box className="mb-6 flex items-center justify-between">
<Typography variant="h6" className="font-semibold text-white">
Validators
</Typography>
<FilterInput
placeholder="Filter your 0x02 validators by index or public key..."
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
/>
</Box>
<ValidatorsWrapper>
<TableContainer>
<Table>
<TableHead>
<TableRow className="bg-[#171717]">
<CustomTableHeaderCell
onClick={() => setSortAscending((prev) => !prev)}
>
<TableSortLabel
active={true}
direction={sortAscending ? "asc" : "desc"}
>
Index
</TableSortLabel>
</CustomTableHeaderCell>
<CustomTableHeaderCell>Public Key</CustomTableHeaderCell>
<CustomTableHeaderCell>Current Balance</CustomTableHeaderCell>
<CustomTableHeaderCell>Remaining Balance</CustomTableHeaderCell>
<CustomTableHeaderCell>Withdrawal Amount</CustomTableHeaderCell>
</TableRow>
</TableHead>
<TableBody>
{filteredValidators.map((validator, index) => {
const entry = entries.find(
(e) => e.validator.pubkey === validator.pubkey,
);
const remainingBalance = getRemainingBalance(validator);
const maxWithdrawal = Math.max(
0,
validator.totalBalance - MINIMUM_BALANCE,
);
const canWithdraw =
validator.totalBalance >
MINIMUM_BALANCE + MIN_WITHDRAWAL_AMOUNT;
const hasWithdrawal = new BigNumber(
entry?.withdrawalAmount || 0,
).gt(0);
return (
<CustomTableRow
key={validator.pubkey}
index={index}
onMouseEnter={() => setHoveredRow(validator.pubkey)}
onMouseLeave={() => setHoveredRow(null)}
>
<CustomTableCell>{validator.index}</CustomTableCell>
<CustomTableCell>
<Typography className="font-semibold">
<ExplorerLink
hash={validator.pubkey}
shorten
type="publickey"
/>
</Typography>
</CustomTableCell>
<CustomTableCell>
<Typography
className="text-sm font-semibold"
sx={{
color: hasWithdrawal ? "#ef4444" : "#ffffff",
textDecoration: hasWithdrawal
? "line-through"
: "none",
}}
>
{formatBalance(validator.totalBalance)} ETH
</Typography>
</CustomTableCell>
<CustomTableCell className="text-sm font-semibold">
<Typography
className="text-sm font-semibold"
sx={{
color: hasWithdrawal ? "#22c55e" : "#ffffff",
}}
>
{formatBalance(remainingBalance)} ETH
</Typography>
</CustomTableCell>
<CustomTableCell
className="text-sm font-semibold"
sx={{ overflow: "visible" }}
>
<Box
sx={{ position: "relative", display: "inline-block" }}
>
<CustomTextField
className="w-[200px]"
size="small"
placeholder={
canWithdraw ? "Enter amount" : "Not enough balance"
}
type="number"
value={entry?.withdrawalAmount.toString() || ""}
onBlur={() => onInputBlur(validator)}
onChange={(e) =>
handleWithdrawalAmountChange(
validator.pubkey,
e.target.value,
)
}
disabled={!canWithdraw}
slotProps={{
input: {
inputProps: {
min: 0,
max: maxWithdrawal,
onWheel: (e) => e.currentTarget.blur(),
onKeyDown: (e) => {
if (
e.key === "+" ||
e.key === "-" ||
e.key === "e" ||
e.key === "E"
) {
e.preventDefault();
}
},
},
},
}}
/>
{entry?.withdrawalAmount &&
hoveredRow === validator.pubkey && (
<IconButton
size="small"
onClick={() =>
handleWithdrawalAmountChange(
validator.pubkey,
"",
)
}
sx={{
position: "absolute",
left: "100%",
top: "50%",
transform: "translateY(-50%)",
marginLeft: "4px",
padding: "4px",
zIndex: 1000,
color: "white",
}}
>
<DoDisturbIcon sx={{ fontSize: 16 }} />
</IconButton>
)}
</Box>
</CustomTableCell>
</CustomTableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
</ValidatorsWrapper>
</Box>
);
};