import { MsgMigrateContractEncodeObject } from "@cosmjs/cosmwasm-stargate"; import { toUtf8 } from "@cosmjs/encoding"; import dynamic from "next/dynamic"; import { useEffect, useRef, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; const JsonEditor = dynamic(() => import("../../../inputs/JsonEditor"), { ssr: false }); interface MsgMigrateContractFormProps { readonly fromAddress: string; readonly setMsgGetter: (msgGetter: MsgGetter) => void; readonly deleteMsg: () => void; } const MsgMigrateContractForm = ({ fromAddress, setMsgGetter, deleteMsg, }: MsgMigrateContractFormProps) => { const { chain } = useChains(); const [contractAddress, setContractAddress] = useState(""); const [codeId, setCodeId] = useState(""); const [msgContent, setMsgContent] = useState("{}"); const jsonError = useRef(false); const [contractAddressError, setContractAddressError] = useState(""); const [codeIdError, setCodeIdError] = useState(""); const trimmedInputs = trimStringsObj({ contractAddress, codeId }); useEffect(() => { // eslint-disable-next-line no-shadow const { contractAddress, codeId } = trimmedInputs; const isMsgValid = (): boolean => { setContractAddressError(""); setCodeIdError(""); if (jsonError.current) { return false; } const addressErrorMsg = checkAddress(contractAddress, chain.addressPrefix); if (addressErrorMsg) { setContractAddressError(`Invalid address for network ${chain.chainId}: ${addressErrorMsg}`); return false; } if (!codeId || !Number.isSafeInteger(Number(codeId)) || Number(codeId) <= 0) { setCodeIdError("Code ID must be a positive integer"); return false; } return true; }; const msgContentUtf8Array = (() => { try { // The JsonEditor does not escape \n or remove whitespaces, so we need to parse + stringify return toUtf8(JSON.stringify(JSON.parse(msgContent))); } catch { return undefined; } })(); const msgValue = MsgCodecs[MsgTypeUrls.Migrate].fromPartial({ sender: fromAddress, contract: contractAddress, codeId: BigInt(codeId), msg: msgContentUtf8Array, }); const msg: MsgMigrateContractEncodeObject = { typeUrl: MsgTypeUrls.Migrate, value: msgValue }; setMsgGetter({ isMsgValid, msg }); }, [chain.addressPrefix, chain.chainId, fromAddress, msgContent, setMsgGetter, trimmedInputs]); return (

MsgMigrateContract

{ setContractAddress(target.value); setContractAddressError(""); }} error={contractAddressError} placeholder={`E.g. ${exampleAddress(0, chain.addressPrefix)}`} />
{ setCodeId(target.value); setCodeIdError(""); }} error={codeIdError} />
{ setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}"); jsonError.current = !!contentErrors; }} />
); }; export default MsgMigrateContractForm;