-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseExecute.ts
41 lines (36 loc) · 1.18 KB
/
useExecute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { parseError } from '@/helpers/parseError'
import { queryClient } from '@/pages/_app'
import { DeliverTxResponse, ExecuteResult } from '@cosmjs/cosmwasm-stargate'
import { useMutation } from '@tanstack/react-query'
import useToaster from './useToaster'
type Transaction = {
onSubmit: () => Promise<ExecuteResult>
onSuccess?: () => void
}
const mock = {
transactionHash: '455C577EBCACEA50D9E8E9A0E621B1121E05D97974DFD9EDFFFB367B2F13BC24',
} as DeliverTxResponse
const useExecute = ({ onSubmit, onSuccess }: Transaction) => {
const toaster = useToaster()
const tx = useMutation<ExecuteResult, Error>({
mutationFn: onSubmit,
onSuccess: (res: ExecuteResult) => {
const { transactionHash } = res
toaster.success({
message: 'Transaction Successful',
txHash: transactionHash,
})
queryClient.invalidateQueries({ queryKey: ['balances'] })
queryClient.invalidateQueries({ queryKey: ['staked'] })
onSuccess?.()
},
onError: (error) => {
const parsedError = parseError(error?.message ?? "")
toaster.error({
message: parsedError || 'Transaction Failed',
})
},
})
return tx
}
export default useExecute