-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sim
committed
Jan 24, 2022
1 parent
5700909
commit 41027dd
Showing
10 changed files
with
187 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.main { | ||
overflow: hidden; | ||
width: 100%; | ||
} | ||
|
||
.link { | ||
font-weight: var(--normal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ContractInfo } from "@terra-money/terra.js" | ||
import { FinderLink } from "components/general" | ||
import { Card } from "components/layout" | ||
import ContractItemActions from "./ContractItemActions" | ||
import ContractDetails from "./ContractDetails" | ||
import styles from "./ContractItem.module.scss" | ||
|
||
const ContractItem = (props: ContractInfo) => { | ||
const { address } = props | ||
|
||
return ( | ||
<Card | ||
title={<FinderLink className={styles.link}>{address}</FinderLink>} | ||
extra={<ContractItemActions />} | ||
mainClassName={styles.main} | ||
bordered | ||
> | ||
<ContractDetails {...props} /> | ||
</Card> | ||
) | ||
} | ||
|
||
export default ContractItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useCallback, useMemo } from "react" | ||
import { useTranslation } from "react-i18next" | ||
import { useForm } from "react-hook-form" | ||
import { AccAddress, MsgMigrateContract } from "@terra-money/terra.js" | ||
import { parseJSON, validateMsg } from "utils/data" | ||
import { useAddress } from "data/wallet" | ||
import { useBankBalance } from "data/queries/bank" | ||
import { Form, FormItem } from "components/form" | ||
import { Input, EditorInput } from "components/form" | ||
import validate from "../validate" | ||
import Tx, { getInitialGasDenom } from "../Tx" | ||
|
||
interface TxValues { | ||
id?: number | ||
msg?: string | ||
} | ||
|
||
const MigrateContractForm = ({ contract }: { contract: AccAddress }) => { | ||
const { t } = useTranslation() | ||
|
||
const address = useAddress() | ||
const bankBalance = useBankBalance() | ||
|
||
/* tx context */ | ||
const initialGasDenom = getInitialGasDenom(bankBalance) | ||
|
||
/* form */ | ||
const form = useForm<TxValues>({ mode: "onChange" }) | ||
const { register, watch, handleSubmit, formState } = form | ||
const { errors } = formState | ||
const values = watch() | ||
|
||
/* tx */ | ||
const createTx = useCallback( | ||
({ id, msg }: TxValues) => { | ||
if (!address || !(id && msg)) return | ||
if (!validateMsg(msg)) return | ||
|
||
const migrate_msg = parseJSON(msg) | ||
const msgs = [new MsgMigrateContract(address, contract, id, migrate_msg)] | ||
|
||
return { msgs } | ||
}, | ||
[address, contract] | ||
) | ||
|
||
/* fee */ | ||
const estimationTxValues = useMemo(() => values, [values]) | ||
|
||
const tx = { | ||
initialGasDenom, | ||
estimationTxValues, | ||
createTx, | ||
onSuccess: { label: t("Contract"), path: "/contract" }, | ||
} | ||
|
||
return ( | ||
<Tx {...tx}> | ||
{({ fee, submit }) => ( | ||
<Form onSubmit={handleSubmit(submit.fn)}> | ||
<FormItem label={t("Code ID")} error={errors.id?.message}> | ||
<Input | ||
{...register("id", { | ||
valueAsNumber: true, | ||
required: "Code ID is required", | ||
min: { | ||
value: 0, | ||
message: "Code ID must be a positive integer", | ||
}, | ||
validate: { | ||
integer: (value) => | ||
Number.isInteger(value) || "Code ID must be an integer", | ||
}, | ||
})} | ||
inputMode="decimal" | ||
placeholder="1" | ||
autoFocus | ||
/> | ||
</FormItem> | ||
|
||
<FormItem | ||
label="Migrate msg" // do not translate this | ||
error={errors.msg?.message} | ||
> | ||
<EditorInput {...register("msg", { validate: validate.msg() })} /> | ||
</FormItem> | ||
|
||
{fee.render()} | ||
{submit.button} | ||
</Form> | ||
)} | ||
</Tx> | ||
) | ||
} | ||
|
||
export default MigrateContractForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useTranslation } from "react-i18next" | ||
import { useParams } from "react-router-dom" | ||
import { Page, Card } from "components/layout" | ||
import TxContext from "../TxContext" | ||
import MigrateContractForm from "./MigrateContractForm" | ||
|
||
const MigrateContractTx = () => { | ||
const { t } = useTranslation() | ||
const { contract } = useParams() | ||
|
||
if (!contract) throw new Error("Contract is not defined") | ||
|
||
return ( | ||
<Page title={t("Migrate")} small> | ||
<Card> | ||
<TxContext> | ||
<MigrateContractForm contract={contract} /> | ||
</TxContext> | ||
</Card> | ||
</Page> | ||
) | ||
} | ||
|
||
export default MigrateContractTx |