Adapt msg forms

This commit is contained in:
abefernan
2023-06-14 20:37:40 +02:00
parent 6413149f61
commit 1eeb26c0a0
9 changed files with 100 additions and 98 deletions
@@ -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 });
@@ -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 {}
}, [
@@ -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 {}
}, [
@@ -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 {}
}, [
@@ -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 {}
}, [
@@ -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 {}
@@ -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,
@@ -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 {}
}, [
+10 -10
View File
@@ -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 <MsgSendForm fromAddress={senderAddress} {...restProps} />;
case "delegate":
case MsgTypeUrls.Delegate:
return <MsgDelegateForm delegatorAddress={senderAddress} {...restProps} />;
case "undelegate":
case MsgTypeUrls.Undelegate:
return <MsgUndelegateForm delegatorAddress={senderAddress} {...restProps} />;
case "redelegate":
case MsgTypeUrls.BeginRedelegate:
return <MsgRedelegateForm delegatorAddress={senderAddress} {...restProps} />;
case "claimRewards":
case MsgTypeUrls.WithdrawDelegatorReward:
return <MsgClaimRewardsForm delegatorAddress={senderAddress} {...restProps} />;
case "setWithdrawAddress":
case MsgTypeUrls.SetWithdrawAddress:
return <MsgSetWithdrawAddressForm delegatorAddress={senderAddress} {...restProps} />;
case "createVestingAccount":
case MsgTypeUrls.CreateVestingAccount:
return <MsgCreateVestingAccountForm fromAddress={senderAddress} {...restProps} />;
case "msgTransfer":
case MsgTypeUrls.Transfer:
return <MsgTransferForm fromAddress={senderAddress} {...restProps} />;
default:
return null;