From dfa32c4ada29c0e7e71cd5a948757c983891538b Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Fri, 19 May 2023 13:45:01 +0200 Subject: [PATCH] Add and use MsgSetWithdrawAddressForm --- .../MsgForm/MsgSetWithdrawAddressForm.tsx | 97 +++++++++++++++++++ .../forms/CreateTxForm/MsgForm/index.tsx | 3 + components/forms/CreateTxForm/index.tsx | 1 + 3 files changed, 101 insertions(+) create mode 100644 components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx new file mode 100644 index 0000000..530ee56 --- /dev/null +++ b/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx @@ -0,0 +1,97 @@ +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 { isTxMsgSetWithdrawAddress } from "../../../../lib/txMsgHelpers"; +import { TxMsg, TxMsgSetWithdrawAddress } from "../../../../types/txMsg"; +import Input from "../../../inputs/Input"; +import StackableContainer from "../../../layout/StackableContainer"; + +interface MsgSetWithdrawAddressFormProps { + readonly delegatorAddress: string; + readonly setMsgGetter: (msgGetter: MsgGetter) => void; + readonly deleteMsg: () => void; +} + +const MsgSetWithdrawAddressForm = ({ + delegatorAddress, + setMsgGetter, + deleteMsg, +}: MsgSetWithdrawAddressFormProps) => { + const { state } = useAppContext(); + assert(state.chain.addressPrefix, "addressPrefix missing"); + + const [withdrawAddress, setWithdrawAddress] = useState(""); + const [withdrawAddressError, setWithdrawAddressError] = useState(""); + + useEffect(() => { + try { + setWithdrawAddressError(""); + + const isMsgValid = (msg: TxMsg): msg is TxMsgSetWithdrawAddress => { + assert(state.chain.addressPrefix, "addressPrefix missing"); + + const addressErrorMsg = checkAddress(withdrawAddress, state.chain.addressPrefix); + if (addressErrorMsg) { + setWithdrawAddressError( + `Invalid address for network ${state.chain.chainId}: ${addressErrorMsg}`, + ); + return false; + } + + return isTxMsgSetWithdrawAddress(msg); + }; + + const msg: TxMsgSetWithdrawAddress = { + typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", + value: { delegatorAddress, withdrawAddress }, + }; + + setMsgGetter({ isMsgValid, msg }); + } catch {} + }, [ + delegatorAddress, + setMsgGetter, + state.chain.addressPrefix, + state.chain.chainId, + withdrawAddress, + ]); + + return ( + + +

MsgSetWithdrawAddress

+
+ setWithdrawAddress(target.value)} + error={withdrawAddressError} + placeholder={`E.g. ${exampleAddress(0, state.chain.addressPrefix)}`} + /> +
+ +
+ ); +}; + +export default MsgSetWithdrawAddressForm; diff --git a/components/forms/CreateTxForm/MsgForm/index.tsx b/components/forms/CreateTxForm/MsgForm/index.tsx index fea65d0..a0f6165 100644 --- a/components/forms/CreateTxForm/MsgForm/index.tsx +++ b/components/forms/CreateTxForm/MsgForm/index.tsx @@ -4,6 +4,7 @@ import MsgClaimRewardsForm from "./MsgClaimRewardsForm"; import MsgDelegateForm from "./MsgDelegateForm"; import MsgRedelegateForm from "./MsgRedelegateForm"; import MsgSendForm from "./MsgSendForm"; +import MsgSetWithdrawAddressForm from "./MsgSetWithdrawAddressForm"; import MsgUndelegateForm from "./MsgUndelegateForm"; type MsgFormProps = { @@ -26,6 +27,8 @@ const MsgForm = ({ txType, senderAddress, ...restProps }: MsgFormProps) => { return ; case "claimRewards": return ; + case "setWithdrawAddress": + return ; default: return null; } diff --git a/components/forms/CreateTxForm/index.tsx b/components/forms/CreateTxForm/index.tsx index 67ff3d8..4fce992 100644 --- a/components/forms/CreateTxForm/index.tsx +++ b/components/forms/CreateTxForm/index.tsx @@ -145,6 +145,7 @@ const CreateTxForm = ({ router, senderAddress, accountOnChain }: CreateTxFormPro