import { Decimal } from "@cosmjs/math"; import { assert } from "@cosmjs/utils"; 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 setMsgGetter: (msgGetter: MsgGetter) => void; readonly deleteMsg: () => void; } const MsgUndelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg, }: MsgUndelegateFormProps) => { const { state } = useAppContext(); assert(state.chain.addressPrefix, "addressPrefix missing"); const [validatorAddress, setValidatorAddress] = useState(""); const [amount, setAmount] = useState("0"); const [validatorAddressError, setValidatorAddressError] = useState(""); const [amountError, setAmountError] = useState(""); useEffect(() => { try { assert(state.chain.denom, "denom missing"); setValidatorAddressError(""); setAmountError(""); const isMsgValid = (msg: TxMsg): msg is TxMsgUndelegate => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); if (addressErrorMsg) { setValidatorAddressError( `Invalid address for network ${state.chain.chainId}: ${addressErrorMsg}`, ); return false; } if (!amount || Number(amount) <= 0) { setAmountError("Amount must be greater than 0"); return false; } 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, state.chain.displayDenomExponent, validatorAddress, ]); return (

MsgUndelegate

setValidatorAddress(target.value)} error={validatorAddressError} placeholder={`E.g. ${exampleAddress(0, state.chain.addressPrefix)}`} />
setAmount(target.value)} error={amountError} />
); }; export default MsgUndelegateForm;