import { Decimal } from "@cosmjs/math"; import { MsgSendEncodeObject } from "@cosmjs/stargate"; 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 { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; interface MsgSendFormProps { readonly fromAddress: string; readonly setMsgGetter: (msgGetter: MsgGetter) => void; readonly deleteMsg: () => void; } const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) => { const { state } = useAppContext(); assert(state.chain.addressPrefix, "addressPrefix missing"); const [toAddress, setToAddress] = useState(""); const [amount, setAmount] = useState("0"); const [toAddressError, setToAddressError] = useState(""); const [amountError, setAmountError] = useState(""); useEffect(() => { try { assert(state.chain.denom, "denom missing"); setToAddressError(""); setAmountError(""); const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(toAddress, state.chain.addressPrefix); if (addressErrorMsg) { setToAddressError( `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 true; }; const amountInAtomics = amount ? Decimal.fromUserInput(amount, Number(state.chain.displayDenomExponent)).atomics : "0"; const msgValue = MsgCodecs[MsgTypeUrls.Send].fromPartial({ fromAddress, toAddress, amount: [{ amount: amountInAtomics, denom: state.chain.denom }], }); const msg: MsgSendEncodeObject = { typeUrl: MsgTypeUrls.Send, value: msgValue }; setMsgGetter({ isMsgValid, msg }); } catch {} }, [ amount, fromAddress, setMsgGetter, state.chain.addressPrefix, state.chain.chainId, state.chain.denom, state.chain.displayDenomExponent, toAddress, ]); return (

MsgSend

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