-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from alleslabs/component/migrate-step2
Component/migrate step2 [merge #108 first]
- Loading branch information
Showing
14 changed files
with
540 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Icon } from "@chakra-ui/react"; | ||
import type { | ||
MigrateResult, | ||
SigningCosmWasmClient, | ||
} from "@cosmjs/cosmwasm-stargate"; | ||
import type { StdFee } from "@cosmjs/stargate"; | ||
import { pipe } from "@rx-stream/pipe"; | ||
import { MdCheckCircle } from "react-icons/md"; | ||
import type { Observable } from "rxjs"; | ||
|
||
import { ExplorerLink } from "lib/components/ExplorerLink"; | ||
import type { ContractAddr, TxResultRendering } from "lib/types"; | ||
import { TxStreamPhase } from "lib/types"; | ||
import { formatUFee } from "lib/utils"; | ||
|
||
import { catchTxError } from "./common/catchTxError"; | ||
import { postTx } from "./common/post"; | ||
import { sendingTx } from "./common/sending"; | ||
|
||
interface MigrateTxParams { | ||
sender: string; | ||
contractAddress: ContractAddr; | ||
codeId: number; | ||
migrateMsg: object; | ||
fee: StdFee; | ||
client: SigningCosmWasmClient; | ||
onTxSucceed?: (txHash: string) => void; | ||
onTxFailed?: () => void; | ||
} | ||
|
||
export const migrateContractTx = ({ | ||
sender, | ||
contractAddress, | ||
codeId, | ||
migrateMsg, | ||
fee, | ||
client, | ||
onTxSucceed, | ||
onTxFailed, | ||
}: MigrateTxParams): Observable<TxResultRendering> => { | ||
return pipe( | ||
sendingTx(fee), | ||
postTx<MigrateResult>({ | ||
postFn: () => | ||
client.migrate( | ||
sender, | ||
contractAddress, | ||
codeId, | ||
migrateMsg, | ||
fee, | ||
undefined | ||
), | ||
}), | ||
({ value: txInfo }) => { | ||
onTxSucceed?.(txInfo.transactionHash); | ||
return { | ||
value: null, | ||
phase: TxStreamPhase.SUCCEED, | ||
receipts: [ | ||
{ | ||
title: "Tx Hash", | ||
value: txInfo.transactionHash, | ||
html: ( | ||
<ExplorerLink type="tx_hash" value={txInfo.transactionHash} /> | ||
), | ||
}, | ||
{ | ||
title: "Tx Fee", | ||
value: `${formatUFee( | ||
txInfo.events.find((e) => e.type === "tx")?.attributes[0].value ?? | ||
"0u" | ||
)}`, | ||
}, | ||
], | ||
receiptInfo: { | ||
header: "Migration Completed", | ||
headerIcon: ( | ||
<Icon as={MdCheckCircle} fontSize="24px" color="success.main" /> | ||
), | ||
}, | ||
actionVariant: "migrate", | ||
} as TxResultRendering; | ||
} | ||
)().pipe(catchTxError(onTxFailed)); | ||
}; |
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,47 @@ | ||
import type { StdFee } from "@cosmjs/stargate"; | ||
import { useWallet } from "@cosmos-kit/react"; | ||
import { useCallback } from "react"; | ||
|
||
import { migrateContractTx } from "lib/app-fns/tx/migrate"; | ||
import type { ContractAddr, Option } from "lib/types"; | ||
|
||
export interface MigrateStreamParams { | ||
contractAddress: ContractAddr; | ||
codeId: number; | ||
migrateMsg: object; | ||
estimatedFee: Option<StdFee>; | ||
onTxSucceed?: (txHash: string) => void; | ||
onTxFailed?: () => void; | ||
} | ||
|
||
export const useMigrateTx = () => { | ||
const { address, getCosmWasmClient } = useWallet(); | ||
|
||
return useCallback( | ||
async ({ | ||
contractAddress, | ||
codeId, | ||
migrateMsg, | ||
estimatedFee, | ||
onTxSucceed, | ||
onTxFailed, | ||
}: MigrateStreamParams) => { | ||
const client = await getCosmWasmClient(); | ||
if (!address || !client) | ||
throw new Error("Please check your wallet connection."); | ||
if (!estimatedFee) return null; | ||
|
||
return migrateContractTx({ | ||
sender: address, | ||
contractAddress, | ||
codeId, | ||
migrateMsg, | ||
fee: estimatedFee, | ||
client, | ||
onTxSucceed, | ||
onTxFailed, | ||
}); | ||
}, | ||
[address, getCosmWasmClient] | ||
); | ||
}; |
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,76 @@ | ||
import { Flex, Radio, RadioGroup } from "@chakra-ui/react"; | ||
import { useState } from "react"; | ||
import type { Control, FieldPath, FieldValues } from "react-hook-form"; | ||
|
||
import { CodeSelect } from "lib/pages/instantiate/component"; | ||
import type { Option } from "lib/types"; | ||
|
||
import type { FormStatus } from "./forms"; | ||
import { ControllerInput } from "./forms"; | ||
|
||
interface CodeSelectSectionProps<T extends FieldValues> { | ||
codeId: string; | ||
name: FieldPath<T>; | ||
control: Control<T>; | ||
error: Option<string>; | ||
onCodeSelect: (codeId: string) => void; | ||
status: FormStatus; | ||
} | ||
|
||
export const CodeSelectSection = <T extends FieldValues>({ | ||
codeId, | ||
name, | ||
control, | ||
error, | ||
onCodeSelect, | ||
status, | ||
}: CodeSelectSectionProps<T>) => { | ||
const [method, setMethod] = useState<"select-existing" | "fill-manually">( | ||
"select-existing" | ||
); | ||
|
||
return ( | ||
<> | ||
<RadioGroup | ||
onChange={(nextVal: "select-existing" | "fill-manually") => | ||
setMethod(nextVal) | ||
} | ||
value={method} | ||
w="100%" | ||
> | ||
<Flex justify="space-around"> | ||
<Radio value="select-existing" size="lg"> | ||
Select from your code | ||
</Radio> | ||
<Radio value="fill-manually" size="lg"> | ||
Fill Code ID manually | ||
</Radio> | ||
</Flex> | ||
</RadioGroup> | ||
<form style={{ width: "100%" }}> | ||
{method === "select-existing" ? ( | ||
<CodeSelect | ||
mt="16px" | ||
mb="32px" | ||
onCodeSelect={onCodeSelect} | ||
codeId={codeId} | ||
status={status} | ||
/> | ||
) : ( | ||
<ControllerInput | ||
name={name} | ||
control={control} | ||
type="number" | ||
status={status} | ||
error={error} | ||
label="Code ID" | ||
helperText="Input existing Code ID manually" | ||
variant="floating" | ||
my="32px" | ||
rules={{ required: "Code ID is required" }} | ||
/> | ||
)} | ||
</form> | ||
</> | ||
); | ||
}; |
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
Oops, something went wrong.