From f4c53edb76882a8d45707f595eb4860201ab352f Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:57:43 +0200 Subject: [PATCH 1/3] Prefix current create form with Old --- .../MsgForm/MsgBeginRedelegateForm.tsx | 0 .../MsgForm/MsgCreateVestingAccountForm.tsx | 0 .../MsgForm/MsgDelegateForm.tsx | 0 .../MsgForm/MsgExecuteContractForm.tsx | 0 .../MsgForm/MsgFundCommunityPoolForm.tsx | 0 .../MsgForm/MsgInstantiateContract2Form.tsx | 0 .../MsgForm/MsgInstantiateContractForm.tsx | 0 .../MsgForm/MsgMigrateContractForm.tsx | 0 .../MsgForm/MsgSendForm.tsx | 0 .../MsgForm/MsgSetWithdrawAddressForm.tsx | 0 .../MsgForm/MsgTransferForm.tsx | 0 .../MsgForm/MsgUndelegateForm.tsx | 0 .../MsgForm/MsgUpdateAdminForm.tsx | 0 .../MsgForm/MsgVoteForm.tsx | 0 .../MsgWithdrawDelegatorRewardForm.tsx | 0 .../MsgForm/index.tsx | 0 components/forms/OldCreateTxForm/index.tsx | 348 ++++++++++++++++++ 17 files changed, 348 insertions(+) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgBeginRedelegateForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgCreateVestingAccountForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgDelegateForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgExecuteContractForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgFundCommunityPoolForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgInstantiateContract2Form.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgInstantiateContractForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgMigrateContractForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgSendForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgSetWithdrawAddressForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgTransferForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgUndelegateForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgUpdateAdminForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgVoteForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/MsgWithdrawDelegatorRewardForm.tsx (100%) rename components/forms/{CreateTxForm => OldCreateTxForm}/MsgForm/index.tsx (100%) create mode 100644 components/forms/OldCreateTxForm/index.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgBeginRedelegateForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgBeginRedelegateForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgBeginRedelegateForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgBeginRedelegateForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgDelegateForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgDelegateForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgExecuteContractForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgExecuteContractForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgFundCommunityPoolForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgFundCommunityPoolForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgFundCommunityPoolForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgFundCommunityPoolForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgInstantiateContractForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgInstantiateContractForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgMigrateContractForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgMigrateContractForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgMigrateContractForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgMigrateContractForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgSendForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgSendForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgSetWithdrawAddressForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgTransferForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgTransferForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgTransferForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgUndelegateForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgUndelegateForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgUpdateAdminForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgUpdateAdminForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgUpdateAdminForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgUpdateAdminForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgVoteForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgVoteForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgVoteForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgVoteForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/MsgWithdrawDelegatorRewardForm.tsx b/components/forms/OldCreateTxForm/MsgForm/MsgWithdrawDelegatorRewardForm.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/MsgWithdrawDelegatorRewardForm.tsx rename to components/forms/OldCreateTxForm/MsgForm/MsgWithdrawDelegatorRewardForm.tsx diff --git a/components/forms/CreateTxForm/MsgForm/index.tsx b/components/forms/OldCreateTxForm/MsgForm/index.tsx similarity index 100% rename from components/forms/CreateTxForm/MsgForm/index.tsx rename to components/forms/OldCreateTxForm/MsgForm/index.tsx diff --git a/components/forms/OldCreateTxForm/index.tsx b/components/forms/OldCreateTxForm/index.tsx new file mode 100644 index 00000000..a0e9f152 --- /dev/null +++ b/components/forms/OldCreateTxForm/index.tsx @@ -0,0 +1,348 @@ +import { loadValidators } from "@/context/ChainsContext/helpers"; +import { DbTransactionParsedDataJson } from "@/graphql"; +import { createDbTx } from "@/lib/api"; +import { toastError, toastSuccess } from "@/lib/utils"; +import { MsgTypeUrl, MsgTypeUrls } from "@/types/txMsg"; +import { EncodeObject } from "@cosmjs/proto-signing"; +import { Account, calculateFee } from "@cosmjs/stargate"; +import { assert, sleep } from "@cosmjs/utils"; +import { NextRouter, withRouter } from "next/router"; +import { useRef, useState } from "react"; +import { toast } from "sonner"; +import { useChains } from "../../../context/ChainsContext"; +import { exportMsgToJson, gasOfTx } from "../../../lib/txMsgHelpers"; +import Button from "../../inputs/Button"; +import Input from "../../inputs/Input"; +import StackableContainer from "../../layout/StackableContainer"; +import MsgForm from "./MsgForm"; + +export interface MsgGetter { + readonly isMsgValid: () => boolean; + readonly msg: EncodeObject; +} + +interface OldCreateTxFormProps { + readonly router: NextRouter; + readonly senderAddress: string; + readonly accountOnChain: Account; +} + +const OldCreateTxForm = ({ router, senderAddress, accountOnChain }: OldCreateTxFormProps) => { + const { + chain, + validatorState: { validators }, + chainsDispatch, + } = useChains(); + + const [processing, setProcessing] = useState(false); + const [msgTypes, setMsgTypes] = useState([]); + const [msgKeys, setMsgKeys] = useState([]); + const msgGetters = useRef([]); + const [memo, setMemo] = useState(""); + const [gasLimit, setGasLimit] = useState(gasOfTx([])); + const [gasLimitError, setGasLimitError] = useState(""); + + const addMsgType = (newMsgType: MsgTypeUrl) => { + setMsgKeys((oldMsgKeys) => [...oldMsgKeys, crypto.randomUUID()]); + setMsgTypes((oldMsgTypes) => { + const newMsgTypes = [...oldMsgTypes, newMsgType]; + setGasLimit(gasOfTx(newMsgTypes)); + return newMsgTypes; + }); + }; + + const addMsgWithValidator = (newMsgType: MsgTypeUrl) => { + if (!validators.length) { + loadValidators(chainsDispatch); + } + + addMsgType(newMsgType); + }; + + const createTx = async () => { + const loadingToastId = toast.loading("Creating transaction"); + setProcessing(true); + // If it fails too fast, toast.dismiss does not work + await sleep(500); + + try { + assert(typeof accountOnChain.accountNumber === "number", "accountNumber missing"); + assert(msgGetters.current.length, "form filled incorrectly"); + + const msgs = msgGetters.current + .filter(({ isMsgValid }) => isMsgValid()) + .map(({ msg }) => exportMsgToJson(msg)); + + if (!msgs.length || msgs.length !== msgTypes.length) { + return; + } + + if (!Number.isSafeInteger(gasLimit) || gasLimit <= 0) { + setGasLimitError("gas limit must be a positive integer"); + return; + } + + const txData: DbTransactionParsedDataJson = { + accountNumber: accountOnChain.accountNumber, + sequence: accountOnChain.sequence, + chainId: chain.chainId, + msgs, + fee: calculateFee(gasLimit, chain.gasPrice), + memo, + }; + + const txId = await createDbTx(accountOnChain.address, chain.chainId, txData); + toastSuccess("Transaction created with ID", txId); + router.push(`/${chain.registryName}/${senderAddress}/transaction/${txId}`); + } catch (e) { + console.error("Failed to create transaction:", e); + toastError({ + description: "Failed to create transaction", + fullError: e instanceof Error ? e : undefined, + }); + setProcessing(false); + } finally { + toast.dismiss(loadingToastId); + } + }; + + return ( + +

Create New Transaction

+ {msgTypes.length ? ( + msgTypes.map((msgType, index) => ( + { + msgGetters.current = [ + ...msgGetters.current.slice(0, index), + msgGetter, + ...msgGetters.current.slice(index + 1), + ]; + }} + deleteMsg={() => { + msgGetters.current.splice(index, 1); + setMsgKeys((oldMsgKeys) => [ + ...oldMsgKeys.slice(0, index), + ...oldMsgKeys.slice(index + 1), + ]); + setMsgTypes((oldMsgTypes) => { + const newMsgTypes: MsgTypeUrl[] = oldMsgTypes.slice(); + newMsgTypes.splice(index, 1); + setGasLimit(gasOfTx(newMsgTypes)); + return newMsgTypes; + }); + }} + /> + )) + ) : ( + +

Add at least one message to this transaction

+
+ )} +
+ setGasLimit(Number(target.value))} + /> +
+
+ +
+
+ setMemo(target.value)} + /> +
+

Add New Msg

+
+
+ +
    +
  • +
  • +
+
+
+ +
    +
  • +
  • +
+
+
+ +
    +
  • +
  • +
+
+
+ +
    +
  • +
  • +
+
+
+ +
    +
  • +
  • +
  • +
  • +
  • +
  • +
+
+
+ +
    +
  • +
  • +
  • +
  • +
  • +
  • +
+
+
+ +
    +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
  • +
+
+
+