diff --git a/src/components/CSVFileUploader/CSVFileUploadForm.tsx b/src/components/CSVFileUploader/CSVFileUploadForm.tsx index 8f6e380b4..8b50f5001 100644 --- a/src/components/CSVFileUploader/CSVFileUploadForm.tsx +++ b/src/components/CSVFileUploader/CSVFileUploadForm.tsx @@ -86,7 +86,7 @@ const CSVFileUploadForm = ({ onClose }: { onClose: () => void }) => { }; const onSubmit = async ({ file, sender }: FormFields) => { - const account = getAccount(sender) as ImplicitAccount; + const account = getAccount(sender); Papa.parse(file[0], { skipEmptyLines: true, complete: (rows: ParseResult) => onCSVFileUploadComplete(account, rows), diff --git a/src/components/sendForm/SendForm.tsx b/src/components/sendForm/SendForm.tsx index 99ce28f1e..3f8c4b116 100644 --- a/src/components/sendForm/SendForm.tsx +++ b/src/components/sendForm/SendForm.tsx @@ -1,7 +1,6 @@ import { useToast } from "@chakra-ui/react"; import { TezosToolkit, TransferParams } from "@taquito/taquito"; import { useEffect, useRef, useState } from "react"; -import { ImplicitAccount } from "../../types/Account"; import { RawPkh } from "../../types/Address"; import { Operation } from "../../types/Operation"; import { useGetImplicitAccount } from "../../utils/hooks/accountHooks"; @@ -73,7 +72,7 @@ export const SendForm = ({ const addToBatch = async (operation: Operation, senderPkh: RawPkh) => { // TODO: add support for Multisig - const sender = getAccount(senderPkh) as ImplicitAccount; + const sender = getAccount(senderPkh); try { // TODO: add support for Multisig diff --git a/src/components/sendForm/components/SignButton.tsx b/src/components/sendForm/components/SignButton.tsx index 7ec8c4127..9ed686ec7 100644 --- a/src/components/sendForm/components/SignButton.tsx +++ b/src/components/sendForm/components/SignButton.tsx @@ -26,13 +26,17 @@ const SignButton: React.FC<{ signerAccount: ImplicitAccount; network: TezosNetwork; }> = ({ signerAccount, network, onSubmit }) => { - const { register, handleSubmit, formState } = useForm<{ password: string }>({ mode: "onBlur" }); - const { errors } = formState; + const { + register, + handleSubmit, + formState: { errors }, + } = useForm<{ password: string }>({ mode: "onBlur" }); const getSecretKey = useGetSecretKey(); const toast = useToast(); const [isLoading, setIsLoading] = useState(false); + // wrapper function that handles changing the isLoading state & error handling const handleSign = async (getToolkit: () => Promise) => { if (isLoading) { return; @@ -98,7 +102,7 @@ const SignButton: React.FC<{ )} {signerAccount.type === AccountType.SOCIAL && } {signerAccount.type === AccountType.LEDGER && ( - )} diff --git a/src/components/sendForm/steps/FillStep.tsx b/src/components/sendForm/steps/FillStep.tsx index 3da05ea12..4585ab2e9 100644 --- a/src/components/sendForm/steps/FillStep.tsx +++ b/src/components/sendForm/steps/FillStep.tsx @@ -346,8 +346,8 @@ export const SendTezOrNFTForm = ({ const buildTezFromFormValues = ( formValues: FormValues, - getImplicitAccount: (pkh: RawPkh) => ImplicitAccount | undefined, - getMultisigAccount: (pkh: RawPkh) => MultisigAccount | undefined, + getImplicitAccount: (pkh: RawPkh) => ImplicitAccount, + getMultisigAccount: (pkh: RawPkh) => MultisigAccount, parameter?: TransferParams["parameter"] ): FormOperations => { const value: Operation[] = [ @@ -361,23 +361,23 @@ const buildTezFromFormValues = ( if (formValues.proposalSigner !== undefined) { return { type: "proposal", - signer: getImplicitAccount(formValues.proposalSigner) as ImplicitAccount, + signer: getImplicitAccount(formValues.proposalSigner), content: value, - sender: getMultisigAccount(formValues.sender) as MultisigAccount, + sender: getMultisigAccount(formValues.sender), }; } return { type: "implicit", content: value, - signer: getImplicitAccount(formValues.sender) as ImplicitAccount, + signer: getImplicitAccount(formValues.sender), }; }; const buildTokenFromFormValues = ( formValues: FormValues, asset: Token, - getImplicitAccount: (pkh: RawPkh) => ImplicitAccount | undefined, - getMultisigAccount: (pkh: RawPkh) => MultisigAccount | undefined + getImplicitAccount: (pkh: RawPkh) => ImplicitAccount, + getMultisigAccount: (pkh: RawPkh) => MultisigAccount ): FormOperations => { const token = [ toOperation(asset, { @@ -390,16 +390,16 @@ const buildTokenFromFormValues = ( if (formValues.proposalSigner !== undefined) { return { type: "proposal", - signer: getImplicitAccount(formValues.proposalSigner) as ImplicitAccount, + signer: getImplicitAccount(formValues.proposalSigner), content: token, - sender: getMultisigAccount(formValues.sender) as MultisigAccount, + sender: getMultisigAccount(formValues.sender), }; } return { type: "implicit", content: token, - signer: getImplicitAccount(formValues.sender) as ImplicitAccount, + signer: getImplicitAccount(formValues.sender), }; }; @@ -434,7 +434,7 @@ export const FillStep: React.FC<{ onSubmit({ type: "implicit", content: [delegation], - signer: getImplicitAccount(formValues.sender) as ImplicitAccount, + signer: getImplicitAccount(formValues.sender), }); }} /> @@ -508,7 +508,7 @@ export const FillStep: React.FC<{ onSubmit({ type: "implicit", content: mode.data.batch, - signer: getImplicitAccount(mode.data.signer) as ImplicitAccount, + signer: getImplicitAccount(mode.data.signer), }); }} /> diff --git a/src/utils/hooks/accountHooks.ts b/src/utils/hooks/accountHooks.ts index 36cb5e984..b3efd3579 100644 --- a/src/utils/hooks/accountHooks.ts +++ b/src/utils/hooks/accountHooks.ts @@ -5,6 +5,7 @@ import { LedgerAccount, MultisigAccount, SocialAccount, + ImplicitAccount, } from "../../types/Account"; import { RawPkh } from "../../types/Address"; import { decrypt } from "../aes"; @@ -21,9 +22,11 @@ export const useImplicitAccounts = () => { return useAppSelector(s => s.accounts.items); }; +// For cleaner code and ease of use this hook returns an ImplicitAccount +// Please make sure not to pass in non existing Pkh export const useGetImplicitAccount = () => { const accounts = useImplicitAccounts(); - return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh); + return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh) as ImplicitAccount; }; export const useReset = () => { @@ -141,10 +144,11 @@ export const useMultisigAccounts = (): MultisigAccount[] => { return multisigs.map((m, i) => multisigToAccount(m, `Multisig Account ${i}`)); }; +// For cleaner code and ease of use this hook returns a MultisigAccount +// Please make sure not to pass in non existing Pkh export const useGetMultisigAccount = () => { const accounts = useMultisigAccounts(); - return (pkh: RawPkh): MultisigAccount | undefined => - accounts.find(account => account.address.pkh === pkh); + return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh) as MultisigAccount; }; export const useAllAccounts = (): Account[] => {