Skip to content

Commit

Permalink
Changed asyncWrapper to logPromiseFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
ioay committed Feb 25, 2024
1 parent 9c99552 commit 13ba81e
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions dapp/src/components/Header/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,7 @@ function ConnectButton({
const colorScheme = !account ? "error" : undefined

const handleClick = () => {
asyncWrapper(requestAccount())
logPromiseFailure(requestAccount())
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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])

Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,7 +15,7 @@ export default function SignMessageModal() {
const handleSignMessage = useExecuteFunction(signMessage, goNext)

const handleSignMessageWrapper = useCallback(() => {
asyncWrapper(handleSignMessage())
logPromiseFailure(handleSignMessage())
}, [handleSignMessage])

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -34,7 +34,7 @@ export default function SignMessageModal() {

// TODO: Remove when SDK is ready
setTimeout(() => {
asyncWrapper(handleSignMessage())
logPromiseFailure(handleSignMessage())
}, 5000)
}, [setStatus, handleSignMessage])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -60,7 +60,8 @@ function ActionFormModal({ defaultType }: { defaultType: ActionFlowType }) {
)

const handleSubmitFormWrapper = useCallback(
(values: TokenAmountFormValues) => asyncWrapper(handleSubmitForm(values)),
(values: TokenAmountFormValues) =>
logPromiseFailure(handleSubmitForm(values)),
[handleSubmitForm],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -27,7 +27,7 @@ export default function MissingAccountModal({
const { name, symbol } = getCurrencyByType(currency)

const handleClick = () => {
asyncWrapper(requestAccount())
logPromiseFailure(requestAccount())
}

return (
Expand Down
4 changes: 2 additions & 2 deletions dapp/src/components/shared/Form/FormTokenBalanceInput.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions dapp/src/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
@@ -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])
}
4 changes: 2 additions & 2 deletions dapp/src/hooks/useInitializeAcreSdk.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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])
}
8 changes: 0 additions & 8 deletions dapp/src/utils/async.ts

This file was deleted.

2 changes: 1 addition & 1 deletion dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions dapp/src/utils/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* If the promise fails, log the underlying error but maintain the failed
* promise.
*
* Does nothing to successful promises.
*/
export function logPromiseFailure<T>(promise: Promise<T>) {
return () => {
promise.catch((error) => {
throw error
})
}
}

0 comments on commit 13ba81e

Please sign in to comment.