diff --git a/dapp/src/components/Header/ConnectWallet.tsx b/dapp/src/components/Header/ConnectWallet.tsx index 038a069ff..7879363bf 100644 --- a/dapp/src/components/Header/ConnectWallet.tsx +++ b/dapp/src/components/Header/ConnectWallet.tsx @@ -9,7 +9,7 @@ import { import { CurrencyBalance } from "#/components/shared/CurrencyBalance" import { TextMd } from "#/components/shared/Typography" import { Bitcoin, EthereumIcon } from "#/assets/icons" -import { truncateAddress, asyncWrapper } from "#/utils" +import { truncateAddress, logPromiseFailure } from "#/utils" export type ConnectButtonsProps = { leftIcon: typeof Icon @@ -25,7 +25,7 @@ function ConnectButton({ const colorScheme = !account ? "error" : undefined const handleClick = () => { - asyncWrapper(requestAccount()) + logPromiseFailure(requestAccount()) } return ( diff --git a/dapp/src/components/TransactionModal/ActiveStakingStep/DepositBTCModal.tsx b/dapp/src/components/TransactionModal/ActiveStakingStep/DepositBTCModal.tsx index 8273aff92..7ad202f69 100644 --- a/dapp/src/components/TransactionModal/ActiveStakingStep/DepositBTCModal.tsx +++ b/dapp/src/components/TransactionModal/ActiveStakingStep/DepositBTCModal.tsx @@ -8,7 +8,7 @@ import { } from "#/hooks" import Alert from "#/components/shared/Alert" import { TextMd } from "#/components/shared/Typography" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import { PROCESS_STATUSES } from "#/types" import StakingStepsModalContent from "./StakingStepsModalContent" @@ -38,7 +38,7 @@ export default function DepositBTCModal() { // to make sure for the moment that it doesn't return an error about funds not found // TODO: Remove the delay when SDK is updated setTimeout(() => { - asyncWrapper(handleStake()) + logPromiseFailure(handleStake()) }, 10000) }, [setStatus, handleStake]) @@ -48,7 +48,7 @@ export default function DepositBTCModal() { const handledDepositBTC = useCallback(() => { if (!tokenAmount?.amount || !btcAddress) return - asyncWrapper(sendBitcoinTransaction(tokenAmount?.amount, btcAddress)) + logPromiseFailure(sendBitcoinTransaction(tokenAmount?.amount, btcAddress)) }, [btcAddress, sendBitcoinTransaction, tokenAmount]) return ( diff --git a/dapp/src/components/TransactionModal/ActiveStakingStep/SignMessageModal.tsx b/dapp/src/components/TransactionModal/ActiveStakingStep/SignMessageModal.tsx index ac6fb44a9..84803da08 100644 --- a/dapp/src/components/TransactionModal/ActiveStakingStep/SignMessageModal.tsx +++ b/dapp/src/components/TransactionModal/ActiveStakingStep/SignMessageModal.tsx @@ -4,7 +4,7 @@ import { useModalFlowContext, useStakeFlowContext, } from "#/hooks" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import AlertReceiveSTBTC from "#/components/shared/AlertReceiveSTBTC" import { PROCESS_STATUSES } from "#/types" import StakingStepsModalContent from "./StakingStepsModalContent" @@ -15,7 +15,7 @@ export default function SignMessageModal() { const handleSignMessage = useExecuteFunction(signMessage, goNext) const handleSignMessageWrapper = useCallback(() => { - asyncWrapper(handleSignMessage()) + logPromiseFailure(handleSignMessage()) }, [handleSignMessage]) useEffect(() => { diff --git a/dapp/src/components/TransactionModal/ActiveUnstakingStep/SignMessageModal.tsx b/dapp/src/components/TransactionModal/ActiveUnstakingStep/SignMessageModal.tsx index dcf681c60..0f9bd1003 100644 --- a/dapp/src/components/TransactionModal/ActiveUnstakingStep/SignMessageModal.tsx +++ b/dapp/src/components/TransactionModal/ActiveUnstakingStep/SignMessageModal.tsx @@ -4,7 +4,7 @@ import { PROCESS_STATUSES } from "#/types" import { Button, ModalBody, ModalFooter, ModalHeader } from "@chakra-ui/react" import { TextMd } from "#/components/shared/Typography" import AlertReceiveSTBTC from "#/components/shared/AlertReceiveSTBTC" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" export default function SignMessageModal() { const { setStatus } = useModalFlowContext() @@ -34,7 +34,7 @@ export default function SignMessageModal() { // TODO: Remove when SDK is ready setTimeout(() => { - asyncWrapper(handleSignMessage()) + logPromiseFailure(handleSignMessage()) }, 5000) }, [setStatus, handleSignMessage]) diff --git a/dapp/src/components/TransactionModal/ModalContentWrapper/ActionFormModal.tsx b/dapp/src/components/TransactionModal/ModalContentWrapper/ActionFormModal.tsx index 65f60f58f..7ef402ea2 100644 --- a/dapp/src/components/TransactionModal/ModalContentWrapper/ActionFormModal.tsx +++ b/dapp/src/components/TransactionModal/ModalContentWrapper/ActionFormModal.tsx @@ -16,7 +16,7 @@ import { } from "#/hooks" import { ACTION_FLOW_TYPES, ActionFlowType } from "#/types" import { TokenAmountFormValues } from "#/components/shared/TokenAmountForm/TokenAmountFormBase" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import { REFERRAL } from "#/constants" import StakeFormModal from "../ActiveStakingStep/StakeFormModal" import UnstakeFormModal from "../ActiveUnstakingStep/UnstakeFormModal" @@ -60,7 +60,8 @@ function ActionFormModal({ defaultType }: { defaultType: ActionFlowType }) { ) const handleSubmitFormWrapper = useCallback( - (values: TokenAmountFormValues) => asyncWrapper(handleSubmitForm(values)), + (values: TokenAmountFormValues) => + logPromiseFailure(handleSubmitForm(values)), [handleSubmitForm], ) diff --git a/dapp/src/components/TransactionModal/ModalContentWrapper/MissingAccountModal.tsx b/dapp/src/components/TransactionModal/ModalContentWrapper/MissingAccountModal.tsx index 21391e063..826dc325f 100644 --- a/dapp/src/components/TransactionModal/ModalContentWrapper/MissingAccountModal.tsx +++ b/dapp/src/components/TransactionModal/ModalContentWrapper/MissingAccountModal.tsx @@ -10,7 +10,7 @@ import { } from "@chakra-ui/react" import { TextMd } from "#/components/shared/Typography" import Alert from "#/components/shared/Alert" -import { asyncWrapper, getCurrencyByType } from "#/utils" +import { logPromiseFailure, getCurrencyByType } from "#/utils" import { CurrencyType, RequestAccountParams } from "#/types" type MissingAccountModalProps = { @@ -27,7 +27,7 @@ export default function MissingAccountModal({ const { name, symbol } = getCurrencyByType(currency) const handleClick = () => { - asyncWrapper(requestAccount()) + logPromiseFailure(requestAccount()) } return ( diff --git a/dapp/src/components/shared/Form/FormTokenBalanceInput.tsx b/dapp/src/components/shared/Form/FormTokenBalanceInput.tsx index 5eefb5933..00fe36074 100644 --- a/dapp/src/components/shared/Form/FormTokenBalanceInput.tsx +++ b/dapp/src/components/shared/Form/FormTokenBalanceInput.tsx @@ -1,6 +1,6 @@ import React from "react" import { useField } from "formik" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import TokenBalanceInput, { TokenBalanceInputProps } from "../TokenBalanceInput" export type FormTokenBalanceInputProps = { @@ -13,7 +13,7 @@ export function FormTokenBalanceInput({ const [field, meta, helpers] = useField(name) const setAmount = (value?: bigint) => { - asyncWrapper(helpers.setValue(value)) + logPromiseFailure(helpers.setValue(value)) } return ( diff --git a/dapp/src/hooks/useInitApp.ts b/dapp/src/hooks/useInitApp.ts index c29509199..f1bbffa22 100644 --- a/dapp/src/hooks/useInitApp.ts +++ b/dapp/src/hooks/useInitApp.ts @@ -1,12 +1,12 @@ import { useEffect } from "react" import { fetchBTCPriceUSD } from "#/store/btc" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import { useAppDispatch } from "./store" export function useInitApp() { const dispatch = useAppDispatch() useEffect(() => { - asyncWrapper(dispatch(fetchBTCPriceUSD())) + logPromiseFailure(dispatch(fetchBTCPriceUSD())) }, [dispatch]) } diff --git a/dapp/src/hooks/useInitializeAcreSdk.ts b/dapp/src/hooks/useInitializeAcreSdk.ts index eb1afce46..ab05a9aac 100644 --- a/dapp/src/hooks/useInitializeAcreSdk.ts +++ b/dapp/src/hooks/useInitializeAcreSdk.ts @@ -1,6 +1,6 @@ import { useEffect } from "react" import { ETHEREUM_NETWORK } from "#/constants" -import { asyncWrapper } from "#/utils" +import { logPromiseFailure } from "#/utils" import { useAcreContext } from "#/acre-react/hooks" import { useWalletContext } from "./useWalletContext" @@ -14,6 +14,6 @@ export function useInitializeAcreSdk() { const initSDK = async (ethAddress: string) => { await init(ethAddress, ETHEREUM_NETWORK) } - asyncWrapper(initSDK(ethAccount.address)) + logPromiseFailure(initSDK(ethAccount.address)) }, [ethAccount?.address, init]) } diff --git a/dapp/src/utils/async.ts b/dapp/src/utils/async.ts deleted file mode 100644 index df3657065..000000000 --- a/dapp/src/utils/async.ts +++ /dev/null @@ -1,8 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function asyncWrapper(func: Promise) { - return () => { - func.catch((error) => { - throw error - }) - } -} diff --git a/dapp/src/utils/index.ts b/dapp/src/utils/index.ts index 30a1624a9..0d75ebea8 100644 --- a/dapp/src/utils/index.ts +++ b/dapp/src/utils/index.ts @@ -5,5 +5,5 @@ export * from "./currency" export * from "./chain" export * from "./text" export * from "./time" -export * from "./async" +export * from "./promise" export * from "./externalApi" diff --git a/dapp/src/utils/promise.ts b/dapp/src/utils/promise.ts new file mode 100644 index 000000000..7203b8b39 --- /dev/null +++ b/dapp/src/utils/promise.ts @@ -0,0 +1,11 @@ +/** + * If the promise fails, log the underlying error but maintain the failed + * promise. + * + * Does nothing to successful promises. + */ +export function logPromiseFailure(promise: Promise) { + promise.catch((error) => { + throw error + }) +}