From 2e0a80dabff3c24861168024f113ea62f6aba4ac Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Fri, 5 May 2023 13:25:24 +0200 Subject: [PATCH] Use msgGetter and deleteMsg in MsgUndelegateForm --- .../MsgForm/MsgUndelegateForm.tsx | 105 +++++++++++------- 1 file changed, 66 insertions(+), 39 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx index 2e32184..3536e99 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx @@ -1,17 +1,25 @@ import { Decimal } from "@cosmjs/math"; import { assert } from "@cosmjs/utils"; -import { Dispatch, SetStateAction, useCallback, useEffect, useState } from "react"; +import { useEffect, useState } from "react"; +import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; +import { isTxMsgUndelegate } from "../../../../lib/txMsgHelpers"; import { TxMsg, TxMsgUndelegate } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; +import StackableContainer from "../../../layout/StackableContainer"; interface MsgUndelegateFormProps { readonly delegatorAddress: string; - readonly setCheckAndGetMsg: Dispatch TxMsg | null) | undefined>>; + readonly setMsgGetter: (msgGetter: MsgGetter) => void; + readonly deleteMsg: () => void; } -const MsgUndelegateForm = ({ delegatorAddress, setCheckAndGetMsg }: MsgUndelegateFormProps) => { +const MsgUndelegateForm = ({ + delegatorAddress, + setMsgGetter, + deleteMsg, +}: MsgUndelegateFormProps) => { const { state } = useAppContext(); assert(state.chain.addressPrefix, "addressPrefix missing"); @@ -21,41 +29,52 @@ const MsgUndelegateForm = ({ delegatorAddress, setCheckAndGetMsg }: MsgUndelegat const [validatorAddressError, setValidatorAddressError] = useState(""); const [amountError, setAmountError] = useState(""); - const checkAndGetMsg = useCallback(() => { - assert(state.chain.addressPrefix, "addressPrefix missing"); - assert(state.chain.denom, "denom missing"); + useEffect(() => { + try { + assert(state.chain.denom, "denom missing"); - const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); - if (addressErrorMsg) { - setValidatorAddressError( - `Invalid address for network ${state.chain.chainId}: ${addressErrorMsg}`, - ); - return null; - } + setValidatorAddressError(""); + setAmountError(""); - if (!amount || Number(amount) <= 0) { - setAmountError("Amount must be greater than 0"); - return null; - } + const isMsgValid = (msg: TxMsg): msg is TxMsgUndelegate => { + assert(state.chain.addressPrefix, "addressPrefix missing"); - const amountInAtomics = Decimal.fromUserInput( - amount, - Number(state.chain.displayDenomExponent), - ).atomics; + const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); + if (addressErrorMsg) { + setValidatorAddressError( + `Invalid address for network ${state.chain.chainId}: ${addressErrorMsg}`, + ); + return false; + } - const msg: TxMsgUndelegate = { - typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", - value: { - delegatorAddress, - validatorAddress, - amount: [{ amount: amountInAtomics, denom: state.chain.denom }], - }, - }; + if (!amount || Number(amount) <= 0) { + setAmountError("Amount must be greater than 0"); + return false; + } - return msg; + return isTxMsgUndelegate(msg); + }; + + const amountInAtomics = Decimal.fromUserInput( + amount || "0", + Number(state.chain.displayDenomExponent), + ).atomics; + + const msg: TxMsgUndelegate = { + typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", + value: { + delegatorAddress, + validatorAddress, + amount: [{ amount: amountInAtomics, denom: state.chain.denom }], + }, + }; + + setMsgGetter({ isMsgValid, msg }); + } catch {} }, [ amount, delegatorAddress, + setMsgGetter, state.chain.addressPrefix, state.chain.chainId, state.chain.denom, @@ -63,12 +82,12 @@ const MsgUndelegateForm = ({ delegatorAddress, setCheckAndGetMsg }: MsgUndelegat validatorAddress, ]); - useEffect(() => { - setCheckAndGetMsg(() => checkAndGetMsg); - }, [checkAndGetMsg, setCheckAndGetMsg]); - return ( - <> + + +

MsgUndelegate

- +
); };