Skip to content

Commit

Permalink
Added generic async wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ioay committed Jan 21, 2024
1 parent 813abcb commit e5ae9c8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
6 changes: 2 additions & 4 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, Ethereum } from "#/static/icons"
import { truncateAddress } from "#/utils"
import { truncateAddress, asyncWrapper } from "#/utils"

export type ConnectButtonsProps = {
leftIcon: typeof Icon
Expand All @@ -25,9 +25,7 @@ function ConnectButton({
const colorScheme = !account ? "error" : undefined

const handleClick = () => {
requestAccount().catch((error) => {
throw error
})
asyncWrapper(requestAccount())
}

return (
Expand Down
5 changes: 2 additions & 3 deletions dapp/src/components/Modals/Staking/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { Highlight } from "@chakra-ui/react"
import { useModalFlowContext, useSignMessage } from "#/hooks"
import Alert from "#/components/shared/Alert"
import { TextMd } from "#/components/shared/Typography"
import { asyncWrapper } from "#/utils"
import StakingSteps from "./components/StakingSteps"

export default function SignMessage() {
const { goNext } = useModalFlowContext()
const { signMessage } = useSignMessage(goNext)

const handleClick = () => {
signMessage().catch((error) => {
throw error
})
asyncWrapper(signMessage())
}

return (
Expand Down
6 changes: 2 additions & 4 deletions dapp/src/components/Modals/Support/MissingAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@chakra-ui/react"
import { TextMd } from "#/components/shared/Typography"
import Alert from "#/components/shared/Alert"
import { getCurrencyByType } from "#/utils"
import { asyncWrapper, getCurrencyByType } from "#/utils"
import { CurrencyType, RequestAccountParams } from "#/types"

type MissingAccountProps = {
Expand All @@ -26,9 +26,7 @@ export default function MissingAccount({
const { name, symbol } = getCurrencyByType(currency)

const handleClick = () => {
requestAccount().catch((error) => {
throw error
})
asyncWrapper(requestAccount())
}

return (
Expand Down
5 changes: 2 additions & 3 deletions dapp/src/components/shared/Form/FormTokenBalanceInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react"
import { useField } from "formik"
import { asyncWrapper } from "#/utils"
import TokenBalanceInput, { TokenBalanceInputProps } from "../TokenBalanceInput"

export type FormTokenBalanceInputProps = {
Expand All @@ -12,9 +13,7 @@ export function FormTokenBalanceInput({
const [field, meta, helpers] = useField(name)

const setAmount = (value?: bigint) => {
helpers.setValue(value).catch((error) => {
throw error
})
asyncWrapper(helpers.setValue(value))
}

return (
Expand Down
8 changes: 8 additions & 0 deletions dapp/src/utils/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function asyncWrapper(func: Promise<any>) {
return () => {
func.catch((error) => {
throw error
})
}
}
1 change: 1 addition & 0 deletions dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./numbers"
export * from "./address"
export * from "./forms"
export * from "./currency"
export * from "./async"

0 comments on commit e5ae9c8

Please sign in to comment.