From 1eeb26c0a011d59abf54f714d0ec0839b34307ef Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 14 Jun 2023 20:37:40 +0200 Subject: [PATCH] Adapt msg forms --- .../MsgForm/MsgClaimRewardsForm.tsx | 17 ++++++----- .../MsgForm/MsgCreateVestingAccountForm.tsx | 26 ++++++++-------- .../CreateTxForm/MsgForm/MsgDelegateForm.tsx | 22 +++++++------- .../MsgForm/MsgRedelegateForm.tsx | 24 +++++++-------- .../CreateTxForm/MsgForm/MsgSendForm.tsx | 22 +++++++------- .../MsgForm/MsgSetWithdrawAddressForm.tsx | 15 +++++----- .../CreateTxForm/MsgForm/MsgTransferForm.tsx | 30 +++++++++---------- .../MsgForm/MsgUndelegateForm.tsx | 22 +++++++------- .../forms/CreateTxForm/MsgForm/index.tsx | 20 ++++++------- 9 files changed, 100 insertions(+), 98 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgClaimRewardsForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgClaimRewardsForm.tsx index 1251886..9ada9e5 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgClaimRewardsForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgClaimRewardsForm.tsx @@ -1,10 +1,11 @@ +import { MsgWithdrawDelegatorRewardEncodeObject } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; +import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgClaimRewards } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgClaimRewards } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -29,7 +30,7 @@ const MsgClaimRewardsForm = ({ try { setValidatorAddressError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgClaimRewards => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); @@ -40,12 +41,14 @@ const MsgClaimRewardsForm = ({ return false; } - return isTxMsgClaimRewards(msg); + return true; }; - const msg: TxMsgClaimRewards = { - typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", - value: { delegatorAddress, validatorAddress }, + const msgValue: MsgWithdrawDelegatorReward = { delegatorAddress, validatorAddress }; + + const msg: MsgWithdrawDelegatorRewardEncodeObject = { + typeUrl: MsgTypeUrls.WithdrawDelegatorReward, + value: msgValue, }; setMsgGetter({ isMsgValid, msg }); diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx index 424515d..dc8b2d2 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx @@ -1,12 +1,13 @@ import { Decimal } from "@cosmjs/math"; +import { EncodeObject } from "@cosmjs/proto-signing"; import { assert } from "@cosmjs/utils"; +import { MsgCreateVestingAccount } from "cosmjs-types/cosmos/vesting/v1beta1/tx"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { timestampFromDatetimeLocal } from "../../../../lib/dateHelpers"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgCreateVestingAccount } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgCreateVestingAccount } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -66,7 +67,7 @@ const MsgCreateVestingAccountForm = ({ setAmountError(""); setEndTimeError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgCreateVestingAccount => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(toAddress, state.chain.addressPrefix); @@ -87,24 +88,23 @@ const MsgCreateVestingAccountForm = ({ return false; } - return isTxMsgCreateVestingAccount(msg); + return true; }; const amountInAtomics = amount ? Decimal.fromUserInput(amount, Number(state.chain.displayDenomExponent)).atomics : "0"; - const msg: TxMsgCreateVestingAccount = { - typeUrl: "/cosmos.vesting.v1beta1.MsgCreateVestingAccount", - value: { - fromAddress, - toAddress, - amount: [{ amount: amountInAtomics, denom: state.chain.denom }], - endTime: timestampFromDatetimeLocal(endTime), - delayed, - }, + const msgValue: MsgCreateVestingAccount = { + fromAddress, + toAddress, + amount: [{ amount: amountInAtomics, denom: state.chain.denom }], + endTime: timestampFromDatetimeLocal(endTime), + delayed, }; + const msg: EncodeObject = { typeUrl: MsgTypeUrls.CreateVestingAccount, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); } catch {} }, [ diff --git a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx index 6819458..efc7389 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx @@ -1,11 +1,12 @@ import { Decimal } from "@cosmjs/math"; +import { MsgDelegateEncodeObject } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; +import { MsgDelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgDelegate } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgDelegate } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -32,7 +33,7 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg setValidatorAddressError(""); setAmountError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgDelegate => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); @@ -48,7 +49,7 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg return false; } - return isTxMsgDelegate(msg); + return true; }; const amountInAtomics = Decimal.fromUserInput( @@ -56,15 +57,14 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg Number(state.chain.displayDenomExponent), ).atomics; - const msg: TxMsgDelegate = { - typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", - value: { - delegatorAddress, - validatorAddress, - amount: { amount: amountInAtomics, denom: state.chain.denom }, - }, + const msgValue: MsgDelegate = { + delegatorAddress, + validatorAddress, + amount: { amount: amountInAtomics, denom: state.chain.denom }, }; + const msg: MsgDelegateEncodeObject = { typeUrl: MsgTypeUrls.Delegate, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); } catch {} }, [ diff --git a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx index 096b270..1f74a4b 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx @@ -1,11 +1,12 @@ import { Decimal } from "@cosmjs/math"; +import { EncodeObject } from "@cosmjs/proto-signing"; import { assert } from "@cosmjs/utils"; +import { MsgBeginRedelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgRedelegate } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgRedelegate } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -39,7 +40,7 @@ const MsgRedelegateForm = ({ setValidatorDstAddressError(""); setAmountError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgRedelegate => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const srcAddressErrorMsg = checkAddress(validatorSrcAddress, state.chain.addressPrefix); @@ -63,7 +64,7 @@ const MsgRedelegateForm = ({ return false; } - return isTxMsgRedelegate(msg); + return true; }; const amountInAtomics = Decimal.fromUserInput( @@ -71,16 +72,15 @@ const MsgRedelegateForm = ({ Number(state.chain.displayDenomExponent), ).atomics; - const msg: TxMsgRedelegate = { - typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", - value: { - delegatorAddress, - validatorSrcAddress, - validatorDstAddress, - amount: { amount: amountInAtomics, denom: state.chain.denom }, - }, + const msgValue: MsgBeginRedelegate = { + delegatorAddress, + validatorSrcAddress, + validatorDstAddress, + amount: { amount: amountInAtomics, denom: state.chain.denom }, }; + const msg: EncodeObject = { typeUrl: MsgTypeUrls.BeginRedelegate, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); } catch {} }, [ diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx index e667cdb..0ce64e3 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx @@ -1,11 +1,12 @@ import { Decimal } from "@cosmjs/math"; +import { MsgSendEncodeObject } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; +import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgSend } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgSend } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -32,7 +33,7 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) setToAddressError(""); setAmountError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgSend => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(toAddress, state.chain.addressPrefix); @@ -48,22 +49,21 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) return false; } - return isTxMsgSend(msg); + return true; }; const amountInAtomics = amount ? Decimal.fromUserInput(amount, Number(state.chain.displayDenomExponent)).atomics : "0"; - const msg: TxMsgSend = { - typeUrl: "/cosmos.bank.v1beta1.MsgSend", - value: { - fromAddress, - toAddress, - amount: [{ amount: amountInAtomics, denom: state.chain.denom }], - }, + const msgValue: MsgSend = { + fromAddress, + toAddress, + amount: [{ amount: amountInAtomics, denom: state.chain.denom }], }; + const msg: MsgSendEncodeObject = { typeUrl: MsgTypeUrls.Send, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); } catch {} }, [ diff --git a/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx index 530ee56..6d78493 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx @@ -1,10 +1,11 @@ +import { EncodeObject } from "@cosmjs/proto-signing"; import { assert } from "@cosmjs/utils"; +import { MsgSetWithdrawAddress } from "cosmjs-types/cosmos/distribution/v1beta1/tx"; 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 { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -29,7 +30,7 @@ const MsgSetWithdrawAddressForm = ({ try { setWithdrawAddressError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgSetWithdrawAddress => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(withdrawAddress, state.chain.addressPrefix); @@ -40,13 +41,11 @@ const MsgSetWithdrawAddressForm = ({ return false; } - return isTxMsgSetWithdrawAddress(msg); + return true; }; - const msg: TxMsgSetWithdrawAddress = { - typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", - value: { delegatorAddress, withdrawAddress }, - }; + const msgValue: MsgSetWithdrawAddress = { delegatorAddress, withdrawAddress }; + const msg: EncodeObject = { typeUrl: MsgTypeUrls.SetWithdrawAddress, value: msgValue }; setMsgGetter({ isMsgValid, msg }); } catch {} diff --git a/components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx index 726c73f..b0a5d1a 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx @@ -1,11 +1,12 @@ +import { MsgTransferEncodeObject } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; +import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; import Long from "long"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useAppContext } from "../../../../context/AppContext"; import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers"; -import { isTxMsgTransfer } from "../../../../lib/txMsgHelpers"; -import { TxMsg, TxMsgTransfer } from "../../../../types/txMsg"; +import { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -54,7 +55,7 @@ const MsgTransferForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgTransferFo setToAddressError(""); setTimeoutError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgTransfer => { + const isMsgValid = (): boolean => { if (!sourcePort) { setSourcePortError("Source port is required"); return false; @@ -89,7 +90,7 @@ const MsgTransferForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgTransferFo setTimeoutError("Timeout must be a valid timestamp in the future"); } - return isTxMsgTransfer(msg); + return true; }; const timeoutTimestamp = (() => { @@ -105,19 +106,18 @@ const MsgTransferForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgTransferFo } })(); - const msg: TxMsgTransfer = { - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: { - sender: fromAddress, - receiver: toAddress, - token: { denom, amount }, - sourcePort, - sourceChannel, - timeoutTimestamp, - memo, - }, + const msgValue: MsgTransfer = { + sender: fromAddress, + receiver: toAddress, + token: { denom, amount }, + sourcePort, + sourceChannel, + timeoutTimestamp, + memo, }; + const msg: MsgTransferEncodeObject = { typeUrl: MsgTypeUrls.Transfer, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); }, [ amount, diff --git a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx index fccb754..0b1f586 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx @@ -1,11 +1,12 @@ import { Decimal } from "@cosmjs/math"; +import { MsgUndelegateEncodeObject } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; +import { MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; 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 { MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; import StackableContainer from "../../../layout/StackableContainer"; @@ -36,7 +37,7 @@ const MsgUndelegateForm = ({ setValidatorAddressError(""); setAmountError(""); - const isMsgValid = (msg: TxMsg): msg is TxMsgUndelegate => { + const isMsgValid = (): boolean => { assert(state.chain.addressPrefix, "addressPrefix missing"); const addressErrorMsg = checkAddress(validatorAddress, state.chain.addressPrefix); @@ -52,7 +53,7 @@ const MsgUndelegateForm = ({ return false; } - return isTxMsgUndelegate(msg); + return true; }; const amountInAtomics = Decimal.fromUserInput( @@ -60,15 +61,14 @@ const MsgUndelegateForm = ({ Number(state.chain.displayDenomExponent), ).atomics; - const msg: TxMsgUndelegate = { - typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", - value: { - delegatorAddress, - validatorAddress, - amount: { amount: amountInAtomics, denom: state.chain.denom }, - }, + const msgValue: MsgUndelegate = { + delegatorAddress, + validatorAddress, + amount: { amount: amountInAtomics, denom: state.chain.denom }, }; + const msg: MsgUndelegateEncodeObject = { typeUrl: MsgTypeUrls.Undelegate, value: msgValue }; + setMsgGetter({ isMsgValid, msg }); } catch {} }, [ diff --git a/components/forms/CreateTxForm/MsgForm/index.tsx b/components/forms/CreateTxForm/MsgForm/index.tsx index 1489fa9..bcd9027 100644 --- a/components/forms/CreateTxForm/MsgForm/index.tsx +++ b/components/forms/CreateTxForm/MsgForm/index.tsx @@ -1,5 +1,5 @@ import { MsgGetter } from ".."; -import { MsgType } from "../../../../types/txMsg"; +import { MsgTypeUrl, MsgTypeUrls } from "../../../../types/txMsg"; import MsgClaimRewardsForm from "./MsgClaimRewardsForm"; import MsgCreateVestingAccountForm from "./MsgCreateVestingAccountForm"; import MsgDelegateForm from "./MsgDelegateForm"; @@ -10,7 +10,7 @@ import MsgTransferForm from "./MsgTransferForm"; import MsgUndelegateForm from "./MsgUndelegateForm"; interface MsgFormProps { - readonly msgType: MsgType; + readonly msgType: MsgTypeUrl; readonly senderAddress: string; readonly setMsgGetter: (msgGetter: MsgGetter) => void; readonly deleteMsg: () => void; @@ -18,21 +18,21 @@ interface MsgFormProps { const MsgForm = ({ msgType, senderAddress, ...restProps }: MsgFormProps) => { switch (msgType) { - case "send": + case MsgTypeUrls.Send: return ; - case "delegate": + case MsgTypeUrls.Delegate: return ; - case "undelegate": + case MsgTypeUrls.Undelegate: return ; - case "redelegate": + case MsgTypeUrls.BeginRedelegate: return ; - case "claimRewards": + case MsgTypeUrls.WithdrawDelegatorReward: return ; - case "setWithdrawAddress": + case MsgTypeUrls.SetWithdrawAddress: return ; - case "createVestingAccount": + case MsgTypeUrls.CreateVestingAccount: return ; - case "msgTransfer": + case MsgTypeUrls.Transfer: return ; default: return null;