From 4162a4cfcdd8a284a1fd400db8dffbcee4661280 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 20 Apr 2023 12:36:00 +0200 Subject: [PATCH] Refactored register node function --- .../ui/src/components/Setup/RegisterNode.tsx | 223 +++++++++++++----- 1 file changed, 162 insertions(+), 61 deletions(-) diff --git a/build/ui/src/components/Setup/RegisterNode.tsx b/build/ui/src/components/Setup/RegisterNode.tsx index fcfeb93..cc0a763 100644 --- a/build/ui/src/components/Setup/RegisterNode.tsx +++ b/build/ui/src/components/Setup/RegisterNode.tsx @@ -1,11 +1,23 @@ import React, { useState, useEffect } from "react"; -import { Typography, Button, Chip, Box, TextField, CircularProgress, Link } from "@mui/material"; +import { + Typography, + Button, + Chip, + Box, + TextField, + CircularProgress, + Link, +} from "@mui/material"; import OpenInNewIcon from "@mui/icons-material/OpenInNew"; import CopyToClipboardButton from "../Buttons/CopyToClipboardButton"; import { AppService } from "../../services/AppService"; import { RocketpoolData } from "../../types/RocketpoolData"; import { CanRegisterNode } from "../../types/CanRegisterNode"; -import { enoughEthBalance, isValidEthAddress, toEther, toEtherString } from "../../utils/Utils"; +import { + enoughEthBalance, + isValidEthAddress, + toEtherString, +} from "../../utils/Utils"; import { NodeCanSetWithdrawalAddress } from "../../types/NodeCanSetWithdrawalAddress"; import { RocketpoolContext } from "../Providers/Context"; import { TxResponse } from "../../types/TxResponse"; @@ -25,13 +37,16 @@ const RegisterNode: React.FC = ({ const [actionsEnabled, setActionsEnabled] = useState(true); const [canRegisterNode, setCanRegisterNode] = useState(); const [txResponse, setTxResponse] = useState(); - const [canSetWithdrawalAddress, setCanSetWithdrawalAddress] = useState(); + const [canSetWithdrawalAddress, setCanSetWithdrawalAddress] = + useState(); const [addressEntered, setAddressEntered] = useState(""); const [addressError, setAddressError] = useState(""); - const { rocketpoolValue, updateRocketpoolValue } = React.useContext(RocketpoolContext); + const { rocketpoolValue, updateRocketpoolValue } = + React.useContext(RocketpoolContext); const minimumRpl8Eth = data?.networkRplPrice?.minPer8EthMinipoolRplStake ?? 0; - const minimumRpl16Eth = data?.networkRplPrice?.minPer16EthMinipoolRplStake ?? 0; + const minimumRpl16Eth = + data?.networkRplPrice?.minPer16EthMinipoolRplStake ?? 0; const minimumRpl = Math.min(minimumRpl8Eth, minimumRpl16Eth); const ethBalance = data?.nodeStatus?.accountBalances.eth ?? 0; const rplBalance = data?.nodeStatus?.accountBalances.rpl ?? 0; @@ -42,62 +57,127 @@ const RegisterNode: React.FC = ({ var canRegisterNode = await appService.canRegisterNode(); setCanRegisterNode(canRegisterNode); setActionsEnabled(canRegisterNode.canRegister); - }; + } useEffect(() => { fetchData(); }, [data]); const handleRegisterNodeClick = async () => { + setTxs([]); + setIsLoading(true); + setActionsEnabled(false); + try { - setTxs([]); - setIsLoading(true); - setActionsEnabled(false); - var canNodeRegister = await appService.canRegisterNode(); - if (!canNodeRegister.canRegister) { return } - var tx = await appService.nodeRegister(); - console.log(tx) - setTxResponse(tx); - if (tx.status !== "success") { return } - setTxs([...txs, tx.txHash]); - var waitResponse = await appService.wait(tx.txHash); - if (waitResponse.status !== "success") { return } - var canWithdrawalRegister = await appService.getNodeCanSetWithdrawalAddress(addressEntered); - setCanSetWithdrawalAddress(canWithdrawalRegister); - if (!canWithdrawalRegister.canSet) { return } - tx = await appService.nodeSetWithdrawalAddress(addressEntered); - setTxResponse(tx); - if (tx.status !== "success") { return } - setTxs([...txs, tx.txHash]); - waitResponse = await appService.wait(tx.txHash); - var canSetSmoothingPool = await appService.getNodeCanSetSmoothingPoolStatus(); - if (canSetSmoothingPool.status !== "success") { return } - tx = await appService.nodeSetSmoothingPoolStatus(); - console.log(tx); - setTxResponse(tx); - if (tx.status !== "success") { return } - setTxs([...txs, tx.txHash]); - waitResponse = await appService.wait(tx.txHash); + await checkCanNodeRegister(); + + await registerNode(); + + await checkCanSetWithdrawalAddress(); + + await setWithdrawalAddress(); + + await checkCanSetSmoothingPoolStatus(); + + await setSmoothingPoolStatus(); } finally { setIsLoading(false); setActionsEnabled(true); setCanSetWithdrawalAddress(undefined); - var walletStatus = await appService.getWalletStatus(); - var nodeStatus = await appService.getNodeStatus(); + const walletStatus = await appService.getWalletStatus(); + const nodeStatus = await appService.getNodeStatus(); updateRocketpoolValue( new RocketpoolData( rocketpoolValue?.network, walletStatus, nodeStatus, rocketpoolValue?.nodeSync, - rocketpoolValue?.networkRplPrice, + rocketpoolValue?.networkRplPrice ) ); } }; + async function checkCanNodeRegister() { + const { canRegister } = await appService.canRegisterNode(); + if (!canRegister) { + throw new Error("Cannot register node"); + } + } + + async function registerNode() { + const registrationTx = await appService.nodeRegister(); + console.debug(registrationTx); + setTxResponse(registrationTx); + + if (registrationTx.status !== "success") { + throw new Error("Call to nodeRegister failed"); + } + + setTxs([...txs, registrationTx.txHash]); + + const waitResponse = await appService.wait(registrationTx.txHash); + + if (waitResponse.status !== "success") { + throw new Error("Call to wait failed"); + } + } + + async function checkCanSetWithdrawalAddress() { + const canSetWithdrawalAddress = + await appService.getNodeCanSetWithdrawalAddress(addressEntered); + + setCanSetWithdrawalAddress(canSetWithdrawalAddress); + + if (!canSetWithdrawalAddress.canSet) { + throw new Error("Cannot set withdrawal address"); + } + } + + async function setWithdrawalAddress() { + const setWithdrawalAddressTx = await appService.nodeSetWithdrawalAddress( + addressEntered + ); + + console.debug(setWithdrawalAddressTx); + + setTxResponse(setWithdrawalAddressTx); + + if (setWithdrawalAddressTx.status !== "success") { + throw new Error("Call to nodeSetWithdrawalAddress failed"); + } + setTxs([...txs, setWithdrawalAddressTx.txHash]); + + await appService.wait(setWithdrawalAddressTx.txHash); + } + + async function checkCanSetSmoothingPoolStatus() { + const canSetSmoothingPool = + await appService.getNodeCanSetSmoothingPoolStatus(); + + if (canSetSmoothingPool.status !== "success") { + throw new Error("Call to getNodeCanSetSmoothingPoolStatus failed"); + } + } + + async function setSmoothingPoolStatus() { + const setSmoothingPoolStatusTx = + await appService.nodeSetSmoothingPoolStatus(); + console.debug(setSmoothingPoolStatusTx); + setTxResponse(setSmoothingPoolStatusTx); + + if (setSmoothingPoolStatusTx.status !== "success") { + throw new Error("Call to nodeSetSmoothingPoolStatus failed"); + } + setTxs([...txs, setSmoothingPoolStatusTx.txHash]); + await appService.wait(setSmoothingPoolStatusTx.txHash); + } + const isValidAddressEntered = (address: string) => { - return isValidEthAddress(address) && (address !== data?.walletStatus?.accountAddress); + return ( + isValidEthAddress(address) && + address !== data?.walletStatus?.accountAddress + ); }; const handleAddressChange = (event: any) => { @@ -106,23 +186,26 @@ const RegisterNode: React.FC = ({ if (!isValidAddressEntered(newValue)) { setAddressError(`${newValue} is not a valid Ethereum address`); } else { - setAddressError(''); + setAddressError(""); } }; const enoughTokens = () => { - return enoughEthBalance(data?.nodeStatus, data?.networkRplPrice) && enoughRpl; + return ( + enoughEthBalance(data?.nodeStatus, data?.networkRplPrice) && enoughRpl + ); }; return ( * + *': { marginTop: '15px' } - }}> + sx={{ + display: "flex", + flexDirection: "column", + alignItems: "center", + minHeight: "100vh", + "& > * + *": { marginTop: "15px" }, + }} + > <> You must have enough ETH and RPL in order to Register your node @@ -131,17 +214,17 @@ const RegisterNode: React.FC = ({ Transfer tokens to your address:{" "} {data?.walletStatus?.accountAddress} - +
- - Balances - + Balances - {' '} + />{" "} = ({ Configure your withdrawal address - The withdrawal address must be an address of which we are certain we have access and the private key. + The withdrawal address must be an address of which we are certain we + have access and the private key.
= ({ sx={{ marginTop: 2 }} />
- {txs.map((tx, index) => ( - + View transaction {index + 1} on Etherscan @@ -198,6 +299,6 @@ const RegisterNode: React.FC = ({
); -} +}; export default RegisterNode;