import { MsgMigrateContractEncodeObject } from "@cosmjs/cosmwasm-stargate"; import { toUtf8 } from "@cosmjs/encoding"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; interface MsgMigrateContractFormProps { readonly fromAddress: string; readonly setMsgGetter: (msgGetter: MsgGetter) => void; readonly deleteMsg: () => void; } const MsgMigrateContractForm = ({ fromAddress, setMsgGetter, deleteMsg, }: MsgMigrateContractFormProps) => { const { chain } = useChains(); const [codeId, setCodeId] = useState(""); const [contractAddress, setContractAddress] = useState(""); const [msgContent, setMsgContent] = useState("{}"); const [codeIdError, setCodeIdError] = useState(""); const [contractAddressError, setContractAddressError] = useState(""); const [msgContentError, setMsgContentError] = useState(""); useEffect(() => { setCodeIdError(""); setContractAddressError(""); setMsgContentError(""); const isMsgValid = (): boolean => { if (!codeId || !Number.isSafeInteger(Number(codeId)) || Number(codeId) <= 0) { setCodeIdError("Code ID must be a positive integer"); return false; } const addressErrorMsg = checkAddress(contractAddress, chain.addressPrefix); if (addressErrorMsg) { setContractAddressError(`Invalid address for network ${chain.chainId}: ${addressErrorMsg}`); return false; } try { JSON.parse(msgContent); } catch { setMsgContentError("Msg must be valid JSON"); return false; } return true; }; const msgValue = MsgCodecs[MsgTypeUrls.Migrate].fromPartial({ sender: fromAddress, contract: contractAddress, codeId, msg: toUtf8(msgContent), }); const msg: MsgMigrateContractEncodeObject = { typeUrl: MsgTypeUrls.Migrate, value: msgValue }; setMsgGetter({ isMsgValid, msg }); }, [ codeId, contractAddress, fromAddress, msgContent, setMsgGetter, chain.addressPrefix, chain.chainId, chain.denom, ]); return (

MsgMigrateContract

setCodeId(target.value)} error={codeIdError} />
setContractAddress(target.value)} error={contractAddressError} placeholder={`E.g. ${exampleAddress(0, chain.addressPrefix)}`} />
setMsgContent(target.value)} error={msgContentError} placeholder={"Enter msg JSON"} />
); }; export default MsgMigrateContractForm;